#include <bits/stdc++.h>
#pragma comment(linker, "/stack:64000000")
using namespace std;
template <typename first>
inline first abs(const first& a) {
  return a < 0 ? -a : a;
}
template <typename first>
inline first sqr(const first& a) {
  return a * a;
}
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-9;
const int INF = 1000 * 1000 * 1000;
const int N = 500 + 3;
int n, m;
int a[N][N];
int srow[N][N], scol[N][N];
int ans = -INF;
int d[2][N][N];
int sumR(int i, int lf, int rg) {
  return srow[i][rg] - (lf == 0 ? 0 : srow[i][lf - 1]);
}
int sumC(int j, int lf, int rg) {
  return scol[rg][j] - (lf == 0 ? 0 : scol[lf - 1][j]);
}
void calc2() {
  memset(d, 0, sizeof(d));
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++)
      if (i + 4 < n && j + 4 < m) {
        d[0][i][j] = sumR(i, j, j + 4) + sumR(i + 4, j, j + 4) +
                     sumC(j + 4, i + 1, i + 3) + sumR(i + 2, j, j + 2) +
                     a[i + 3][j];
        ans = max(ans, d[0][i][j]);
      }
  int cur = 1;
  for (int len = 9; len <= min(n, m); len += 4) {
    for (int i = 0; i < int(n); i++)
      for (int j = 0; j < int(m); j++)
        if (i + len - 1 < n && j + len - 1 < m) {
          int sum = sumR(i, j, j + len - 1);
          sum += sumR(i + len - 1, j, j + len - 1);
          sum += sumC(j, i + 2, i + len - 2);
          sum += sumC(j + len - 1, i + 1, i + len - 2);
          sum += sumR(i + 2, j + 1, j + 1);
          sum += d[cur ^ 1][i + 2][j + 2];
          d[cur][i][j] = sum;
          ans = max(ans, sum);
        }
    cur ^= 1;
  }
}
void calc1() {
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++)
      if (i + 2 < n && j + 2 < m) {
        d[0][i][j] =
            sumR(i, j, j + 2) + sumR(i + 2, j, j + 2) + a[i + 1][j + 2];
        ans = max(ans, d[0][i][j]);
      }
  int cur = 1;
  for (int len = 7; len <= min(n, m); len += 4) {
    for (int i = 0; i < int(n); i++)
      for (int j = 0; j < int(m); j++)
        if (i + len - 1 < n && j + len - 1 < m) {
          int sum = sumR(i, j, j + len - 1);
          sum += sumR(i + len - 1, j, j + len - 1);
          sum += sumC(j, i + 2, i + len - 2);
          sum += sumC(j + len - 1, i + 1, i + len - 2);
          sum += sumR(i + 2, j + 1, j + 1);
          sum += d[cur ^ 1][i + 2][j + 2];
          d[cur][i][j] = sum;
          ans = max(ans, sum);
        }
    cur ^= 1;
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++) scanf("%d", &a[i][j]);
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++)
      srow[i][j] = a[i][j] + (j == 0 ? 0 : srow[i][j - 1]);
  for (int j = 0; j < int(m); j++)
    for (int i = 0; i < int(n); i++)
      scol[i][j] = a[i][j] + (i == 0 ? 0 : scol[i - 1][j]);
  calc1();
  calc2();
  cout << ans << endl;
  return 0;
}
