#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define N 1000005
long long int n,k,m;
long long int i,p;
int check_v(long long int s, long long int index,long long int n){
    if(s==0){
        return 1;
    }
    // cout<<"In check_v:"<<"s:"<<s<<"index:"<<index<<"n:"<<n<<"\n";
    long long int ans = 1;
    long long int flag=1;
    long long int i=1;
    double v=s;
    // cout<<"s:"<<s<<"index"<<index<<"n:"<<n<<"p:"<<p<<"\n";
    while(i<=index){
        v = (double)v/p;
        if(v<=n){
            return 1;
        }
        i++;
    }
    return 0;
}
long long int expo(long long int a,long long int b){
    long long int x = b;
    long long int ans=1,y=a;
    while(x>0){
        if(x%2==1){
            ans = (ans*y)%MOD;
        }
        y = (y*y)%MOD;
        x = x/2;
    }
    return ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input1.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int t;
    cin>>t;

    vector<long long int> arr(N);
    // vector<int>freq(N);
    int j,i;
    while(t--){
        long long int p1 = 0,p2 = 0;
        cin>>n>>p;
        for(i=0;i<n;i++){
            cin>>arr[i];
        }
        sort(arr.begin(),arr.begin()+n,greater<int>());
        p1 = arr[0];
        long long int curr_sum = expo(p,arr[0]);
        map<long long int,long long int> m;
        for(i=1;i<n;i++){
            // cout<<"i:"<<i<<"curr_sum:"<<curr_sum<<"\n";
            if(curr_sum==0){
                curr_sum = (curr_sum + expo(p,arr[i]))%MOD;
                p1 = arr[i];
            }
            else{
                curr_sum = (curr_sum - expo(p,arr[i])+MOD)%MOD;
                if(m.find(arr[i])==m.end()){
                    m[arr[i]]=1;
                    if(m[p1]==1){
                        curr_sum = 0;
                        m.clear();
                    }
                }
                else{
                    m[arr[i]]++;
                    long long int curr = arr[i];
                    while(m[curr]==p){
                        m[curr]=0;
                        if(m.find(curr+1)==m.end()){
                            m[curr+1]=1;
                        }
                        else{
                            m[curr+1]++;
                        }
                        curr++;
                    }
                    if(m[p1]==1){
                        curr_sum = 0;
                        m.clear();
                        continue;
                    }
                }
            }
            // map<int,int>::iterator itr;
            // for(itr = m.begin();itr!=m.end();itr++){
            //     cout<<"first:"<<itr->first<<"second:"<<itr->second<<"\n";
            // }
        }
        cout<<curr_sum<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define N 1000005
long long int n,k,m;
long long int i,p;
int check_v(long long int s, long long int index,long long int n){
    if(s==0){
        return 1;
    }
    // cout<<"In check_v:"<<"s:"<<s<<"index:"<<index<<"n:"<<n<<"\n";
    long long int ans = 1;
    long long int flag=1;
    long long int i=1;
    double v=s;
    // cout<<"s:"<<s<<"index"<<index<<"n:"<<n<<"p:"<<p<<"\n";
    while(i<=index){
        v = (double)v/p;
        if(v<=n){
            return 1;
        }
        i++;
    }
    return 0;
}
long long int expo(long long int a,long long int b){
    long long int x = b;
    long long int ans=1,y=a;
    while(x>0){
        if(x%2==1){
            ans = (ans*y)%MOD;
        }
        y = (y*y)%MOD;
        x = x/2;
    }
    return ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input1.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int t;
    cin>>t;

    vector<long long int> arr(N);
    // vector<int>freq(N);
    int j,i;
    int g = t;
    while(t--){
        long long int p1 = 0,p2 = 0;
        cin>>n>>p;
        if(g==9999){
            cout<<"n"<<n<<"p"<<p;
            for(i=0;i<n;i++){
                cout<<"i"<<arr[i];
            }
            cout<<"\n";
        }
        for(i=0;i<n;i++){
            cin>>arr[i];
        }
        sort(arr.begin(),arr.begin()+n,greater<int>());
        p1 = arr[0];
        long long int curr_sum = expo(p,arr[0]);
        map<long long int,long long int> m;
        for(i=1;i<n;i++){
            // cout<<"i:"<<i<<"curr_sum:"<<curr_sum<<"\n";
            if(curr_sum==0){
                curr_sum = (curr_sum + expo(p,arr[i]))%MOD;
                p1 = arr[i];
            }
            else{
                curr_sum = (curr_sum - expo(p,arr[i])+MOD)%MOD;
                if(m.find(arr[i])==m.end()){
                    m[arr[i]]=1;
                    if(m[p1]==1){
                        // curr_sum = 0;
                        m.clear();
                    }
                }
                else{
                    m[arr[i]]++;
                    long long int curr = arr[i];
                    while(m[curr]==p){
                        m[curr]=0;
                        if(m.find(curr+1)==m.end()){
                            m[curr+1]=1;
                        }
                        else{
                            m[curr+1]++;
                        }
                        curr++;
                    }
                    if(m[p1]==1){
                        // curr_sum = 0;
                        m.clear();
                        continue;
                    }
                }
            }
            // map<int,int>::iterator itr;
            // for(itr = m.begin();itr!=m.end();itr++){
            //     cout<<"first:"<<itr->first<<"second:"<<itr->second<<"\n";
            // }
        }
        cout<<curr_sum<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define N 1000005
long long int n,k,m;
long long int i,p;
int check_v(long long int s, long long int index,long long int n){
    if(s==0){
        return 1;
    }
    // cout<<"In check_v:"<<"s:"<<s<<"index:"<<index<<"n:"<<n<<"\n";
    long long int ans = 1;
    long long int flag=1;
    long long int i=1;
    double v=s;
    // cout<<"s:"<<s<<"index"<<index<<"n:"<<n<<"p:"<<p<<"\n";
    while(i<=index){
        v = (double)v/p;
        if(v<=n){
            return 1;
        }
        i++;
    }
    return 0;
}
long long int expo(long long int a,long long int b){
    long long int x = b;
    long long int ans=1,y=a;
    while(x>0){
        if(x%2==1){
            ans = (ans*y)%MOD;
        }
        y = (y*y)%MOD;
        x = x/2;
    }
    return ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input1.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int t;
    cin>>t;

    vector<long long int> arr(N);
    vector<int>freq(N);
    int j,i;
    while(t--){
        long long int p1 = 0,p2 = 0;
        cin>>n>>p;
        for(i=0;i<n;i++){
            cin>>arr[i];
        }
        sort(arr.begin(),arr.begin()+n,greater<int>());
        p1 = arr[0];
        long long int curr_sum = expo(p,arr[0]);
        map<int,int> m;
        for(i=1;i<n;i++){
            // cout<<"i:"<<i<<"curr_sum:"<<curr_sum<<"\n";
            if(curr_sum==0){
                curr_sum = (curr_sum + expo(p,arr[i]))%MOD;
                p1 = arr[i];
            }
            else{
                curr_sum = (curr_sum - expo(p,arr[i])+MOD)%MOD;
                if(m.find(arr[i])==m.end()){
                    m[arr[i]]=1;
                    if(m[p1]==1){
                        curr_sum = 0;
                        m.clear();
                    }
                }
                else{
                    m[arr[i]]++;
                    int curr = arr[i];
                    while(m[curr]==p){
                        m[curr]=0;
                        if(m.find(curr+1)==m.end()){
                            m[curr+1]=1;
                        }
                        else{
                            m[curr+1]++;
                        }
                        curr++;
                    }
                    if(m[p1]==1){
                        curr_sum = 0;
                        m.clear();
                        continue;
                    }
                }
            }
            // map<int,int>::iterator itr;
            // for(itr = m.begin();itr!=m.end();itr++){
            //     cout<<"first:"<<itr->first<<"second:"<<itr->second<<"\n";
            // }
        }
        cout<<curr_sum<<"\n";
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define fo(i,n) for(i=0;i<n;i++)
int dp[1005][1005][4],arr[1005][1005],n,m;
ll ans=0;
int fun(int,int,int);
int main()
{
  int i,j;cin>>n>>m;
  memset(dp,-1,sizeof(dp));
  memset(arr,0,sizeof(arr));
  for(i=1;i<=n;i++)
  {
    // arr[i][0]=0;
    for(j=1;j<=m;j++)
     {
       cin>>arr[i][j];
       // arr[j][0]=0;
     }
  }
  for(i=1;i<=n;i++)
  {
    for(j=1;j<=m;j++)
     {
       ll val=
          max(fun(i,j-1,0)+fun(i-1,j,1)+fun(i+1,j,2)+fun(i,j+1,3),
          fun(i-1,j,0)+fun(i,j+1,1)+fun(i,j-1,2)+fun(i+1,j,3));
        if(i>1&&j>1&&i<n&&j<m)
       ans=max(ans,val);
     }
  }
  cout<<ans;
  return 0;
}
int fun(int i,int j,int op)
{
 if(dp[i][j][op]!=-1)
    return dp[i][j][op];
 if(i==0||j==0||i==n+1||j==m+1)
   return dp[i][j][op]=0;
 if(op==0)
 {
   return dp[i][j][op]=(arr[i][j]+max(fun(i-1,j,0),fun(i,j-1,0)));
 }
 if(op==1)
 {
   return dp[i][j][op]=(arr[i][j]+max(fun(i-1,j,1),fun(i,j+1,1)));
  }
 if(op==2)
 {
   return dp[i][j][op]=(arr[i][j]+max(fun(i+1,j,2),fun(i,j-1,2)));
 }
 if(op==3)
 {
   return dp[i][j][op]=(arr[i][j]+max(fun(i+1,j,3),fun(i,j+1,3)));
 }
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define fo(i,n) for(i=0;i<n;i++)
int dp[1005][1005][4],arr[1005][1005],n,m;
ll ans=0;
int fun(int,int,int);
int main()
{
  int i,j;cin>>n>>m;
  memset(dp,0,sizeof(dp));
  memset(arr,0,sizeof(arr));
  for(i=1;i<=n;i++)
  {
    // arr[i][0]=0;
    for(j=1;j<=m;j++)
     {
       cin>>arr[i][j];
       // arr[j][0]=0;
     }
  }
  for(i=1;i<=n;i++)
  {
    for(j=1;j<=m;j++)
     {
       ll val=
      (max(fun(i-1,j,0),fun(i,j-1,0))+
       max(fun(i-1,j,1),fun(i,j+1,1))+
       max(fun(i+1,j,2),fun(i,j-1,2))+
       max(fun(i+1,j,3),fun(i,j+1,3)));
       if((fun(i,j+1,1)>fun(i-1,j,1))&&(fun(i,j+1,3)>fun(i+1,j,3)))
       {
           val-=fun(i,j+1,3);
           val=val+max(fun(i+1,j,3),fun(i-1,j,1));
       }
       ans=max(ans,val);
     }
  }
  cout<<ans;
  return 0;
}
int fun(int i,int j,int op)
{
 if(op==0)
 {
   if(i==0||j==0)
   return 0;
   return (arr[i][j]+max(fun(i-1,j,0),fun(i,j-1,0)));
 }
 if(op==1)
 {
   if(i==0||j==m+1)
   return 0;
   return (arr[i][j]+max(fun(i-1,j,1),fun(i,j+1,1)));
  }
 if(op==2)
 {
   if(i==n+1||j==0)
   return 0;
   return (arr[i][j]+max(fun(i+1,j,2),fun(i,j-1,2)));
 }
 if(op==3)
 {
   if(i==n+1||j==m+1)
   return 0;
   return (arr[i][j]+max(fun(i+1,j,3),fun(i,j+1,3)));
 }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define fo(i,n) for(i=0;i<n;i++)
int dp[1005][1005][4],arr[1005][1005],n,m;
ll ans=0;
int fun(int,int,int);
int main()
{
  int i,j;cin>>n>>m;
  memset(dp,0,sizeof(dp));
  memset(arr,0,sizeof(arr));
  for(i=1;i<=n;i++)
  {
    // arr[i][0]=0;
    for(j=1;j<=m;j++)
     {
       cin>>arr[i][j];
       // arr[j][0]=0;
     }
  }
  for(i=1;i<=n;i++)
  {
    for(j=1;j<=m;j++)
     {
       ll val=
      (max(fun(i-1,j,0),fun(i,j-1,0))+
       max(fun(i-1,j,1),fun(i,j+1,1))+
       max(fun(i+1,j,2),fun(i,j-1,2))+
       max(fun(i+1,j,3),fun(i,j+1,3)));
       ans=max(ans,val);
     }
  }
  cout<<ans;
  return 0;
}
int fun(int i,int j,int op)
{
 if(op==0)
 {
   if(i==0||j==0)
   return 0;
   return (arr[i][j]+max(fun(i-1,j,0),fun(i,j-1,0)));
 }
 if(op==1)
 {
   if(i==0||j==m+1)
   return 0;
   return (arr[i][j]+max(fun(i-1,j,1),fun(i,j+1,1)));
  }
 if(op==2)
 {
   if(i==n+1||j==0)
   return 0;
   return (arr[i][j]+max(fun(i+1,j,2),fun(i,j-1,2)));
 }
 if(op==3)
 {
   if(i==n+1||j==m+1)
   return 0;
   return (arr[i][j]+max(fun(i+1,j,3),fun(i,j+1,3)));
 }
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pi       acos(-1)
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define F first
#define S second
#define ppb pop_back
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) (m*n)/GCD(m,n)
#define rep(i,a,n)  for(int i=a ; i<n ; i++)
#define repe(i,a,n) for(int i=a;i<=n;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
////-------------------------------------------------------------------------
#define ld long double
#define int long long ////////////
////define ll long long
////--------------------------------------------------------------------------
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef vector<pii > vpii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef map<int,int> mii;
typedef unordered_map<int,int> umii;
typedef map<char,int> mci;

struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);}

size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};

typedef unordered_map<int, int, custom_hash> safe_map;
typedef gp_hash_table<int, int, custom_hash> safe_hash_table;
////--------------------------------------------------------------------------
#define itv for(auto it= v.begin(); it != v.end(); ++it )
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(ALL(container),element) != container.end())
#define   bs binary_search
#define  lb(v,val)  lower_bound(ALL(v), val)
#define  ub(v,val)  upper_bound(ALL(v), val)
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define maxc(v) *max_element(ALL(v))
#define minc(v) *min_element(ALL(v))
#define isvowel(a) (a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
////--------------------------------------------------------------------------
 /*
#define dbg1(a) cout<<" *"<<a<<" ";
#define dbg2(a,b) cout<<" *"<<a<<" **"<<b<<" "     /////
#define dbg cout<<"move"
*/
#define nl "\n"
#define vin(v,n)   vi v(n); rep(i,0,n)  cin>>v[i]
#define dbg cerr <<"At line "<<__LINE__<<" move "<<nl
#define dbg1(x) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<nl
#define dbg2(x,y) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<nl
#define dbg3(x,y,z) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<nl
#define prv(v)     rep(i,0,sz(v))  cerr<<v[i]<<" "; cerr<<endl
#define sz(s)         (int)((s).size())
#define coutsp(k)   cout<<setprecision(k)  //set precision (total length k icluding decimal and non decimal)
#define coutfsp(k)   cout<<fixed<<setprecision(k)  //fixed set precision(after decimal fixing)
#define trav(a, x) for(auto& a : x)
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define displaycerr(x) trav(a,x) cerr<<a<<" ";cerr<<endl

#define MOD 1000000007
#define POD 998244353
#define INF   (int)2e18   //------

const int N=1e5+1;

//-------------------------------------------------------------------------------

void do2win(int test)
{

  int n,m;  cin>>n>>m;
  int a[n+1][m+1];  rep(i,1,n+1) rep(j,1,m+1)  cin>>a[i][j];

  int dp1[n+2][m+2]={},dp2[n+2][m+2]={};

  rep(i,1,n+1)
  {
    rep(j,1,m+1)
    {
      dp1[i][j]=max(dp1[i][j-1],dp1[i][j-1])+a[i][j];
    }
  }

  rev(i,n,1)
  {
    rep(j,1,n+1)
   {
     dp2[i][j]=max(dp2[i+1][j],dp2[i][j-1])+a[i][j];
   }
  }

  int ans=0;

  rep(i,1,n+1)
  {
    rep(j,1,m+1)
    {
      ans=max(ans,dp1[i][j]+dp2[i][j]);
    }
  }

  cout<<ans;

}
//-------------------------------------------------------------------------------

signed main()
{

   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt","w", stderr);
   #endif

   // #ifndef ONLINE_JUDGE
   //  cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms";
   // #endif

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   FAST;
cout<<fixed<<setprecision(20);

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   int test;  test=1;
///////---------------------------------|||||
            // cin>>test;        ////////>>>>>>>
//////----------------------------------|||||

    for(int i=1;i<=test;i++)
    {
       // cout<<"Case #"<<i<<": ";

      do2win(i);
      cout<<nl;   //  IMPORTANT FOR NEXT LINE
    }

   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pi       acos(-1)
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define F first
#define S second
#define ppb pop_back
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) (m*n)/GCD(m,n)
#define rep(i,a,n)  for(int i=a ; i<n ; i++)
#define repe(i,a,n) for(int i=a;i<=n;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
////-------------------------------------------------------------------------
#define ld long double
#define int long long ////////////
////define ll long long
////--------------------------------------------------------------------------
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef vector<pii > vpii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef map<int,int> mii;
typedef unordered_map<int,int> umii;
typedef map<char,int> mci;

struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);}

size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};

typedef unordered_map<int, int, custom_hash> safe_map;
typedef gp_hash_table<int, int, custom_hash> safe_hash_table;
////--------------------------------------------------------------------------
#define itv for(auto it= v.begin(); it != v.end(); ++it )
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(ALL(container),element) != container.end())
#define   bs binary_search
#define  lb(v,val)  lower_bound(ALL(v), val)
#define  ub(v,val)  upper_bound(ALL(v), val)
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define maxc(v) *max_element(ALL(v))
#define minc(v) *min_element(ALL(v))
#define isvowel(a) (a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
////--------------------------------------------------------------------------
 /*
#define dbg1(a) cout<<" *"<<a<<" ";
#define dbg2(a,b) cout<<" *"<<a<<" **"<<b<<" "     /////
#define dbg cout<<"move"
*/
#define nl "\n"
#define vin(v,n)   vi v(n); rep(i,0,n)  cin>>v[i]
#define dbg cerr <<"At line "<<__LINE__<<" move "<<nl
#define dbg1(x) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<nl
#define dbg2(x,y) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<nl
#define dbg3(x,y,z) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<nl
#define prv(v)     rep(i,0,sz(v))  cerr<<v[i]<<" "; cerr<<endl
#define sz(s)         (int)((s).size())
#define coutsp(k)   cout<<setprecision(k)  //set precision (total length k icluding decimal and non decimal)
#define coutfsp(k)   cout<<fixed<<setprecision(k)  //fixed set precision(after decimal fixing)
#define trav(a, x) for(auto& a : x)
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define displaycerr(x) trav(a,x) cerr<<a<<" ";cerr<<endl

#define MOD 1000000007
#define POD 998244353
#define INF   (int)2e18   //------

const int N=1e5+1;

//-------------------------------------------------------------------------------

void do2win(int test)
{

  int n,m;  cin>>n>>m;
  int a[n+1][m+1];  rep(i,1,n+1) rep(j,1,m+1)  cin>>a[i][j];

  int dp1[n+2][m+2]={},dp2[n+2][m+2]={},dp3[n+2][m+2]={},dp4[n+2][m+2]={};

  rep(i,1,n+1)
  {
    rep(j,1,m+1)
    {
      dp1[i][j]=max(dp1[i-1][j],dp1[i][j-1])+a[i][j];
    }
  }

    rep(i,1,n+1)
  {
    rev(j,m,1)
    {
      dp2[i][j]=max(dp2[i-1][j],dp2[i][j+1])+a[i][j];
    }
  }

  rev(i,n,1)
  {
    rep(j,1,m+1)
   {
     dp3[i][j]=max(dp3[i+1][j],dp3[i][j-1])+a[i][j];
   }
  }

    rev(i,n,1)
  {
    rev(j,m,1)
   {
     dp4[i][j]=max(dp4[i+1][j],dp4[i][j+1])+a[i][j];
   }
  }

  int ans=0;

  rep(i,2,n)
  {
    rep(j,2,m)
    {
      ans=max(ans,dp1[i][j]+dp2[i][j]+dp3[i][j]+dp4[i][j]-4*a[i][j]);
    }
  }

  cout<<ans;

}
//-------------------------------------------------------------------------------

signed main()
{

   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt","w", stderr);
   #endif

   // #ifndef ONLINE_JUDGE
   //  cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms";
   // #endif

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   FAST;
cout<<fixed<<setprecision(20);

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   int test;  test=1;
///////---------------------------------|||||
            // cin>>test;        ////////>>>>>>>
//////----------------------------------|||||

    for(int i=1;i<=test;i++)
    {
       // cout<<"Case #"<<i<<": ";

      do2win(i);
      cout<<nl;   //  IMPORTANT FOR NEXT LINE
    }

   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pi       acos(-1)
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define F first
#define S second
#define ppb pop_back
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) (m*n)/GCD(m,n)
#define rep(i,a,n)  for(int i=a ; i<n ; i++)
#define repe(i,a,n) for(int i=a;i<=n;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
////-------------------------------------------------------------------------
#define ld long double
#define int long long ////////////
////define ll long long
////--------------------------------------------------------------------------
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef vector<pii > vpii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef map<int,int> mii;
typedef unordered_map<int,int> umii;
typedef map<char,int> mci;

struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);}

size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};

typedef unordered_map<int, int, custom_hash> safe_map;
typedef gp_hash_table<int, int, custom_hash> safe_hash_table;
////--------------------------------------------------------------------------
#define itv for(auto it= v.begin(); it != v.end(); ++it )
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(ALL(container),element) != container.end())
#define   bs binary_search
#define  lb(v,val)  lower_bound(ALL(v), val)
#define  ub(v,val)  upper_bound(ALL(v), val)
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define maxc(v) *max_element(ALL(v))
#define minc(v) *min_element(ALL(v))
#define isvowel(a) (a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
////--------------------------------------------------------------------------
 /*
#define dbg1(a) cout<<" *"<<a<<" ";
#define dbg2(a,b) cout<<" *"<<a<<" **"<<b<<" "     /////
#define dbg cout<<"move"
*/
#define nl "\n"
#define vin(v,n)   vi v(n); rep(i,0,n)  cin>>v[i]
#define dbg cerr <<"At line "<<__LINE__<<" move "<<nl
#define dbg1(x) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<nl
#define dbg2(x,y) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<nl
#define dbg3(x,y,z) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<nl
#define prv(v)     rep(i,0,sz(v))  cerr<<v[i]<<" "; cerr<<endl
#define sz(s)         (int)((s).size())
#define coutsp(k)   cout<<setprecision(k)  //set precision (total length k icluding decimal and non decimal)
#define coutfsp(k)   cout<<fixed<<setprecision(k)  //fixed set precision(after decimal fixing)
#define trav(a, x) for(auto& a : x)
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define displaycerr(x) trav(a,x) cerr<<a<<" ";cerr<<endl

#define MOD 1000000007
#define POD 998244353
#define INF   (int)2e18   //------

const int N=1e5+1;

//-------------------------------------------------------------------------------

void do2win(int test)
{

  int n,m;  cin>>n>>m;
  int a[n+1][m+1];  rep(i,1,n+1) rep(j,1,m+1)  cin>>a[i][j];

  int dp1[n+2][m+2]={},dp2[n+2][m+2]={},dp3[n+2][m+2]={},dp4[n+2][m+2]={};

  rep(i,1,n+1)
  {
    rep(j,1,m+1)
    {
      dp1[i][j]=max(dp1[i][j-1],dp1[i][j-1])+a[i][j];
    }
  }

    rep(i,1,n+1)
  {
    rev(j,m,1)
    {
      dp2[i][j]=max(dp2[i-1][j],dp2[i][j+1])+a[i][j];
    }
  }

  rev(i,n,1)
  {
    rep(j,1,m+1)
   {
     dp3[i][j]=max(dp3[i+1][j],dp3[i][j-1])+a[i][j];
   }
  }

    rev(i,n,1)
  {
    rev(j,m,1)
   {
     dp4[i][j]=max(dp4[i+1][j],dp4[i][j+1])+a[i][j];
   }
  }

  int ans=0;

  rep(i,1,n+1)
  {
    rep(j,1,m+1)
    {
      ans=max(ans,dp1[i][j]+dp2[i][j]+dp3[i][j]+dp4[i][j]-4*a[i][j]);
    }
  }

  cout<<ans;

}
//-------------------------------------------------------------------------------

signed main()
{

   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt","w", stderr);
   #endif

   // #ifndef ONLINE_JUDGE
   //  cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms";
   // #endif

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   FAST;
cout<<fixed<<setprecision(20);

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   int test;  test=1;
///////---------------------------------|||||
            // cin>>test;        ////////>>>>>>>
//////----------------------------------|||||

    for(int i=1;i<=test;i++)
    {
       // cout<<"Case #"<<i<<": ";

      do2win(i);
      cout<<nl;   //  IMPORTANT FOR NEXT LINE
    }

   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pi       acos(-1)
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define F first
#define S second
#define ppb pop_back
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) (m*n)/GCD(m,n)
#define rep(i,a,n)  for(int i=a ; i<n ; i++)
#define repe(i,a,n) for(int i=a;i<=n;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
////-------------------------------------------------------------------------
#define ld long double
#define int long long ////////////
////define ll long long
////--------------------------------------------------------------------------
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef vector<pii > vpii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef map<int,int> mii;
typedef unordered_map<int,int> umii;
typedef map<char,int> mci;

struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);}

size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};

typedef unordered_map<int, int, custom_hash> safe_map;
typedef gp_hash_table<int, int, custom_hash> safe_hash_table;
////--------------------------------------------------------------------------
#define itv for(auto it= v.begin(); it != v.end(); ++it )
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(ALL(container),element) != container.end())
#define   bs binary_search
#define  lb(v,val)  lower_bound(ALL(v), val)
#define  ub(v,val)  upper_bound(ALL(v), val)
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define maxc(v) *max_element(ALL(v))
#define minc(v) *min_element(ALL(v))
#define isvowel(a) (a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
////--------------------------------------------------------------------------
 /*
#define dbg1(a) cout<<" *"<<a<<" ";
#define dbg2(a,b) cout<<" *"<<a<<" **"<<b<<" "     /////
#define dbg cout<<"move"
*/
#define nl "\n"
#define vin(v,n)   vi v(n); rep(i,0,n)  cin>>v[i]
#define dbg cerr <<"At line "<<__LINE__<<" move "<<nl
#define dbg1(x) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<nl
#define dbg2(x,y) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<nl
#define dbg3(x,y,z) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<nl
#define prv(v)     rep(i,0,sz(v))  cerr<<v[i]<<" "; cerr<<endl
#define sz(s)         (int)((s).size())
#define coutsp(k)   cout<<setprecision(k)  //set precision (total length k icluding decimal and non decimal)
#define coutfsp(k)   cout<<fixed<<setprecision(k)  //fixed set precision(after decimal fixing)
#define trav(a, x) for(auto& a : x)
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define displaycerr(x) trav(a,x) cerr<<a<<" ";cerr<<endl

#define MOD 1000000007
#define POD 998244353
#define INF   (int)2e18   //------

const int N=1e5+1;

//-------------------------------------------------------------------------------

void do2win(int test)
{

  int n,m;  cin>>n>>m;
  int a[n+1][m+1];  rep(i,1,n+1) rep(j,1,m+1)  cin>>a[i][j];

  int dp1[n+2][m+2]={},dp2[n+2][m+2]={},dp3[n+2][m+2]={},dp4[n+2][m+2]={};

  rep(i,1,n+1)
  {
    rep(j,1,m+1)
    {
      dp1[i][j]=max(dp1[i][j-1],dp1[i][j-1])+a[i][j];
    }
  }

    rep(i,1,n+1)
  {
    rev(j,m,1)
    {
      dp2[i][j]=max(dp2[i-1][j],dp2[i][j+1])+a[i][j];
    }
  }

  rev(i,n,1)
  {
    rep(j,1,m+1)
   {
     dp3[i][j]=max(dp3[i+1][j],dp3[i][j-1])+a[i][j];
   }
  }

    rev(i,n,1)
  {
    rev(j,m,1)
   {
     dp4[i][j]=max(dp4[i+1][j],dp4[i][j+1])+a[i][j];
   }
  }

  int ans=0;

  rep(i,2,n)
  {
    rep(j,2,m)
    {
      ans=max(ans,dp1[i][j]+dp2[i][j]+dp3[i][j]+dp4[i][j]-4*a[i][j]);
    }
  }

  cout<<ans;

}
//-------------------------------------------------------------------------------

signed main()
{

   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt","w", stderr);
   #endif

   // #ifndef ONLINE_JUDGE
   //  cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms";
   // #endif

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   FAST;
cout<<fixed<<setprecision(20);

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   int test;  test=1;
///////---------------------------------|||||
            // cin>>test;        ////////>>>>>>>
//////----------------------------------|||||

    for(int i=1;i<=test;i++)
    {
       // cout<<"Case #"<<i<<": ";

      do2win(i);
      cout<<nl;   //  IMPORTANT FOR NEXT LINE
    }

   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pi       acos(-1)
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define F first
#define S second
#define ppb pop_back
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) (m*n)/GCD(m,n)
#define rep(i,a,n)  for(int i=a ; i<n ; i++)
#define repe(i,a,n) for(int i=a;i<=n;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
////-------------------------------------------------------------------------
#define ld long double
#define int long long ////////////
////define ll long long
////--------------------------------------------------------------------------
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef vector<pii > vpii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef map<int,int> mii;
typedef unordered_map<int,int> umii;
typedef map<char,int> mci;

struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);}

size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};

typedef unordered_map<int, int, custom_hash> safe_map;
typedef gp_hash_table<int, int, custom_hash> safe_hash_table;
////--------------------------------------------------------------------------
#define itv for(auto it= v.begin(); it != v.end(); ++it )
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(ALL(container),element) != container.end())
#define   bs binary_search
#define  lb(v,val)  lower_bound(ALL(v), val)
#define  ub(v,val)  upper_bound(ALL(v), val)
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define maxc(v) *max_element(ALL(v))
#define minc(v) *min_element(ALL(v))
#define isvowel(a) (a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
////--------------------------------------------------------------------------
 /*
#define dbg1(a) cout<<" *"<<a<<" ";
#define dbg2(a,b) cout<<" *"<<a<<" **"<<b<<" "     /////
#define dbg cout<<"move"
*/
#define nl "\n"
#define vin(v,n)   vi v(n); rep(i,0,n)  cin>>v[i]
#define dbg cerr <<"At line "<<__LINE__<<" move "<<nl
#define dbg1(x) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<nl
#define dbg2(x,y) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<nl
#define dbg3(x,y,z) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<nl
#define prv(v)     rep(i,0,sz(v))  cerr<<v[i]<<" "; cerr<<endl
#define sz(s)         (int)((s).size())
#define coutsp(k)   cout<<setprecision(k)  //set precision (total length k icluding decimal and non decimal)
#define coutfsp(k)   cout<<fixed<<setprecision(k)  //fixed set precision(after decimal fixing)
#define trav(a, x) for(auto& a : x)
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define displaycerr(x) trav(a,x) cerr<<a<<" ";cerr<<endl

#define MOD 1000000007
#define POD 998244353
#define INF   (int)2e18   //------

const int N=1e5+1;

//-------------------------------------------------------------------------------

void do2win(int test)
{

  int n,m;  cin>>n>>m;
  int a[n+1][m+1];  rep(i,1,n+1) rep(j,1,m+1)  cin>>a[i][j];

  int dp1[n+2][m+2]={},dp2[n+2][m+2]={},dp3[n+2][m+2]={},dp4[n+2][m+2]={};

  rep(i,1,n+1)
  {
    rep(j,1,m+1)
    {
      dp1[i][j]=max(dp1[i-1][j],dp1[i][j-1])+a[i][j];
    }
  }

    rep(i,1,n+1)
  {
    rev(j,m,1)
    {
      dp2[i][j]=max(dp2[i-1][j],dp2[i][j+1])+a[i][j];
    }
  }

  rev(i,n,1)
  {
    rep(j,1,m+1)
   {
     dp3[i][j]=max(dp3[i+1][j],dp3[i][j-1])+a[i][j];
   }
  }

    rev(i,n,1)
  {
    rev(j,m,1)
   {
     dp4[i][j]=max(dp4[i+1][j],dp4[i][j+1])+a[i][j];
   }
  }

  int ans=0;

  rep(i,2,n)
  {
    rep(j,2,m)
    {
      ans=max(ans,dp1[i][j-1]+dp2[i-1][j]+dp3[i+1][j]+dp4[i][j+1]);

      ans=max(ans,dp1[i-1][j]+dp2[i][j+1]+dp3[i][j-1]+dp4[i+1][j]);
    }
  }

  cout<<ans;

}
//-------------------------------------------------------------------------------

signed main()
{

   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt","w", stderr);
   #endif

   // #ifndef ONLINE_JUDGE
   //  cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms";
   // #endif

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   FAST;
cout<<fixed<<setprecision(20);

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   int test;  test=1;
///////---------------------------------|||||
            // cin>>test;        ////////>>>>>>>
//////----------------------------------|||||

    for(int i=1;i<=test;i++)
    {
       // cout<<"Case #"<<i<<": ";

      do2win(i);
      cout<<nl;   //  IMPORTANT FOR NEXT LINE
    }

   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pi       acos(-1)
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define F first
#define S second
#define ppb pop_back
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) (m*n)/GCD(m,n)
#define rep(i,a,n)  for(int i=a ; i<n ; i++)
#define repe(i,a,n) for(int i=a;i<=n;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
////-------------------------------------------------------------------------
#define ld long double
#define int long long ////////////
////define ll long long
////--------------------------------------------------------------------------
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef vector<pii > vpii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef map<int,int> mii;
typedef unordered_map<int,int> umii;
typedef map<char,int> mci;

struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);}

size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};

typedef unordered_map<int, int, custom_hash> safe_map;
typedef gp_hash_table<int, int, custom_hash> safe_hash_table;
////--------------------------------------------------------------------------
#define itv for(auto it= v.begin(); it != v.end(); ++it )
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(ALL(container),element) != container.end())
#define   bs binary_search
#define  lb(v,val)  lower_bound(ALL(v), val)
#define  ub(v,val)  upper_bound(ALL(v), val)
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define maxc(v) *max_element(ALL(v))
#define minc(v) *min_element(ALL(v))
#define isvowel(a) (a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
////--------------------------------------------------------------------------
 /*
#define dbg1(a) cout<<" *"<<a<<" ";
#define dbg2(a,b) cout<<" *"<<a<<" **"<<b<<" "     /////
#define dbg cout<<"move"
*/
#define nl "\n"
#define vin(v,n)   vi v(n); rep(i,0,n)  cin>>v[i]
#define dbg cerr <<"At line "<<__LINE__<<" move "<<nl
#define dbg1(x) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<nl
#define dbg2(x,y) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<nl
#define dbg3(x,y,z) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<nl
#define prv(v)     rep(i,0,sz(v))  cerr<<v[i]<<" "; cerr<<endl
#define sz(s)         (int)((s).size())
#define coutsp(k)   cout<<setprecision(k)  //set precision (total length k icluding decimal and non decimal)
#define coutfsp(k)   cout<<fixed<<setprecision(k)  //fixed set precision(after decimal fixing)
#define trav(a, x) for(auto& a : x)
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define displaycerr(x) trav(a,x) cerr<<a<<" ";cerr<<endl

#define MOD 1000000007
#define POD 998244353
#define INF   (int)2e18   //------

const int N=1e5+1;

//-------------------------------------------------------------------------------

void do2win(int test)
{

  int n,m;  cin>>n>>m;
  int a[n+1][m+1];  rep(i,1,n+1) rep(j,1,m+1)  cin>>a[i][j];

  int dp1[n+2][m+2]={},dp2[n+2][m+2]={},dp3[n+2][m+2]={},dp4[n+2][m+2]={};

  rep(i,1,n+1)
  {
    rep(j,1,m+1)
    {
      dp1[i][j]=max(dp1[i][j-1],dp1[i][j-1])+a[i][j];
    }
  }

    rep(i,1,n+1)
  {
    rev(j,m,1)
    {
      dp2[i][j]=max(dp2[i-1][j],dp2[i][j+1])+a[i][j];
    }
  }

  rev(i,n,1)
  {
    rep(j,1,m+1)
   {
     dp3[i][j]=max(dp3[i+1][j],dp3[i][j-1])+a[i][j];
   }
  }

    rev(i,n,1)
  {
    rev(j,m,1)
   {
     dp4[i][j]=max(dp4[i+1][j],dp4[i][j+1])+a[i][j];
   }
  }

  int ans=0;

  rep(i,2,n)
  {
    rep(j,2,m)
    {
      ans=max(ans,dp1[i][j-1]+dp2[i-1][j]+dp3[i+1][j]+dp4[i][j+1]);

      ans=max(ans,dp1[i-1][j]+dp2[i][j+1]+dp3[i][j-1]+dp4[i+1][j]);
    }
  }

  cout<<ans;

}
//-------------------------------------------------------------------------------

signed main()
{

   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt","w", stderr);
   #endif

   // #ifndef ONLINE_JUDGE
   //  cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms";
   // #endif

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   FAST;
cout<<fixed<<setprecision(20);

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   int test;  test=1;
///////---------------------------------|||||
            // cin>>test;        ////////>>>>>>>
//////----------------------------------|||||

    for(int i=1;i<=test;i++)
    {
       // cout<<"Case #"<<i<<": ";

      do2win(i);
      cout<<nl;   //  IMPORTANT FOR NEXT LINE
    }

   return 0;
}


//BISMILLAH
//RABBI JIDNI ILMA
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ull  unsigned long long
#define vll  vector <long long>
#define pll pair <long long,long long>
#define f first
#define s second
#define up upper_bound
#define lp lower_bound
#define pq priority_queue
#define inf 1e10
#define minf -1e10
#define pi 3.14159265
#define mod 1000000000
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);
#define rep(p,q) for(ll i=p;i<q;i++)
#define per(p,q) for(ll i=p-1;i>=0;i--)
#define o 0
int main()
{
    fastio
    ll n,m;
    cin>>n>>m;
    ll ara[1003][1003],dp[1003][1003],dp2[1003][1003],lahub[1003][1003],lahubina[1003][1003];
    memset(dp,0,sizeof(dp));
    memset(dp2,0,sizeof(dp2));
    memset(lahub,0,sizeof(lahub));
    memset(lahubina,0,sizeof(lahubina));
    for(ll i=1;i<=n;i++)for(ll j=1;j<=m;j++)cin>>ara[i][j];
    //lahubina start
    for(ll i=n;i>0;i--)for(ll j=1;j<=m;j++)
    dp[i][j]=max(dp[i+1][j],dp[i][j-1])+ara[i][j];
    //lahub start
    for(ll i=1;i<=n;i++)for(ll j=1;j<=m;j++)dp2[i][j]=max(dp2[i-1][j],dp2[i][j-1])+ara[i][j];
    //lahub end
    for(ll i=n;i>0;i--)for(ll j=m;j>0;j--)lahub[i][j]=max(lahub[i+1][j],lahub[i][j+1])+ara[i][j];
    //lahubina end
    for(ll i=1;i<=n;i++)for(ll j=m;j>0;j--)lahubina[i][j]=max(lahubina[i-1][j],lahubina[i][j+1])+ara[i][j];
    //answer
    ll ans=minf;
    for(ll i=2;i<n;i++)for(ll j=2;j<m;j++)ans=max({ans,dp2[i-1][j]+dp[i][j-1]+lahub[i+1][j]+lahubina[i][j+1],dp[i+1][j]+lahubina[i-1][j]+dp[i][j-1]+lahub[i][j+1]});
    cout<<ans<<endl;
}


//BISMILLAH
//RABBI JIDNI ILMA
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ull  unsigned long long
#define vll  vector <long long>
#define pll pair <long long,long long>
#define f first
#define s second
#define up upper_bound
#define lp lower_bound
#define pq priority_queue
#define inf 1e10
#define minf -1e10
#define pi 3.14159265
#define mod 1000000000
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);
#define rep(p,q) for(ll i=p;i<q;i++)
#define per(p,q) for(ll i=p-1;i>=0;i--)
#define o 0
int main()
{
    fastio
    ll n,m;
    cin>>n>>m;
    ll ara[100][100],dp[100][100],dp2[100][100],lahub[100][100],lahubina[100][100];
    memset(dp,0,sizeof(dp));
    for(ll i=1;i<=n;i++)for(ll j=1;j<=m;j++)cin>>ara[i][j];
    dp2[1][1]=ara[1][1];dp[n][1]=ara[n][1];
    //lahubina start
    for(ll i=2;i<=m;i++)
    dp[n][i]=(dp[n][i-1]+ara[n][i]);
    for(ll i=n-1;i>0;i--)
       dp[i][1]=((dp[i+1][1])+ara[i][1]);
    for(ll i=n-1;i>0;i--)
         for(ll j=2;j<=m;j++)
            dp[i][j]=max(dp[i+1][j],dp[i][j-1])+ara[i][j];
    //lahub start
    for(ll i=2;i<=m;i++)
    dp2[n][i]=((dp2[1][i-1])+ara[1][i]);
    for(ll i=2;i<=n;i++)
    dp2[i][1]=((dp2[i-1][1])+ara[i][1]);
    for(ll i=2;i<=n;i++)
    for(ll j=2;j<=m;j++)
    dp2[i][j]=max(dp2[i-1][j],dp2[i][j-1])+ara[i][j];
    //lahub end
    lahub[n][m]=ara[n][m];
    for(ll i=n-1;i>0;i--)lahub[i][m]=lahub[i+1][m]+ara[i][m];
    for(ll i=m-1;i>0;i--)lahub[n][i]=lahub[n][i+1]+ara[n][i];
    for(ll i=n-1;i>0;i--)for(ll j=m-1;j>0;j--)lahub[i][j]=max(lahub[i+1][j],lahub[i][j+1])+ara[i][j];
    //lahubina end
    lahubina[1][m]=ara[1][m];
    for(ll i=m-1;i>0;i--)lahubina[1][i]=lahubina[1][i+1]+ara[1][i];
    for(ll i=2;i<=n;i++)lahubina[i][m]=lahubina[i-1][m]+ara[1][m];
    for(ll i=2;i<=n;i++)for(ll j=m-1;j>0;j--)lahubina[i][j]=max(lahubina[i-1][j],lahubina[i][j+1])+ara[i][j];
    //answer
    ll ans=minf;
    for(ll i=1;i<=n;i++)for(ll j=1;j<=m;j++)ans=max(ans,dp[i][j]+dp2[i][j]+lahub[i][j]+lahubina[i][j]-4*ara[i][j]);
    cout<<ans<<endl;

}


//BISMILLAH
//RABBI JIDNI ILMA
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ull  unsigned long long
#define vll  vector <long long>
#define pll pair <long long,long long>
#define f first
#define s second
#define up upper_bound
#define lp lower_bound
#define pq priority_queue
#define inf 1e10
#define minf -1e10
#define pi 3.14159265
#define mod 1000000000
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);
#define rep(p,q) for(ll i=p;i<q;i++)
#define per(p,q) for(ll i=p-1;i>=0;i--)
#define o 0
int main()
{
    fastio
    ll n,m;
    cin>>n>>m;
    ll ara[1001][1001],dp[1001][1001],dp2[1001][1001],lahub[1001][1001],lahubina[1001][1001];
    memset(dp,0,sizeof(dp));
    memset(dp2,0,sizeof(dp2));
    memset(lahub,0,sizeof(lahub));
    memset(lahubina,0,sizeof(lahubina));
    for(ll i=1;i<=n;i++)for(ll j=1;j<=m;j++)cin>>ara[i][j];
    dp2[1][1]=ara[1][1];dp[n][1]=ara[n][1];
    //lahubina start
    for(ll i=2;i<=m;i++)
    dp[n][i]=(dp[n][i-1]+ara[n][i]);
    for(ll i=n-1;i>0;i--)
       dp[i][1]=((dp[i+1][1])+ara[i][1]);
    for(ll i=n-1;i>0;i--)
         for(ll j=2;j<=m;j++)
            dp[i][j]=max(dp[i+1][j],dp[i][j-1])+ara[i][j];
    //lahub start
    for(ll i=2;i<=m;i++)
    dp2[n][i]=((dp2[1][i-1])+ara[1][i]);
    for(ll i=2;i<=n;i++)
    dp2[i][1]=((dp2[i-1][1])+ara[i][1]);
    for(ll i=2;i<=n;i++)
    for(ll j=2;j<=m;j++)
    dp2[i][j]=max(dp2[i-1][j],dp2[i][j-1])+ara[i][j];
    //lahub end
    lahub[n][m]=ara[n][m];
    for(ll i=n-1;i>0;i--)lahub[i][m]=lahub[i+1][m]+ara[i][m];
    for(ll i=m-1;i>0;i--)lahub[n][i]=lahub[n][i+1]+ara[n][i];
    for(ll i=n-1;i>0;i--)for(ll j=m-1;j>0;j--)lahub[i][j]=max(lahub[i+1][j],lahub[i][j+1])+ara[i][j];
    //lahubina end
    lahubina[1][m]=ara[1][m];
    for(ll i=m-1;i>0;i--)lahubina[1][i]=lahubina[1][i+1]+ara[1][i];
    for(ll i=2;i<=n;i++)lahubina[i][m]=lahubina[i-1][m]+ara[1][m];
    for(ll i=2;i<=n;i++)for(ll j=m-1;j>0;j--)lahubina[i][j]=max(lahubina[i-1][j],lahubina[i][j+1])+ara[i][j];
    //answer
    ll ans=minf;
    for(ll i=1;i<=n;i++)for(ll j=1;j<=m;j++)ans=max(ans,dp[i][j]+dp2[i][j]+lahub[i][j]+lahubina[i][j]-4*ara[i][j]);
    cout<<ans<<endl;

}


//BISMILLAH
//RABBI JIDNI ILMA
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ull  unsigned long long
#define vll  vector <long long>
#define pll pair <long long,long long>
#define f first
#define s second
#define up upper_bound
#define lp lower_bound
#define pq priority_queue
#define inf 1e10
#define minf -1e10
#define pi 3.14159265
#define mod 1000000000
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);
#define rep(p,q) for(ll i=p;i<q;i++)
#define per(p,q) for(ll i=p-1;i>=0;i--)
#define o 0
int main()
{
    fastio
    ll ara[1005][1005];
    ll n,m;
    cin>>n>>m;
    for(ll i=1;i<=n;i++)for(ll j=1;j<=m;j++)cin>>ara[i][j];
    ll lahub_starts[1001][1001]={0};
    for(ll i=1;i<=n;i++)
        for(ll j=1;j<=m;j++)
        lahub_starts[i][j]=max(lahub_starts[i-1][j],lahub_starts[i][j-1])+ara[i][j];
    ll lahub_end[1001][1001]={0};
    for(ll i=n;i>0;i--)
        for(ll j=m;j>0;j--)
        lahub_end[i][j]=max(lahub_end[i][j+1],lahub_end[i+1][j])+ara[i][j];
    ll lahubina_starts[1001][1001]={0};
    for(ll i=n;i>0;i--)
        for(ll j=1;j<=m;j++)
        lahubina_starts[i][j]=max(lahubina_starts[i+1][j],lahubina_starts[i][j-1])+ara[i][j];
    ll lahubina_end[1001][1001]={0};
    for(ll i=1;i<=n;i++)
        for(ll j=m;j>0;j--)
        lahubina_end[i][j]=max(lahubina_end[i][j+1],lahubina_end[i-1][j])+ara[i][j];
    ll ans=minf;
    for(ll i=2;i<n;i++)
        for(ll j=2;j<m;j++)
    {
        ll p=lahub_starts[i-1][j]+lahubina_starts[i][j-1]+lahub_end[i+1][j]+lahubina_end[i][j+1];
        ll q=lahubina_starts[i+1][j]+lahubina_end[i-1][j]+lahub_starts[i][j-1]+lahub_end[i][j+1];
        ans=max({ans,p,q});
    }
   cout<<ans<<endl;

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 5002;

ll a[N][N], dp1[N][N], dp2[N][N], dp3[N][N], dp4[N][N];
int n,m;
int main()
{
    fastIO;
    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++){
        for (int j=1; j<=m; j++){
            dp1[i][j] = a[i][j] + max (dp1[i-1][j] , dp1[i][j-1]);
        }
    }

    for (int i=n ; i>=1;  i--){
        for (int j=m; j>=1 ; j--){
            dp2[i][j] = a[i][j] + max (dp2[i+1][j] , dp2[i][j+1]);
        }
    }
    for (int i=n ; i>=1 ; i--){
        for (int j=1; j<=m; j++){
            dp3[i][j] = a[i][j] + max (dp3[i+1][j] , dp3[i][j-1]);
        }
    }

    for (int i=1 ; i<=n; i++){
        for (int j=m; j>=1 ; j--){
            dp4[i][j] = a[i][j] + max (dp4[i-1][j] , dp4[i][j+1]);
        }
    }

    ll ans= 0;
    for (int i=1; i<=n; i++){
        for (int j=1; j<=m; j++){

            ans= max (ans, dp1[i][j] - 4*a[i][j] + dp2[i][j] + dp3[i][j] + dp4[i][j]);
        }
    }

    cout<<ans<<endl;

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 5002;

int a[N][N], dp1[N][N], dp2[N][N], dp3[N][N], dp4[N][N];
int n,m;
int main()
{
    fastIO;
    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++){
        for (int j=1; j<=m; j++){
            dp1[i][j] = a[i][j] + max (dp1[i-1][j] , dp1[i][j-1]);
        }
    }

    for (int i=n ; i>=1;  i--){
        for (int j=m; j>=1 ; j--){
            dp2[i][j] = a[i][j] + max (dp2[i+1][j] , dp2[i][j+1]);
        }
    }
    for (int i=n ; i>=1 ; i--){
        for (int j=1; j<=m; j++){
            dp3[i][j] = a[i][j] + max (dp3[i+1][j] , dp3[i][j-1]);
        }
    }

    for (int i=1 ; i<=n; i++){
        for (int j=m; j>=1 ; j--){
            dp4[i][j] = a[i][j] + max (dp4[i-1][j] , dp4[i][j+1]);
        }
    }

    int ans= 0;
    for (int i=1; i<=n; i++){
        for (int j=1; j<=m; j++){

            ans= max (ans, dp1[i][j] - 4*a[i][j] + dp2[i][j] + dp3[i][j] + dp4[i][j]);
        }
    }

    cout<<ans<<endl;

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 5e5;

ll s[N],p[N],c[N];

vector <int> v[N];
map <int, int >mapp;

int main()
{
    fastIO;
    int n,m,k;
    cin>>n>>m>>k;

    for (int i=1; i<=n; i++)
        cin>>p[i];

    for (int i=1; i<=n; i++){
        cin>>s[i];
        v[s[i]].pb(p[i]);
    }

    for (int i=0; i<N; i++){
        sort(v[i].begin(), v[i].end(), greater<int>() );
    }

    int cnt=0;

    for (int i=1; i<=k; i++){
        cin>>c[i];
        if (v[s[c[i]]][0] != p[c[i]])
            cnt++;
    }

    cout<<cnt;

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1002;

int a[N][N], dp1[N][N], dp2[N][N], dp3[N][N], dp4[N][N];
int n,m;

int main()
{
    fastIO;
    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++){
        for (int j=1; j<=m; j++){
            dp1[i][j] = a[i][j] + max (dp1[i-1][j] , dp1[i][j-1]);
        }
    }

    for (int i=n ; i>=1 ; i--){
        for (int j=1; j<=m; j++){
            dp3[i][j] = a[i][j] + max (dp3[i+1][j] , dp3[i][j-1]);
        }
    }

    for (int i=1 ; i<=n; i++){
        for (int j=m; j>=1 ; j--){
            dp4[i][j] = a[i][j] + max (dp4[i-1][j] , dp4[i][j+1]);
        }
    }

    for (int i=n ; i>=1;  i--){
        for (int j=m; j>=1 ; j--){
            dp2[i][j] = a[i][j] + max (dp2[i+1][j] , dp2[i][j+1]);
        }
    }

    int ans= 0;
    for (int i=1; i<=n; i++){
        for (int j=1; j<=m; j++){

            ans=max (ans,dp1[i-1][j]+dp2[i+1][j]+dp3[i][j-1]+dp4[i][j+1]);
            ans=max (ans,dp1[i][j-1]+dp2[i][j+1]+dp3[i+1][j]+dp4[i-1][j]);
        }
    }
    cout<<ans<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1002;

int a[N][N], dp1[N][N], dp2[N][N], dp3[N][N], dp4[N][N];
int n,m;
int main()
{
    fastIO;
    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++){
        for (int j=1; j<=m; j++){
            dp1[i][j] = a[i][j] + max (dp1[i-1][j] , dp1[i][j-1]);
        }
    }

    for (int i=n ; i>=1;  i--){
        for (int j=m; j>=1 ; j--){
            dp2[i][j] = a[i][j] + max (dp2[i+1][j] , dp2[i][j+1]);
        }
    }
    for (int i=n ; i>=1 ; i--){
        for (int j=1; j<=m; j++){
            dp3[i][j] = a[i][j] + max (dp3[i+1][j] , dp3[i][j-1]);
        }
    }

    for (int i=1 ; i<=n; i++){
        for (int j=m; j>=1 ; j--){
            dp4[i][j] = a[i][j] + max (dp4[i-1][j] , dp4[i][j+1]);
        }
    }

    int ans= 0;
    for (int i=1; i<=n; i++){
        for (int j=1; j<=m; j++){

            ans= max (ans, dp1[i][j] - 4*a[i][j] + dp2[i][j] + dp3[i][j] + dp4[i][j]);
        }
    }

    cout<<ans<<endl;

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1002;

int a[N][N], dp1[N][N], dp2[N][N], dp3[N][N], dp4[N][N];
int n,m;

int main()
{
    fastIO;
    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++){
        for (int j=1; j<=m; j++){
            dp1[i][j] = a[i][j] + max (dp1[i-1][j] , dp1[i][j-1]);
        }
    }

    for (int i=n ; i>=1;  i--){
        for (int j=m; j>=1 ; j--){
            dp2[i][j] = a[i][j] + max (dp2[i+1][j] , dp2[i][j+1]);
        }
    }

    for (int i=n ; i>=1 ; i--){
        for (int j=1; j<=m; j++){
            dp3[i][j] = a[i][j] + max (dp3[i+1][j] , dp3[i][j-1]);
        }
    }

    for (int j=m; j>=1 ; j--){
        for (int i=1 ; i<=n; i++){
            dp4[i][j] = a[i][j] + max (dp4[i-1][j] , dp4[i][j+1]);
        }
    }

    int ans= 0;
    for (int i=2; i<n; i++){
        for (int j=2; j<m; j++){

            ans=max (ans,dp1[i-1][j]+dp2[i+1][j]+dp3[i][j-1]+dp4[i][j+1]);
            ans=max (ans,dp1[i][j-1]+dp2[i][j+1]+dp3[i+1][j]+dp4[i-1][j]);
        }
    }
    cout<<ans<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 1002;

int a[N][N], dp1[N][N], dp2[N][N], dp3[N][N], dp4[N][N];
int n,m;

int main()
{
    fastIO;
    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++){
        for (int j=1; j<=m; j++){
            dp1[i][j] = a[i][j] + max (dp1[i-1][j] , dp1[i][j-1]);
        }
    }

    for (int i=n ; i>=1 ; i--){
        for (int j=1; j<=m; j++){
            dp3[i][j] = a[i][j] + max (dp3[i+1][j] , dp3[i][j-1]);
        }
    }

    for (int i=1 ; i<=n; i++){
        for (int j=m; j>=1 ; j--){
            dp4[i][j] = a[i][j] + max (dp4[i-1][j] , dp4[i][j+1]);
        }
    }

    for (int i=n ; i>=1;  i--){
        for (int j=m; j>=1 ; j--){
            dp2[i][j] = a[i][j] + max (dp2[i+1][j] , dp2[i][j+1]);
        }
    }

    int ans= 0;
    for (int i=1; i<=n; i++){
        for (int j=1; j<=m; j++){

            ans= max (ans, dp1[i][j] - 4*a[i][j] + dp2[i][j] + dp3[i][j] + dp4[i][j]);
        }
    }

    cout<<ans<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<int , int> pp;
#define X first
#define Y second
typedef long long int ll;
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 5002;

int a[N][N], dp1[N][N], dp2[N][N], dp3[N][N], dp4[N][N];
int n,m;
int main()
{
    fastIO;
    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++){
        for (int j=1; j<=m; j++){
            dp1[i][j] = a[i][j] + max (dp1[i-1][j] , dp1[i][j-1]);
        }
    }

    for (int i=n ; i>=1;  i--){
        for (int j=m; j>=1 ; j--){
            dp2[i][j] = a[i][j] + max (dp2[i+1][j] , dp2[i][j+1]);
        }
    }
    for (int i=n ; i>=1 ; i--){
        for (int j=1; j<=m; j++){
            dp3[i][j] = a[i][j] + max (dp3[i+1][j] , dp3[i][j-1]);
        }
    }

    for (int i=1 ; i<=n; i++){
        for (int j=m; j>=1 ; j--){
            dp4[i][j] = a[i][j] + max (dp4[i-1][j] , dp4[i][j+1]);
        }
    }

    ll ans= 0;
    for (int i=1; i<=n; i++){
        for (int j=1; j<=m; j++){

            ans= max (ans, dp1[i][j] - 4*a[i][j] + dp2[i][j] + dp3[i][j] + dp4[i][j]);
        }
    }

    cout<<ans<<endl;

}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'
#define pf printf
#define sf scanf
#define left __left
#define right __right

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000009
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);//cin.tie(0);//cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

inline ll power(ll bs,ll k)
{
    ll x = 1LL,y = bs;
    if(k == 0) return 1LL;

    while(k > 0){
        if(k % 2) x *= y;

        y *= y;
        k /= 2;
    }

    return x;
}

//debug
//#ifdef nxt
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template<class T>
string to_str(T t){ stringstream ss; ss << t; return ss.str();}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
/*#else
#define dbg(args...)
#endif //nxt*/

#define MX 2100
ll a[MX][MX],n,m;

int main()
{
    //freopen("input.txt","r",stdin);
    optimize();
    fraction();
    cin >> n >> m;
    ll i,j;
    for(i = 1; i <= n; ++i){
        for(j = 1 ;j <= m; ++j) cin >> a[i][j];
    }

    ll ans = 0LL;

    ll dp1[MX][MX],dp2[MX][MX],dp3[MX][MX],dp4[MX][MX];
    mem(dp1,0LL);
    mem(dp2,0LL);
    mem(dp3,0LL);
    mem(dp4,0LL);
    for(i = 1; i <= n;++i){
        for(j = 1; j <= m; ++j) dp1[i][j] = a[i][j] + max(dp1[i - 1][j],dp1[i][j - 1]);
    }

    for(i = n; i >= 1; --i){
        for(j = 1; j <= m ; ++j) dp2[i][j] = a[i][j] + max(dp2[i + 1][j],dp2[i][j - 1]);
    }
    for(i = 1; i <= n; ++i){
        for(j = m; j >= 1; --j) dp3[i][j] = a[i][j] + max(dp3[i - 1][j] , dp3[i][j + 1]);
    }
    for(i = n; i >= 1; --i){
        for(j = m; j >= 1; --j) dp4[i][j] = a[i][j] + max(dp4[i + 1][j],dp4[i][j + 1]);
    }

    for(i = 2; i < n; ++i){
        for(j = 2; j < m; ++j){

            ll x = dp1[i - 1][j]; //  coming from up lahub the upper sum
            ll y = dp2[i][j - 1]; // coming from left lahubina the left sum
            ll x2 =  dp4[i + 1][j];// going down lahub the right downward sum
            ll y2 = dp3[i][j + 1]; // going up lbina, right upward sum
            ll val1 = x + y + x2 + y2 ;
            //dbg(x,y,x2,y2,val1);
            x = dp1[i][j - 1];
            y = dp2[i + 1][j];
            x2 = dp4[i][j + 1];
            y2 = dp3[i - 1][j];
            ll val2 = x + y + x2 + y2;
            //dbg(x,y,x2,y2,val2);
            ans = max(ans,max(val1,val2));
            //dbg(i,j,val1,val2,ans);
            //dbg(i,j,dp1[i][j],dp2[i][j],dp3[i][j],dp4[i][j]);
            //dbg(val1,val2,ans);
        }
        //cout << endl;
    }
    cout << ans;
    return 0;
}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'
#define pf printf
#define sf scanf
#define left __left
#define right __right

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000009
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);//cin.tie(0);//cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

inline ll power(ll bs,ll k)
{
    ll x = 1LL,y = bs;
    if(k == 0) return 1LL;

    while(k > 0){
        if(k % 2) x *= y;

        y *= y;
        k /= 2;
    }

    return x;
}

//debug
//#ifdef nxt
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template<class T>
string to_str(T t){ stringstream ss; ss << t; return ss.str();}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
/*#else
#define dbg(args...)
#endif //nxt*/

#define MX 2100
ll sum[MX][MX],a[MX][MX],n,m,sm[MX];

int main()
{
    optimize();
    fraction();
    cin >> n >> m;
    ll i,j;
    for(i = 0; i < n; ++i){
        for(j = 0 ;j < m; ++j) cin >> a[i][j];
    }
    mem(sum,0LL);
    for(i = 0; i < n; ++i){
        mem(sm,0LL);
        for(j = 0; j < m; ++j){
            sm[j] = sm[max(j - 1,0LL)] + a[i][j];
            sum[i][j] = sm[j] + sum[max(i - 1LL,0LL)][max(j,0LL)];
        }
    }
    ll ans = 0LL;
    for(i = 1; i < n; ++i){
        for(j = 1; j < m; ++j){
            ll x = sum[i - 1][j]; //  coming from up lahub the upper sum
            ll y = sum[n - 1][j - 1] - sum[i - 1][j - 1]; // coming from left lahubina the left sum
            ll x2 = sum[n - 1][m - 1] - sum[i][m - 1] - sum[n - 1][j - 1] + sum[i][j - 1]; // going down lahub the right downward sum
            ll y2 = sum[i][m - 1] - sum[i][j]; // going up lbina, right upward sum
            ll val1 = x + y + x2 + y2 ;
            //dbg(x,y,x2,y2,val1);
            x = sum[i][j - 1];
            y = sum[n - 1][j] - sum[i][j];
            x2 = sum[n - 1][m - 1] - sum[n - 1][j] - sum[i - 1][m - 1] + sum[i - 1][j];
            y2 = sum[i - 1][m - 1] - sum[i - 1][j - 1];
            ll val2 = x + y + x2 + y2;
            //dbg(x,y,x2,y2,val2);
            ans = max(ans,max(val1,val2));
            //dbg(i,j,val1,val2,ans);
        }
        //cout << endl;
    }
    cout << ans;
    return 0;
}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'
#define pf printf
#define sf scanf
#define left __left
#define right __right

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000009
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);//cin.tie(0);//cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

inline ll power(ll bs,ll k)
{
    ll x = 1LL,y = bs;
    if(k == 0) return 1LL;

    while(k > 0){
        if(k % 2) x *= y;

        y *= y;
        k /= 2;
    }

    return x;
}

//debug
//#ifdef nxt
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template<class T>
string to_str(T t){ stringstream ss; ss << t; return ss.str();}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
/*#else
#define dbg(args...)
#endif //nxt*/

#define MX 100
ll a[MX][MX],n,m;

int main()
{
    //freopen("input.txt","r",stdin);
    optimize();
    fraction();
    cin >> n >> m;
    ll i,j;
    for(i = 1; i <= n; ++i){
        for(j = 1 ;j <= m; ++j) cin >> a[i][j];
    }

    ll ans = 0LL;

    ll dp1[MX][MX],dp2[MX][MX],dp3[MX][MX],dp4[MX][MX];
    mem(dp1,0LL);
    mem(dp2,0LL);
    mem(dp3,0LL);
    mem(dp4,0LL);
    for(i = 1; i <= n;++i){
        for(j = 1; j <= m; ++j) dp1[i][j] = a[i][j] + max(dp1[i - 1][j],dp1[i][j - 1]);
    }

    for(i = n; i >= 1; --i){
        for(j = 1; j <= m ; ++j) dp2[i][j] = a[i][j] + max(dp2[i + 1][j],dp2[i][j - 1]);
    }
    for(i = 1; i <= n; ++i){
        for(j = m; j >= 1; --j) dp3[i][j] = a[i][j] + max(dp3[i - 1][j] , dp3[i][j + 1]);
    }
    for(i = n; i >= 1; --i){
        for(j = m; j >= 1; --j) dp4[i][j] = a[i][j] + max(dp4[i + 1][j],dp4[i][j + 1]);
    }

    for(i = 2; i < n; ++i){
        for(j = 2; j < m; ++j){

            ll x = dp1[i - 1][j]; //  coming from up lahub the upper sum
            ll y = dp2[i][j - 1]; // coming from left lahubina the left sum
            ll x2 =  dp4[i + 1][j];// going down lahub the right downward sum
            ll y2 = dp3[i][j + 1]; // going up lbina, right upward sum
            ll val1 = x + y + x2 + y2 ;
            //dbg(x,y,x2,y2,val1);
            x = dp1[i][j - 1];
            y = dp2[i + 1][j];
            x2 = dp4[i][j + 1];
            y2 = dp3[i - 1][j];
            ll val2 = x + y + x2 + y2;
            //dbg(x,y,x2,y2,val2);
            ans = max(ans,max(val1,val2));
            //dbg(i,j,val1,val2,ans);
            //dbg(i,j,dp1[i][j],dp2[i][j],dp3[i][j],dp4[i][j]);
            //dbg(val1,val2,ans);
        }
        //cout << endl;
    }
    cout << ans;
    return 0;
}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'
#define pf printf
#define sf scanf
#define left __left
#define right __right

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000009
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);//cin.tie(0);//cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

inline ll power(ll bs,ll k)
{
    ll x = 1LL,y = bs;
    if(k == 0) return 1LL;

    while(k > 0){
        if(k % 2) x *= y;

        y *= y;
        k /= 2;
    }

    return x;
}

//debug
//#ifdef nxt
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template<class T>
string to_str(T t){ stringstream ss; ss << t; return ss.str();}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
/*#else
#define dbg(args...)
#endif //nxt*/

#define MX 2100
ll a[MX][MX],n,m;

int main()
{
    //freopen("input.txt","r",stdin);
    optimize();
    fraction();
    cin >> n >> m;
    ll i,j;
    for(i = 1; i <= n; ++i){
        for(j = 1 ;j <= m; ++j) cin >> a[i][j];
    }

    ll ans = 0LL;

    ll dp1[MX][MX],dp2[MX][MX],dp3[MX][MX],dp4[MX][MX];
    mem(dp1,0LL);
    mem(dp2,0LL);
    mem(dp3,0LL);
    mem(dp4,0LL);
    for(i = 1; i <= n;++i){
        for(j = 1; j <= m; ++j) dp1[i][j] = a[i][j] + max(dp1[i - 1][j],dp1[i][j - 1]);
    }

    for(i = n; i >= 1; --i){
        for(j = 1; j <= m ; ++j) dp2[i][j] = a[i][j] + max(dp2[i + 1][j],dp2[i][j - 1]);
    }
    for(i = 1; i <= n; ++i){
        for(j = m; j >= 1; --j) dp3[i][j] = a[i][j] + max(dp3[i - 1][j] , dp3[i][j + 1]);
    }
    for(i = n; i >= 1; --i){
        for(j = m; j >= 1; --j) dp4[i][j] = a[i][j] + max(dp4[i + 1][j],dp4[i][j + 1]);
    }

    for(i = 1; i < n; ++i){
        for(j = 1; j < m; ++j){

            ll x = dp1[i - 1][j]; //  coming from up lahub the upper sum
            ll y = dp2[i][j - 1]; // coming from left lahubina the left sum
            ll x2 =  dp4[i + 1][j];// going down lahub the right downward sum
            ll y2 = dp3[i][j + 1]; // going up lbina, right upward sum
            ll val1 = x + y + x2 + y2 ;
            //dbg(x,y,x2,y2,val1);
            x = dp1[i][j - 1];
            y = dp2[i + 1][j];
            x2 = dp4[i][j + 1];
            y2 = dp3[i - 1][j];
            ll val2 = x + y + x2 + y2;
            //dbg(x,y,x2,y2,val2);
            ans = max(ans,max(val1,val2));
            //dbg(i,j,val1,val2,ans);
            //dbg(i,j,dp1[i][j],dp2[i][j],dp3[i][j],dp4[i][j]);
            //dbg(val1,val2,ans);
        }
        //cout << endl;
    }
    cout << ans;
    return 0;
}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'
#define pf printf
#define sf scanf
#define left __left
#define right __right

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000009
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);//cin.tie(0);//cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

inline ll power(ll bs,ll k)
{
    ll x = 1LL,y = bs;
    if(k == 0) return 1LL;

    while(k > 0){
        if(k % 2) x *= y;

        y *= y;
        k /= 2;
    }

    return x;
}

//debug
//#ifdef nxt
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template<class T>
string to_str(T t){ stringstream ss; ss << t; return ss.str();}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
/*#else
#define dbg(args...)
#endif //nxt*/

#define MX 1100
ll a[MX][MX],n,m;

int main()
{
    optimize();
    fraction();
    cin >> n >> m;
    ll i,j;
    for(i = 1; i <= n; ++i){
        for(j = 1 ;j <= m; ++j) cin >> a[i][j];
    }

    ll ans = 0LL;

    ll dp1[MX][MX],dp2[MX][MX],dp3[MX][MX],dp4[MX][MX];
    mem(dp1,0LL);
    mem(dp2,0LL);
    mem(dp3,0LL);
    mem(dp4,0LL);
    for(i = 1; i <= n;++i){
        for(j = 1; j <= m; ++j) dp1[i][j] = a[i][j] + max(dp1[i - 1][j],dp1[i][j - 1]);
    }

    for(i = n; i >= 1; --i){
        for(j = 1; j <= m ; ++j) dp2[i][j] = a[i][j] + max(dp2[i + 1][j],dp2[i][j - 1]);
    }
    for(i = 1; i <= n; ++i){
        for(j = m; j >= 1; --j) dp3[i][j] = a[i][j] + max(dp3[i - 1][j] , dp3[i][j + 1]);
    }
    for(i = n; i >= 1; --i){
        for(j = m; j >= 1; --j) dp4[i][j] = a[i][j] + max(dp4[i + 1][j],dp4[i][j + 1]);
    }

    for(i = 1; i < n; ++i){
        for(j = 1; j < m; ++j){

            ll x = dp1[i - 1][j]; //  coming from up lahub the upper sum
            ll y = dp2[i][j - 1]; // coming from left lahubina the left sum
            ll x2 =  dp4[i + 1][j];// going down lahub the right downward sum
            ll y2 = dp3[i][j + 1]; // going up lbina, right upward sum
            ll val1 = x + y + x2 + y2 ;
            //dbg(x,y,x2,y2,val1);
            x = dp1[i][j - 1];
            y = dp2[i + 1][j];
            x2 = dp4[i][j + 1];
            y2 = dp3[i][j + 1];
            ll val2 = x + y + x2 + y2;
            //dbg(x,y,x2,y2,val2);
            ans = max(ans,max(val1,val2));
            //dbg(i,j,val1,val2,ans);

        }
        //cout << endl;
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct fdp
{
    int bl ;
    int br ;
    int tl ;
    int tr ;
};
int n,m,maxi;
fdp f[1000][1000]; int a[1000][1000] ;
void dptl ()
{
    for (int i=1;i <= n; i++)
        for (int j=1 ;j<= m ; j++)
    {
        f[i][j].tl = max ( f[i-1][j].tl,f[i][j-1].tl) +a[i][j];

    }
}
void dptr ()
{
    for (int i=1;i <= n; i++)
        for (int j=m ;j>= 1 ;j--)
    {
        f[i][j].tr = max ( f[i-1][j].tr,f[i][j+1].tr) +a[i][j] ;

    }
}
void dpbl ()
{
    for (int i=n;i >=1; i--)
        for (int j=1 ;j<= m ; j++)
    {
        f[i][j].bl = max ( f[i+1][j].bl, f[i][j-1].bl) +a[i][j] ;
    }
}
void dpbr ()
{
    for (int i=n;i >= 1; i--)
        for (int j=m ;j>= 1 ; j--)
    {
        f[i][j].br = max ( f[i+1][j].br , f[i][j+1].br) +a[i][j] ;
    }
}
bool ck (int i , int j)
{
    if (i < 1) return false ;
    if (i > n ) return false ;
    if (j < 1) return false ;
    if (j > n ) return false ;
    return true ;
}
int main()
{
    maxi = -1 ;
  //  freopen ("429B.inp","r",stdin);
   // freopen ("429B.out","w",stdout);
    cin >> n>>m ;
    for (int i=1;i<= n ; i++)
        for (int j=1 ; j <= n ; j++)
    {
        cin>>a[i][j];
    }
    dpbl() ;
    dpbr() ;
    dptl () ;
    dptr () ;
    for (int i=1; i <= n ; i++)
        for (int j = 1 ; j <= m ; j++)
    {
        if (ck(i-1,j))
        {
            if (ck(i+1,j))
            {
                if(ck (i,j-1))
                {
                    if (ck(i,j+1))
                    {
                        maxi = max (maxi, f[i-1][j].tl+f[i+1][j].br+f[i][j-1].bl+f[i][j+1].tr);
                        maxi = max (maxi, f[i-1][j].tr+f[i+1][j].bl+f[i][j-1].tl+f[i][j+1].br);
                    }
                }
            }
        }
    }
    cout << maxi <<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct fdp
{
    long long bl ;
    long long br ;
    long long tl ;
    long long tr ;
};
int n,m;
fdp f[1000][1000]; int a[1000][1000] ;
long long maxi =-1;
void dptl ()
{
    for (int i=1;i <= n; i++)
        for (int j=1 ;j<= m ; j++)
    {
        f[i][j].tl = max ( f[i-1][j].tl,f[i][j-1].tl) +a[i][j];

    }
}
void dptr ()
{
    for (int i=1;i<=n; i++)
        for (int j=m ;j>= 1 ;j--)
    {
        f[i][j].tr = max (f[i-1][j].tr,f[i][j+1].tr) +a[i][j] ;
    }
}
void dpbl ()
{
    for (int i=n;i >=1; i--)
        for (int j=1 ;j<= m ; j++)
    {
        f[i][j].bl = max ( f[i+1][j].bl, f[i][j-1].bl) +a[i][j] ;
    }
}
void dpbr ()
{
    for (int i=n;i >= 1; i--)
        for (int j=m ;j>= 1 ; j--)
    {
        f[i][j].br = max ( f[i+1][j].br , f[i][j+1].br) +a[i][j] ;
    }
}
bool ck (int i , int j)
{
    if (i < 1) return false ;
    if (i > n ) return false ;
    if (j < 1) return false ;
    if (j > n ) return false ;
    return true ;
}
int main()
{
    maxi = -1 ;
 //   freopen ("429B.inp","r",stdin);
 //   freopen ("429B.out","w",stdout);
    cin >>n>>m ;
    for (int i=1;i<= n ; i++)
        for (int j=1 ; j <= m; j++)
    {
        cin>>a[i][j];
    }
    dpbl() ;
    dpbr() ;
    dptl () ;
    dptr () ;
    for (int i=2; i < n ; i++)
        for (int j = 2 ; j < m ; j++)
    {
                        maxi = max (maxi, f[i-1][j].tl+f[i+1][j].br+f[i][j-1].bl+f[i][j+1].tr);
                        maxi = max (maxi, f[i-1][j].tr+f[i+1][j].bl+f[i][j-1].tl+f[i][j+1].br);
    }
    /*
for (int i=1; i <= n ; i++)
{
        for (int j = 1 ; j <= m ; j++)
    {
        cout << f[i][j].bl<< " ";
    }
    cout <<endl ;
}
cout <<"*******************************"<<endl ;
for (int i=1; i <= n ; i++)
{
        for (int j = 1 ; j <= m ; j++)
    {
        cout << f[i][j].br<< " ";
    }
    cout <<endl ;
}
cout <<"*******************************"<<endl ;
for (int i=1; i <= n ; i++)
{
        for (int j = 1 ; j <= m ; j++)
    {
        cout << f[i][j].tl<< " ";
    }
    cout <<endl ;
}
cout <<"*******************************"<<endl ;
for (int i=1; i <= n ; i++)
{
        for (int j = 1 ; j <= m ; j++)
    {
        cout << f[i][j].tr<< " ";
    }
    cout <<endl ;
}
cout <<"*******************************"<<endl ;
*/
        cout << maxi <<endl;
    return 0;
}