#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> grid(min(n, m), vector<int>(max(n, m)));
  LL tot = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int x;
      cin >> x;
      (n > m ? grid[j][i] : grid[i][j]) = x;
      tot += x;
    }
  }
  if (n > m) swap(n, m);
  if (n <= 4) {
    cout << tot << endl;
    return 0;
  }
  vector<LL> row(n), col(m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      row[i] += grid[i][j];
      col[j] += grid[i][j];
    }
  }
  LL ans = 0;
  {
    priority_queue<LL> heap;
    for (int i = 0; i < n; i++) heap.push(row[i]);
    LL curr = 0;
    for (int t = 0; t < 4; t++) {
      curr += heap.top();
      heap.pop();
    }
    ans = max(ans, curr);
  }
  {
    priority_queue<LL> heap;
    for (int j = 0; j < m; j++) heap.push(col[j]);
    LL curr = 0;
    for (int t = 0; t < 4; t++) {
      curr += heap.top();
      heap.pop();
    }
    ans = max(ans, curr);
  }
  for (int i = 0; i < n; i++) {
    priority_queue<LL> heap;
    for (int j = 0; j < m; j++) {
      heap.push(col[j] - grid[i][j]);
    }
    LL curr = row[i];
    for (int t = 0; t < 3; t++) {
      curr += heap.top();
      heap.pop();
    }
    ans = max(ans, curr);
  }
  for (int j = 0; j < m; j++) {
    priority_queue<LL> heap;
    for (int i = 0; i < n; i++) {
      heap.push(row[i] - grid[i][j]);
    }
    LL curr = col[j];
    for (int t = 0; t < 3; t++) {
      curr += heap.top();
      heap.pop();
    }
    ans = max(ans, curr);
  }
  for (int i = 0; i < n; i++) {
    for (int ii = i + 1; ii < n; ii++) {
      LL best[2] = {0, 0};
      for (int j = 0; j < m; j++) {
        LL val = col[j] - grid[i][j] - grid[ii][j];
        if (val > best[1]) swap(val, best[1]);
        if (best[1] > best[0]) swap(best[1], best[0]);
      }
      ans = max(ans, row[i] + row[ii] + best[0] + best[1]);
    }
  }
  cout << ans << endl;
  return 0;
}
