#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 op(int a, int b) { return max(a, b); } int e() { return -INF; } 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; segtreeseg0(v0); segtreeseg1(v1); segtreeseg2(v2); segtreeseg3(v3); rep(i, h) {// (i,0) (i,w-1) //cout << i << endl; int val1 = seg2.prod(0, i); int val2 = seg2.prod(i + w, h + w - 1); int val3 = seg3.prod(0, h - 1 - i); int val4 = seg3.prod(w + (h - 1) - i, h + w - 1); //cout << val1 << " " << val2 << " " << val3 << " " << val4 << endl; chmax(ans, v0[i] + max({ val1,val2,val3,val4 })); } rep(j, w) {//(0,j),(h-1,j) //cout << j << endl; int val1 = seg2.prod(0, j); int val2 = seg2.prod(j + h, h + w - 1); int val3 = seg3.prod(0, j); int val4 = seg3.prod(j + h, h + w - 1); //cout << val1 << " " << val2 << " " << val3 << " " << val4 << endl; chmax(ans, v1[j] + max({ val1,val2,val3,val4 })); } 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]); } 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; }