#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
long long a[maxn], x[maxn], y[maxn], c[7], d[7], ans;
int n, m;
bool cmp(long long x, long long y) { return x > y; }
void sol(int pos, int num, int flag) {
  memset(d, 0, sizeof(d));
  if (flag) {
    for (int i = 1; i <= pos; i++) {
      long long w = y[i];
      for (int j = 0; j < 4 - num; j++) {
        w -= a[(c[j] - 1) * m + i];
      }
      d[num] = w;
      sort(d, d + num + 1, cmp);
    }
    long long ac = 0;
    for (int i = 0; i < num; i++) {
      ac += d[i];
    }
    for (int j = 0; j < 4 - num; j++) {
      ac += x[c[j]];
    }
    ans = max(ans, ac);
  } else {
    for (int i = 1; i <= pos; i++) {
      long long w = x[i];
      for (int j = 0; j < 4 - num; j++) {
        w -= a[(i - 1) * m + c[j]];
      }
      d[num] = w;
      sort(d, d + num + 1, cmp);
    }
    long long ac = 0;
    for (int i = 0; i < num; i++) {
      ac += d[i];
    }
    for (int j = 0; j < 4 - num; j++) {
      ac += y[c[j]];
    }
    ans = max(ans, ac);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  long long sum = 0;
  for (int i = 1; i <= n * m; i++) {
    scanf("%lld", &a[i]);
    sum += a[i];
  }
  memset(d, 0, sizeof(d));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      x[i] += a[(i - 1) * m + j];
    }
    d[4] = x[i];
    sort(d, d + 5, cmp);
  }
  for (int i = 0; i < 4; i++) {
    ans += d[i];
  }
  long long xxx = 0;
  memset(d, 0, sizeof(d));
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      y[i] += a[(j - 1) * m + i];
    }
    d[4] = y[i];
    sort(d, d + 5, cmp);
  }
  for (int i = 0; i < 4; i++) {
    xxx += d[i];
  }
  ans = max(xxx, ans);
  if (n <= 4 || m <= 4) {
    printf("%lld\n", sum);
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    c[0] = i;
    sol(m, 3, 1);
  }
  for (int i = 1; i <= m; i++) {
    c[0] = i;
    sol(n, 3, 0);
  }
  if (n > m) {
    for (int i = 1; i <= m; i++) {
      for (int j = i + 1; j <= m; j++) {
        c[0] = i;
        c[1] = j;
        sol(n, 2, 0);
      }
    }
  } else {
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        c[0] = i;
        c[1] = j;
        sol(m, 2, 1);
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
