#include using namespace std; int B[111][111]; int A[111][111]; int R, C; stack> st; int calc(int ni, int nj, int di, int dj) { ni += di; nj += dj; int ret = 0; while (true) { if (ni <= 0 || ni > R || nj <= 0 || nj > C) break; if (B[ni][nj] <= 0) break; ret += B[ni][nj]; B[ni][nj] = 0; st.emplace(ni, nj); ni += di; nj += dj; } return ret; } void fuku() { while (!st.empty()) { auto [i, j] = st.top(); st.pop(); B[i][j] = A[i][j]; } } int main() { cin >> R >> C; for (auto& a : A) { fill(a, a + (C + 10), 0); } for (int i = 1; i <= R; i ++) { for (int j = 1; j <= C; j ++) { cin >> A[i][j]; } } for (int i = 0; i <= R + 1; i ++) { for (int j = 0; j <= C + 1; j ++) { B[i][j] = A[i][j]; } } int ans = 0; for (int i1 = 0; i1 <= R + 1; i1 ++) { for (int j1 = 0; j1 <= C + 1; j1 ++) { if (0 < i1 && i1 <= R && 0 < j1 && j1 <= C) continue; for (int di = -1; di < 2; di ++) { for (int dj = -1; dj < 2; dj ++) { if (di == 0 && dj == 0) continue; for (int i2 = 0; i2 <= R + 1; i2 ++) { for (int j2 = 0; j2 <= C + 1; j2 ++) { if (0 < i2 && i2 <= R && 0 < j2 && j2 <= C) continue; for (int di2 = -1; di2 < 2; di2 ++) { for (int dj2 = -1; dj2 < 2; dj2 ++) { if (di2 == 0 && dj2 == 0) continue; ans = max(ans, calc(i1, j1, di, dj) + calc(i2, j2, di2, dj2)); fuku(); } } } } } } } } cout << ans << endl; }