#include using namespace std; typedef long long int ll; typedef unsigned long long ull; typedef long double ld; int dx[8] = { 1, 0, -1, 0, 1, 1, -1, -1 }, dy[8] = { 0, 1, 0, -1, 1, -1, 1, -1 }; const long long mod = 998244353; const ll inf = 1LL << 60; const int INF = 5e8; int main() { int h, w; cin >> h >> w; vector> a(h, vector(w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; } } vector row(h), col(w); vector diag(h + w - 1), diag2(h + w - 1); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { row[i] += a[i][j]; col[j] += a[i][j]; diag[i + j] += a[i][j]; diag2[i - j + w - 1] += a[i][j]; } } auto check = [&](int a, int b, int c) { return b <= a and a <= c; }; int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { ans = max(ans, row[i] + col[j] - a[i][j]); } } for (int i = 0; i < h; i++) { for (int j = 0; j < h + w - 1; j++) { ans = max(ans, row[i] + diag[j] - ((check(j - i, 0, w - 1) ? a[i][j - i] : 0))); } } for (int i = 0; i < w; i++) { for (int j = 0; j < h + w - 1; j++) { ans = max(ans, col[i] + diag[j] - ((check(j - i, 0, h - 1) ? a[j - i][i] : 0))); } } for (int i = 0; i < h; i++) { for (int j = 0; j < h + w - 1; j++) { ans = max(ans, row[i] + diag2[j] - ((check(i - j + w - 1, 0, w - 1) ? a[i][i - j + w - 1] : 0))); } } for (int i = 0; i < w; i++) { for (int j = 0; j < h + w - 1; j++) { ans = max(ans, col[i] + diag2[j] - ((check(i - j + w - 1, 0, h - 1) ? a[i - j + w - 1][i] : 0))); } } for (int i = 0; i < h + w - 1; i++) { for (int j = 0; j < h + w - 1; j++) { if ((i - j + w - 1) % 2) { ans = max(ans, diag[i] + diag2[j]); } else { ans = max(ans, diag[i] + diag2[j] - (((check((i - j + w - 1) / 2, 0, w - 1) and check((i + j - w + 1) / 2, 0, h - 1)) ? a[(i + j - w + 1) / 2][(i - j + w - 1) / 2] : 0))); } } } for (int i = 0; i < h; i++) { for (int j = i + 1; j < h; j++) ans = max(ans, row[i] + row[j]); } for (int i = 0; i < w; i++) { for (int j = i + 1; j < w; j++) ans = max(ans, col[i] + col[j]); } for (int i = 0; i < h + w - 1; i++) { for (int j = i + 1; j < h + w - 1; j++) ans = max(ans, diag[i] + diag[j]); } for (int i = 0; i < h + w - 1; i++) { for (int j = i + 1; j < h + w - 1; j++) ans = max(ans, diag2[i] + diag2[j]); } cout << ans << endl; }