#include using namespace std; typedef long long ll; typedef long double ld; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } else { return false; } } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } else { return false; } } #define rep(i, s, n) for(int i = (s); i < n; i++) #define reps(i, s, n) for(int i = (s); i <= n; i++) #define rrep(i, n, s) for(int i = n; i >= (s); i--) #define all(x) (x).begin(), (x).end() #define print(V) for (int z = 0; z < (V).size(); z++) { cout << V[z] << " "; } cout << endl; #define MOD 998244353 #define MOD2 1000000007 #define INF 1LL << 60 #define PI acos(-1) int h, w; vector< vector > A; vector< vector > seen; vector< vector > vec; int vx[8] = { -1, -1, 0, 1, 1, 1, 0, -1 }; int vy[8] = { 0, -1, -1, -1, 0, 1, 1, 1 }; ll calc(int x, int y, int s, bool flag) { ll res = 0; while (1) { x += vx[s]; y += vy[s]; if (x < 0 || x >= h + 2) { break; } if (y < 0 || y >= w + 2) { break; } if (!seen[x][y]) { res += A[x][y]; if (flag) { seen[x][y] = true; } } } return res; } ll solve(vector d1, vector d2) { ll res = 0; ll d1_calc = 0, d2_calc = 0; rep(i, 0, 8) { seen.assign(h + 2, vector(w + 2, false)); d1_calc = calc(d1[0], d1[1], i, true); rep(j, 0, 8) { d2_calc = calc(d2[0], d2[1], j, false); chmax(res, d1_calc + d2_calc); } } return res; } int main(void) { cin >> h >> w; A.assign(h + 2, vector(w + 2)); rep(i, 1, h + 1) { rep(j, 1, w + 1) { cin >> A[i][j]; } } vector p(2); //[0]: x座標, [1]: y座標 rep(i, 0, h + 2) { if (i == 0 || i == h + 1) { rep(j, 0, w + 2) { p[0] = i; p[1] = j; vec.push_back(p); } } else { for (int j = 0; j < w + 2; j += (w + 1)) { p[0] = i; p[1] = j; vec.push_back(p); } } } //rep(i, 0, vec.size()) { // print(vec[i]); //} seen.assign(h + 2, vector(w + 2, false)); int vs = vec.size(); ll ans = 0; rep(i, 0, vs) { rep(j, 0, vs) { if (i == j) { continue; } chmax(ans, solve(vec[i], vec[j])); } } cout << ans << endl; return 0; }