#include <bits/stdc++.h>
using namespace std;
const int N = 100004;
priority_queue<long long, vector<long long>, greater<long long>> q;
long long sr[N], sc[N], z[N], b[N];
int n, m;
long long &a(int i, int j) { return z[(i - 1) * m + j]; }
long long fun1() {
  while (!q.empty()) q.pop();
  for (int i = 1; i <= n; i++) {
    long long s = 0;
    for (int j = 1; j <= m; j++) s += a(i, j);
    q.push(s);
    if (q.size() > 4) q.pop();
  }
  long long s = 0;
  while (!q.empty()) {
    s += q.top();
    q.pop();
  }
  return s;
}
long long fun2() {
  long long ans = 0;
  memset(sr, 0, sizeof(sr));
  memset(sc, 0, sizeof(sc));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      sr[i] += a(i, j);
      sc[j] += a(i, j);
    }
  for (int i = 1; i <= n; i++) {
    long long nw = sr[i];
    while (!q.empty()) q.pop();
    for (int j = 1; j <= m; j++) {
      q.push(sc[j] - a(i, j));
      if (q.size() > 3) q.pop();
    }
    while (!q.empty()) {
      nw += q.top();
      q.pop();
    }
    ans = max(ans, nw);
  }
  return ans;
}
void T() {
  int p = 0;
  for (int j = 1; j <= m; j++)
    for (int i = 1; i <= n; i++) b[++p] = a(i, j);
  for (int i = 1; i <= n * m; i++) z[i] = b[i];
  swap(n, m);
}
long long fun3() {
  long long ans = 0;
  memset(sr, 0, sizeof(sr));
  memset(sc, 0, sizeof(sc));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      sr[i] += a(i, j);
      sc[j] += a(i, j);
    }
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      long long nw = sr[i] + sr[j];
      while (!q.empty()) q.pop();
      for (int k = 1; k <= m; k++) {
        q.push(sc[k] - a(i, k) - a(j, k));
        if (q.size() > 2) q.pop();
      }
      while (!q.empty()) {
        nw += q.top();
        q.pop();
      }
      ans = max(ans, nw);
    }
  return ans;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%lld", &a(i, j));
  long long ans = 0;
  ans = max(ans, fun1());
  ans = max(ans, fun2());
  T();
  ans = max(ans, fun1());
  ans = max(ans, fun2());
  if (n > m) T();
  ans = max(ans, fun3());
  printf("%lld\n", ans);
}
