#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 1;
const long long INF = (long long)1e18;
int n, m;
vector<vector<int> > a, b;
vector<long long> sumRow, sumCol;
long long getZeroRow() {
  vector<long long> tmp = sumCol;
  sort(tmp.begin(), tmp.end());
  long long res = 0;
  for (int i = 1; i <= 4; ++i) {
    if (tmp.empty()) break;
    res += tmp.back();
    tmp.pop_back();
  }
  return res;
}
long long getOneRow() {
  long long res = 0;
  for (int i = 1; i <= n; ++i) {
    long long sum1 = 0, sum2 = 0, sum3 = 0;
    long long total = sumRow[i];
    for (int j = 1; j <= m; ++j) {
      long long tmp = sumCol[j] - a[i][j];
      if (tmp >= sum1) {
        sum3 = sum2, sum2 = sum1, sum1 = tmp;
      } else if (tmp >= sum2) {
        sum3 = sum2, sum2 = tmp;
      } else if (tmp >= sum3) {
        sum3 = tmp;
      }
    }
    total += sum1 + sum2 + sum3;
    res = max(res, total);
  }
  return res;
}
long long getTwoRow() {
  long long res = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      long long total = sumRow[i] + sumRow[j];
      long long sum1 = 0, sum2 = 0;
      for (int k = 1; k <= m; ++k) {
        long long tmp = sumCol[k] - a[i][k] - a[j][k];
        if (tmp >= sum1) {
          sum2 = sum1, sum1 = tmp;
        } else if (tmp >= sum2) {
          sum2 = tmp;
        }
      }
      total += sum1 + sum2;
      res = max(res, total);
    }
  }
  return res;
}
long long getThreeRow() {
  long long res = 0;
  for (int j = 1; j <= m; ++j) {
    long long total = sumCol[j];
    long long sum1 = 0, sum2 = 0, sum3 = 0;
    for (int i = 1; i <= n; ++i) {
      long long tmp = sumRow[i] - a[i][j];
      if (tmp >= sum1) {
        sum3 = sum2, sum2 = sum1, sum1 = tmp;
      } else if (tmp >= sum2) {
        sum3 = sum2, sum2 = tmp;
      } else if (tmp >= sum3) {
        sum3 = tmp;
      }
    }
    total += sum1 + sum2 + sum3;
    res = max(res, total);
  }
  return res;
}
long long getFourRow() {
  sort(sumRow.begin(), sumRow.end());
  long long res = 0;
  for (int i = 1; i <= 4; ++i) {
    if (sumRow.empty()) break;
    res += sumRow.back();
    sumRow.pop_back();
  }
  return res;
}
void solve() {
  cin >> n >> m;
  a.assign(min(n, m) + 1, vector<int>(max(n, m) + 1, 0));
  b.assign(n + 1, vector<int>(m + 1, 0));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> b[i][j];
      if (n <= m)
        a[i][j] = b[i][j];
      else
        a[j][n - i + 1] = b[i][j];
    }
  }
  if (n > m) swap(n, m);
  sumRow.assign(n + 1, 0);
  sumCol.assign(m + 1, 0);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      sumRow[i] += a[i][j];
      sumCol[j] += a[i][j];
    }
  }
  long long res = 0;
  res = max(res, getZeroRow());
  res = max(res, getOneRow());
  res = max(res, getTwoRow());
  res = max(res, getThreeRow());
  res = max(res, getFourRow());
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
