#include #include using namespace std; using namespace atcoder; //using mint = modint998244353; //const int mod = 998244353; //using mint = modint1000000007; //const int mod = 1000000007; const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int h, w; cin >> h >> w; vector a(h, vector(w)); vector b(h, vector>(w)); rep(i, h)rep(j, w)cin >> a[i][j]; // i,j,i+j,j-i+(h-1) vectorv0(h), v1(w), v2(h + w - 1), v3(h + w - 1); rep(i, h)rep(j, w) { v0[i] += a[i][j]; v1[j] += a[i][j]; v2[i + j] += a[i][j]; v3[j - i + (h - 1)] += a[i][j]; } int ans = 0; auto v2l = v2, v2r = v2, v3l = v3, v3r = v3; rep2(i, 1, h + w - 1) v2l[i] = max(v2l[i - 1], v2l[i]); rep2(i, 1, h + w - 1) v3l[i] = max(v3l[i - 1], v3l[i]); rrep(i, h + w - 2) v2r[i] = max(v2r[i + 1], v2r[i]); rrep(i, h + w - 2) v3r[i] = max(v3r[i + 1], v3r[i]); rep(i, h) {// (i,0) (i,w-1) //cout << i << endl; int val = -INF; if (0 != i)chmax(val, v2l[i - 1]); if ((h + w - 1) != i + w)chmax(val, v2r[i + w]); if (0 != (h - 1 - i))chmax(val, v3l[h - 2 - i]); if ((h + w - 1) != (w + (h - 1) - i))chmax(val, v3r[w + (h - 1) - i]); chmax(ans, v0[i] + val); } rep(j, w) {//(0,j),(h-1,j) //cout << j << endl; int val = -INF; if (0 != j)chmax(val, v2l[j - 1]); if ((h + w - 1) != (j + h))chmax(val, v2r[j + h]); if (0 != j)chmax(val, v3l[j - 1]); if ((h + w - 1) != (j + h))chmax(val, v3r[j + h]); chmax(ans, v0[j] + val); } rep(i, h)rep(j, w) { vectorv = { v0[i],v1[j],v2[i + j],v3[j - i + (h - 1)] }; sort(allR(v)); chmax(ans, v[0] + v[1] - a[i][j]); } rep(i, v2.size())rep(j, v3.size()) { int x = i; int y = j - (h - 1); if (0 == abs(x - y) % 2)continue; chmax(ans, v2[i] + v3[j]); } sort(allR(v0)); if (v0.size() >= 2)chmax(ans, v0[0] + v0[1]); sort(allR(v1)); if (v1.size() >= 2)chmax(ans, v1[0] + v1[1]); sort(allR(v2)); if (v2.size() >= 2)chmax(ans, v2[0] + v2[1]); sort(allR(v3)); if (v3.size() >= 2)chmax(ans, v3[0] + v3[1]); cout << ans << endl; return 0; }