|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.Deque; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.StringTokenizer; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + static int[] dc = {1, 0, -1, 0}; |
| 10 | + static int[] dr = {0, 1, 0, -1}; |
| 11 | + static int[][] paper; |
| 12 | + static int maxValue = -(int) 1e9; |
| 13 | + |
| 14 | + static int N; |
| 15 | + static int M; |
| 16 | + |
| 17 | + public static void main(String[] args) throws IOException { |
| 18 | + |
| 19 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 20 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 21 | + N = Integer.parseInt(st.nextToken()); |
| 22 | + M = Integer.parseInt(st.nextToken()); |
| 23 | + paper = new int[N][M]; |
| 24 | + |
| 25 | + for (int i = 0; i < N; i++) { |
| 26 | + st = new StringTokenizer(br.readLine()); |
| 27 | + for (int j = 0; j < M; j++) { |
| 28 | + paper[i][j] = Integer.parseInt(st.nextToken()); |
| 29 | + } |
| 30 | + |
| 31 | + } |
| 32 | + int[][] visited = new int[N][M]; |
| 33 | + for (int i = 0; i < N; i++) { |
| 34 | + for (int j = 0; j < M; j++) { |
| 35 | + |
| 36 | + visited[i][j] = 1; |
| 37 | + findSum(i, j, 1, paper[i][j], visited); |
| 38 | + visited[i][j] = 0; |
| 39 | + findTsum(i, j); |
| 40 | + |
| 41 | + |
| 42 | + } |
| 43 | + } |
| 44 | + System.out.println(maxValue); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + private static void findTsum(int col, int row) { |
| 49 | + int Tvalue = 0; |
| 50 | + if (0 <= col - 1 && 0 <= row - 1 && row + 1 < M) { //ㅗ |
| 51 | + Tvalue = paper[col - 1][row] + paper[col][row] + paper[col][row + 1] + paper[col][row - 1]; |
| 52 | + maxValue = Math.max(maxValue, Tvalue); |
| 53 | + } |
| 54 | + if (col + 1 < N && 0 <= row - 1 && row + 1 < M) { // ㅜ |
| 55 | + Tvalue = paper[col + 1][row] + paper[col][row] + paper[col][row + 1] + paper[col][row - 1]; |
| 56 | + maxValue = Math.max(maxValue, Tvalue); |
| 57 | + } |
| 58 | + if (0 <= col - 1 && col + 1 < N && row + 1 < M) { //ㅏ |
| 59 | + Tvalue = paper[col - 1][row] + paper[col][row] + paper[col][row + 1] + paper[col + 1][row]; |
| 60 | + maxValue = Math.max(maxValue, Tvalue); |
| 61 | + } |
| 62 | + if (0 <= col - 1 && col + 1 < N && 0 <= row - 1) { //ㅓ |
| 63 | + Tvalue = paper[col - 1][row] + paper[col][row] + paper[col][row - 1] + paper[col + 1][row]; |
| 64 | + maxValue = Math.max(maxValue, Tvalue); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + private static void findSum(int col, int row, int depth, int total, int[][] visited) { |
| 70 | + |
| 71 | + if (depth == 4) { |
| 72 | + maxValue = Math.max(maxValue, total); |
| 73 | + return; |
| 74 | + } |
| 75 | + for (int i = 0; i < 4; i++) { |
| 76 | + int cc = dc[i] + col; |
| 77 | + int cr = dr[i] + row; |
| 78 | + if (0 <= cc && cc < N && 0 <= cr && cr < M) { |
| 79 | + if (visited[cc][cr] == 0) { |
| 80 | + visited[cc][cr] = 1; |
| 81 | + findSum(cc, cr, depth + 1, total + paper[cc][cr], visited); |
| 82 | + visited[cc][cr] = 0; |
| 83 | + |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | +} |
0 commit comments