#include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 998244353; // constexpr int MOD = 1000000007; constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}; constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; int main() { int h, w; cin >> h >> w; vector a(h + 2, vector(w + 2, 0)); FOR(i, 1, h + 1) FOR(j, 1, w + 1) cin >> a[i][j]; vector> outside; REP(j, w + 2) outside.emplace_back(0, j); FOR(i, 1, h + 1) outside.emplace_back(i, 0); FOR(i, 1, h + 1) outside.emplace_back(i, w + 1); REP(j, w + 2) outside.emplace_back(h + 1, j); const int n = outside.size(); int ans = 0; vector num(h + 2, vector(w + 2, 0)); REP(i, n) FOR(dy1, -1, 2) FOR(dx1, -1, 2) { if (dy1 == 0 && dx1 == 0) continue; int score = 0; for (auto [y, x] = outside[i]; 0 <= y && y < h + 2 && 0 <= x && x < w + 2; y += dy1, x += dx1) { ++num[y][x]; score += a[y][x]; } FOR(j, i + 1, n) FOR(dy2, -1, 2) FOR(dx2, -1, 2) { if (dy2 == 0 && dx2 == 0) continue; int final_score = score; for (auto [y, x] = outside[j]; 0 <= y && y < h + 2 && 0 <= x && x < w + 2; y += dy2, x += dx2) { if (num[y][x]++ == 0) final_score += a[y][x]; } chmax(ans, final_score); for (auto [y, x] = outside[j]; 0 <= y && y < h + 2 && 0 <= x && x < w + 2; y += dy2, x += dx2) { --num[y][x]; } } for (auto [y, x] = outside[i]; 0 <= y && y < h + 2 && 0 <= x && x < w + 2; y += dy1, x += dx1) { --num[y][x]; } } cout << ans << '\n'; return 0; }