#include using namespace std; struct p{ double x,y; }; p psub(p a,p b){ return {a.x-b.x,a.y-b.y};} double dst(p a, p b, p c, p d){ a = psub(a,b); c = psub(c,d); double ret = a.x*c.y - a.y*c.x; return ret; } pair crossP(p a, p b, p c, p d){ double D = dst(b,a,d,c), s = dst(c,a,d,c), t = dst(b,a,a,c); s /= D; t /= D; if(D == 0.0) return {-1e9,-1e9}; if(s < 0.0 || 1.0 < s || t < 0.0 || 1.0 < t){return {-1e9,-1e9};} return {a.x + s*(b.x-a.x), a.y + s*(b.y-a.y)}; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int H,W; cin >> H >> W; if(W == 1) swap(H,W); vector> A(H,vector(W)); for(auto &h : A) for(auto &w : h) cin >> w; if(H == 1){ int sum = 0; for(auto h : A) for(auto w : h) sum += w; cout << sum << endl; return 0; } vector> beam; for(int i=0; i= H || y < 0 || y >= W){ i2 = x-a; k2 = y-b; break; } } if(!one && grid == 1) continue; if(one && grid == 1) one = false; beam.push_back({i,k,i2,k2,a,b,p}); } } } int bs = beam.size(),answer = 0; for(int i=0; i> ab,comp; ab.insert({a1,b1}); ab.insert({a2,b2}); comp.insert({-1,1}); comp.insert({1,1}); if(ab == comp && (x1+y1)%2 != (x3+y3)%2) check = false; if(check){ auto[cx,cy] = crossP(p{x1+0.0,y1+0.0},p{x2+0.0,y2+0.0},p{x3+0.0,y3+0.0},p{x4+0.0,y4+0.0}); if(cx >= 0 && cx < H && cy >= 0 && cy < W) now -= A.at(cx).at(cy); } answer = max(answer,now); } } cout << answer << endl; }