import std; void main () { int H, W; readln.read(H, W); int[][] A = new int[][](H, W); foreach (i; 0..H) A[i] = readln.split.to!(int[]); solve(H, W, A); } void solve (int H, int W, int[][] A) { /** * vertical horizontally DUR DLR * | -------> _ \ * | /\ _\/ * v / * * 0 1 2 0 0 3 2 1 0 * 1 1 4 * 2 2 5 * 3 4 5 * */ int[] vertical = new int[](W); int[] horizontally = new int[](H); int[] DLR = new int[](H+W-1); int[] DUR = new int[](H+W-1); foreach (j; 0..W) foreach (i; 0..H) vertical[j] += A[i][j]; foreach (i; 0..H) foreach (j; 0..W) horizontally[i] += A[i][j]; bool isInGrid (int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; } { /* DLRの計算 */ int idx = 0; int y = 0, x = W-1; int dy = 1, dx = 1; for (; 0 < x; x--) { int k = 0; while (isInGrid(y+k*dy, x+k*dx)) { DLR[idx] += A[y+k*dy][x+k*dx]; k++; } idx++; } for (; y < H; y++) { int k = 0; while (isInGrid(y+k*dy, x+k*dx)) { DLR[idx] += A[y+k*dy][x+k*dx]; k++; } idx++; } } { /* DURの計算 */ int idx = 0; int y = 0, x = 0; int dy = -1, dx = 1; for (; y < H-1; y++) { int k = 0; while (isInGrid(y+k*dy, x+k*dx)) { DUR[idx] += A[y+k*dy][x+k*dx]; k++; } idx++; } for (; x < W; x++) { int k = 0; while (isInGrid(y+k*dy, x+k*dx)) { DUR[idx] += A[y+k*dy][x+k*dx]; k++; } idx++; } } /* 累積和計算はできた。。 writeln(vertical); writeln(horizontally); writeln(DLR); writeln(DUR); */ long ans = -long.max; /* 同じやつ2つ */ if (1 < vertical.length) ans = max(ans, vertical.dup.sort!"a>b"[0..2].sum); if (1 == vertical.length) ans = max(ans, vertical.fold!max); if (1 < horizontally.length) ans = max(ans, horizontally.dup.sort!"a>b"[0..2].sum); if (1 == horizontally.length) ans = max(ans, horizontally.fold!max); if (1 < DUR.length) ans = max(ans, DUR.dup.sort!"a>b"[0..2].sum); if (1 == DUR.length) ans = max(ans, DUR.fold!max); if (1 < DLR.length) ans = max(ans, DLR.dup.sort!"a>b"[0..2].sum); if (1 == DLR.length) ans = max(ans, DLR.fold!max); /* 違うやつ2つ */ /* 1. vertical and horizontally */ foreach (i; 0..W) foreach (j; 0..H) ans = max(ans, vertical[i]+horizontally[j] - A[j][i]); /* 2. vertical and DUR */ foreach (i; 0..W) foreach (j; 0..H+W-1) { if (isInGrid(i, j-i)) { ans = max(ans, vertical[i]+DUR[j] - A[i][j-i]); } else { ans = max(ans, vertical[i] + DUR[j]); } } /* 3. vertical and DLR */ foreach (i; 0..W) foreach (j; 0..H+W-1) { if (isInGrid(1-W+i+j, i)) { ans = max(ans, vertical[i]+DLR[j] - A[1-W+i+j][i]); } else { ans = max(ans, vertical[i]+DLR[j]); } } /* 4. horizontally + DUR */ foreach (i; 0..H) foreach (j; 0..H+W-1) { if (isInGrid(j-i, i)) { ans = max(ans, horizontally[i]+DUR[j] - A[j-i][i]); } else { ans = max(ans, horizontally[i]+DUR[j]); } } /* 5. horizontally + DLR */ foreach (i; 0..H) foreach (j; 0..H+W-1) { if (isInGrid(i, W-1+i-j)) { ans = max(ans, horizontally[i]+DLR[j] - A[i][W-1+i-j]); } else { ans = max(ans, horizontally[i]+DLR[j]); } } /* 6. DUR and DLR */ foreach (i; 0..H+W-1) foreach (j; 0..H+W-1) { if ((W-1+j-i)%2 == 0 && isInGrid(j-(W-1+j-i)/2, (W-1+j-i)/2)) { ans = max(ans, DLR[i]+DUR[j] - A[j-(W-1+j-i)/2][(W-1+j-i)/2]); } else { ans = max(ans, DLR[i]+DUR[j]); } } writeln(ans); } void read(T...)(string S, ref T args) { auto buf = S.split; foreach (i, ref arg; args) { arg = buf[i].to!(typeof(arg)); } }