#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
int n, m;
int d[1 << 10][1 << 10];
int dr[1 << 10][1 << 10];
int dc[1 << 10][1 << 10];
int gg(int a1, int b1, int a2, int b2, int last) {
  int res = -(1 << 30);
  while (a1 >= 0 && b1 >= 0 && a2 < n && b2 < m) {
    last += dr[a1][b2] - dr[a1][b1];
    last += dr[a2][b2] - dr[a2][b1];
    last += dc[b2][a2 + 1] - dc[b2][a1];
    last += dc[b1][a2] - dc[b1][a1 + 2];
    last += d[a1 + 2][b1 + 1];
    res = max(res, last);
    a1 -= 2;
    b1 -= 2;
    a2 += 2;
    b2 += 2;
  }
  return res;
}
int get1(int r, int c) {
  int sz = 1;
  int a1 = r - sz;
  int b1 = c - sz;
  int a2 = r + sz;
  int b2 = c + sz;
  if (!(a1 >= 0 && b1 >= 0 && a2 < n && b2 < m)) return -(1 << 30);
  int last = d[r - 1][c - 1] + d[r - 1][c] + d[r - 1][c + 1] + d[r][c + 1] +
             d[r + 1][c - 1] + d[r + 1][c] + d[r + 1][c + 1];
  a1 -= 2;
  b1 -= 2;
  a2 += 2;
  b2 += 2;
  int res = max(last, gg(a1, b1, a2, b2, last));
  return res;
}
int get2(int r, int c) {
  int sz = 2;
  int a1 = r - sz;
  int b1 = c - sz;
  int a2 = r + sz;
  int b2 = c + sz;
  int last = d[r][c];
  int res = gg(a1, b1, a2, b2, last);
  return res;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (int)(n); ++i) {
    for (int j = 0; j < (int)(m); ++j) {
      int v;
      scanf("%d", &v);
      d[i][j] = v;
      dr[i][j + 1] = dr[i][j] + v;
      dc[j][i + 1] = dc[j][i] + v;
    }
  }
  int res = -(1 << 30);
  for (int i = 0; i < (int)(n); ++i) {
    for (int j = 0; j < (int)(m); ++j) {
      res = max(res, get1(i, j));
      res = max(res, get2(i, j));
    }
  }
  printf("%d\n", res);
  if (res == -(1 << 30)) throw 42;
  return 0;
}
