#include <bits/stdc++.h>
using namespace std;
vector<long long> v[100005];
long long h[100005], l[100005];
long long h1[100005], l1[100005];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int i, j;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      int t;
      scanf("%d", &t);
      v[i].push_back(t);
    }
  }
  for (i = 0; i < n; i++) {
    long long sum = 0;
    for (j = 0; j < m; j++) {
      sum += v[i][j];
    }
    h[i] = h1[i] = sum;
  }
  sort(h1, h1 + n, greater<long long>());
  long long s1 = 0, s2 = 0;
  for (i = 0; i < 4 && i < n; i++) {
    s1 += h1[i];
  }
  for (j = 0; j < m; j++) {
    long long sum = 0;
    for (i = 0; i < n; i++) {
      sum += v[i][j];
    }
    l[j] = l1[j] = sum;
  }
  sort(l1, l1 + m, greater<long long>());
  long long s3 = 0, s4 = 0;
  for (i = 0; i < 4 && i < n; i++) {
    s2 += l1[i];
  }
  long long sum = max(s1, s2);
  for (i = 0; i < n; i++) {
    long long tt = h[i];
    priority_queue<long long> q;
    for (j = 0; j < m; j++) {
      q.push(l[j] - v[i][j]);
    }
    for (int j = 0; j < 3 && q.size(); j++) {
      tt += q.top();
      q.pop();
    }
    sum = max(tt, sum);
  }
  for (j = 0; j < m; j++) {
    long long tt = l[j];
    priority_queue<long long> q;
    for (i = 0; i < n; i++) {
      q.push(h[i] - v[i][j]);
    }
    for (int i = 0; i < 3 && q.size(); i++) {
      tt += q.top();
      q.pop();
    }
    sum = max(tt, sum);
  }
  if (min(n, m) >= 2) {
    if (n > m) {
      for (j = 0; j < m - 1; j++) {
        for (int k = j + 1; k < m; k++) {
          long long tt = l[j] + l[k];
          for (i = 0; i < n; i++) {
            h[i] -= v[i][j] + v[i][k];
          }
          long long ma1 = -1, ma2 = -1;
          for (i = 0; i < n; i++) {
            if (h[i] > ma1) {
              ma2 = ma1;
              ma1 = h[i];
            } else if (h[i] > ma2) {
              ma2 = h[i];
            }
          }
          for (i = 0; i < n; i++) {
            h[i] += v[i][j] + v[i][k];
          }
          sum = max(tt + ma1 + ma2, sum);
        }
      }
    } else {
      for (j = 0; j < n - 1; j++) {
        for (int k = j + 1; k < n; k++) {
          long long tt = h[j] + h[k];
          for (i = 0; i < m; i++) {
            l[i] -= v[j][i] + v[k][i];
          }
          long long ma1 = -1, ma2 = -1;
          for (i = 0; i < m; i++) {
            if (l[i] > ma1) {
              ma2 = ma1;
              ma1 = l[i];
            } else if (l[i] > ma2) {
              ma2 = l[i];
            }
          }
          for (i = 0; i < m; i++) {
            l[i] += v[j][i] + v[k][i];
          }
          sum = max(tt + ma1 + ma2, sum);
        }
      }
    }
  }
  printf("%lld\n", sum);
}
