#include<iostream>
#define ll long long

using namespace std;

const ll inf = (ll)1e18;
ll mat[1505][1505],dp[1505][1505],sp[1505][1505];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=m;++j)
		{
			cin>>mat[i][j];
			dp[i][j]=-inf;
			sp[i][j]=sp[i][j-1]+mat[i][j];
		}
	}
	ll ans=-inf,aux;
	for(int i=1;i<=n;i++)
	{
		if(i%2==1)
		{
			aux=dp[i-1][1];
			for(int j=2;j<=m;j++)
			{
				dp[i][j]=sp[i][j]+aux;
				aux=max(aux,dp[i-1][j]);
				if(i==n)
				{
				    ans=max(ans,dp[i][j]);
				}
			}
		}
		else
		{
			aux=dp[i-1][m];
			for(int j=m-1;j>=1;j--)
			{
				dp[i][j]=sp[i][j]+aux;
				aux=max(aux,dp[i-1][j]);
				if(i==n)
				{
				    ans=max(ans,dp[i][j]);
				}
			}
		}
	}
	cout<<ans;
	return 0;
}


#include<iostream>

using namespace std;

int mat[1505][1505],dp[1505][1505],sp[1505][1505];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=m;++j)
		{
			cin>>mat[i][j];
			dp[i][j]=-15000005;
			sp[i][j]=sp[i][j-1]+mat[i][j];
		}
	}
	int ans=-15000005,aux;
	for(int i=1;i<=n;i++)
	{
		if(i%2==1)
		{
			aux=dp[i-1][1];
			for(int j=2;j<=m;j++)
			{
				dp[i][j]=sp[i][j]+aux;
				aux=max(aux,dp[i-1][j]);
				if(i==n)
				{
				    ans=max(ans,dp[i][j]);
				}
			}
		}
		else
		{
			aux=dp[i-1][m];
			for(int j=m-1;j>=1;j--)
			{
				dp[i][j]=sp[i][j]+aux;
				aux=max(aux,dp[i-1][j]);
				if(i==n)
				{
				    ans=max(ans,dp[i][j]);
				}
			}
		}
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
#define int long long
#define ull  unsigned long long
#define pb push_back

using namespace std;
using namespace __gnu_pbds;
typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = 2100;
int dp[N][N], f[N], a[N][N];
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n, m;
    int ans = -1e9;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) cin >> a[i][j];
    for (int i = 1; i <= n; i++){
        int x = 0;
        int y = 0;
        int t = 1;
        if (i % 2 == 1){
            t = 2;
            dp[i][1] = -1e9;
            x = a[i][1];
        }
        for (int j = t; j <= m; j++){
            x += a[i][j];
            if (i % 2) y = f[j - 1]; else y = f[j + 1];
            dp[i][j] = x + y;
            if (i == n) ans = max(ans, dp[i][j]);
        }
        if (i % 2 == 0){
            f[0] = -1e9;
            for (int j = 1; j <= m; j++){
                f[j] = -1e9;
                f[j] = max(f[j - 1], dp[i][j]);
            }
        } else{
            f[m + 1] = -1e9;
            for (int j = m; j >= 1; j--){
                f[j] = -1e9;
                f[j] = max(f[j + 1], dp[i][j]);
            }
        }
    }
    cout << ans;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
#define int long long
#define ull  unsigned long long
#define pb push_back

using namespace std;
using namespace __gnu_pbds;
typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = 2100;
int dp[N][N], f[N], a[N][N];
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n, m;
    int ans = -1e9;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) cin >> a[i][j];
    for (int i = 1; i <= n; i++){
        int x = 0;
        int y = 0;
        for (int j = 1; j <= n; j++){
            x += a[i][j];
            if (i % 2) y = f[j - 1]; else y = f[j + 1];
            dp[i][j] = x + y;
            if (i == n) ans = max(ans, dp[i][j]);
        }
        if (i % 2 == 0){
            f[0] = -1e9;
            for (int j = 1; j <= n; j++){
                f[j] = -1e9;
                f[j] = max(f[j - 1], dp[i][j]);
            }
        } else{
            f[n + 1] = -1e9;
            for (int j = n; j >= 1; j--){
                f[j] = -1e9;
                f[j] = max(f[j + 1], dp[i][j]);
            }
        }
    }
    cout << ans;
}


#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

typedef long long int ll;

int n,m;
int x;
ll v[2000][2000];
ll sol[2000][2000];
ll acum[2000][2000];

int main()
{
  //ios::sync_with_stdio(false);
  //cin>>n>>m;
  scanf("%d %d",&n,&m);
  for (int i=1;i<=n;i++) {
    for (int j=1;j<=m;j++) {
      //cin>>x;
      scanf("%d",&x);
      v[i][j]=x;
      v[i][j]+=v[i][j-1];
    }
  }
  for (int j=2;j<=m;j++)
    sol[1][j]=v[1][j];
  acum[1][m]=sol[1][m];
  for (int j=m-1;j>=2;j--)
    acum[1][j]=max(sol[1][j],acum[1][j+1]);
  for (int i=2;i<=n;i++) {
    if (i%2==0) {
      for (int j=1;j<=m-1;j++)
	sol[i][j]=v[i][j]+acum[i-1][j+1];
      acum[i][1]=sol[i][1];
      for (int j=2;j<=m-1;j++)
	acum[i][j]=max(sol[i][j],acum[i][j-1]);
    } else {
      for (int j=2;j<=m;j++)
	sol[i][j]=v[i][j]+acum[i-1][j-1];
      acum[i][m]=sol[i][m];
      for (int j=m-1;j>=2;j--)
	acum[i][j]=max(sol[i][j],acum[i][j+1]);
    }
  }
  int jini,jfin;
  ll solfin;
  if (n%2==0) {
    jini=1;jfin=m-1;
    solfin=sol[n][1];
  } else {
    jini=2;jfin=m;
    solfin=sol[n][2];
  }
  for (int j=jini;j<=jfin;j++)
    solfin=max(solfin,sol[n][j]);
  cout<<solfin<<endl;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;

#define REDL(a ,n) ll a[n];for(ll i=0;i<n;i++) cin>>a[i];
#define pb push_back
#define fi first
#define se second
typedef  long long ll;

typedef pair<ll,ll> pairll;

void coordinate_compress(ll a[], ll n)
{
    ll b[n];
    for(ll i=0; i<n; i++)
        b[i]=a[i];
    sort(b, b+n);
    // ll c[n];
    //  memset(c, 0, sizeof(c));
    for(ll i=0; i<n; i++)
    {
        a[i]=lower_bound(b, b+n, a[i])-b;//+c[a[i]];
        // c[a[i]]++;
    }
}
const int maxn=1600;
ll a[maxn][maxn];
ll pref[maxn];
ll suff[maxn];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin>>n>>m;
    ll a[n][m];
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            cin>>a[i][j];

    for(int i=0; i<n; i++)
        for(int j=1; j<m; j++)
            a[i][j]+=a[i][j-1];

    for(int i=n-2; i>=0; i--)
    {
        pref[0]=a[i+1][0];
        for(int j=1;j<m;j++)
            pref[j]=max(a[i+1][j] , pref[j-1]);
        suff[m-1]=a[i+1][m-1];
        for(int j=m-2;j>=0;j--)
            suff[j]=max(a[i+1][j] , suff[j+1]);
        for(int j=0; j<m; j++)
        {
            if(i%2==0)
            {
                if(j!=0)
                    a[i][j]=a[i][j]+pref[j-1];
            }

            else
            {
                if(j!=m-1);
                a[i][j]=a[i][j]+suff[j+1];
            }

        }
    }

            ll mm=LLONG_MIN;
            int index=-1;
    for(int i=1; i<m; i++)
        if(mm<a[0][i])

        {
            mm=a[0][i];
            index=i;
        }
   /* ll sum=index;
    for(int i=1; i<n; i++)
    {

        if(i%2==0)
        {
             mm=LLONG_MIN;
             ll index2=-1;
            for(int j=index+1; j<m; j++)
                if(mm<a[i][j])

                {
                    mm=a[i][j];
                    index2=j;
                }
            index=index2;
            sum+=index;
        }

        else
        {
             mm=LLONG_MIN;
            ll index2=-1;
            for(int j=0; j<index; j++)
                if(mm<a[i][j])

                {
                    mm=a[i][j];
                    index2=j;
                }
            index=index2;
            sum+=index;
        }

    }*/
    cout<<mm<<endl;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;

#define REDL(a ,n) ll a[n];for(ll i=0;i<n;i++) cin>>a[i];
#define pb push_back
#define fi first
#define se second
typedef  long long ll;

typedef pair<ll,ll> pairll;

void coordinate_compress(ll a[], ll n)
{
    ll b[n];
    for(ll i=0; i<n; i++)
        b[i]=a[i];
    sort(b, b+n);
    // ll c[n];
    //  memset(c, 0, sizeof(c));
    for(ll i=0; i<n; i++)
    {
        a[i]=lower_bound(b, b+n, a[i])-b;//+c[a[i]];
        // c[a[i]]++;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin>>n>>m;
    ll a[n][m];
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            cin>>a[i][j];

    for(int i=0; i<n; i++)
        for(int j=1; j<m; j++)
            a[i][j]+=a[i][j-1];
    for(int j=1; j<m; j++)
        a[n-1][j]=max(a[n-1][j-1], a[n-1][j]);
    for(int i=n-2; i>=0; i--)
    {
        for(int j=0; j<m; j++)
        {
            if(i%2==0)
            {
                if(j!=0)
                    a[i][j]=a[i][j]+a[i+1][j-1];
            }

            else
            {
                if(j!=m-1);
                a[i][j]=a[i][j]+a[i+1][m-1];
            }

        }
    }

            ll mm=LLONG_MIN;
            int index=-1;
    for(int i=0; i<m; i++)
        if(mm<a[0][i])

        {
            mm=a[0][i];
            index=i;
        }
   /* ll sum=index;
    for(int i=1; i<n; i++)
    {

        if(i%2==0)
        {
             mm=LLONG_MIN;
             ll index2=-1;
            for(int j=index+1; j<m; j++)
                if(mm<a[i][j])

                {
                    mm=a[i][j];
                    index2=j;
                }
            index=index2;
            sum+=index;
        }

        else
        {
             mm=LLONG_MIN;
            ll index2=-1;
            for(int j=0; j<index; j++)
                if(mm<a[i][j])

                {
                    mm=a[i][j];
                    index2=j;
                }
            index=index2;
            sum+=index;
        }

    }*/
    cout<<mm<<endl;
}


#include <bits/stdc++.h>

using namespace std;

#define yyiizhaaaachooook ios_base::sync_with_stdio();cin.tie();cout.tie();
#define ops cout<<"ops"<<endl;

#define DIM 1507
#define DIMM 7
#define INF 100000000000007.0
#define X 200000
#define MODULO 998244353

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pllll;
typedef string str;
typedef char cr;

ll n,m;
ll a[DIM][DIM],d[DIM][DIM],p[DIM][DIM],mp[DIM][DIM],ms[DIM][DIM];
ll res;

void read()
{
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
}

void preproces()
{
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
      p[i][j]=p[i][j-1]+a[i][j];
}

void proces()
{
    for(int j=1;j<=m;j++)d[1][j]=p[1][j];

    mp[1][0]=(-INF);
    for(int j=1;j<=m;j++)mp[1][j]=max(mp[1][j-1],d[1][j]);

    ms[1][m+1]=(-INF);
    for(int j=m;j>=1;j--)ms[1][j]=max(ms[1][j+1],d[1][j]);

    for(int i=2;i<=n;i++){
     for(int j=1;j<=m;j++){
      if(i%2==0 && j<m)d[i][j]=p[i][j]+ms[i-1][j+1];
      if(i%2==1 && j>1)d[i][j]=p[i][j]+mp[i-1][j-1];
     }

     mp[i][0]=(-INF);
     for(int j=1;j<=m;j++)mp[i][j]=max(mp[i][j-1],d[i][j]);

     ms[i][m+1]=(-INF);
     for(int j=m;j>=1;j--)ms[i][j]=max(ms[i][j+1],d[i][j]);
    }
}

void print()
{
    cout<<mp[n][m]<<endl;
}

int main()
{
    yyiizhaaaachooook;

    read();

    preproces();

    proces();

    print();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define yyiizhaaaachooook ios_base::sync_with_stdio();cin.tie();cout.tie();
#define ops cout<<"ops"<<endl;

#define DIM 1507
#define DIMM 7
#define INF 100000000000007.0
#define X 200000
#define MODULO 998244353

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pllll;
typedef string str;
typedef char cr;

ll n,m;
ll a[DIM][DIM],d[DIM][DIM],p[DIM][DIM],mp[DIM][DIM],ms[DIM][DIM];
ll res;

void read()
{
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
          scanf("%lld",&a[i][j]);
        }
    }
}

void preproces()
{
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
      p[i][j]=p[i][j-1]+a[i][j];
}

void proces()
{
    for(int j=1;j<=m;j++)d[1][j]=p[1][j];

    mp[1][0]=(-INF);
    for(int j=1;j<=m;j++)mp[1][j]=max(mp[1][j-1],d[1][j]);

    ms[1][m+1]=(-INF);
    for(int j=m;j>=1;j--)ms[1][j]=max(ms[1][j+1],d[1][j]);

    for(int i=2;i<=n;i++){

     for(int j=1;j<=m;j++){
      if(i%2==0 && j<m)d[i][j]=p[i][j]+ms[i-1][j+1];
      if(i%2==1 && j>1)d[i][j]=p[i][j]+mp[i-1][j-1];
     }
     if(i%2==0)d[i][m]=(-INF);
     else d[i][1]=(-INF);

     mp[i][0]=(-INF);
     for(int j=1;j<=m;j++)mp[i][j]=max(mp[i][j-1],d[i][j]);

     ms[i][m+1]=(-INF);
     for(int j=m;j>=1;j--)ms[i][j]=max(ms[i][j+1],d[i][j]);
    }

    /*cout<<endl;
    for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++){
      cout<<d[i][j]<<' ';
     }
     cout<<endl;
    }
    cout<<endl;

    cout<<endl;
    for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++){
      cout<<mp[i][j]<<' ';
     }
     cout<<endl;
    }
    cout<<endl;

    cout<<endl;
    for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++){
      cout<<ms[i][j]<<' ';
     }
     cout<<endl;
    }
    cout<<endl;*/
}

void print()
{
    cout<<mp[n][m]<<endl;
}

int main()
{
    //yyiizhaaaachooook;

    read();

    preproces();

    proces();

    print();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define yyiizhaaaachooook ios_base::sync_with_stdio();cin.tie();cout.tie();
#define ops cout<<"ops"<<endl;

#define DIM 1507
#define DIMM 7
#define INF 100000000000007.0
#define X 200000
#define MODULO 998244353

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pllll;
typedef string str;
typedef char cr;

ll n,m;
ll a[DIM][DIM],d[DIM][DIM],p[DIM][DIM],mp[DIM][DIM],ms[DIM][DIM];
ll res;

void read()
{
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
}

void preproces()
{
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
      p[i][j]=p[i][j-1]+a[i][j];
}

void proces()
{
    for(int j=1;j<=m;j++)d[1][j]=p[1][j];

    mp[1][1]=d[1][1];
    for(int j=2;j<=m;j++)mp[1][j]=max(mp[1][j-1],d[1][j]);

    ms[1][m]=d[1][m];
    for(int j=(m-1);j>=1;j--)ms[1][j]=max(ms[1][j+1],d[1][j]);

    for(int i=2;i<=n;i++){
     for(int j=1;j<=m;j++){
      if(i%2==0 && j<m)d[i][j]=p[i][j]+ms[i-1][j+1];
      if(i%2==1 && j>1)d[i][j]=p[i][j]+mp[i-1][j-1];
     }

     mp[i][1]=d[i][1];
     for(int j=2;j<=m;j++)mp[i][j]=max(mp[i][j-1],d[i][j]);

     ms[i][m]=d[i][m];
     for(int j=(m-1);j>=1;j--)ms[i][j]=max(ms[i][j+1],d[i][j]);

    }
}

void print()
{
    cout<<mp[n][m]<<endl;
}

int main()
{
    yyiizhaaaachooook;

    read();

    preproces();

    proces();

    print();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define yyiizhaaaachooook ios_base::sync_with_stdio();cin.tie();cout.tie();
#define ops cout<<"ops"<<endl;

#define DIM 1507
#define DIMM 7
#define INF 100000000000007.0
#define X 200000
#define MODULO 998244353

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pllll;
typedef string str;
typedef char cr;

ll n,m;
ll a[DIM][DIM],d[DIM][DIM],p[DIM][DIM],mp[DIM][DIM],ms[DIM][DIM];
ll res;

void read()
{
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
        }
    }
}

void preproces()
{
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
      p[i][j]=p[i][j-1]+a[i][j];
}

void proces()
{
    for(int j=1;j<=m;j++)d[1][j]=p[1][j];

    mp[1][0]=(-INF);
    for(int j=1;j<=m;j++)mp[1][j]=max(mp[1][j-1],d[1][j]);

    ms[1][m+1]=(-INF);
    for(int j=m;j>=1;j--)ms[1][j]=max(ms[1][j+1],d[1][j]);

    for(int i=2;i<=n;i++){

     for(int j=1;j<=m;j++){
      if(i%2==0 && j<m)d[i][j]=p[i][j]+ms[i-1][j+1];
      if(i%2==1 && j>1)d[i][j]=p[i][j]+mp[i-1][j-1];
     }
     if(i%2==0)d[i][m]=(-INF);
     else d[i][1]=(-INF);

     mp[i][0]=(-INF);
     for(int j=1;j<=m;j++)mp[i][j]=max(mp[i][j-1],d[i][j]);

     ms[i][m+1]=(-INF);
     for(int j=m;j>=1;j--)ms[i][j]=max(ms[i][j+1],d[i][j]);
    }

    /*cout<<endl;
    for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++){
      cout<<d[i][j]<<' ';
     }
     cout<<endl;
    }
    cout<<endl;

    cout<<endl;
    for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++){
      cout<<mp[i][j]<<' ';
     }
     cout<<endl;
    }
    cout<<endl;

    cout<<endl;
    for(int i=1;i<=n;i++){
     for(int j=1;j<=m;j++){
      cout<<ms[i][j]<<' ';
     }
     cout<<endl;
    }
    cout<<endl;*/
}

void print()
{
    cout<<mp[n][m]<<endl;
}

int main()
{
    yyiizhaaaachooook;

    read();

    preproces();

    proces();

    print();
    return 0;
}


#include <iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<deque>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<cstdio>
#include <utility>
#include <cstring>
#include <bitset>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <functional>
using namespace std;
#define inf 92233720368547757
#define mn 1505
#define FLN "test"
#define int long long
typedef pair<int, int> ii;
//common data structures and templates
template<typename T>
struct SegmentTree{
};
//gb
int n, m;
int mxu[mn], mxd[mn], s[mn];
int f[mn][2];
//functions here
void input(){
}
void solve(){
}
//#define LOCAL 1

signed main(signed argc, char** argv) {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
#ifdef LOCAL
	   freopen(FLN".inp", "r", stdin);
	   freopen(FLN".out", "w", stdout);
#endif
  cin >> n >> m;
  mxu[0] = mxd[m+1] = -inf;
  for(int i=1; i<= n;++i){
    for(int j=1; j<= m; ++j){
      cin >> s[j]; s[j] += s[j-1];
      f[j][0] = s[j] + mxd[j+1];
      f[j][1] = s[j] + mxu[j-1];
    }

    for(int j=1; j<= m; ++j){
      mxu[j] = max(mxu[j-1], f[j][0]);
      mxd[m+1-j] = max(mxd[m+1-(j-1)], f[(m+1-j)][1]);
    }
  }
  int ans = -inf;
  if(n&1) for(int i=1; i<= m; ++i) ans = max(ans, f[i][1]);
  else for(int i=1; i<= m; ++i) ans = max(ans, f[i][0]);
  cout << ans;
	return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")

using namespace std;

#define int long long
#define double long double
#define _ << ' ' <<
#define For(i,z) for(int32_t i=0;i<(z);i++)
#define sqr(a) ((a)*(a))

#define pii pair<int, int>
#define pdd pair<double, double>
#define f first
#define s second

template<typename T, typename K> inline void umax(T &a, K b) { a = max(a, (T)b); }
template<typename T, typename K> inline void umin(T &a, K b) { a = min(a, (T)b); }

const int32_t N = 1510;
const int INF = 1e16 + 10;
//const pii NINF = make_pair(INF, INF);
const double EPS = 1e-6;
const int II = 1e9;
//const int AMOD = 99194853094755497;
const int MOD = 1e9+7;

int dp[N][N];

int32_t main() {
    // freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    For(i, N) For (j, N) dp[i][j] = -II;

    int n, m; cin >> n >> m;
    vector <vector<int> > ar(n, vector <int> (m));
    For (i, n) For (j, m) cin >> ar[i][j];
    int s = 0;
    For (j, m) {s += ar[0][j]; dp[0][j] = s;}

    For (i, n-1){
        s = 0;
        if (i % 2 == 0) {
            vector <int> mx(m, -II); mx[m-1] = dp[i][m-1];
            for (int j = m-2; j> 0; j--) mx[j] = max(mx[j+1], dp[i][j]);

            For (j, m-1) {
                s += ar[i+1][j];
                umax(dp[i+1][j], mx[j+1] + s);
            }
        } else {
            int mx = -II; s = ar[i+1][0];
            for (int j = 1; j < m; j++) {
                umax(mx, dp[i][j-1]);
                s += ar[i+1][j];
                umax(dp[i+1][j], mx + s);
            }
        }
    }

    int ans = 0;
    For (j, m)
        umax(ans, dp[n-1][j]);
    cout << ans << endl;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")

using namespace std;

#define int long long
#define double long double
#define _ << ' ' <<
#define For(i,z) for(int32_t i=0;i<(z);i++)
#define sqr(a) ((a)*(a))

#define pii pair<int, int>
#define pdd pair<double, double>
#define f first
#define s second

template<typename T, typename K> inline void umax(T &a, K b) { a = max(a, (T)b); }
template<typename T, typename K> inline void umin(T &a, K b) { a = min(a, (T)b); }

const int32_t N = 1510;
const int INF = 1e16 + 10;
//const pii NINF = make_pair(INF, INF);
const double EPS = 1e-6;
const int II = 1e9;
//const int AMOD = 99194853094755497;
const int MOD = 1e9+7;

int dp[N][N];

int32_t main() {
    // freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    For(i, N) For (j, N) dp[i][j] = -II;

    int n, m; cin >> n >> m;
    vector <vector<int> > ar(n, vector <int> (m));
    For (i, n) For (j, m) cin >> ar[i][j];
    int s = 0;
    For (j, m) {s += ar[0][j]; dp[0][j] = s;}

    For (i, n-1){
        s = 0;
        if (i % 2 == 0) {
            vector <int> mx(m, -II); mx[m-1] = dp[i][m-1];
            for (int j = m-2; j> 0; j--) mx[j] = max(mx[j+1], dp[i][j]);

            For (j, m-1) {
                s += ar[i+1][j];
                umax(dp[i+1][j], mx[j+1] + s);
            }
        } else {
            int mx = -II; s = ar[i+1][0];
            for (int j = 1; j < m; j++) {
                umax(mx, dp[i][j-1]);
                s += ar[i+1][j];
                umax(dp[i+1][j], mx + s);
            }
        }
    }

    int ans = -II;
    For (j, m)
        umax(ans, dp[n-1][j]);
    cout << ans << endl;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")

using namespace std;

#define int long long
#define double long double
#define _ << ' ' <<
#define For(i,z) for(int32_t i=0;i<(z);i++)
#define sqr(a) ((a)*(a))

#define pii pair<int, int>
#define pdd pair<double, double>
#define f first
#define s second

template<typename T, typename K> inline void umax(T &a, K b) { a = max(a, (T)b); }
template<typename T, typename K> inline void umin(T &a, K b) { a = min(a, (T)b); }

const int32_t N = 1510;
const int INF = 1e16 + 10;
//const pii NINF = make_pair(INF, INF);
const double EPS = 1e-6;
const int II = 1e9;
//const int AMOD = 99194853094755497;
const int MOD = 1e9+7;

int dp[N][N];

int32_t main() {
    // freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n, m; cin >> n >> m;
    vector <vector<int> > ar(n, vector <int> (m));
    For (i, n) For (j, m) cin >> ar[i][j];
    int s = 0;
    For (j, m) {s += ar[0][j]; dp[0][j] = s;}

    For (i, n){
        s = 0;
        if (i % 2) {
            vector <int> mx(m); mx[m-1] = dp[i][m-1];
            for (int j = m-2; j>= 0; j--) mx[j] = max(mx[j+1], dp[i][j]);
            For (j, m-1) {
                s += ar[i][j];
                umax(dp[i+1][j], mx[j+1] + s);
            }
        } else {
            int mx = 0;
            for (int j = 1; j < m; j++) {
                umax(mx, dp[i][j-1]);
                s += ar[i][j];
                umax(dp[i+1][j], mx + s);
            }
        }
    }

    int ans = 0;
    For (j, m)
        umax(ans, dp[n-1][j]);
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define md 1000000007
#define mx 1e18
#define pb push_back
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int  main()
{
	fastio
	#ifndef ONLINE_JUDGE
	freopen("input.txt" , "r" , stdin);
	freopen("output.txt" , "w" , stdout);
	#endif

	ll int n,m;
	cin>>n>>m;

	ll int ar[n+1][m+1];
	memset(ar,0,sizeof(ar));
	for(ll int i=1;i<=n;i++)
		for(ll int j=1;j<=m;j++)
			cin>>ar[i][j];

	ll int pre[n+1][m+1];
	memset(pre,0,sizeof(pre));
	for(ll int i=1;i<=n;i++)
		for(ll int j=1;j<=m;j++)
			pre[i][j]=pre[i][j-1]+ar[i][j];

	ll int dp[n+1][m+1],pdp[n+1][m+1],sdp[n+1][m+1];
	dp[1][1]=ar[1][1];
	for(ll int i=2;i<=m;i++){
		dp[1][i]=pre[1][i];
		pdp[1][i] = max(pdp[1][i-1] , dp[1][i]);
	}
	sdp[1][m]=dp[1][m];
	for(ll int i=m-1;i>=1;i--)
		sdp[1][i] = max(sdp[1][i+1] , dp[1][i]);

	for(ll int i=2;i<=n;i++)
	{
		for(ll int j=1;j<=m;j++)
		{
			if(i%2==0 && j<m)
			{
				dp[i][j] = pre[i][j] + sdp[i-1][j+1];
			}
			if(i%2==1 && j>1)
			{
				dp[i][j] = pre[i][j] + pdp[i-1][j-1];
			}
		}

		pdp[i][1] = dp[i][1];
		for(ll int j=2;j<=m;j++)
			pdp[i][j] = max(pdp[i][j-1] , dp[i][j]);
		sdp[i][m] = dp[i][m];
		for(ll int j=m-1;j>=1;j--)
			sdp[i][j] = max(sdp[i][j+1] , dp[i][j]);
	}

	ll int ans=INT_MIN;

	for(ll int i=1;i<=m;i++)
	{
		if(n%2==0)
		{
			if(i==m)continue;
			ans=max(ans,dp[n][i]);
		}
		else
		{
			if(i==1)continue;
			ans=max(ans,dp[n][i]);
		}
	}

	// for(ll int i=1;i<=n;i++)
	// {
	// 	for(ll int j=1;j<=m;j++)
	// 	{
	// 		cout<<dp[i][j]<<" ";
	// 	}
	// 	cout<<endl;
	// }

	// for(ll int i=1;i<=n;i++)
	// {
	// 	for(ll int j=1;j<=m;j++)
	// 	{
	// 		cout<<pre[i][j]<<" ";
	// 	}
	// 	cout<<endl;
	// }

	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define md 1000000007
#define mx 1e18
#define pb push_back
// #define read(n) scanf("%d",&n)
// #define prll int (x) prll int f("%d",x)
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int  main()
{
	fastio
	#ifndef ONLINE_JUDGE
	freopen("input.txt" , "r" , stdin);
	freopen("output.txt" , "w" , stdout);
	#endif

	ll int  n,m;
	cin>>n>>m;

	ll int  ar[1505][1505];
	memset(ar,0,sizeof(ar));

	ll int  pre[1505][1505];
	memset(pre,0,sizeof(pre));

	for(ll int  i=1;i<=n;i++)
	{
		for(ll int  j=1;j<=m;j++)
		{
			cin>>ar[i][j];
		}
	}

	memset(pre , 0 ,sizeof(pre));

	for(ll int  i=1;i<=n;i++)
	{
		for(ll int  j=1;j<=m;j++)
		{
			pre[i][j] = pre[i][j-1] + ar[i][j];
		}
	}

	ll int  dp[n+1][m+1];
	memset(dp , 0 , sizeof(dp));

	ll int  predp[n+1][m+1],suffdp[n+1][m+1];
	memset(predp , 0 ,sizeof(predp));
	memset(suffdp , 0 ,sizeof(suffdp));

	for(ll int  i=1;i<=n;i++)
	{
		if(i%2==1)
		{
			dp[i][1] = INT_MIN;
		}
		else
		{
			dp[i][1] = ar[i][1];
		}
	}

	for(ll int  i=2;i<=m;i++)
	{
		dp[1][i] = max(dp[1][i-1] , max(pre[1][i-1], pre[1][i]));
		predp[1][i] = max(pre[1][i-1] , dp[1][i]);
	}

	suffdp[1][m] = dp[1][m];

	for(ll int  i=m-1;i>=1;i--)
	{
		suffdp[1][i] = max(suffdp[1][i+1] , dp[1][i]);
	}

	for(ll int  i=1;i<=n;i++)
	{
		for(ll int  j=1;j<=m;j++)
		{
			if(i%2==0 && j<m)
			{
				dp[i][j] = pre[i][j] + suffdp[i-1][j+1];
			}
			else if(i%2==1 && j>1)
			{
				dp[i][j] = pre[i][j] + predp[i-1][j-1];
			}
		}

		predp[i][1] = dp[i][1];
		for(ll int  j=2;j<=m;j++)
		{
			predp[i][j] = max(pre[i][j-1] , dp[i][j]);
		}

		suffdp[i][m] = dp[i][m];
		for(ll int  j=m-1;j>=1;j--)
		{
			suffdp[i][j] = max(suffdp[i][j+1] , dp[i][j]);
		}
	}

	ll int  ans=INT_MIN;

	for(ll int  i=1;i<=m;i++)
	{
		ans=max(ans , dp[n][i]);
	}

	cout<<ans<<endl;

	return 0;
}


// Problem : E. Comb
// Contest : School Personal Contest #2 (Winter Computer School 2010/11) - Codeforces Beta Round #43 (ACM-ICPC Rules)
// URL : https://codeforces.com/problemset/problem/46/E
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
#define pb push_back
#define PI acos(-1.0)
#define EPS 1e-4
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define fout(S,x)            cout<<fixed<<setprecision(x)<<(S)<<endl
#define present(c,x) ((c).find(x) != (c).end())
#define T() ll t;cin>>t;while(t--)
#define mem(input,b) memset(input,b,sizeof(input))
#define ff first
#define ss second
#define fread(input) freopen("input","r",stdin)
#define fwrite(b) freopen("b","w",stdout)
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define FOREACH(i,c) for (__typeof ((c).begin()) i = (c).begin() ; i != (c).end() ; i++)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
const int maxn = 1500 + 10;
const ll INF = 1e18;

int n, m;
int a[maxn][maxn];

ll dp[maxn][maxn];

ll pre[maxn], suf[maxn];

ll solve() {
    ll sum = 0;

    for(int j = 1; j <= m; ++j) {
	sum += a[1][j];
	dp[1][j] = sum;
    }

    for(int i = 2; i <= n; ++i) {
	sum = 0;
	pre[0] = -INF;

	for(int j = 1; j <= m; ++j)
	    pre[j] = max(pre[j - 1], dp[i - 1][j]);

	sum = 0;
	suf[m + 1] = -INF;

	for(int j = m; j >= 1; --j)
	    suf[j] = max(suf[j + 1], dp[i - 1][j]);

	sum = 0;
	for(int c = 1; c <= m; ++c) {
	    sum += a[i][c];

	    if(i % 2)
		dp[i][c] = sum + pre[c - 1];
	    else
		dp[i][c] = sum + suf[c + 1];
	}
    }

    ll ans = -INF;

    for(int c = 1; c <= m; ++c)
	ans = max(ans, dp[n][c]);

    return ans;
}

int main() {
    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; ++i)
	for(int j = 1; j <= m; ++j)
	    scanf("%d", &a[i][j]);

    printf("%lld\n", solve());
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ret return
const int mod = 1000000007;
const int Max = 1000;
ll arr[1501][1501];
ll dp[1501][1501];

// ll coinMax(int i, int n, int m, int j, int alt)
// {

//     if (i == n)
//         ret 0;

//     if (alt != 0 && dp[i][j] != -1)
//         ret dp[i][j];

//     ll coins = INT64_MIN;

//     if (alt == 0)
//     {
//         for (int c = 1; c < m; c++)
//         {
//             coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
//         }
//     }
//     else if (alt == 1) // smaller
//     {
//         for (int c = j - 1; c >= 0; c--)
//         {
//             coins = max(coinMax(i + 1, n, m, c, 2) + arr[i][c], coins);
//         }
//     }
//     else // larger
//     {
//         for (int c = j + 1; c < m; c++)
//         {
//             coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
//         }
//     }

//     ret dp[i][j] = coins;
// }

int main()
{

    int n, m;
    // scanf("%d %d", &n, &m);
    cin>>n>>m;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            // scanf("%lld" ,&arr[i][j]);
            cin>>arr[i][j];
            dp[i][j] = INT64_MIN;
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 1; j < m; j++)
        {
            arr[i][j] += arr[i][j - 1];
        }
    }

    for (int j = 0; j < m; j++)
        dp[0][j] = arr[0][j];

    int Lmax[n][m], Rmax[n][m];
    int maxind = 0;
    Lmax[0][0] = 0;
    ll maxsum = dp[0][0];
    for (int j = 1; j < m; j++)
    {
        Lmax[0][j] = maxind;
        if (dp[0][j] > maxsum)
        {
            maxsum = dp[0][j];
            maxind = j;
        }
    }

    Rmax[0][m - 1] = m - 1;
    maxind = m - 1;
    maxsum = dp[0][m - 1];
    for (int j = m - 2; j >= 0; j--)
    {
        Rmax[0][j] = maxind;
        if (dp[0][j] > maxsum)
        {
            maxsum = dp[0][j];
            maxind = j;
        }
    }

    int alt = 1;

    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (alt == 0 && j == 0 || alt == 1 && j == m - 1)
                continue;

            if (alt == 0)
            {
                dp[i][j] = arr[i][j] + dp[i - 1][Lmax[i - 1][j]];
            }
            else
            {
                dp[i][j] = arr[i][j] + dp[i - 1][Rmax[i - 1][j]];
            }
        }

        int maxind = 0;
        Lmax[i][0] = 0;
        ll maxsum = dp[i][0];
        for (int j = 1; j < m; j++)
        {
            Lmax[i][j] = maxind;
            if (dp[i][j] > maxsum)
            {
                maxsum = dp[i][j];
                maxind = j;
            }
        }

        Rmax[i][m - 1] = m - 1;
        maxind = m - 1;
        maxsum = dp[i][m - 1];
        for (int j = m - 2; j >= 0; j--)
        {
            Rmax[i][j] = maxind;
            if (dp[i][j] > maxsum)
            {
                maxsum = dp[i][j];
                maxind = j;
            }
        }

        alt ^= 1;
    }

    // cout << coinMax(0, n, m ,0, 0) << endl;
    ll ans = INT64_MIN;
    for (int j = 0; j < m; j++)
    {
        ans = max(ans, dp[n - 1][j]);
    }

    // printf("%lld\n", ans);
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ret return
const int mod = 1000000007;
const int Max = 1000;
ll arr[1501][1501];
ll dp[1501][1501];

// ll coinMax(int i, int n, int m, int j, int alt)
// {

//     if (i == n)
//         ret 0;

//     if (alt != 0 && dp[i][j] != -1)
//         ret dp[i][j];

//     ll coins = INT64_MIN;

//     if (alt == 0)
//     {
//         for (int c = 1; c < m; c++)
//         {
//             coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
//         }
//     }
//     else if (alt == 1) // smaller
//     {
//         for (int c = j - 1; c >= 0; c--)
//         {
//             coins = max(coinMax(i + 1, n, m, c, 2) + arr[i][c], coins);
//         }
//     }
//     else // larger
//     {
//         for (int c = j + 1; c < m; c++)
//         {
//             coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
//         }
//     }

//     ret dp[i][j] = coins;
// }

int main()
{

    int n, m;
    scanf("%d %d", &n, &m);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            scanf("%lld" ,&arr[i][j]);
            dp[i][j] = INT64_MIN;
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 1; j < m; j++)
        {
            arr[i][j] += arr[i][j - 1];
        }
    }

    for (int j = 0; j < m; j++)
        dp[0][j] = arr[0][j];

    int Lmax[n][m], Rmax[n][m];
    int maxind = 0;
    Lmax[0][0] = 0;
    ll maxsum = dp[0][0];
    for (int j = 1; j < m; j++)
    {
        Lmax[0][j] = maxind;
        if (dp[0][j] > maxsum)
        {
            maxsum = dp[0][j];
            maxind = j;
        }
    }

    Rmax[0][m - 1] = m - 1;
    maxind = m - 1;
    maxsum = dp[0][m - 1];
    for (int j = m - 2; j >= 0; j--)
    {
        Rmax[0][j] = maxind;
        if (dp[0][j] > maxsum)
        {
            maxsum = dp[0][j];
            maxind = j;
        }
    }

    int alt = 1;

    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (alt == 0 && j == 0 || alt == 1 && j == m - 1)
                continue;

            if (alt == 0)
            {
                dp[i][j] = arr[i][j] + dp[i - 1][Lmax[i - 1][j]];
            }
            else
            {
                dp[i][j] = arr[i][j] + dp[i - 1][Rmax[i - 1][j]];
            }
        }

        int maxind = 0;
        Lmax[i][0] = 0;
        ll maxsum = dp[i][0];
        for (int j = 1; j < m; j++)
        {
            Lmax[i][j] = maxind;
            if (dp[i][j] > maxsum)
            {
                maxsum = dp[i][j];
                maxind = j;
            }
        }

        Rmax[i][m - 1] = m - 1;
        maxind = m - 1;
        maxsum = dp[i][m - 1];
        for (int j = m - 2; j >= 0; j--)
        {
            Rmax[i][j] = maxind;
            if (dp[i][j] > maxsum)
            {
                maxsum = dp[i][j];
                maxind = j;
            }
        }

        alt ^= 1;
    }

    // cout << coinMax(0, n, m ,0, 0) << endl;
    ll ans = INT64_MIN;
    for (int j = 0; j < m; j++)
    {
        ans = max(ans, dp[n - 1][j]);
    }
    // cout << ans << endl;
    printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ret return
const int mod = 1000000007;
const int Max = 1000;
ll arr[1501][1501];
ll dp[1501][1501];
ll coinMax(int i, int n, int m, int j, int alt)
{

    if (i == n)
        ret 0;

    if (alt != 0 && dp[i][j] != -1)
        ret dp[i][j];

    ll coins = INT64_MIN;

    if (alt == 0)
    {
        for (int c = 1; c < m; c++)
        {
            coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
        }
    }
    else if (alt == 1) // smaller
    {
        for (int c = j - 1; c >= 0; c--)
        {
            coins = max(coinMax(i + 1, n, m, c, 2) + arr[i][c], coins);
        }
    }
    else // larger
    {
        for (int c = j + 1; c < m; c++)
        {
            coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
        }
    }

    ret dp[i][j] = coins;
}

int main()
{

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> arr[i][j];
            dp[i][j] = INT64_MIN;
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 1; j < m; j++)
        {
            arr[i][j] += arr[i][j - 1];
        }
    }

    for (int j = 0; j < m; j++)
        dp[0][j] = arr[0][j];

    int Lmax[n][m], Rmax[n][m];
    int maxind = 0;
    Lmax[0][0] = 0;
    ll maxsum = dp[0][0];
    for (int j = 1; j < m; j++)
    {
        Lmax[0][j] = maxind;
        if (dp[0][j] > maxsum)
        {
            maxsum = dp[0][j];
            maxind = j;
        }
    }

    Rmax[0][m - 1] = m - 1;
    maxind = m - 1;
    maxsum = dp[0][m - 1];
    for (int j = m - 2; j >= 0; j--)
    {
        Rmax[0][j] = maxind;
        if (dp[0][j] > maxsum)
        {
            maxsum = dp[0][j];
            maxind = j;
        }
    }

    int alt = 1;

    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (alt == 0 && j == 0 || alt == 1 && j == m - 1)
                continue;

            if (alt == 0)
            {
                dp[i][j] = arr[i][j] + dp[i - 1][Lmax[i - 1][j]];
            }
            else
            {
                dp[i][j] = arr[i][j] + dp[i - 1][Rmax[i - 1][j]];
            }
        }

        int maxind = 0;
        Lmax[i][0] = 0;
        ll maxsum = dp[i][0];
        for (int j = 1; j < m; j++)
        {
            Lmax[i][j] = maxind;
            if (dp[i][j] > maxsum)
            {
                maxsum = dp[i][j];
                maxind = j;
            }
        }

        Rmax[i][m - 1] = m - 1;
        maxind = m - 1;
        maxsum = dp[i][m - 1];
        for (int j = m - 2; j >= 0; j--)
        {
            Rmax[i][j] = maxind;
            if (dp[i][j] > maxsum)
            {
                maxsum = dp[i][j];
                maxind = j;
            }
        }

        alt ^= 1;
    }

    // cout << coinMax(0, n, m ,0, 0) << endl;
    ll ans = INT64_MIN;
    for (int j = 0; j < m; j++)
    {
        ans = max(ans, dp[n - 1][j]);
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define ret return
const int mod = 1000000007;
const int Max = 1000;
ll arr[1501][1501];
ll dp[1501][1501];

// ll coinMax(int i, int n, int m, int j, int alt)
// {

//     if (i == n)
//         ret 0;

//     if (alt != 0 && dp[i][j] != -1)
//         ret dp[i][j];

//     ll coins = INT64_MIN;

//     if (alt == 0)
//     {
//         for (int c = 1; c < m; c++)
//         {
//             coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
//         }
//     }
//     else if (alt == 1) // smaller
//     {
//         for (int c = j - 1; c >= 0; c--)
//         {
//             coins = max(coinMax(i + 1, n, m, c, 2) + arr[i][c], coins);
//         }
//     }
//     else // larger
//     {
//         for (int c = j + 1; c < m; c++)
//         {
//             coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
//         }
//     }

//     ret dp[i][j] = coins;
// }

int main()
{

    int n, m;
    // scanf("%d %d", &n, &m);
    cin>>n>>m;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            // scanf("%lld" ,&arr[i][j]);
            cin>>arr[i][j];
            dp[i][j] = INT64_MIN;
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 1; j < m; j++)
        {
            arr[i][j] += arr[i][j - 1];
        }
    }

    for (int j = 0; j < m; j++)
        dp[0][j] = arr[0][j];

    int Lmax[n][m], Rmax[n][m];
    int maxind = 0;
    Lmax[0][0] = 0;
    ll maxsum = dp[0][0];
    for (int j = 1; j < m; j++)
    {
        Lmax[0][j] = maxind;
        if (dp[0][j] > maxsum)
        {
            maxsum = dp[0][j];
            maxind = j;
        }
    }

    Rmax[0][m - 1] = m - 1;
    maxind = m - 1;
    maxsum = dp[0][m - 1];
    for (int j = m - 2; j >= 0; j--)
    {
        Rmax[0][j] = maxind;
        if (dp[0][j] > maxsum)
        {
            maxsum = dp[0][j];
            maxind = j;
        }
    }

    int alt = 1;

    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (alt == 0 && j == 0 || alt == 1 && j == m - 1)
                continue;

            if (alt == 0)
            {
                dp[i][j] = arr[i][j] + dp[i - 1][Lmax[i - 1][j]];
            }
            else
            {
                dp[i][j] = arr[i][j] + dp[i - 1][Rmax[i - 1][j]];
            }
        }

        int maxind = 0;
        Lmax[i][0] = 0;
        ll maxsum = dp[i][0];
        for (int j = 1; j < m; j++)
        {
            Lmax[i][j] = maxind;
            if (dp[i][j] > maxsum)
            {
                maxsum = dp[i][j];
                maxind = j;
            }
        }

        Rmax[i][m - 1] = m - 1;
        maxind = m - 1;
        maxsum = dp[i][m - 1];
        for (int j = m - 2; j >= 0; j--)
        {
            Rmax[i][j] = maxind;
            if (dp[i][j] > maxsum)
            {
                maxsum = dp[i][j];
                maxind = j;
            }
        }

        alt ^= 1;
    }

    // cout << coinMax(0, n, m ,0, 0) << endl;
    ll ans = INT64_MIN;
    for (int j = 0; j < m; j++)
    {
        ans = max(ans, dp[n - 1][j]);
    }

    // printf("%lld\n", ans);
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ret return
const int mod = 1000000007;
const int Max = 1000;
ll arr[1501][1501];
ll dp[1501][1501];
ll coinMax(int i, int n, int m, int j , int alt )
{

    if (i == n)
        ret 0;

    if (alt != 0 && dp[i][j] != -1)
        ret dp[i][j];

    ll coins = 0;

    if (alt == 0)
    {
        for (int c = 1; c < m; c++)
        {
            coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
        }
    }
    else if (alt == 1) // smaller
    {
        for (int c = j - 1; c >= 0; c--)
        {
            coins = max(coinMax(i + 1, n, m, c, 2) + arr[i][c], coins);
        }
    }
    else // larger
    {
        for (int c = j + 1; c < m; c++)
        {
            coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
        }
    }

    ret dp[i][j] = coins;
}

int main()
{

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> arr[i][j];
            dp[i][j] = -1;
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 1; j < m; j++)
        {
            arr[i][j] += arr[i][j - 1];
        }
    }

    cout << coinMax(0, n, m ,-1, 0) << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ret return
const int mod = 1000000007;
const int Max = 1000;
ll arr[1501][1501];
ll dp[1501][1501];
ll coinMax(int i, int n, int m, int j , int alt )
{

    if (i == n)
        ret 0;

    if (alt != 0 && dp[i][j] != -1)
        ret dp[i][j];

    ll coins = INT64_MIN;

    if (alt == 0)
    {
        for (int c = 1; c < m; c++)
        {
            coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
        }
    }
    else if (alt == 1) // smaller
    {
        for (int c = j - 1; c >= 0; c--)
        {
            coins = max(coinMax(i + 1, n, m, c, 2) + arr[i][c], coins);
        }
    }
    else // larger
    {
        for (int c = j + 1; c < m; c++)
        {
            coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
        }
    }

    ret dp[i][j] = coins;
}

int main()
{

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> arr[i][j];
            dp[i][j] = -1;
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 1; j < m; j++)
        {
            arr[i][j] += arr[i][j - 1];
        }
    }

    cout << coinMax(0, n, m ,0, 0) << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ret return
const int mod = 1000000007;
const int Max = 1000;
ll arr[1501][1501];
ll dp[1501][1501];
ll coinMax(int i, int n, int m, int j , int alt )
{

    if (i == n)
        ret 0;

    if (alt != 0 && dp[i][j] != -1)
        ret dp[i][j];

    ll coins = 0;

    if (alt == 0)
    {
        for (int c = 1; c < m; c++)
        {
            coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
        }
    }
    else if (alt == 1) // smaller
    {
        for (int c = j - 1; c >= 0; c--)
        {
            coins = max(coinMax(i + 1, n, m, c, 2) + arr[i][c], coins);
        }
    }
    else // larger
    {
        for (int c = j + 1; c < m; c++)
        {
            coins = max(coinMax(i + 1, n, m, c, 1) + arr[i][c], coins);
        }
    }

    ret dp[i][j] = coins;
}

int main()
{

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> arr[i][j];
            dp[i][j] = -1;
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 1; j < m; j++)
        {
            arr[i][j] += arr[i][j - 1];
        }
    }

    cout << coinMax(0, n, m ,0, 0) << endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define sc second
#define forn(i,n) for(ll i = 1;i<=n;++i)
#define pb push_back
#define endl '\n'
#define po(x) (1ll<<x)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pp;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> satan_set;
const ll DIM = 1500+7;
const ll INF = 10E16;
ll n,m,dp[DIM][DIM],A[DIM][DIM];
int main()
{
    //freopen("input.txt","r",stdin);
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n>>m;
    forn(i,n)
        forn(j,m)
            cin>>A[i][j];
    forn(i,n)forn(j,m)dp[i][j] = -INF;
    dp[1][0] = 0;
    forn(i,m){
        dp[1][i] = dp[1][i-1]+A[1][i];
    }
    for(ll i = 2;i<=n;i++){
        if (i%2==1){
            ll mx = -INF,sum = A[i][1];
            for(ll j = 2;j<=m;j++){
                mx = max(mx,dp[i-1][j-1]);
                sum+=A[i][j];
                dp[i][j] = mx+sum;
            }
        }
        else{
            ll sum = 0;
            forn(j,m-1)sum+=A[i][j];
            ll mx = -INF;
            for(ll j = m-1;j>=1;j--){
                mx = max(mx,dp[i-1][j+1]);
                dp[i][j] = mx+sum;
                sum-=A[i][j];
            }
        }
    }
    ll res = -INF;
    forn(i,m)res = max(res,dp[n][i]);
    cout<<res<<endl;
    return 0;
}

// 1 2 3
//


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define sc second
#define forn(i,n) for(ll i = 1;i<=n;++i)
#define pb push_back
#define endl '\n'
#define po(x) (1ll<<x)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pp;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> satan_set;
const ll DIM = 3000+7;
const ll INF = 10E16;
ll n,m,dp[DIM][DIM],A[DIM][DIM];
int main()
{
    //freopen("input.txt","r",stdin);
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n>>m;
    forn(i,n)
        forn(j,m)
            cin>>A[i][j];
    forn(i,n)forn(j,m)dp[i][j] = -INF;
    dp[1][0] = 0;
    forn(i,m){
        dp[1][i] = dp[1][i-1]+A[1][i];
    }
    for(ll i = 2;i<=n;i++){
        if (i%2==1){
            ll mx = -INF,sum = 0;
            for(ll j = 2;j<=m;j++){
                mx = max(mx,dp[i-1][j-1]);
                sum+=A[i][j];
                dp[i][j] = mx+sum;
            }
        }
        else{
            ll sum = 0;
            forn(j,m-1)sum+=A[i][j];
            ll mx = -INF;
            for(ll j = m-1;j>=1;j--){
                mx = max(mx,dp[i-1][j+1]);
                dp[i][j] = mx+sum;
                sum-=A[i][j];
            }
        }
    }
    ll res = -INF;
    forn(i,m)res = max(res,dp[n][i]);
    cout<<res<<endl;
    return 0;
}

// 1 2 3
//


// Author : Sarthak Kapoor
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define repba(i,b,a) for(int i=b;i>=a;--i)
#define repab(i,a,b) for(int i=a;i<b;++i)
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define msi map<string,int>
#define mii map<int,int>
#define viip vector<pair<int,pair<int,int> > >
#define mp make_pair
#define vip vector<pair<int,int> >
#define pb push_back
#define po pop_back
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}
ll fastexp(ll x,ll a){
    ll res = 1;
    while(a>0){
        if(a&1){
            res = (res*x)%mod;
        }
        a=a>>1;
        x=(x*x)%mod;
    }
    return res;
}
ll inverse(ll n){
    return fastexp(n,mod-2);
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	#ifndef ONLINE_JUDGE
    	freopen("input.txt", "r", stdin);
    	freopen("output.txt", "w", stdout);
	#endif
	int n,m;
	cin>>n>>m;
	ll a[n][m];
	ll dp[n][m];
	// ll sum[n][m];
	rep(i,n){
		rep(j,m){
			cin>>a[i][j];
			dp[i][j]=0;
			// sum[i][j]=0;
		}
	}
	multiset<ll> s;
	dp[0][0] = a[0][0];
	s.insert(a[0][0]);
	repa(j,1,m){
		dp[0][j] = dp[0][j-1] + a[0][j];
		s.insert(dp[0][j]);
	}
	auto it = s.rbegin();
	// cout<<*it;
	const ll ninf = -1e12;
	repa(i,1,n){
		ll sum = 0;
		multiset<ll> temp;
		rep(j,m){
			if(i % 2){
				sum += a[i][j];
				auto it = s.find(dp[i-1][j]);
				if(it != s.end())s.erase(it);
				if(j == m-1){
					dp[i][j] = ninf;
					continue;
				}
				ll m1 = *(s.rbegin());
				// cout<<m1<<"\n";
				dp[i][j] = sum + m1;
				// temp.insert(dp[i][j]);
			}
			else{
				sum += a[i][j];
				ll m1 = ninf;
				if(!s.empty()){
					m1 = *(s.rbegin());
				}
				dp[i][j] = m1 + sum;
				s.insert(dp[i-1][j]);
				temp.insert(dp[i][j]);
			}
		}
		s.clear();
		if(i % 2 == 0){
			s = temp;
		}
	}
	ll ans = ninf;
	// rep(i,n){
	// 	rep(j,m){
	// 		cout<<dp[i][j]<<" ";
	// 		// ans = max(ans,dp[i][j]);
	// 	}
	// 	cout<<"\n";
	// }
	rep(i,m){
		ans = max(ans,dp[n-1][i]);
	}
	cout<<ans;
	return 0;
}


import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Zyflair Griffane
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		PandaScanner in = new PandaScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		E solver = new E();
		solver.solve(1, in, out);
		out.close();
	}
}

class E {
    public void solve(int testNumber, PandaScanner in, PrintWriter out) {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] table = new int[n + 2][m + 2];
        int[][] prefix = new int[n + 2][m + 2];
        int[][] dp = new int[n + 2][m + 2];
        for (int i = 1; i <= n; i++) {
            dp[i][0] = Integer.MIN_VALUE;
            dp[i][m + 1] = Integer.MIN_VALUE;
            for (int j = 1; j <= m; j++) {
                table[i][j] = in.nextInt();
                prefix[i][j] = table[i][j] + prefix[i][j - 1];
            }
            if ((i & 1) == 0) {
                for (int j = 1; j <= m; j++) {
                    dp[i][j] = Math.max(dp[i][j - 1], prefix[i][j] + dp[i - 1][j + 1]);
                }
            }
            else {
                for (int j = m; j >= 1; j--) {
                    dp[i][j] = Math.max(dp[i][j + 1], prefix[i][j] + dp[i - 1][j - 1]);
                }
            }
        }
        out.println(ArrayUtil.max(dp[n]));
    }
}

class PandaScanner {
    public BufferedReader br;
    public StringTokenizer st;
    public InputStream in;

    public PandaScanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(this.in = in));
    }

    public String nextLine() {
        try {
            return br.readLine();
        }
        catch (Exception e) {
            return null;
        }
    }

    public String next() {
        if (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(nextLine().trim());
            return next();
        }
        return st.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    }

class ArrayUtil {

    /**
     * @param arr The array whose maximum integer is to be determined.
     * @return The maximum integer of the array.
     */
    public static int max(int[] arr) {
        if (arr.length == 0) {
            throw new RuntimeException("Passed in array has length of 0");
        }
        int max = arr[0];
        for (int a: arr) {
            if (a > max) {
                max = a;
            }
        }
        return max;
    }

    }


import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Zyflair Griffane
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		PandaScanner in = new PandaScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		E solver = new E();
		solver.solve(1, in, out);
		out.close();
	}
}

class E {
    final int MIN = -100000000;
    public void solve(int testNumber, PandaScanner in, PrintWriter out) {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] table = new int[n + 2][m + 2];
        int[][] prefix = new int[n + 2][m + 2];
        int[][] dp = new int[n + 2][m + 2];
        for (int i = 1; i <= n; i++) {
            dp[i][0] = MIN;
            dp[i][m + 1] = MIN;
            for (int j = 1; j <= m; j++) {
                table[i][j] = in.nextInt();
                prefix[i][j] = table[i][j] + prefix[i][j - 1];
            }
            if ((i & 1) == 0) {
                for (int j = 1; j <= m; j++) {
                    dp[i][j] = Math.max(dp[i][j - 1], prefix[i][j] + dp[i - 1][j + 1]);
                }
            }
            else {
                for (int j = m; j >= 1; j--) {
                    dp[i][j] = Math.max(dp[i][j + 1], prefix[i][j] + dp[i - 1][j - 1]);
                }
            }
        }
        out.println(ArrayUtil.max(dp[n]));
    }
}

class PandaScanner {
    public BufferedReader br;
    public StringTokenizer st;
    public InputStream in;

    public PandaScanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(this.in = in));
    }

    public String nextLine() {
        try {
            return br.readLine();
        }
        catch (Exception e) {
            return null;
        }
    }

    public String next() {
        if (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(nextLine().trim());
            return next();
        }
        return st.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    }

class ArrayUtil {

    /**
     * @param arr The array whose maximum integer is to be determined.
     * @return The maximum integer of the array.
     */
    public static int max(int[] arr) {
        if (arr.length == 0) {
            throw new RuntimeException("Passed in array has length of 0");
        }
        int max = arr[0];
        for (int a: arr) {
            if (a > max) {
                max = a;
            }
        }
        return max;
    }

    }


import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Zyflair Griffane
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		PandaScanner in = new PandaScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		E solver = new E();
		solver.solve(1, in, out);
		out.close();
	}
}

class E {
    final int MIN = -100000000;
    public void solve(int testNumber, PandaScanner in, PrintWriter out) {
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] table = new int[n + 2][m + 2];
        long[][] prefix = new long[n + 2][m + 2];
        long[][] dp = new long[n + 2][m + 2];
        for (int i = 1; i <= n; i++) {
            dp[i][0] = MIN;
            dp[i][m + 1] = MIN;
            for (int j = 1; j <= m; j++) {
                table[i][j] = in.nextInt();
                prefix[i][j] = table[i][j] + prefix[i][j - 1];
            }
            if ((i & 1) == 0) {
                for (int j = 1; j <= m; j++) {
                    dp[i][j] = Math.max(dp[i][j - 1], prefix[i][j] + dp[i - 1][j + 1]);
                }
            }
            else {
                for (int j = m; j >= 1; j--) {
                    dp[i][j] = Math.max(dp[i][j + 1], prefix[i][j] + dp[i - 1][j - 1]);
                }
            }
        }
        out.println(ArrayUtil.max(dp[n]));
    }
}

class PandaScanner {
    public BufferedReader br;
    public StringTokenizer st;
    public InputStream in;

    public PandaScanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(this.in = in));
    }

    public String nextLine() {
        try {
            return br.readLine();
        }
        catch (Exception e) {
            return null;
        }
    }

    public String next() {
        if (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(nextLine().trim());
            return next();
        }
        return st.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    }

class ArrayUtil {

    /**
     * @param arr The array whose maximum long integer is to be determined.
     * @return The maximum long integer of the array.
     */
    public static long max(long[] arr) {
        if (arr.length == 0) {
            throw new RuntimeException("Passed in array has length of 0");
        }
        long max = arr[0];
        for (long a: arr) {
            if (a > max) {
                max = a;
            }
        }
        return max;
    }

    }


import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Nguyen Trung Hieu - vuondenthanhcong11@yahoo.com
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskE {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
        int n = in.readInt();
        int m = in.readInt();
        long[][] a = new long[n][m];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                a[i][j] = in.readInt();
                if (j > 0)
                    a[i][j] += a[i][j - 1];
            }
        }
        long[][] best = new long[n][m];
        for (int j = 0; j < m; ++j) {
            best[0][j] = a[0][j];
        }
        for (int i = 1; i < n; ++i) {
            Arrays.fill(best[i], -(Long.MAX_VALUE / 10));
            if (i % 2 == 0) {
                for (int j = 1; j < m; ++j) {
                    best[i][j] = Math.max(best[i][j - 1] - a[i][j - 1] + a[i][j], best[i - 1][j - 1] + a[i][j]);
                }
            } else {
                for (int j = m - 2; j >= 0; --j) {
                    best[i][j] = Math.max(best[i][j + 1] - a[i][j + 1] + a[i][j], best[i - 1][j + 1] + a[i][j]);
                }
            }
        }
        long res = -(Long.MAX_VALUE / 10);
        for (long x : best[n - 1])
            res = Math.max(res, x);
        out.printLine(res);
    }
}

class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int readInt() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
    }

    }

class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }

    public void print(Object...objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }

    public void printLine(Object...objects) {
        print(objects);
        writer.println();
    }

    public void close() {
        writer.close();
    }

}


import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

public class Main {
    static int mod = (int)1e9+7;
    static int inf = (int)1e9+7;
    static double eps = 1e-15;
    static int N = 1505;
    long[][] sum = new long[N][N], dp = new long[N][N];
    void work() throws Exception {
        int n = nextInt();
        int m = nextInt();
        for(int i = 0; i < n; i ++) {
            for(int j = 0; j < m; j ++) {
                sum[i][j] = nextLong();
                if(j > 0) sum[i][j] += sum[i][j-1];
            }
        }
        for(int i = 0; i < n; i ++) {
            if(i%2==1) {
                for(int j = 0; j < m; j ++) {
                    dp[i][j] = sum[i][j] + dp[i-1][j+1];
                    if(j>0 && dp[i][j-1]>dp[i][j]) dp[i][j] = dp[i][j-1];
                }
            }
            else {
                for(int j = m-1; j > 0; j --) {
                    dp[i][j] = sum[i][j];
                    if(i > 0) dp[i][j] += dp[i-1][j-1];
                    if(j<m-1 && dp[i][j+1]>dp[i][j]) dp[i][j] = dp[i][j+1];
                }

            }
        }
        if(n%2==1) out.println(dp[n-1][1]);
        else out.println(dp[n-1][m-2]);
    }
    static PrintWriter out = new PrintWriter(System.out);
  //  Scanner cin = new Scanner(System.in);

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));;
    StringTokenizer str = null;
    private String next() throws Exception{
        while (str == null || !str.hasMoreElements())
            str = new StringTokenizer(in.readLine());
        return str.nextToken();
    }
    private int nextInt() throws Exception{
        return Integer.parseInt(next());
    }
    private long nextLong() throws Exception{
        return Long.parseLong(next());
    }
    static int dx[] = {0,1,0,-1};
    static int dy[] = {1,0,-1,0};
    class pair implements Comparable<pair>{
        int first, second;
        pair(int x, int y) {
            first = x;
            second = y;
        }
        public int compareTo(pair o) {
            // TODO auto-generated method stub
            if(first != o.first) return ((Integer)first).compareTo(o.first);
            else return ((Integer)second).compareTo(o.second);
        }
    }
    int upper_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A;
        int pos = r;
        r--;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (A[mid] <= val) {
                l = mid + 1;
            } else {
                pos = mid;
                r = mid - 1;
            }
        }
        return pos;
    }
    int lower_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A;
        int pos = r;
        r--;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (A[mid] < val) {
                l = mid + 1;
            } else {
                pos = mid;
                r = mid - 1;
            }
        }
        return pos;
    }

    DecimalFormat df=new DecimalFormat("0.0000000000");
    public static void main(String[] args) throws Exception{
        Main wo = new Main();
        wo.work();
        out.close();
    }
}


import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

public class Main {
    static int mod = (int)1e9+7;
    static int inf = (int)1e9+7;
    static double eps = 1e-15;
    static int N = 1505;
    long[][] sum = new long[N][N], dp = new long[N][N];
    void work() throws Exception {
        int n = cin.nextInt();
        int m = cin.nextInt();
        for(int i = 0; i < n; i ++) {
            for(int j = 0; j < m; j ++) {
                sum[i][j] = cin.nextLong();
                if(j > 0) sum[i][j] += sum[i][j-1];
            }
        }
        for(int i = 0; i < n; i ++) {
            if(i%2==1) {
                for(int j = 0; j < m; j ++) {
                    dp[i][j] = sum[i][j] + dp[i-1][j+1];
                    if(j>0 && dp[i][j-1]>dp[i][j]) dp[i][j] = dp[i][j-1];
                }
            }
            else {
                for(int j = m-1; j > 0; j --) {
                    dp[i][j] = sum[i][j];
                    if(i > 0) dp[i][j] += dp[i-1][j-1];
                    if(j<m-1 && dp[i][j+1]>dp[i][j]) dp[i][j] = dp[i][j+1];
                }

            }
        }
        if(n%2==1) out.println(dp[n-1][1]);
        else out.println(dp[n-1][m-2]);
    }
    static PrintWriter out = new PrintWriter(System.out);
    Scanner cin = new Scanner(System.in);

//  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));;
//  StringTokenizer str = null;
//    private String next() throws Exception{
//      while (str == null || !str.hasMoreElements())
//          str = new StringTokenizer(in.readLine());
//      return str.nextToken();
//    }
//    private int nextInt() throws Exception{
//      return Integer.parseInt(next());
//    }
//    private long nextLong() throws Exception{
//      return Long.parseLong(next());
//    }
    static int dx[] = {0,1,0,-1};
    static int dy[] = {1,0,-1,0};
    class pair implements Comparable<pair>{
        int first, second;
        pair(int x, int y) {
            first = x;
            second = y;
        }
        public int compareTo(pair o) {
            // TODO auto-generated method stub
            if(first != o.first) return ((Integer)first).compareTo(o.first);
            else return ((Integer)second).compareTo(o.second);
        }
    }
    int upper_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A;
        int pos = r;
        r--;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (A[mid] <= val) {
                l = mid + 1;
            } else {
                pos = mid;
                r = mid - 1;
            }
        }
        return pos;
    }
    int lower_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A;
        int pos = r;
        r--;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (A[mid] < val) {
                l = mid + 1;
            } else {
                pos = mid;
                r = mid - 1;
            }
        }
        return pos;
    }

    DecimalFormat df=new DecimalFormat("0.0000000000");
    public static void main(String[] args) throws Exception{
        Main wo = new Main();
        wo.work();
        out.close();
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Solution implements Runnable {
	private BufferedReader br;
	private StringTokenizer tok;
	private PrintWriter out;

	public static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

	void solve() throws IOException {
		int n = nextInt();
		int m = nextInt();
		int[][] table = new int[n + 1][m + 1];
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= m; ++j) {
				table[i][j] = nextInt();
			}
		long[][] dp = new long[n + 1][m + 1];
		for (int j = 1; j <= m; ++j) {
			dp[1][j] = table[1][j];
			dp[1][j] += dp[1][j - 1];
		}
		for (int i = 2; i <= n; ++i)
			if (i % 2 == 1) {
				long[] h = new long[m + 1];
				h[0] = Integer.MIN_VALUE / 2;
				for (int j = 1; j <= m; ++j) {
					h[j] = Math.max(dp[i - 1][j], h[j - 1]);
				}
				long ps = table[i][1];
				for (int j = 2; j <= m; ++j) {
					ps += table[i][j];
					dp[i][j] = h[j - 1] + ps;
				}
			} else {
				long[] h = new long[m + 1];
				for (int j = m; j > 0; --j) {
					h[j] = dp[i - 1][j];
					if (j < m && h[j + 1] > h[j]) {
						h[j] = h[j + 1];
					}
				}
				long ps = 0;
				for (int j = 1; j < m; ++j) {
					ps += table[i][j];
					dp[i][j] = h[j + 1] + ps;
				}
			}
/*		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= m; ++j) {
				out.print(dp[i][j] + " ");
			}
			out.println();
		}*/
		long res = Integer.MIN_VALUE / 2;
		int beg = 1;
		int end = m;
		if (n % 2 == 0) {
			--end;
		} else {
			++beg;
		}
		for (int j = beg; j <= end; ++j) {
			res = Math.max(res, dp[n][j]);
		}
		out.print(res);
	}

	public void run() {
		try {
			if (ONLINE_JUDGE) {
				br = new BufferedReader(new InputStreamReader(System.in));
				out = new PrintWriter(System.out);
			} else {
				br = new BufferedReader(new FileReader(new File("input.txt")));
				out = new PrintWriter(new File("output.txt"));
			}
			solve();
			br.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	public static void main(String[] args) {
		new Solution().run();
	}

	String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens())
			tok = new StringTokenizer(br.readLine());
		return tok.nextToken();
	}

	String nextString() throws IOException {
		return nextToken();
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	BigInteger nextBigInteger() throws IOException {
		return new BigInteger(nextToken());
	}
}


#include <bits/stdc++.h>
#define DIM 1507
#define INF 1e12
using namespace std;
long long n,m,a[DIM][DIM],d[DIM][DIM],pref[DIM][DIM],suf[DIM][DIM],sum[DIM][DIM];
int main()
{
scanf("%I64d",&n);
scanf("%I64d",&m);
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j)
        {
      scanf("%I64d",&a[i][j]);
            sum[i][j]=sum[i][j-1]+a[i][j];
        }
scanf("%I64d",&n);
    for(int i=1; i<=n; ++i)
    {
        for(int j=1; j<=m; ++j)
        {
            if(i%2==1) d[i][j]=sum[i][j]+pref[i-1][j-1];
            else d[i][j]=sum[i][j]+suf[i-1][j+1];

        }
        pref[i][0]=-INF;
        for(int j=1; j<=m; ++j) pref[i][j]=max(pref[i][j-1],d[i][j]);
        suf[i][m+1]=-INF;
        for(int j=m; j>=1; --j) suf[i][j]=max(suf[i][j+1],d[i][j]);

    }
    long long res=d[n][1];
    for(int i=2; i<=m; ++i) res=max(res,d[n][i]);
  printf("%I64d",res);

}


#include <bits/stdc++.h>
#define DIM 1507
#define INF 100000000000
using namespace std;
long long n,m,a[DIM][DIM],d[DIM][DIM],pref[DIM][DIM],suf[DIM][DIM],sum[DIM][DIM];
int main()
{
    cin>>n>>m;
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j)
        {
            cin>>a[i][j];
            sum[i][j]=sum[i][j-1]+a[i][j];
        }

    for(int i=1; i<=n; ++i)
    {
        for(int j=1; j<=m; ++j)
        {
            if(i%2==1) d[i][j]=sum[i][j]+pref[i-1][j-1];
            else d[i][j]=sum[i][j]+suf[i-1][j+1];

        }
        pref[i][0]=-INF;
        for(int j=1; j<=m; ++j) pref[i][j]=max(pref[i][j-1],d[i][j]);
        suf[i][m+1]=-INF;
        for(int j=m; j>=1; --j) suf[i][j]=max(suf[i][j+1],d[i][j]);

    }
    long long res=d[n][1];
    for(int i=2; i<=m; ++i) res=max(res,d[n][2]);
    cout<<res;

}


#include <bits/stdc++.h>
#define DIM 1507
#define INF 1e12
using namespace std;
long long n,m,a[DIM][DIM],d[DIM][DIM],pref[DIM][DIM],suf[DIM][DIM],sum[DIM][DIM];
int main()
{
    cin>>n>>m;
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j)
        {
            cin>>a[i][j];
            sum[i][j]=sum[i][j-1]+a[i][j];
        }

    for(int i=1; i<=n; ++i)
    {
        for(int j=1; j<=m; ++j)
        {
            if(i%2==1) d[i][j]=sum[i][j]+pref[i-1][j-1];
            else d[i][j]=sum[i][j]+suf[i-1][j+1];

        }
        pref[i][0]=-INF;
        for(int j=1; j<=m; ++j) pref[i][j]=max(pref[i][j-1],d[i][j]);
        suf[i][m+1]=-INF;
        for(int j=m; j>=1; --j) suf[i][j]=max(suf[i][j+1],d[i][j]);

    }
    long long res=d[n][1];
    for(int i=2; i<=m; ++i) res=max(res,d[n][2]);
    cout<<res;

}


#include <bits/stdc++.h>
#define DIM 1507
#define INF 1e12
using namespace std;
long long n,m,a[DIM][DIM],d[DIM][DIM],pref[DIM][DIM],suf[DIM][DIM],sum[DIM][DIM];
int main()
{
    cin>>n>>m;
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j)
        {
            cin>>a[i][j];
            sum[i][j]=sum[i][j-1]+a[i][j];
        }

    for(int i=1; i<=n; ++i)
    {
        for(int j=1; j<=m; ++j)
        {
            if(i%2==1) d[i][j]=sum[i][j]+pref[i-1][j-1];
            else d[i][j]=sum[i][j]+suf[i-1][j+1];

        }
        pref[i][0]=-INF;
        for(int j=1; j<=m; ++j) pref[i][j]=max(pref[i][j-1],d[i][j]);
        suf[i][m+1]=-INF;
        for(int j=m; j>=1; --j) suf[i][j]=max(suf[i][j+1],d[i][j]);

    }
    long long res=d[n][1];
    for(int i=2; i<=m; ++i) res=max(res,d[n][i]);
    cout<<res;

}


#include<bits/stdc++.h>
#define ios std::ios::sync_with_stdio(false)
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define int long long
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
#define mst(a,n) memset(a,n,sizeof(a))
#define dbg(a, b) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<'\n';
#define debug(a, b, c, d)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<'\n';
#define debugg(a, b, c, d, e, f)  cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "#e<<": "<<e<<" | "<<#f<<": "<<f<<'\n';
#define printa(a,l,r) rep(_i,l,r) {cout << a[_i] << " \n"[_i==r];}
#define printaa(a,u,d,l,r) rep(_i,u,d)rep(_j,l,r) {cout<<a[_i][_j]<<" \n"[_j==r];}
#define printset(st) for(auto _it:st){cout << _it << " ";} cout << '\n';
#define printmap(mp) for(auto &_it:mp){cout << _it.se << " ";} cout << '\n';
//ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll a,ll b){return a*b/gcd(a,b);}
//ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}
//ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;}
//ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;}
//freopen("C:/Users/zyj19/Desktop/out.txt","w",stdout);int num = 0;time_t begin,end;double ret;begin=clock();end=clock();ret=double(end-begin)/CLOCKS_PER_SEC;cout<<"runtime: "<<ret<<endl;fclose(stdout);
using namespace std;
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');}
//struct Edge{int nxt,to;}e[N<<1];int head[N],TOT;void add_edge(int u,int v){e[++TOT].nxt=head[u];e[TOT].to = v;head[u] = TOT;}
//struct node{int k,l,r;bool operator <(const node& A)const{return k < A.k;}};
//const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1500 + 10, MOD = 1e9+7, inf = 0x3f3f3f3f;
int a[N][N],mp[N][N],dp[N][N];
signed main()
{
	ios;
	int n,m;
	cin >> n >> m;
	rep(i,1,n) rep(j,1,m){
		cin >> a[i][j];
	}
	rep(i,1,n) rep(j,1,m){
		mp[i][j] = mp[i][j-1] + a[i][j];
	}
						//	printaa(mp,1,n,1,m);
	rep(i,1,n){
		if (i & 1){
			dp[i][1] = -inf; mp[i][1] = 0;
			rep(j,2,m){
				dp[i][j] = max(dp[i][j-1]-mp[i][j-1],dp[i-1][j-1]);
				dp[i][j] += mp[i][j];
			}
		}
		else if (!(i & 1)){
			dp[i][m] = -inf;  mp[i][m] = 0;
			per(j,m-1,1){
				dp[i][j] = max(dp[i][j+1]-mp[i][j+1],dp[i-1][j+1]);
				dp[i][j] += mp[i][j];
			}
		}
	}
					//	printaa(dp,1,n,1,m);
	int ans = -inf;
	rep(i,1,m){
		ans = max(ans,dp[n][i]);
	}
	cout << ans << '\n';
    return 0;
}


#include<bits/stdc++.h>
#define ios std::ios::sync_with_stdio(false)
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define int long long
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
#define mst(a,n) memset(a,n,sizeof(a))
#define dbg(a, b) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<'\n';
#define debug(a, b, c, d)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<'\n';
#define debugg(a, b, c, d, e, f)  cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "#e<<": "<<e<<" | "<<#f<<": "<<f<<'\n';
#define printa(a,l,r) rep(_i,l,r) {cout << a[_i] << " \n"[_i==r];}
#define printaa(a,u,d,l,r) rep(_i,u,d)rep(_j,l,r) {cout<<a[_i][_j]<<" \n"[_j==r];}
#define printset(st) for(auto _it:st){cout << _it << " ";} cout << '\n';
#define printmap(mp) for(auto &_it:mp){cout << _it.se << " ";} cout << '\n';
//ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll a,ll b){return a*b/gcd(a,b);}
//ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}
//ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;}
//ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;}
//freopen("C:/Users/zyj19/Desktop/out.txt","w",stdout);int num = 0;time_t begin,end;double ret;begin=clock();end=clock();ret=double(end-begin)/CLOCKS_PER_SEC;cout<<"runtime: "<<ret<<endl;fclose(stdout);
using namespace std;
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');}
//struct Edge{int nxt,to;}e[N<<1];int head[N],TOT;void add_edge(int u,int v){e[++TOT].nxt=head[u];e[TOT].to = v;head[u] = TOT;}
//struct node{int k,l,r;bool operator <(const node& A)const{return k < A.k;}};
//const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1500 + 10, MOD = 1e9+7, inf = 0x3f3f3f3f;
int a[N][N],mp[N][N],dp[N][N];
signed main()
{
	int n,m;
	read(n); read(m);
	rep(i,1,n) rep(j,1,m){
		read(a[i][j]);
	}
	rep(i,1,n) rep(j,1,m){
		mp[i][j] = mp[i][j-1] + a[i][j];
	}
						//	printaa(mp,1,n,1,m);
	rep(i,1,n){
		if (i & 1){
			dp[i][1] = -inf; mp[i][1] = 0;
			rep(j,2,m){
				dp[i][j] = max(dp[i][j-1]-mp[i][j-1],dp[i-1][j-1]);
				dp[i][j] += mp[i][j];
			}
		}
		else if (!(i & 1)){
			dp[i][m] = -inf;  mp[i][m] = 0;
			per(j,m-1,1){
				dp[i][j] = max(dp[i][j+1]-mp[i][j+1],dp[i-1][j+1]);
				dp[i][j] += mp[i][j];
			}
		}
	}
					//	printaa(dp,1,n,1,m);
	int ans = -inf;
	rep(i,1,m){
		ans = max(ans,dp[n][i]);
	}
	cout << ans << '\n';
    return 0;
}


#include<bits/stdc++.h>
#define ios std::ios::sync_with_stdio(false)
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
#define mst(a,n) memset(a,n,sizeof(a))
#define dbg(a, b) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<'\n';
#define debug(a, b, c, d)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<'\n';
#define debugg(a, b, c, d, e, f)  cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "#e<<": "<<e<<" | "<<#f<<": "<<f<<'\n';
#define printa(a,l,r) rep(_i,l,r) {cout << a[_i] << " \n"[_i==r];}
#define printaa(a,u,d,l,r) rep(_i,u,d)rep(_j,l,r) {cout<<a[_i][_j]<<" \n"[_j==r];}
#define printset(st) for(auto _it:st){cout << _it << " ";} cout << '\n';
#define printmap(mp) for(auto &_it:mp){cout << _it.se << " ";} cout << '\n';
//ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll a,ll b){return a*b/gcd(a,b);}
//ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}
//ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;}
//ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;}
//freopen("C:/Users/zyj19/Desktop/out.txt","w",stdout);int num = 0;time_t begin,end;double ret;begin=clock();end=clock();ret=double(end-begin)/CLOCKS_PER_SEC;cout<<"runtime: "<<ret<<endl;fclose(stdout);
using namespace std;
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');}
//struct Edge{int nxt,to;}e[N<<1];int head[N],TOT;void add_edge(int u,int v){e[++TOT].nxt=head[u];e[TOT].to = v;head[u] = TOT;}
//struct node{int k,l,r;bool operator <(const node& A)const{return k < A.k;}};
//const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 3000 + 10, MOD = 1e9+7, inf = 0x3f3f3f3f;
int a[N][N],mp[N][N],dp[N][N];
signed main()
{
	int n,m;
	cin >> n >> m;
	rep(i,1,n) rep(j,1,m){
		cin >> a[i][j];
	}
	rep(i,1,n) rep(j,1,m){
		mp[i][j] = mp[i][j-1] + a[i][j];
	}
						//	printaa(mp,1,n,1,m);
	rep(i,1,n){
		if (i & 1){
			dp[i][1] = -inf; mp[i][1] = 0;
			rep(j,2,m){
				dp[i][j] = max(dp[i][j-1]-mp[i][j-1],dp[i-1][j-1]);
				dp[i][j] += mp[i][j];
			}
		}
		else if (!(i & 1)){
			dp[i][m] = -inf;  mp[i][m] = 0;
			per(j,m-1,1){
				dp[i][j] = max(dp[i][j+1]-mp[i][j+1],dp[i-1][j+1]);
				dp[i][j] += mp[i][j];
			}
		}
	}
					//	printaa(dp,1,n,1,m);
	int ans = -inf;
	rep(i,1,m){
		ans = max(ans,dp[n][i]);
	}
	cout << ans << '\n';
    return 0;
}


#include<bits/stdc++.h>
#define ios std::ios::sync_with_stdio(false)
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
#define mst(a,n) memset(a,n,sizeof(a))
#define dbg(a, b) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<'\n';
#define debug(a, b, c, d)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<'\n';
#define debugg(a, b, c, d, e, f)  cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "#e<<": "<<e<<" | "<<#f<<": "<<f<<'\n';
#define printa(a,l,r) rep(_i,l,r) {cout << a[_i] << " \n"[_i==r];}
#define printaa(a,u,d,l,r) rep(_i,u,d)rep(_j,l,r) {cout<<a[_i][_j]<<" \n"[_j==r];}
#define printset(st) for(auto _it:st){cout << _it << " ";} cout << '\n';
#define printmap(mp) for(auto &_it:mp){cout << _it.se << " ";} cout << '\n';
//ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll a,ll b){return a*b/gcd(a,b);}
//ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}
//ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;}
//ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;}
//freopen("C:/Users/zyj19/Desktop/out.txt","w",stdout);int num = 0;time_t begin,end;double ret;begin=clock();end=clock();ret=double(end-begin)/CLOCKS_PER_SEC;cout<<"runtime: "<<ret<<endl;fclose(stdout);
using namespace std;
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');}
//struct Edge{int nxt,to;}e[N<<1];int head[N],TOT;void add_edge(int u,int v){e[++TOT].nxt=head[u];e[TOT].to = v;head[u] = TOT;}
//struct node{int k,l,r;bool operator <(const node& A)const{return k < A.k;}};
//const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 3000 + 10, MOD = 1e9+7, inf = 0x3f3f3f3f;
int a[N][N],mp[N][N],dp[N][N];
signed main()
{
	ios;
	int n,m;
	cin >> n >> m;
	rep(i,1,n) rep(j,1,m){
		cin >> a[i][j];
	}
	rep(i,1,n) rep(j,1,m){
		mp[i][j] = mp[i][j-1] + a[i][j];
	}
						//	printaa(mp,1,n,1,m);
	rep(i,1,n){
		if (i & 1){
			dp[i][1] = -inf; mp[i][1] = 0;
			rep(j,2,m){
				dp[i][j] = max(dp[i][j-1]-mp[i][j-1],dp[i-1][j-1]);
				dp[i][j] += mp[i][j];
			}
		}
		else if (!(i & 1)){
			dp[i][m] = -inf;  mp[i][m] = 0;
			per(j,m-1,1){
				dp[i][j] = max(dp[i][j+1]-mp[i][j+1],dp[i-1][j+1]);
				dp[i][j] += mp[i][j];
			}
		}
	}
					//	printaa(dp,1,n,1,m);
	int ans = -inf;
	rep(i,1,m){
		ans = max(ans,dp[n][i]);
	}
	cout << ans << '\n';
    return 0;
}


#include<bits/stdc++.h>
#define ios std::ios::sync_with_stdio(false)
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define int long long
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
#define mst(a,n) memset(a,n,sizeof(a))
#define dbg(a, b) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<'\n';
#define debug(a, b, c, d)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<'\n';
#define debugg(a, b, c, d, e, f)  cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "#e<<": "<<e<<" | "<<#f<<": "<<f<<'\n';
#define printa(a,l,r) rep(_i,l,r) {cout << a[_i] << " \n"[_i==r];}
#define printaa(a,u,d,l,r) rep(_i,u,d)rep(_j,l,r) {cout<<a[_i][_j]<<" \n"[_j==r];}
#define printset(st) for(auto _it:st){cout << _it << " ";} cout << '\n';
#define printmap(mp) for(auto &_it:mp){cout << _it.se << " ";} cout << '\n';
//ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll a,ll b){return a*b/gcd(a,b);}
//ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}
//ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;}
//ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;}
//freopen("C:/Users/zyj19/Desktop/out.txt","w",stdout);int num = 0;time_t begin,end;double ret;begin=clock();end=clock();ret=double(end-begin)/CLOCKS_PER_SEC;cout<<"runtime: "<<ret<<endl;fclose(stdout);
using namespace std;
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');}
//struct Edge{int nxt,to;}e[N<<1];int head[N],TOT;void add_edge(int u,int v){e[++TOT].nxt=head[u];e[TOT].to = v;head[u] = TOT;}
//struct node{int k,l,r;bool operator <(const node& A)const{return k < A.k;}};
//const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1500 + 10, MOD = 1e9+7, inf = 0x3f3f3f3f;
int a[N][N],mp[N][N],dp[N][N];
signed main()
{
	int n,m;
	read(n); read(m);
	rep(i,1,n) rep(j,1,m){
		read(a[i][j]);
		mp[i][j] = mp[i][j-1] + a[i][j];
	}
						//	printaa(mp,1,n,1,m);
	rep(i,1,n){
		if (i & 1){
			dp[i][1] = -inf; mp[i][1] = 0;
			rep(j,2,m){
				dp[i][j] = max(dp[i][j-1]-mp[i][j-1],dp[i-1][j-1]);
				dp[i][j] += mp[i][j];
			}
		}
		else if (!(i & 1)){
			dp[i][m] = -inf;  mp[i][m] = 0;
			per(j,m-1,1){
				dp[i][j] = max(dp[i][j+1]-mp[i][j+1],dp[i-1][j+1]);
				dp[i][j] += mp[i][j];
			}
		}
	}
					//	printaa(dp,1,n,1,m);
	int ans = -inf;
	rep(i,1,m){
		ans = max(ans,dp[n][i]);
	}
	cout << ans << '\n';
    return 0;
}


#include <cstdio>
#include <algorithm>

int const N = 2010;
int const INF = (int)1e9;

int a[N][N];
int pref[N][N];
long long dp[N][N];

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			scanf("%d", a[i] + j);
			pref[i][j + 1] = pref[i][j] + a[i][j];
		}
	}
	for (int i = 1; i <= m; ++i) {
		dp[0][i] = pref[0][i];
	}
	for (int i = 1; i < n; ++i) {
		if (i % 2 == 0) {
			dp[i][1] = -INF;
			long long best = dp[i - 1][1];
			for (int j = 2; j <= m; ++j) {
				dp[i][j] = best + pref[i][j];
				best = std::max(best, dp[i - 1][j]);
			}
		} else {
			dp[i][m] = -INF;
			long long best = dp[i - 1][m];
			for (int j = m - 1; j > 0; --j) {
				dp[i][j] = best + pref[i][j];
				best = std::max(best, dp[i - 1][j]);
			}
		}
	}
	long long ans = 0;
	for (int i = 1; i <= m; ++i) {
		ans = std::max(ans, dp[n - 1][i]);
	}
	printf("%I64d\n", ans);
}


#include <cstdio>
#include <algorithm>

int const N = 2010;
long long const INF = (long long)1e18;

int a[N][N];
int pref[N][N];
long long dp[N][N];

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			scanf("%d", a[i] + j);
			pref[i][j + 1] = pref[i][j] + a[i][j];
		}
	}
	for (int i = 1; i <= m; ++i) {
		dp[0][i] = pref[0][i];
	}
	for (int i = 1; i < n; ++i) {
		if (i % 2 == 0) {
			dp[i][1] = -INF;
			long long best = dp[i - 1][1];
			for (int j = 2; j <= m; ++j) {
				dp[i][j] = best + pref[i][j];
				best = std::max(best, dp[i - 1][j]);
			}
		} else {
			dp[i][m] = -INF;
			long long best = dp[i - 1][m];
			for (int j = m - 1; j > 0; --j) {
				dp[i][j] = best + pref[i][j];
				best = std::max(best, dp[i - 1][j]);
			}
		}
	}
	long long ans = 0;
	for (int i = 1; i <= m; ++i) {
		ans = std::max(ans, dp[n - 1][i]);
	}
	printf("%I64d\n", ans);
}


#include <cstdio>
#include <algorithm>

int const N = 2010;
int const INF = (int)1e9;

int a[N][N];
int pref[N][N];
int dp[N][N];

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			scanf("%d", a[i] + j);
			pref[i][j + 1] = pref[i][j] + a[i][j];
		}
	}
	for (int i = 1; i <= m; ++i) {
		dp[0][i] = pref[0][i];
	}
	for (int i = 1; i < n; ++i) {
		if (i % 2 == 0) {
			dp[i][1] = -INF;
			int best = dp[i - 1][1];;
			for (int j = 2; j <= m; ++j) {
				dp[i][j] = best + pref[i][j];
				best = std::max(best, dp[i - 1][j]);
			}
		} else {
			dp[i][m] = -INF;
			int best = dp[i - 1][m];
			for (int j = m - 1; j > 0; --j) {
				dp[i][j] = best + pref[i][j];
				best = std::max(best, dp[i - 1][j]);
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= m; ++i) {
		ans = std::max(ans, dp[n - 1][i]);
	}
	printf("%d\n", ans);
}


#include <cstdio>
#include <algorithm>

int const N = 2010;
long long const INF = (long long)1e18;

int a[N][N];
int pref[N][N];
long long dp[N][N];

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			scanf("%d", a[i] + j);
			pref[i][j + 1] = pref[i][j] + a[i][j];
		}
	}
	for (int i = 1; i <= m; ++i) {
		dp[0][i] = pref[0][i];
	}
	for (int i = 1; i < n; ++i) {
		if (i % 2 == 0) {
			dp[i][1] = -INF;
			long long best = dp[i - 1][1];
			for (int j = 2; j <= m; ++j) {
				dp[i][j] = best + pref[i][j];
				best = std::max(best, dp[i - 1][j]);
			}
		} else {
			dp[i][m] = -INF;
			long long best = dp[i - 1][m];
			for (int j = m - 1; j > 0; --j) {
				dp[i][j] = best + pref[i][j];
				best = std::max(best, dp[i - 1][j]);
			}
		}
	}
	long long ans = -INF;
	for (int i = 1; i <= m; ++i) {
		ans = std::max(ans, dp[n - 1][i]);
	}
	printf("%I64d\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fll
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
const int maxn=2000;
int dp[maxn][maxn],mxbegin[maxn][maxn],mxend[maxn][maxn];
int sum[maxn][maxn];
int n,m;
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			int x;
			scanf("%d",&x);
			sum[i][j]=(j?sum[i][j-1]:0)+x;
		}
	}
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		{
			dp[i][j]=-inf;
			mxbegin[i][j]=-inf;
			mxend[i][j]=-inf;
		}
	for(int j=0;j<m;j++)
	{
		dp[0][j]=sum[0][j];
	}
	for(int j=0;j<m;j++)
	{
		mxbegin[0][j]=max(dp[0][j],j?mxbegin[0][j-1]:-inf);
	}
	for(int j=m-1;j>=0;j--)
	{
		mxend[0][j]=max(dp[0][j],j+1<m?mxend[0][j+1]:-inf);
	}
	for(int i=1;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			dp[i][j]=max(dp[i][j],sum[i][j]+(i&1?(j+1<m?mxend[i-1][j+1]:-inf):(j?mxbegin[i-1][j-1]:-inf)));
		//	cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
		}
		for(int j=0;j<m;j++)
		{
			mxbegin[i][j]=max(dp[i][j],j?mxbegin[i][j-1]:-inf);
		}
		for(int j=m-1;j>=0;j--)
		{
			mxend[i][j]=max(dp[i][j],j+1<m?mxend[i][j+1]:-inf);
		}
	}
	int ans=-inf;
	for(int j=0;j<m;j++)
		ans=max(ans,dp[n-1][j]);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fll
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
const int maxn=2000;
ll dp[maxn][maxn],mxbegin[maxn][maxn],mxend[maxn][maxn];
ll sum[maxn][maxn];
int n,m;
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			int x;
			scanf("%d",&x);
			sum[i][j]=(j?sum[i][j-1]:0)+x;
		}
	}
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		{
			dp[i][j]=-inf;
			mxbegin[i][j]=-inf;
			mxend[i][j]=-inf;
		}
	for(int j=0;j<m;j++)
	{
		dp[0][j]=sum[0][j];
	}
	for(int j=0;j<m;j++)
	{
		mxbegin[0][j]=max(dp[0][j],j?mxbegin[0][j-1]:-inf);
	}
	for(int j=m-1;j>=0;j--)
	{
		mxend[0][j]=max(dp[0][j],j+1<m?mxend[0][j+1]:-inf);
	}
	for(int i=1;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			dp[i][j]=max(dp[i][j],sum[i][j]+(i&1?(j+1<m?mxend[i-1][j+1]:-inf):(j?mxbegin[i-1][j-1]:-inf)));
		//	cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
		}
		for(int j=0;j<m;j++)
		{
			mxbegin[i][j]=max(dp[i][j],j?mxbegin[i][j-1]:-inf);
		}
		for(int j=m-1;j>=0;j--)
		{
			mxend[i][j]=max(dp[i][j],j+1<m?mxend[i][j+1]:-inf);
		}
	}
	ll ans=-inf;
	for(int j=0;j<m;j++)
		ans=max(ans,dp[n-1][j]);
	printf("%I64d\n",ans);
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define inf 1000000000000

long long int a[1001][1001],pre[1001],suff[1001],sum[1001][1001],dp[1001][1001];
vector<long long int>row[100001];

int main()
{
	ios::sync_with_stdio(false);
	long long int i,j,n,m,ans=-2000000000000,cur;
	cin>>n>>m;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			cin>>a[i][j];
			row[i].push_back(a[i][j]);
		}
	}
	for(i=1;i<=n;i++)
	{
	//	sort(row[i].begin(),row[i].end());
	//	reverse(row[i].begin(),row[i].end());
		for(j=1;j<=m;j++)
		{
			sum[i][j]=sum[i][j-1]+row[i][j-1];
		}
	}
/*	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		cout<<sum[i][j]<<" ";
		cout<<endl;
	}
	cout<<endl;*/
	for(i=1;i<=m;i++)
	dp[1][i]=sum[1][i];
	for(i=1;i<=m;i++)
	{
		if(i==1)
		pre[i]=dp[1][i];
		else
		pre[i]=max(pre[i-1],dp[1][i]);
	}
	for(i=m;i>=1;i--)
	{
		if(i==m)
		suff[i]=dp[1][i];
		else
		suff[i]=max(suff[i+1],dp[1][i]);
	}
	/*cout<<"start"<<endl;
	for(i=1;i<=m;i++)
	cout<<pre[i]<<" ";
	cout<<endl;
	for(i=1;i<=m;i++)
	cout<<suff[i]<<" ";
	cout<<endl;
	cout<<"END"<<endl;*/
	for(i=2;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			if(i%2!=0) //prev is smaller
			{
				cur=sum[i][j];
				if(j==1)
				{
					dp[i][j]=-inf;
					continue;
				}
				dp[i][j]=pre[j-1]+cur;
			}
			else //prev is bigger
			{
				cur=sum[i][j];
				if(j==m)
				{
					dp[i][j]=-inf;
					continue;
				}
				dp[i][j]=suff[j+1]+cur;
			}
		}
		for(j=1;j<=m;j++)
		{
			if(j==1)
			pre[j]=dp[i][j];
			else
			pre[j]=max(pre[j-1],dp[i][j]);
		}
		for(j=m;j>=1;j--)
		{
			if(j==m)
			suff[j]=dp[i][j];
			else
			suff[j]=max(suff[j+1],dp[i][j]);
		}
	}
	/*for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		cout<<dp[i][j]<<" ";
		cout<<endl;
	}*/
	for(j=1;j<=m;j++)
	ans=max(ans,dp[n][j]);
	cout<<ans;
	return 0;
}