#include using namespace std; using ll = long long; using pll = pair; #define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; void solve(){ int h, w; cin >> h >> w; vector> a(h+2, vector(w+2, 0)); for(int i=1; i<=h; i++) for(int j=1; j<=w; j++) cin >> a[i][j]; int ans = 0; int di[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; int dj[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; vector> E; for(int i=0; i<=h+1; i++) for(int j=0; j<=w+1; j++){ if(!(i==0 || i==h+1 || j==0 || j==w+1)) continue; if(i==h+1){ if(j!=0 && j!=w+1) continue; } E.pb({i, j}); } vector> visited(h+2, vector(w+2, false)); for(auto [i0, j0] : E){ for(auto [i1, j1] : E){ for(int k0=0; k0<8; k0++) for(int k1=0; k1<8; k1++){ int sum = 0; { int now_i = i0, now_j = j0; while(true){ now_i = now_i + di[k0], now_j = now_j + dj[k0]; if(now_i<=0 || now_i>=h+1 || now_j<=0 || now_j>=w+1) break; sum += a[now_i][now_j]; visited[now_i][now_j] = true; } } { int now_i = i1, now_j = j1; while(true){ now_i = now_i + di[k1], now_j = now_j + dj[k1]; if(now_i<=0 || now_i>=h+1 || now_j<=0 || now_j>=w+1) break; if(!visited[now_i][now_j]) sum += a[now_i][now_j]; } } { int now_i = i0, now_j = j0; while(true){ now_i = now_i + di[k0], now_j = now_j + dj[k0]; if(now_i<=0 || now_i>=h+1 || now_j<=0 || now_j>=w+1) break; visited[now_i][now_j] = false; } } ans = max(ans, sum); } } } cout << ans << '\n'; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }