#include <bits/stdc++.h>
using namespace std;
int N, M;
int S[1510][1510];
long long F[1510][1510], ans;
deque<pair<long long, int> > Q;
int main() {
  scanf("%d%d", &N, &M);
  memset(S, 0, sizeof(S));
  for (int i = 1; i <= N; i++)
    for (int j = 1, k; j <= M; j++) {
      scanf("%d", &k);
      S[i][j] = S[i][j - 1] + k;
    }
  memset(F, 0, sizeof(F));
  Q.push_front(make_pair(0, 1));
  for (int i = 1; i <= N; i++) {
    if (i & 1) {
      for (int j = M; j > 1; j--) {
        while (!Q.empty() && Q.front().second >= j) Q.pop_front();
        F[i][j] = S[i][j] + Q.front().first;
      }
    } else {
      for (int j = 1; j < M; j++) {
        while (!Q.empty() && Q.front().second <= j) Q.pop_front();
        F[i][j] = S[i][j] + Q.front().first;
      }
    }
    Q.clear();
    if (i & 1) {
      for (int j = M; j > 1; j--)
        if (Q.empty() || F[i][j] > Q.front().first)
          Q.push_front(make_pair(F[i][j], j));
    } else {
      for (int j = 1; j < M; j++)
        if (Q.empty() || F[i][j] > Q.front().first)
          Q.push_front(make_pair(F[i][j], j));
    }
  }
  if (N & 1) {
    ans = F[N][2];
    for (int i = 3; i <= M; i++) ans = max(ans, F[N][i]);
  } else {
    ans = F[N][1];
    for (int i = 2; i < M; i++) ans = max(ans, F[N][i]);
  }
  cout << ans << endl;
  return 0;
}
