//R<3S
#include <bits/stdc++.h>
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(x) for(auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define int long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi >
#define vii vector<pii >
#define mii map<int,int>
#define mll map<ll,ll>

using namespace std;

void solve()
  {
	int n;
	cin>>n;
	string a,b;
	cin>>a>>b;
	vi v;
	int curr=0,x=0;
	repD(i,n-1,0)
	{
		x++;
		if(a[i]==b[i])continue;
		curr++;

		if(b[i]==a[0]){v.pb(1);if(a[0]=='1')a[0]='0';else a[0]='1';}
		// else cout<<1-(b[i]-'0')<<endl;
		if(curr&1)
		{
			int val=a[n-x];
			val=1-val;
			a[0]=char(val+'0');
		}
		else a[0]=a[x/2];

		v.pb(i+1);

	}
	cout<<sz(v)<<" ";

	trav(v)cout<<i<<" ";
	cout<<endl;

  }

signed main()
{
   std::ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   int t;
   t=1;
   cin>>t;
   while(t--)
    {
        solve();
    }

   return 0;
}


//R<3S
#include <bits/stdc++.h>
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(x) for(auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define int long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi >
#define vii vector<pii >
#define mii map<int,int>
#define mll map<ll,ll>

using namespace std;

void solve()
  {
	int n;
	cin>>n;
	string a,b;
	cin>>a>>b;
	vi v;
	int curr=0,x=0,minm=0,maxm=n-1;
	repD(i,n-1,0)
	{
		x++;
		char cmp=a[i];
		if(curr!=0&&curr%2==1)
		{

			if(a[minm]=='0')cmp='1';
			else cmp='0';
		}
		else if(curr!=0)
		{

			cmp=a[maxm];
		}

		if(cmp==b[i]){if(curr&1)minm++;else maxm--;continue;}
		curr++;

		// cout<<minm<<" "<<maxm<<" ";
		if(b[i]==a[0])
		{
			// cout<<b[i]<<"."<<endl;
			v.pb(1);
		}
		// else cout<<1-(b[i]-'0')<<endl;

		v.pb(i+1);

		if(curr%2==1)
		{
			int tempv=n-(maxm-minm);
			int tx=x+1-tempv;
			minm+=tx;

			if(a[minm]=='0')a[0]='1';
			else a[0]='0';
		}
		else
		{
			int tempv=n-(maxm-minm);
			int tx=x+1-tempv;
			maxm-=tx;
			a[0]=a[maxm];
		}

	}
	cout<<sz(v)<<" ";

	trav(v)cout<<i<<" ";
	cout<<endl;

  }

signed main()
{
   std::ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   int t;
   t=1;
   cin>>t;
   while(t--)
    {
        solve();
    }

   return 0;
}


//R<3S
#include <bits/stdc++.h>
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(x) for(auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define int long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi >
#define vii vector<pii >
#define mii map<int,int>
#define mll map<ll,ll>

using namespace std;

void solve()
  {
	int n;
	cin>>n;
	string a,b;
	cin>>a>>b;
	vi v;
	int curr=0,x=0,minm=0,maxm=n-1;
	repD(i,n-1,0)
	{
		x++;
		char cmp=a[i];
		if(curr!=0&&curr%2==1)
		{

			if(a[minm]=='0')cmp='1';
			else cmp='0';
		}
		else if(curr!=0)
		{

			cmp=a[maxm];
		}

		if(cmp==b[i]){if(curr&1)minm++;else maxm--;continue;}

		// cout<<minm<<" "<<maxm<<" ";
		cmp=a[0];
		if(curr!=0&&curr%2==1)
		{

			if(a[maxm]=='0')cmp='1';
			else cmp='0';
		}
		else if(curr!=0)
		{

			cmp=a[minm];
		}
		if(b[i]==cmp)
		{
			// cout<<b[i]<<"."<<endl;
			v.pb(1);

		}
		// else cout<<1-(b[i]-'0')<<endl;

		curr++;

		v.pb(i+1);

		if(curr%2==1)
		{
			int tempv=n-(maxm-minm);
			int tx=x+1-tempv;
			minm+=tx;

		}
		else
		{
			int tempv=n-(maxm-minm);
			int tx=x+1-tempv;
			maxm-=tx;

		}

	}
	// if(a[0]!=b[0])v.pb(1);
	cout<<sz(v)<<" ";

	trav(v)cout<<i<<" ";
	cout<<endl;

  }

signed main()
{
   std::ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   int t;
   t=1;
   cin>>t;
   while(t--)
    {
        solve();
    }

   return 0;
}


//R<3S
#include <bits/stdc++.h>
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(x) for(auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define int long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi >
#define vii vector<pii >
#define mii map<int,int>
#define mll map<ll,ll>

using namespace std;

void solve()
  {
	int n;
	cin>>n;
	string a,b;
	cin>>a>>b;
	vi v;
	int curr=0,x=0,minm=0,maxm=n-1;
	repD(i,n-1,0)
	{
		x++;
		char cmp=a[i];
		if(curr!=0&&curr%2==1)
		{

			if(a[minm]=='0')cmp='1';
			else cmp='0';
		}
		else if(curr!=0)
		{

			cmp=a[maxm];
		}

		if(cmp==b[i]){if(curr&1)minm++;else maxm--;continue;}
		curr++;

		// cout<<minm<<" "<<maxm<<" ";
		if(b[i]==a[0])
		{
			// cout<<b[i]<<"."<<endl;
			v.pb(1);
		}
		// else cout<<1-(b[i]-'0')<<endl;

		v.pb(i+1);

		if(curr%2==1)
		{
			int tempv=n-(maxm-minm);
			int tx=x+1-tempv;
			minm+=tx;

			if(a[minm]=='0')a[0]='1';
			else a[0]='0';
		}
		else
		{
			int tempv=n-(maxm-minm);
			int tx=x+1-tempv;
			maxm-=tx;
			a[0]=a[maxm];
		}

	}
	if(a[0]!=b[0])v.pb(1);
	cout<<sz(v)<<" ";

	trav(v)cout<<i<<" ";
	cout<<endl;

  }

signed main()
{
   std::ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   int t;
   t=1;
   cin>>t;
   while(t--)
    {
        solve();
    }

   return 0;
}


//R<3S
#include <bits/stdc++.h>
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(x) for(auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define int long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi >
#define vii vector<pii >
#define mii map<int,int>
#define mll map<ll,ll>

using namespace std;

void solve()
  {
	int n;
	cin>>n;
	string a,b;
	cin>>a>>b;
	vi v;
	int curr=0,x=0;
	repD(i,n-1,0)
	{
		x++;
		if(a[i]==b[i])continue;
		curr++;

		if(b[i]==a[0]){v.pb(1);if(a[0]=='1')a[0]='0';else a[0]='1';}
		// else cout<<1-(b[i]-'0')<<endl;
		if(curr&1)
		{
			int val=a[n-x];
			val=1-val;
			a[0]=char(val+'0');
		}
		else a[0]=a[x];

		v.pb(i+1);

	}
	cout<<sz(v)<<" ";

	trav(v)cout<<i<<" ";
	cout<<endl;

  }

signed main()
{
   std::ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   int t;
   t=1;
   cin>>t;
   while(t--)
    {
        solve();
    }

   return 0;
}


//R<3S
#include <bits/stdc++.h>
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(x) for(auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define int long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi >
#define vii vector<pii >
#define mii map<int,int>
#define mll map<ll,ll>

using namespace std;

void solve()
  {
	int n;
	cin>>n;
	string a,b;
	cin>>a>>b;
	vi v;
	int curr=0,x=0;
	repD(i,n-1,0)
	{
		x++;
		if(a[i]==b[i])continue;
		curr++;

		if(b[i]==a[0]){v.pb(1);if(a[0]=='1')a[0]='0';else a[0]='1';}
		// else cout<<1-(b[i]-'0')<<endl;
		if(curr&1)
		{
			int val=a[n-(curr/2+1)];
			val=1-val;
			a[0]=char(val+'0');
		}
		else a[0]=a[curr];

		v.pb(i+1);

	}
	cout<<sz(v)<<" ";

	trav(v)cout<<i<<" ";
	cout<<endl;

  }

signed main()
{
   std::ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   int t;
   t=1;
   cin>>t;
   while(t--)
    {
        solve();
    }

   return 0;
}


//R<3S
#include <bits/stdc++.h>
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(x) for(auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define int long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi >
#define vii vector<pii >
#define mii map<int,int>
#define mll map<ll,ll>

using namespace std;

void solve()
  {
	int n;
	cin>>n;
	string a,b;
	cin>>a>>b;
	vi v;
	int curr=0,x=0,minm=0,maxm=n-1;
	repD(i,n-1,0)
	{
		x++;
		if(a[i]==b[i])continue;
		curr++;

		// cout<<minm<<" "<<maxm<<" ";
		if(b[i]==a[0])
		{
			// cout<<b[i]<<endl;
			v.pb(1);
			}
		// else cout<<1-(b[i]-'0')<<endl;
		if(curr%2==1)
		{
			int tempv=n-(maxm-minm);
			int tx=x+1-tempv;
			minm+=tx;

			if(a[minm]=='0')a[0]='1';
			else a[0]='0';
		}
		else
		{
			int tempv=n-(maxm-minm);
			int tx=x+1-tempv;
			maxm-=tx;
			a[0]=a[maxm];
		}

		v.pb(i+1);

	}
	cout<<sz(v)<<" ";

	trav(v)cout<<i<<" ";
	cout<<endl;

  }

signed main()
{
   std::ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   int t;
   t=1;
   cin>>t;
   while(t--)
    {
        solve();
    }

   return 0;
}


//R<3S
#include <bits/stdc++.h>
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(x) for(auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define int long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi >
#define vii vector<pii >
#define mii map<int,int>
#define mll map<ll,ll>

using namespace std;

void solve()
  {
	int n;
	cin>>n;
	string a,b;
	cin>>a>>b;
	vi v;
	int curr=0,x=0,minm=0,maxm=n-1;
	repD(i,n-1,0)
	{
		x++;
		char cmp=a[i];
		if(curr!=0&&curr%2==1)
		{

			if(a[minm]=='0')cmp='1';
			else cmp='0';
		}
		else if(curr!=0)
		{

			cmp=a[maxm];
		}

		if(cmp==b[i])continue;
		curr++;

		if(curr&&curr%2==1)
		{
			int tempv=n-(maxm-minm);
			int tx=x+1-tempv;
			minm+=tx;

			if(a[minm]=='0')a[0]='1';
			else a[0]='0';
		}
		else if(curr)
		{
			int tempv=n-(maxm-minm);
			int tx=x+1-tempv;
			maxm-=tx;
			a[0]=a[maxm];
		}

		// cout<<minm<<" "<<maxm<<" ";
		if(b[i]==a[0])
		{
			// cout<<b[i]<<"."<<endl;
			v.pb(1);
		}
		// else cout<<1-(b[i]-'0')<<endl;

		v.pb(i+1);

	}
	cout<<sz(v)<<" ";

	trav(v)cout<<i<<" ";
	cout<<endl;

  }

signed main()
{
   std::ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   int t;
   t=1;
   cin>>t;
   while(t--)
    {
        solve();
    }

   return 0;
}


#include<bits/stdc++.h>
#define intn long long
#define ls(k) (k)<<1
#define rs(k) (k)<<1|1
#define _0for(i, a) for(int i = 0; i < (a); ++i)
#define _1for(i, a) for(int i = 1; i <=(a); ++i)
#define lowbit(x) ((x)&(-x))
#define debug(x) \
(void)(cerr << "L" << __LINE__\
			<< " : " << #x << " = " \
			<< (x) << endl )
using namespace std;
inline int read()
{
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
char a[1005],b[1005],c[1005];
main(void)
{
	int t=read();
	while(t--)
	{
		int n=read();
		scanf("%s",a+1);
		scanf("%s",b+1);
		for(int i=1;i<=n;i++)
		{
			c[i]=a[i];
		}
		int p=n;
		vector<int >q;
		while(p)
		{

			if(a[1]!=b[p])
			{
				q.push_back(p);
				for(int i=1;i<=p;i++)
				{
					if(c[p-i+1]=='1')
					a[i]='0';
					else a[i]='1';
				}
			}
			else
			{
				q.push_back(1);
				if(a[1]=='0')a[1]='1';
				else a[1]='0';
				c[1]=a[1];
				q.push_back(p);
				for(int i=1;i<=p;i++)
				{
					if(c[p-i+1]=='1')
					a[i]='0';
					else a[i]='1';
				}

			}
			p--;
			for(int i=1;i<=n;i++)
			{
				c[i]=a[i];
			}
		}
		int l=q.size();
		printf("%d",l);
		for(int i=0;i<l;i++)
		{
			printf(" %d",q[i]);
		}
		printf("\n");
	}
}


#include <bits/stdc++.h>
#define debug(x) \
(void)(cerr << "L" << __LINE__\
			<< " : " << #x << " = " \
			<< (x) << endl )
using namespace std;

void solve() {
    int n; cin >> n;
    string a, b; cin >> a >> b;
    vector<int> p1;
    vector<int> p2;
    for (int i = 1; i < n; ++i) {
        if (a[i] != a[i - 1]) {
            p1.push_back(i);
        }
    }
     for (int i = 1; i < n; ++i) {
        if (b[i] != b[i - 1]) {
            p2.push_back(i);
        }
    }

    if(p1.size()%2==p2.size()%2)
    {
    	if(a[0]!=b[0])
    	p1.push_back(n);
	}
	else
	{
		if(a[0]==b[0])
		p1.push_back(n);
	}
	cout<<p1.size()+p2.size()<<' ';
	for (int i : p1) cout << i << ' ';
	for(int i=p2.size()-1;i>=0;i--)
	{
		cout<<p2[i]<<' ';
	}
    cout << '\n';
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}


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

void solve() {
    int n; cin >> n;
    string a, b; cin >> a >> b;
    vector<int> p1;
    vector<int> p2;
    for (int i = 1; i < n; ++i) {
        if (a[i] != a[i - 1]) {
            p1.push_back(i);
        }
    }
     for (int i = 1; i < n; ++i) {
        if (b[i] != b[i - 1]) {
            p2.push_back(i);
        }
    }
    if(p1.size()%2==p2.size())
    {
    	if(a[0]!=b[0])
    	p1.push_back(n);
	}
	else
	{
		if(a[0]==b[0])
		p1.push_back(n);
	}
	cout<<p1.size()+p2.size()<<' ';
	for (int i : p1) cout << i << ' ';
	for(int i=p2.size()-1;i>=0;i--)
	{
		cout<<p2[i]<<' ';
	}
    cout << '\n';
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int tc;
    cin >> tc;
    while(tc--)
    {
        int n;
        string a,b;
        cin >> n;
        cin >> a >> b;
        vector <int> ans;
        for(int i = n-1 ; i >= 0 ; i--)
        {
            if(a[i] != b[i])
            {
                if(i == 0)
                {
                    ans.push_back(1);
                }
                else
                    ans.push_back(1);
                    ans.push_back(i+1);
            }
        }
        cout << ans.size()  << " ";
        for(int i = 0 ; i < ans.size() ; i++) cout << ans[i] << " ";
        cout << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int tc;
    cin >> tc;
    while(tc--)
    {
        int n;
        string a,b;
        cin >> n;
        cin >> a >> b;
        vector <int> ans;
        for(int i = n-1 ; i >= 0 ; i--)
        {
            if(a[i] != b[i])
            {
                if(i == 0)
                {
                    ans.push_back(1);
                }
                else if (i != 0){
                    ans.push_back(1);
                    ans.push_back(i+1);
            }
            }
        }
        cout << ans.size()  << " ";
        for(int i = 0 ; i < ans.size() ; i++) cout << ans[i] << " ";
        cout << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int tc;
    cin >> tc;
    while(tc--)
    {
        int n;
        string a,b;
        cin >> n;
        cin >> a >> b;
        vector <int> ans;
        for(int i = n-1 ; i >= 0 ; i--)
        {
            if(a[i] != b[i])
            {
                ans.push_back(1);
                ans.push_back(i+1);
            }
        }
        cout << ans.size()  << " ";
        for(int i = 0 ; i < ans.size() ; i++) cout << ans[i] << " ";
        cout << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int tc;
    cin >> tc;
    while(tc--)
    {
        int n;
        string a,b;
        cin >> n;
        cin >> a >> b;
        a += '0';
        b += '0';
        vector <int> ans1,ans2;
        for(int i = 1 ; i <= n ; i++)
        {
            if(a[i] != a[i-1])
            {
                ans1.push_back(i);
            }
            if(b[i] != b[i-1])
            {
                ans2.push_back(i);
            }

        }
        ans1.insert(ans1.end(),ans2.rbegin(),ans2.rend());
        cout << ans1.size()  << " ";
        for(int i = 0 ; i < ans1.size() ; i++) cout << ans1[i] << " ";
        cout << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int tc;
    cin >> tc;
    while(tc--)
    {
        int n;
        string a,b;
        cin >> n;
        cin >> a >> b;
        vector <int> ans1,ans2;
        for(int i = 0 ; i < n ; i++)
        {
            if(a[i] != a[i+1])
            {
                ans1.push_back(i+1);
            }
            if(b[i] != b[i+1])
            {
                ans2.push_back(i+1);
            }

        }
        ans1.insert(ans1.end(),ans2.rbegin(),ans2.rend());
        cout << ans1.size()  << " ";
        for(int i = 0 ; i < ans1.size() ; i++) cout << ans1[i] << " ";
        cout << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e3 + 10;
typedef long long ll;
string a, b;
int n, ans[maxn], cnt;
void oper(int pos){
	int i;
	ans[cnt++] = pos + 1;
	for(i=0;i<=pos;++i){
		a[i] = (a[i]=='0'?'1':'0');
	}
	reverse(a.begin(), a.begin() + pos + 1);
}
void solve(){
	int i, j;
	cnt = 0;
	for(i=n-1;i>=0;--i){
		if(a[i] == b[i]) continue;
		if(a[i] != a[0]){
			a[0] = a[i];
			ans[cnt++] = 1;
		}
		oper(i);
	}
	//cout<<"ans:";
	cout<<cnt;
	for(i=0;i<cnt;++i){
		cout<<' '<<ans[i];
	}
	cout<<'\n';
}
int main(){
	ll t, i, j, x;
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--){
		cin>>n>>a>>b;
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
typedef long long ll;
string a, b;
int n, ans[maxn], cnt;
void oper(int pos){
	int i;
	ans[cnt++] = pos + 1;
	for(i=0;i<=pos;++i){
		a[i] = (a[i]=='0'?'1':'0');
	}
	reverse(a.begin(), a.begin() + pos + 1);
}
void solve(){
	int i, j;
	cnt = 0;
	for(i=n-1;i>=0;--i){
		if(a[i] == b[i]) continue;
		if(a[i] != a[0]){
			a[0] = a[i];
			ans[cnt++] = 1;
		}
		oper(i);
	}
	//cout<<"ans:";
	cout<<cnt;
	for(i=0;i<cnt;++i){
		cout<<' '<<ans[i];
	}
	cout<<'\n';
}
int main(){
	ll t, i, j, x;
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--){
		cin>>n>>a>>b;
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 10;
typedef long long ll;
string a, b;
int n, ans[maxn], cnt;
void oper(int pos){
	int i;
	ans[cnt++] = pos + 1;
	for(i=0;i<=pos;++i){
		a[i] = (a[i]=='0'?'1':'0');
	}
	reverse(a.begin(), a.begin() + pos + 1);
}
void solve(){
	int i, j;
	cnt = 0;
	for(i=n-1;i>=0;--i){
		if(a[i] == b[i]) continue;
		if(a[i] != a[0]){
			a[0] = a[i];
			ans[cnt++] = 1;
		}
		oper(i);
	}
	//cout<<"ans:";
	cout<<cnt;
	for(i=0;i<cnt;++i){
		cout<<' '<<ans[i];
	}
	cout<<'\n';
}
int main(){
	ll t, i, j, x;
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--){
		cin>>n>>a>>b;
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
typedef long long ll;
string a, b;
int n, ans[maxn], cnt;
void oper(int pos){
	int i;
	ans[cnt++] = pos + 1;
	for(i=0;i<=pos;++i){
		a[i] = (a[i]=='0'?'1':'0');
	}
	reverse(a.begin(), a.begin() + pos + 1);
}
void solve(){
	int i, j;
	cnt = 0;
	for(i=0;i<n-1;++i){
		if(a[i] != a[i+1]){
			ans[cnt++] = i + 1;
		}
	}
	if(a[n-1] != b[n-1]) ans[cnt++] = n;
	for(i=n-1;i>=1;--i){
		if(b[i] != b[i-1]){
			ans[cnt++] = i;
		}
	}
	cout<<cnt;
	for(i=0;i<cnt;++i) cout<<' '<<ans[i];
	cout<<'\n';
}
int main(){
	ll t, i, j, x;
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--){
		cin>>n>>a>>b;
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define all(x) (x).begin(),(x).end()
#define le(x) ((int)(x).size())
#define pii pair<int,int>
#define PII pair<LL,LL>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define db printf("Here!\n");
using namespace std;
const double eps=1e-8;
const double Pi=4.0*atan(1.0);
const LL inf=1e9;
const int fix=3e5+5;
const int N=3e5+5;
const int M=1e7+5;
const int mod=998244353;
//mt19937 rnd(time(0)),shuffle(a+1,a+1+n,rnd);
int n,m,k,t,T,len,op,x,y,z;
int a[N];
string s1,s2;
void DO(){
    cin>>n>>s1>>s2;
    vector<int>ans;
    for(int i=n-1;i>=0;i--){
        if(s1[0]==s2[i]&&i!=0){
            ans.pb(1);
            ans.pb(i+1);
            s1[0]=(s2[i]=='1'?'0':'1');
        }else ans.pb(i+1);
        s1[0]=(s1[i]=='1'?'0':'1');
        char c='1';
        if(i-1>=0)c=s1[i-1];
        if(i-1>=0&&n>=2)s1[i-1]=(s1[1]=='1'?'0':'1');
        s1[1]=(c=='1'?'0':'1');
    }
    printf("%d ",le(ans));
    for(auto e:ans)printf("%d ",e);puts("");
}
int main(){
    int o;scanf("%d",&o);
    while(o--){
        DO();
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define all(x) (x).begin(),(x).end()
#define le(x) ((int)(x).size())
#define pii pair<int,int>
#define PII pair<LL,LL>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define db printf("Here!\n");
using namespace std;
const double eps=1e-8;
const double Pi=4.0*atan(1.0);
const LL inf=1e9;
const int fix=3e5+5;
const int N=3e5+5;
const int M=1e7+5;
const int mod=998244353;
//mt19937 rnd(time(0)),shuffle(a+1,a+1+n,rnd);
int n,m,k,t,T,len,op,x,y,z;
int a[N];
string s1,s2;
void DO(){
    cin>>n>>s1>>s2;
    vector<int>ans;
    for(int i=n-1;i>=0;i--){
        if(s1[0]==s2[i]){
            ans.pb(1);
            ans.pb(i+1);
            s1[0]=(s2[i]=='1'?'0':'1');
        }else ans.pb(i+1);
        s1[0]=(s1[i]=='1'?'0':'1');
        char c='1';
        if(i-1>=0)c=s1[i-1];
        if(i-1>=0&&n>=2)s1[i-1]=(s1[1]=='1'?'0':'1');
        s1[1]=(c=='1'?'0':'1');
    }
    printf("%d ",le(ans));
    for(auto e:ans)printf("%d ",e);puts("");
}
int main(){
    int o;scanf("%d",&o);
    while(o--){
        DO();
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define all(x) (x).begin(),(x).end()
#define le(x) ((int)(x).size())
#define pii pair<int,int>
#define PII pair<LL,LL>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define db printf("Here!\n");
using namespace std;
const double eps=1e-8;
const double Pi=4.0*atan(1.0);
const LL inf=1e9;
const int fix=3e5+5;
const int N=3e5+5;
const int M=1e7+5;
const int mod=998244353;
//mt19937 rnd(time(0)),shuffle(a+1,a+1+n,rnd);
int n,m,k,t,T,len,op,x,y,z;
int a[N];
string s1,s2;
void DO(){
    cin>>n>>s1>>s2;
    s1+='0';s2+='0';
    vector<int>ans;
    for(int i=1;i<=n;i++){
        if(s1[i]!=s1[i-1])ans.pb(i);
    }
    for(int i=n;i>=1;i--)if(s2[i]!=s2[i-1])ans.pb(i);
    printf("%d ",le(ans));
    for(auto e:ans)printf("%d ",e);puts("");
}
int main(){
    int o;scanf("%d",&o);
    while(o--){
        DO();
    }
    return 0;
}
/*
1
10
0110011011
1000110100
19 1 10 1 9 1 8 1 7 1 6   1   5 1 4 1 3 1 2 1
*/


#include<bits/stdc++.h>
#define LL long long
#define all(x) (x).begin(),(x).end()
#define le(x) ((int)(x).size())
#define pii pair<int,int>
#define PII pair<LL,LL>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define db printf("Here!\n");
using namespace std;
const double eps=1e-8;
const double Pi=4.0*atan(1.0);
const LL inf=1e9;
const int fix=3e5+5;
const int N=3e5+5;
const int M=1e7+5;
const int mod=998244353;
//mt19937 rnd(time(0)),shuffle(a+1,a+1+n,rnd);
int n,m,k,t,T,len,op,x,y,z;
int a[N];
string s1,s2;
void DO(){
    cin>>n>>s1>>s2;
    vector<int>ans;
    for(int i=n-1;i>=0;i--){
        if(s1[0]==s2[i]){
            if(!i)continue;
            ans.pb(1);
            ans.pb(i+1);
            s1[0]=(s2[i]=='1'?'0':'1');
        }else ans.pb(i+1);
        s1[0]=(s1[i]=='1'?'0':'1');
        char c='1';
        if(i-1>=0)c=(s1[i-1]=='1'?'0':'1');
        if(i-1>=0&&n>=2)s1[i-1]=(s1[1]=='1'?'0':'1');
        s1[1]=(c=='1'?'0':'1');
    }
    printf("%d ",le(ans));
    for(auto e:ans)printf("%d ",e);puts("");
}
int main(){
    int o;scanf("%d",&o);
    while(o--){
        DO();
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define all(x) (x).begin(),(x).end()
#define le(x) ((int)(x).size())
#define pii pair<int,int>
#define PII pair<LL,LL>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define db printf("Here!\n");
using namespace std;
const double eps=1e-8;
const double Pi=4.0*atan(1.0);
const LL inf=1e9;
const int fix=3e5+5;
const int N=3e5+5;
const int M=1e7+5;
const int mod=998244353;
//mt19937 rnd(time(0)),shuffle(a+1,a+1+n,rnd);
int n,m,k,t,T,len,op,x,y,z;
int a[N];
string s1,s2;
void DO(){
    cin>>n>>s1>>s2;
    vector<int>ans;
    for(int i=n-1;i>=0;i--){
        if(s1[0]==s2[i]){
            if(!i)continue;
            ans.pb(1);
            ans.pb(i+1);
            s1[0]=(s2[i]=='1'?'0':'1');
        }else ans.pb(i+1);
        s1[0]=(s1[i]=='1'?'0':'1');
        char c='1';
        if(i-1>=0)c=s1[i-1];
        if(i-1>=0&&n>=2)s1[i-1]=(s1[1]=='1'?'0':'1');
        s1[1]=(c=='1'?'0':'1');
    }
    printf("%d ",le(ans));
    for(auto e:ans)printf("%d ",e);puts("");
}
int main(){
    int o;scanf("%d",&o);
    while(o--){
        DO();
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define all(x) (x).begin(),(x).end()
#define le(x) ((int)(x).size())
#define pii pair<int,int>
#define PII pair<LL,LL>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define db printf("Here!\n");
using namespace std;
const double eps=1e-8;
const double Pi=4.0*atan(1.0);
const LL inf=1e9;
const int fix=3e5+5;
const int N=3e5+5;
const int M=1e7+5;
const int mod=998244353;
//mt19937 rnd(time(0)),shuffle(a+1,a+1+n,rnd);
int n,m,k,t,T,len,op,x,y,z;
int a[N];
string s1,s2;
void DO(){
    cin>>n>>s1>>s2;
    vector<int>ans;
    for(int i=n-1;i>=0;i--){
        if(s1[0]==s2[i]){
            if(!i)continue;
            ans.pb(1);
            ans.pb(i+1);
            s1[0]=(s1[0]=='1'?'0':'1');
        }else ans.pb(i+1);
        s1[0]=(s1[i]=='1'?'0':'1');
        char c='1';
        if(i-1>=0)c=s1[i-1];
        if(i-1>=0)s1[i-1]=(s1[1]=='1'?'0':'1');
        s1[1]=(c=='1'?'0':'1');
        if(i>=2&&i-1>2){
            if(i-2==2)s1[i-2]=s1[2]=(s1[2]=='1'?'0':'1');
            else {
                c=s1[i-2];
                s1[i-2]=(s1[2]=='1'?'0':'1');
                s1[2]=(c='1'?'0':'1');
            }
        }
        //cout<<s1[0]<<" "<<s1[1]<<" "<<s1[i-2]<<" "<<s1[i-1]<<endl;
    }
    printf("%d ",le(ans));
    for(auto e:ans)printf("%d ",e);puts("");
}
int main(){
    int o;scanf("%d",&o);
    while(o--){
        DO();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M = 1e9+7;
const ll inf = 1e18+5;
const ll INF=INT_MAX;
using namespace std;
void fast(){cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
ll bs(ll arr[], ll l, ll r, ll x){
	ll mid=0;
    if (r >= l){
		if (arr[mid] == x) return mid;
        if (arr[mid] > x) return bs(arr, l, mid - 1, x);
        return bs(arr, mid + 1, r, x);
    }
    return -1;
}
ll power(ll x,ul y,ll p){
	ll res = 1;
	x = x % p;
	if(x == 0) return 0;
	while (y > 0){
		if (y & 1) res = (res*x) % p;
		y = y>>1;
		x = (x*x) % p;
		}
	return res;
}
ll arr[500000],st[500000];
void buildtree(int si,int ss,int se){
	if(ss==se){
		st[si]=arr[ss];
		return;
	}
	int mid=ss+(se-ss)/2;
	buildtree(2*si,ss,mid);
	buildtree(2*si+1,mid+1,se);
	st[si]=min(st[2*si],st[2*si+1]);
}
int query(int si,int ss,int se,int qs,int qe){
	if(qs>se || qe<ss) //completely outside
		return INF;
	if(ss>=qs && se<=qe)
		return st[si];
	int mid=ss+(se-ss)/2;
	int l=query(2*si,ss,mid,qs,qe);
	int r=query(2*si+1,mid+1,se,qs,qe);
	return min(l,r);
}
void update(int si,int ss,int se,int qi){
	if(ss==se){
		st[si]=arr[ss];
		return;
	}
	int mid=ss+(se-ss)/2;
	if(qi<=mid) update(2*si,ss,mid,qi);
	else update(2*si+1,mid+1,se,qi);
	st[si]=min(st[2*si],st[2*si+1]);
}

void solve(){
	fast();
	ll t;
	cin>>t;
	while(t--){
		ll n;
		cin>>n;
		string a,b;
		cin>>a>>b;
		a=a+"0";
		b=b+"0";
		vector<ll>v;
		vector<ll>v1;
		for(ll i=1;i<=n;i++)
			if(a[i-1]!=a[i]) v.push_back(i);
		for(ll i=1;i<=n;i++)
			if(b[i-1]!=b[i]) v1.push_back(i);

		cout<<v.size()+v1.size()<<" ";
		for(ll i=0;i<(int)v.size();i++) cout<<v[i]<<" ";
		for(ll i=v1.size()-1;i>=0;i--) cout<<v1[i]<<" ";
		cout<<"\n";
	}

 }
int main(){
	fast();
	solve();
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M = 1e9+7;
const ll inf = 1e18+5;
const ll INF=INT_MAX;
using namespace std;
void fast(){cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
ll bs(ll arr[], ll l, ll r, ll x){
	ll mid=0;
    if (r >= l){
		if (arr[mid] == x) return mid;
        if (arr[mid] > x) return bs(arr, l, mid - 1, x);
        return bs(arr, mid + 1, r, x);
    }
    return -1;
}
ll power(ll x,ul y,ll p){
	ll res = 1;
	x = x % p;
	if(x == 0) return 0;
	while (y > 0){
		if (y & 1) res = (res*x) % p;
		y = y>>1;
		x = (x*x) % p;
		}
	return res;
}
ll arr[500000],st[500000];
void buildtree(int si,int ss,int se){
	if(ss==se){
		st[si]=arr[ss];
		return;
	}
	int mid=ss+(se-ss)/2;
	buildtree(2*si,ss,mid);
	buildtree(2*si+1,mid+1,se);
	st[si]=min(st[2*si],st[2*si+1]);
}
int query(int si,int ss,int se,int qs,int qe){
	if(qs>se || qe<ss) //completely outside
		return INF;
	if(ss>=qs && se<=qe)
		return st[si];
	int mid=ss+(se-ss)/2;
	int l=query(2*si,ss,mid,qs,qe);
	int r=query(2*si+1,mid+1,se,qs,qe);
	return min(l,r);
}
void update(int si,int ss,int se,int qi){
	if(ss==se){
		st[si]=arr[ss];
		return;
	}
	int mid=ss+(se-ss)/2;
	if(qi<=mid) update(2*si,ss,mid,qi);
	else update(2*si+1,mid+1,se,qi);
	st[si]=min(st[2*si],st[2*si+1]);
}

void solve(){
	fast();
	ll t;
	cin>>t;
	while(t--){
		ll n;
		cin>>n;
		string a,b;
		cin>>a>>b;
		vector<ll>v;
		for(ll i=0;i<n;i++)
			if(a[i-1]!=a[i]) v.push_back(i);
		for(ll i=0;i<n;i++)
			if(b[i-1]!=b[i]) v.push_back(i);
		cout<<v.size()<<" ";
		for(ll i=0;i<(int)v.size();i++) cout<<v[i]<<" ";
		cout<<"\n";
	}

 }
int main(){
	fast();
	solve();
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M = 1e9+7;
const ll inf = 1e18+5;
const ll INF=INT_MAX;
using namespace std;
void fast(){cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
ll bs(ll arr[], ll l, ll r, ll x){
	ll mid=0;
    if (r >= l){
		if (arr[mid] == x) return mid;
        if (arr[mid] > x) return bs(arr, l, mid - 1, x);
        return bs(arr, mid + 1, r, x);
    }
    return -1;
}
ll power(ll x,ul y,ll p){
	ll res = 1;
	x = x % p;
	if(x == 0) return 0;
	while (y > 0){
		if (y & 1) res = (res*x) % p;
		y = y>>1;
		x = (x*x) % p;
		}
	return res;
}
ll arr[500000],st[500000];
void buildtree(int si,int ss,int se){
	if(ss==se){
		st[si]=arr[ss];
		return;
	}
	int mid=ss+(se-ss)/2;
	buildtree(2*si,ss,mid);
	buildtree(2*si+1,mid+1,se);
	st[si]=min(st[2*si],st[2*si+1]);
}
int query(int si,int ss,int se,int qs,int qe){
	if(qs>se || qe<ss) //completely outside
		return INF;
	if(ss>=qs && se<=qe)
		return st[si];
	int mid=ss+(se-ss)/2;
	int l=query(2*si,ss,mid,qs,qe);
	int r=query(2*si+1,mid+1,se,qs,qe);
	return min(l,r);
}
void update(int si,int ss,int se,int qi){
	if(ss==se){
		st[si]=arr[ss];
		return;
	}
	int mid=ss+(se-ss)/2;
	if(qi<=mid) update(2*si,ss,mid,qi);
	else update(2*si+1,mid+1,se,qi);
	st[si]=min(st[2*si],st[2*si+1]);
}

void solve(){
	fast();
	ll t;
	cin>>t;
	while(t--){
		ll n;
		cin>>n;
		string a,b;
		cin>>a>>b;
		vector<ll>v;
		for(ll i=0;i<n;i++){
			if(a[i]!=b[i]){
				v.push_back(i+1);
				v.push_back(1);
				v.push_back(i+1);
			}
		}
		cout<<v.size()<<" ";
		for(ll i=0;i<v.size();i++) cout<<v[i]<<" ";
		cout<<"\n";
	}

 }
int main(){
	fast();
	solve();
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE

#include<iostream>
#include<cmath>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#include<cmath>
#include<string>

using namespace std;

int   matrix[1006][1005];
int n, m;
long long  dp[4][1005][1005];

int main(){
	while (scanf("%d%d", &n, &m)){
		for (int i = 1; i <= n; i++){
			for (int j = 1; j <= m; j++){
				scanf("%d", &matrix[i][j]);
			}
		}
		memset(dp, 0, sizeof(dp));

		for (int i = 1; i <= n; i++){
			for (int j = 1; j <= m; j++){
				dp[0][i][j] = max(dp[0][i - 1][j], dp[0][i][j - 1]) + matrix[i][j];
			}
		}

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

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

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

		long long ans = -1;
		long long temp;
		for (int i = 1; i <= n; i++){
			for (int j = 1; j <= m; j++){
				temp = dp[1][i][j] + dp[1][i][j] + dp[2][i][j] + dp[3][i][j] - 4 * matrix[i][j];
				if (ans < temp) ans = temp;
			}
		}

		printf("%I64d\n", ans);
	}
	return 0;
}

//
//
////#include<CSpreadSheet.h>
//
//#include<iostream>
//#include<cmath>
//#include<cstdio>
//#include<sstream>
//#include<cstdlib>
//#include<string>
//#include<string.h>
//#include<cstring>
//#include<algorithm>
//#include<vector>
//#include<map>
//#include<set>
//#include<stack>
//#include<list>
//#include<queue>
//#include<ctime>
//#include<bitset>
//#include<cmath>
//#define eps 1e-6
//#define INF 0x3f3f3f3f
//#define PI acos(-1.0)
//#define ll __int64
//#define LL long long
//#define lson l,m,(rt<<1)
//#define rson m+1,r,(rt<<1)|1
//#define M 1000000007
////#pragma comment(linker, "/STACK:1024000000,1024000000")
//using namespace std;
//
//int   matrix[1006][1005];
//int n, m;
//long long  dp_a[1005][1005];
//long long  dp_b[1005][1005];
//long long dp_c[1005][1005];
//long long  dp_d[1005][1005];
//
//
//int main(){
//	while (scanf("%d%d", &n, &m)){
//		for (int i = 1; i <= n; i++){
//			for (int j = 1; j <= m; j++){
//				scanf("%d", &matrix[i][j]);
//			}
//		}
//		memset(dp_a, 0, sizeof(dp_a));
//		memset(dp_c, 0, sizeof(dp_c));
//		memset(dp_b, 0, sizeof(dp_b));
//		memset(dp_d, 0, sizeof(dp_d));
//
//
//		for (int i = 1; i <= n; i++){
//			for (int j = 1; j <= m; j++){
//				dp_a[i][j] = max(dp_a[i - 1][j], dp_a[i][j - 1]) + matrix[i][j];
//			}
//		}
//
//
//
//
//
//
//		for (int i = 1; i <= n; i++){
//			for (int j = m; j >= 1; j--){
//				dp_b[i][j] = max(dp_b[i - 1][j], dp_b[i][j + 1]) + matrix[i][j];
//			}
//		}
//
//
//		for (int i = n; i >= 1; i--){
//			for (int j = 1; j <= m; j++){
//				dp_c[i][j] = max(dp_c[i - 1][j], dp_c[i][j - 1]) + matrix[i][j];
//			}
//		}
//
//
//
//		for (int i = n; i >= 1; i--){
//			for (int j = m; j >= 1; j--){
//				dp_d[i][j] = max(dp_d[i - 1][j], dp_d[i][j - 1]) + matrix[i][j];
//			}
//		}
//
//
//		long long ans = -1;
//		long long temp;
//		for (int i = 1; i <= n; i++){
//			for (int j = 1; j <= m; j++){
//				temp = dp_a[i][j] + dp_b[i][j] + dp_c[i][j] + dp_d[i][j] - 4 * matrix[i][j];
//				if (ans < temp) ans = temp;
//			}
//		}
//
//		printf("%I64d\n", ans);
//	}
//	return 0;
//}
//
//

//char input[105];
//
//int  ans[105][105];
//
//int dp[105][105];
//
//
//int dfs(int i, int j){
//	if (dp[i][j] != -1){
//		return dp[i][j];
//	}
//	else if (i == j){
//		if (input[i] == '('||input[i]==')'){
//			ans[i][j] = -1;
//			dp[i][j] = 1;
//			return dp[i][j];
//		}
//		else {
//			ans[i][j] = -2;
//			dp[i][j] = 1;
//			return dp[i][j];
//		}
//
//	}
//	else {
//		if (input[i] == '('&&input[j] == ')'){
//			if (i == j - 1){
//				ans[i][j] = -1;
//				dp[i][j] = 0;
//				return 0;
//			}
//			else {
//				dp[i][j] = dfs(i + 1, j - 1);
//				ans[i][j] =-3;
//				return dp[i][j];
//			}
//		}
//		else if (input[i] == '['&&input[j] == ']'){
//			if (i == j - 1){
//				ans[i][j] = -2;
//				dp[i][j] = 0;
//				return 0;
//			}
//			else {
//				dp[i][j] = dfs(i + 1, j - 1);
//				ans[i][j] =-4;
//				return dp[i][j];
//			}
//		}
//		else {
//			int min_k = i;
//			int min_a = dfs(i, min_k) + dfs(min_k + 1, j);
//			int temp;
//			for (int k = i + 1; k < j; k++){
//				temp = dfs(i, k) + dfs(k + 1, j);
//				if (temp < min_a){
//					min_a = temp;
//					min_k = k;
//				}
//			}
//			ans[i][j] =min_k;
//			dp[i][j] = min_a;
//			return dp[i][j];
//
//		}
//	}
//}
//
//
//
//
//void print_ans(int i, int j){
//	if (ans[i][j] >= 0){
//		print_ans(i, ans[i][j]);
//		print_ans(ans[i][j] + 1, j);
//	}
//	else if (ans[i][j] == -1){
//		printf("()");
//	}
//	else if (ans[i][j] == -2){
//		printf("[]");
//	}
//	else if (ans[i][j] == -3){
//		printf("(");
//		print_ans(i + 1, j - 1);
//		printf(")");
//	}
//	else {
//		printf("[");
//		print_ans(i + 1, j - 1);
//		printf("]");
//	}
//
//}
//int main(){
//	gets(input);
//		memset(dp, -1, sizeof(dp));
//		int size = 0;
//		while (input[size] != 0){
//			size++;
//		}
//		if (size == 0) {
//			printf("\n");
//
//		}
//		else {
//			dfs(0, size - 1);
//			print_ans(0, size - 1);
//			printf("\n");
//		}
//
//	return 0;
//}


#define ll long long
#define mod 1000000007LL
#define F(a,b,c) for(a=b;a<c;a++)
#define Fr(a,b,c) for(a=b;a>=c;a--)
#define pf printf
#define sf scanf
#include<bits/stdc++.h>
using namespace std;
int a[1002][1002],dp1[1002][1002],dp2[1002][1002];
int f(int i,int j,int n,int m){
	if(dp1[i][j]!=-1)
		return dp1[i][j];
	if(i>=n||j>=m)
		return 0;
	return dp1[i][j]=a[i][j]+max(f(i+1,j,n,m),f(i,j+1,n,m));

}
int f2(int i,int j,int n,int m){
	if(dp2[i][j]!=-1)
		return dp2[i][j];
	if(i<n||j>=m)
		return 0;
	return dp2[i][j]=a[i][j]+max(f2(i-1,j,n,m),f2(i,j+1,n,m));

}

#define pb push_back
#define mp(a,b) make_pair(a,b)
int main(){
int n,m,i,j;
sf("%d%d",&n,&m);
memset(dp1,-1,sizeof(dp1));
memset(dp2,-1,sizeof(dp2));
F(i,0,n)
	F(j,0,m)
		sf("%d",&a[i][j]);
int ma=-1,m1=-1,m2=-1,m3=-1,m4=-1;
F(i,1,n-1)
	F(j,1,m-1)
	{
		m1=f(0,0,i+1,j)+f(i,j+1,n,m);
		m2=f2(n-1,0,i+1,j+1)+f2(i-1,j,0,m);
		m3=f(0,0,i,j+1)+f(i+1,j,n,m);
		m4=f2(n-1,0,i,j)+f2(i,j+1,0,m);
		ma=max(ma,max(m1+m2,m3+m4));
	}

cout<<ma;
return 0;
}


#define ll long long
#define mod 1000000007LL
#define F(a,b,c) for(a=b;a<c;a++)
#define Fr(a,b,c) for(a=b;a>=c;a--)
#define pf printf
#define sf scanf
#include<bits/stdc++.h>
using namespace std;
int a[1002][1002],dp1[1002][1002],dp2[1002][1002];
int f(int i,int j,int n,int m){
	if(dp1[i][j]!=-1)
		return dp1[i][j];
	if(i>=n||j>=m)
		return 0;
	return dp1[i][j]=max(f(i+1,j,n,m)+a[i][j],a[i][j]+f(i,j+1,n,m));

}
int f2(int i,int j,int n,int m){
if(dp2[i][j]!=-1)
		return dp2[i][j];
	if(i<n||j>=m)
		return 0;
	return dp2[i][j]=max(f(i-1,j,n,m)+a[i][j],a[i][j]+f(i,j+1,n,m));

}

#define pb push_back
#define mp(a,b) make_pair(a,b)
int main(){
int n,m,i,j;
sf("%d%d",&n,&m);
memset(dp1,-1,sizeof(dp1));
memset(dp2,-1,sizeof(dp2));
F(i,0,n)
	F(j,0,m)
		sf("%d",&a[i][j]);
int ma=-1,ma1=-1,ma2=-1;
F(i,0,m)
	{
	ma1=f(0,0,n/2+1,i+1)+f(n/2,i,n,m);
	ma2=f2(n-1,0,n/2,i+1)+f2(n/2,i,0,m);
	ma=max(ma,ma1+ma2-2*a[n/2][i]);
	}
cout<<ma;
return 0;
}


#define ll long long
#define mod 1000000007LL
#define F(a,b,c) for(a=b;a<c;a++)
#define Fr(a,b,c) for(a=b;a>=c;a--)
#define pf printf
#define sf scanf
#include<bits/stdc++.h>
using namespace std;
int a[1002][1002],dp1[1002][1002],dp2[1002][1002];
int f(int i,int j,int n,int m){
	if(dp1[i][j]!=-1)
		return dp1[i][j];
	if(i>=n||j>=m)
		return 0;
	return dp1[i][j]=a[i][j]+max(f(i+1,j,n,m),f(i,j+1,n,m));

}
int f2(int i,int j,int n,int m){
	if(dp2[i][j]!=-1)
		return dp2[i][j];
	if(i<n||j>=m)
		return 0;
	return dp2[i][j]=a[i][j]+max(f2(i-1,j,n,m),f2(i,j+1,n,m));

}

#define pb push_back
#define mp(a,b) make_pair(a,b)
int main(){
int n,m,i,j;
sf("%d%d",&n,&m);
memset(dp1,-1,sizeof(dp1));
memset(dp2,-1,sizeof(dp2));
F(i,0,n)
	F(j,0,m)
		sf("%d",&a[i][j]);
int ma=-1,m1=-1,m2=-1,m3=-1,m4=-1;
F(i,1,n-1)
	F(j,1,m-1)
	{
		m1=f(0,0,i+1,j)+f(i,j+1,n,m);
		m2=f2(n-1,0,i+1,j+1)+f2(i-1,j,0,m);
	 	m3=f(0,0,i,j+1)+f(i+1,j,n,m);
		m4=f2(n-1,0,i,j)+f2(i,j+1,0,m);
		ma=max(ma,max(m1+m2,m3+m4));
	}

cout<<ma; s
return 0;
}


#define ll long long
#define mod 1000000007LL
#define F(a,b,c) for(a=b;a<c;a++)
#define Fr(a,b,c) for(a=b;a>=c;a--)
#define pf printf
#define sf scanf
#include<bits/stdc++.h>
using namespace std;
int a[1002][1002],dp1[1002][1002],dp2[1002][1002];
int f(int i,int j,int n,int m){
	if(dp1[i][j]!=-1)
		return dp1[i][j];
	if(i>=n||j>=m)
		return 0;
	return dp1[i][j]=a[i][j]+max(f(i+1,j,n,m),f(i,j+1,n,m));

}
int f2(int i,int j,int n,int m){
	if(dp2[i][j]!=-1)
		return dp2[i][j];
	if(i<n||j>=m)
		return 0;
	return dp2[i][j]=a[i][j]+max(f2(i-1,j,n,m),f2(i,j+1,n,m));

}

#define pb push_back
#define mp(a,b) make_pair(a,b)
int main(){
int n,m,i,j;
sf("%d%d",&n,&m);
memset(dp1,-1,sizeof(dp1));
memset(dp2,-1,sizeof(dp2));
F(i,0,n)
	F(j,0,m)
		sf("%d",&a[i][j]);
int ma=-1,m1=-1,m2=-1,m3=-1,m4=-1;
F(i,1,n-1)
	F(j,1,m-1)
	{
		m1=f(0,0,i+1,j)+f(i,j+1,n,m);
		m2=f2(n-1,0,i+1,j+1)+f2(i-1,j,0,m);
	 	m3=f(0,0,i,j+1)+f(i+1,j,n,m);
		m4=f2(n-1,0,i,j)+f2(i,j+1,0,m);
		ma=max(ma,max(m1+m2,m3+m4));
	}

cout<<ma;
return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#define MP make_pair
#define LL long long
#define L first
#define R second
#define shuf push_back
#define rid pop_back

using namespace std;
const int maxn = 1000 + 4;

int dp[maxn][maxn][4] , n , m , a[maxn][maxn] , max1;
bool mark[maxn][maxn][4];

int memoise(int i , int j , int k)
{
	if(mark[i][j][k])
		return dp[i][j][k];

	mark[i][j][k] = true;

	if(k == 0)
		return max(memoise(i , j - 1 , 0) , memoise(i - 1 , j , 0)) + a[i][j];

	if(k == 1)
		return max(memoise(i , j + 1 , 1) , memoise(i - 1 , j , 1)) + a[i][j];

	if(k == 2)
		return max(memoise(i , j - 1 , 2) , memoise(i + 1 , j , 2)) + a[i][j];

	if(k == 3)
		return max(memoise(i , j + 1 , 3) , memoise(i + 1 , j , 3)) + a[i][j];
}

void pre_process()
{
	dp[1][1][0] = a[1][1];
	dp[1][m][1] = a[1][m];
	dp[n][1][2] = a[n][1];
	dp[n][m][3] = a[n][m];

	mark[1][1][0] = 1;
	mark[1][m][1] = 1;
	mark[n][1][2] = 1;
	mark[n][m][3] = 1;

	for(int i = 0;i <= n + 1;i ++)
	{
		mark[i][0][0] = 1;
		mark[i][0][1] = 1;
		mark[i][0][2] = 1;
		mark[i][0][3] = 1;

		mark[i][m + 1][0] = 1;
		mark[i][m + 1][1] = 1;
		mark[i][m + 1][2] = 1;
		mark[i][m + 1][3] = 1;
	}

	for(int i = 0;i <= m + 1;i ++)
	{
		mark[0][i][0] = 1;
		mark[0][i][1] = 1;
		mark[0][i][2] = 1;
		mark[0][i][3] = 1;

		mark[n + 1][i][0] = 1;
		mark[n + 1][i][1] = 1;
		mark[n + 1][i][2] = 1;
		mark[n + 1][i][3] = 1;
	}
}

int main()
{
	cin >> n >> m;

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

	pre_process();

	for(int i = 1;i <= n;i ++)
		for(int j = 1;j <= m;j ++)
			for(int k = 0;k < 4;k ++)
				dp[i][j][k] = memoise(i , j , k);

	for(int i = 1;i <= n;i ++)
		for(int j = 1;j <= m;j ++)
				max1 = max(max1 , dp[i][j][0] + dp[i][j][1] + dp[i][j][2] + dp[i][j][3] - 4 * a[i][j]);

	cout << max1;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#define L first
#define R second
#define MP make_pair
#define shuf push_back

using namespace std;
const int maxn = 1000 + 4;

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

void input()
{
    cin >> n >> m;

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

void pre_process()
{
    dp1[1][1] = a[1][1];
    dp2[1][m] = a[1][m];
    dp3[n][1] = a[n][1];
    dp4[n][m] = a[n][m];
}

long long answer()
{
    long long max1 = -1;

    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= m;j ++)
            max1 = max(max1 , max(dp1[i - 1][j] + dp2[i][j + 1] + dp3[i][j - 1] + dp4[i + 1][j] , dp1[i][j - 1] + dp2[i - 1][j] + dp3[i + 1][j] + dp4[i][j + 1]));

    return max1;
}

int main()
{
    input();
    pre_process();

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

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

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

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

    cout << answer();

  return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#define L first
#define R second
#define MP make_pair
#define shuf push_back

using namespace std;
const int maxn = 1000 + 4;

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

void input()
{
    cin >> n >> m;

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

void pre_process()
{
    dp1[1][1] = a[1][1];
    dp2[1][m] = a[1][m];
    dp3[n][1] = a[n][1];
    dp4[n][m] = a[n][m];
}

long long answer()
{
    long long max1 = -1;

    for(int i = 2;i < n;i ++)
        for(int j = 2;j < m;j ++)
            max1 = max(max1 , max(dp1[i - 1][j] + dp2[i][j + 1] + dp3[i][j - 1] + dp4[i + 1][j] , dp1[i][j - 1] + dp2[i - 1][j] + dp3[i + 1][j] + dp4[i][j + 1]));

    return max1;
}

int main()
{
    input();
    pre_process();

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

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

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

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

    cout << answer();

  return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#define L first
#define R second
#define MP make_pair
#define shuf push_back

using namespace std;
const int maxn = 1000 + 4;

int n , m , a[maxn][maxn];
long long dp1[maxn][maxn] , dp2[maxn][maxn] , dp3[maxn][maxn] , dp4[maxn][maxn];
bool mark[maxn][maxn][5];

void input()
{
    cin >> n >> m;

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

long long memoise(int i , int j , int k)
{
  if(k == 1)
  {
    if(mark[i][j][1])
        return dp1[i][j];

    mark[i][j][1] = 1;
    return max(memoise(i - 1 , j , k) , memoise(i , j - 1 , k)) + a[i][j];
  }

  if(k == 2)
  {
    if(mark[i][j][2])
        return dp2[i][j];

    mark[i][j][2] = 1;
    return max(memoise(i - 1 , j , k) , memoise(i , j + 1 , k)) + a[i][j];
  }

  if(k == 3)
  {
    if(mark[i][j][3])
        return dp3[i][j];

    mark[i][j][3] = 1;
    return max(memoise(i + 1 , j , k) , memoise(i , j - 1 , k)) + a[i][j];
  }

  if(k == 4)
  {
    if(mark[i][j][4])
        return dp4[i][j];

    mark[i][j][4] = 1;
    return max(memoise(i + 1 , j , k) , memoise(i , j + 1 , k)) + a[i][j];
  }
}

void pre_process()
{
    dp1[1][1] = a[1][1];
    dp2[1][m] = a[1][m];
    dp3[n][1] = a[n][1];
    dp4[n][m] = a[n][m];

    mark[1][1][1] = 1;
    mark[1][m][2] = 1;
    mark[n][1][3] = 1;
    mark[n][m][4] = 1;

    for(int i = 0;i <= n + 1;i ++)
    {
        mark[0][i][1] = 1;
        mark[0][i][2] = 1;
        mark[0][i][3] = 1;
        mark[0][i][4] = 1;

        mark[n + 1][i][1] = 1;
        mark[n + 1][i][2] = 1;
        mark[n + 1][i][3] = 1;
        mark[n + 1][i][4] = 1;
    }

    for(int i = 0;i <= m + 1;i ++)
    {
        mark[i][0][1] = 1;
        mark[i][0][2] = 1;
        mark[i][0][3] = 1;
        mark[i][0][4] = 1;

        mark[i][m + 1][1] = 1;
        mark[i][m + 1][2] = 1;
        mark[i][m + 1][3] = 1;
        mark[i][m + 1][4] = 1;
    }

    return;
}

long long answer()
{
    long long max1 = -1;

    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= m;j ++)
            max1 = max(max1 , dp1[i][j] + dp2[i][j] + dp3[i][j] + dp4[i][j] - 4 * a[i][j]);

    return max1;
}

int main()
{
  input();
  pre_process();

  for(int i = 1;i <= n;i ++)
    for(int j = 1;j <= m;j ++)
        dp1[i][j] = memoise(i , j , 1);

  for(int i = 1;i <= n;i ++)
      for(int j = m;j > 0;j --)
        dp2[i][j] = memoise(i , j , 2);

    for(int i = n;i > 0;i --)
        for(int j = 1;j <= m;j ++)
            dp3[i][j] = memoise(i , j , 3);

    for(int i = n;i > 0;i --)
        for(int j = m;j > 0;j --)
            dp4[i][j] = memoise(i , j, 4);

  cout << answer();

  return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#define MP make_pair
#define LL long long
#define L first
#define R second
#define shuf push_back
#define rid pop_back

using namespace std;
const int maxn = 1000 + 4;

int dp[maxn][maxn][4] , n , m , a[maxn][maxn] , max1;
bool mark[maxn][maxn][4];

int memoise(int i , int j , int k)
{
	if(mark[i][j][k])
		return dp[i][j][k];

	mark[i][j][k] = true;

	if(k == 0)
		return max(memoise(i , j - 1 , 0) , memoise(i - 1 , j , 0)) + a[i][j];

	if(k == 1)
		return max(memoise(i , j + 1 , 1) , memoise(i - 1 , j , 1)) + a[i][j];

	if(k == 2)
		return max(memoise(i , j - 1 , 2) , memoise(i + 1 , j , 2)) + a[i][j];

	if(k == 3)
		return max(memoise(i , j + 1 , 3) , memoise(i + 1 , j , 3)) + a[i][j];
}

void pre_process()
{
	dp[1][1][0] = a[1][1];
	dp[1][m][1] = a[1][m];
	dp[n][1][2] = a[n][1];
	dp[n][m][3] = a[n][m];

	mark[1][1][0] = 1;
	mark[1][m][1] = 1;
	mark[n][1][2] = 1;
	mark[n][m][3] = 1;

	for(int i = 0;i <= n + 1;i ++)
	{
		mark[i][0][0] = 1;
		mark[i][0][1] = 1;
		mark[i][0][2] = 1;
		mark[i][0][3] = 1;

		mark[i][m + 1][0] = 1;
		mark[i][m + 1][1] = 1;
		mark[i][m + 1][2] = 1;
		mark[i][m + 1][3] = 1;
	}

	for(int i = 0;i <= m + 1;i ++)
	{
		mark[0][i][0] = 1;
		mark[0][i][1] = 1;
		mark[0][i][2] = 1;
		mark[0][i][3] = 1;

		mark[n + 1][i][0] = 1;
		mark[n + 1][i][1] = 1;
		mark[n + 1][i][2] = 1;
		mark[n + 1][i][3] = 1;
	}
}

int main()
{
	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 ++)
			for(int k = 0;k < 4;k ++)
				dp[i][j][k] = memoise(i , j , k);

	for(int i = 1;i <= n;i ++)
		for(int j = 1;j <= m;j ++)
				max1 = max(max1 , dp[i][j][0] + dp[i][j][1] + dp[i][j][2] + dp[i][j][3] - 4 * a[i][j]);

	cout << max1;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cstring>
#define For(i,n) for(i=1;i<=n;i++)
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn = 1e3+5;
typedef long long ll;
ll map[maxn][maxn];
ll dp1[maxn][maxn];
ll dp2[maxn][maxn];
ll dp3[maxn][maxn];
ll dp4[maxn][maxn];

int main() {
    int n,a,b,i,j;
    cin >> a >> b;
    For(i,a)
    For(j,b)
        cin >> map[i][j];
    For(i,a)
    For(j,b)
    dp1[i][j] = map[i][j]+max(dp1[i-1][j],dp1[i][j-1]);
    For(i,a)
    for(j=b; j>0; j--)
        dp2[i][j] = map[i][j] + max(dp2[i-1][j],dp2[i][j+1]);
    for(i=a; i>0; i--)
        for(j=b; j>0; j--)
            dp3[i][j] = map[i][j] + max(dp3[i+1][j],dp3[i][j-1]);
    for(i=a; i>0; i--)
        for(j=0; j<=b; j++)
            dp4[i][j] = map[i][j] + max(dp4[i+1][j],dp4[i][j-1]);
    ll ans = 0;
    for(i=2; i<a; i++)
        for(j=2; j<b; j++) {
            ans = max(ans,dp1[i-1][j]+dp2[i][j+1]+dp3[i+1][j]+dp4[i][j-1]);
            ans = max(ans,dp1[i][j-1]+dp2[i-1][j]+dp3[i][j-1]+dp4[i+1][j]);

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


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cstring>
#define For(i,n) for(i=1;i<=n;i++)
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn = 1e3+5;
typedef long long ll;
ll map[maxn][maxn];
ll dp1[maxn][maxn];
ll dp2[maxn][maxn];
ll dp3[maxn][maxn];
ll dp4[maxn][maxn];

int main() {
    int n,a,b,i,j;
    cin >> a >> b;
    For(i,a)
    For(j,b)
        cin >> map[i][j];
    For(i,a)
    For(j,b)
    dp1[i][j] = map[i][j]+max(dp1[i-1][j],dp1[i][j-1]);
    For(i,a)
    for(j=b; j>0; j--)
        dp2[i][j] = map[i][j] + max(dp2[i-1][j],dp2[i][j+1]);
    for(i=a; i>0; i--)
        for(j=b; j>0; j--)
            dp3[i][j] = map[i][j] + max(dp3[i+1][j],dp3[i][j+1]);
    for(i=a; i>0; i--)
        for(j=0; j<=b; j++)
            dp4[i][j] = map[i][j] + max(dp4[i+1][j],dp4[i][j-1]);
    ll ans = 0;
    for(i=2; i<a; i++)
        for(j=2; j<b; j++) {
            ans = max(ans,dp1[i-1][j]+dp2[i][j+1]+dp3[i+1][j]+dp4[i][j-1]);
            ans = max(ans,dp1[i][j-1]+dp2[i-1][j]+dp3[i][j+1]+dp4[i+1][j]);

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


//Saatwik Singh Nagpal
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1000005
#define INF 1000000001
#define MOD 1000000007

#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define mset(a,b) memset(a,b,sizeof(a))
#define m2set(a,m,n,b) memset(a,b,sizeof(a[0][0])*m*n)
#define graphclear(g) for(int i=1;i<=n;i++)g[i].clear()

#define inp_s     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define test()     int test;cin>>test;for(int t=1;t<=test;t++)

struct sort_pred {
	bool operator()(const std::pair<int,int> &left, const std::pair<int,int> &right) {
		return left.second < right.second;
	}
};
//sort(v,v+n,sort_pred())

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define pb push_back
#define PB pop_back
#define pf push_front
#define PF pop_front
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef vector < ll > vll;
typedef vector < vll > vvll;
typedef pair < ll,ll > ll2;
typedef vector < ll2 > vll2;
typedef vector < int > vi;
typedef vector < vi > vvi;
typedef pair < int,int > ii;
typedef vector < ii > vii;
typedef vector < vii > vvii;
typedef pair< int,pair<int,int> > iii;
typedef vector < iii > viii;
#define tr(c,it) for(typeof(c.begin()) it = c.begin(); it != c.end(); it++)

#define gc getchar
#define pc putchar
#define endl "\n"
#define si(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define pin(n) printf("%d\n",n)
#define plln(n) printf("%lld\n",n)
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//Works for forward as well as backward iteration

int gcd(int a, int b)	{return (a == 0) ? b : gcd(b%a, a);}
ll powmod(ll a,ll b){if(b==0)return 1;ll x=powmod(a,b/2);ll y=(x*x)%MOD;if(b%2)return (a*y)%MOD;return y%MOD;}ll n,m,a[1001][1001],dp1[1001][1001],dp2[1001][1001],dp3[1001][1001],dp4[1001][1001];
int main()
{
	//inp_s;
	//test()
	sll(n);sll(m);
	ll mn = INF;
	vll2 p;
	rep(i,0,n)rep(j,0,m){
		sll(a[i][j]);
		mn = min(mn,a[i][j]);
	}
	rep(i,0,n)rep(j,0,m)
		if(a[i][j] == mn)p.pb(mp(i,j));
	ll ans = 0;
	dp1[0][0] = a[0][0];
	dp2[n-1][m-1] = a[n-1][m-1];
	dp3[0][m-1] = a[0][m-1];
	dp4[n-1][0] = a[n-1][0];
	rep(i,1,n){
		dp1[i][0] = dp1[i-1][0]+a[i][0];
		dp3[i][m-1] = dp3[i-1][m-1]+a[i][m-1];
	}
	rep(i,1,m){
		dp1[0][i] = dp1[0][i-1]+a[0][i];
		dp4[n-1][i] = dp4[n-1][i-1]+a[n-1][i];
	}
	for(int i=n-2;i>=0;i--){
		dp2[i][m-1]=dp2[i+1][m-1]+a[i][m-1];
		dp4[i][0] = dp4[i+1][0]+a[i][0];
	}
	for(int i=m-2;i>=0;i--){
		dp2[n-1][i]=dp2[n-1][i+1]+a[n-1][i];
		dp3[0][i] = dp3[0][i+1]+a[0][i];
	}
	rep(i,1,n){
		rep(j,1,m){
			dp1[i][j] = max(dp1[i-1][j],dp1[i][j-1])+a[i][j];
		}
	}
	for(int i = n-2;i>=0;i--){
		for(int j = m-2;j>=0;j--){
			dp2[i][j] = max(dp2[i+1][j],dp2[i][j+1])+a[i][j];
		}
	}
	for(int i = 1;i < n;i++){
		for(int j=m-2;j>=0;j--){
			dp3[i][j] = max(dp3[i][j+1],dp3[i-1][j])+a[i][j];
		}
	}
	for(int i=n-2;i>=0;i--){
		for(int j=1;j<m;j++){
			dp4[i][j] = max(dp4[i+1][j],dp4[i][j-1])+a[i][j];
		}
	}
	/*rep(i,0,p.size()){
		ll tmp=0,t2 = 0,t4 = 0;
		int u = p[i].f;
		int v = p[i].s;
		t2+=dp1[u-1][v];
		t2+=dp3[u][v+1];
		t4+=dp1[u][v-1];
		t4+=dp3[u-1][v];
		t2+=dp2[u+1][v];
		t2+=dp4[u][v-1];
		t4+=dp4[u+1][v];
		t4+=dp2[u][v+1];
		tmp = max(t2,t4);
		ans=max(ans,tmp);
	}*/
	rep(i,1,n-1){
		rep(j,1,m-1){
			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]);
		}
	}
	plln(ans);
	return 0;
}


//Saatwik Singh Nagpal
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1000005
#define INF 1000000001
#define MOD 1000000007

#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define mset(a,b) memset(a,b,sizeof(a))
#define m2set(a,m,n,b) memset(a,b,sizeof(a[0][0])*m*n)
#define graphclear(g) for(int i=1;i<=n;i++)g[i].clear()

#define inp_s     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define test()     int test;cin>>test;for(int t=1;t<=test;t++)

struct sort_pred {
	bool operator()(const std::pair<int,int> &left, const std::pair<int,int> &right) {
		return left.second < right.second;
	}
};
//sort(v,v+n,sort_pred())

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define pb push_back
#define PB pop_back
#define pf push_front
#define PF pop_front
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef vector < ll > vll;
typedef vector < vll > vvll;
typedef pair < ll,ll > ll2;
typedef vector < ll2 > vll2;
typedef vector < int > vi;
typedef vector < vi > vvi;
typedef pair < int,int > ii;
typedef vector < ii > vii;
typedef vector < vii > vvii;
typedef pair< int,pair<int,int> > iii;
typedef vector < iii > viii;
#define tr(c,it) for(typeof(c.begin()) it = c.begin(); it != c.end(); it++)

#define gc getchar
#define pc putchar
#define endl "\n"
#define si(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define pin(n) printf("%d\n",n)
#define plln(n) printf("%lld\n",n)
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//Works for forward as well as backward iteration

int gcd(int a, int b)	{return (a == 0) ? b : gcd(b%a, a);}
ll powmod(ll a,ll b){if(b==0)return 1;ll x=powmod(a,b/2);ll y=(x*x)%MOD;if(b%2)return (a*y)%MOD;return y%MOD;}ll n,m,a[1001][1001],dp1[1001][1001],dp2[1001][1001],dp3[1001][1001],dp4[1001][1001];
int main()
{
	//inp_s;
	//test()
	sll(n);sll(m);
	ll mn = INF;
	vll2 p;
	rep(i,0,n)rep(j,0,m){
		sll(a[i][j]);
		mn = min(mn,a[i][j]);
	}
	rep(i,0,n)rep(j,0,m)
		if(a[i][j] == mn)p.pb(mp(i,j));
	ll ans = 0;
	dp1[0][0] = a[0][0];
	dp2[n-1][m-1] = a[n-1][m-1];
	dp3[0][m-1] = a[0][m-1];
	dp4[n-1][0] = a[n-1][0];
	rep(i,1,n){
		dp1[i][0] = dp1[i-1][0]+a[i][0];
		dp3[i][m-1] = dp3[i-1][m-1]+a[i][m-1];
	}
	rep(i,1,m){
		dp1[0][i] = dp1[0][i-1]+a[0][i];
		dp4[n-1][i] = dp4[n-1][i-1]+a[n-1][i];
	}
	for(int i=n-2;i>=0;i--){
		dp2[i][m-1]=dp2[i+1][m-1]+a[i][m-1];
		dp4[i][0] = dp4[i+1][0]+a[i][0];
	}
	for(int i=m-2;i>=0;i--){
		dp2[n-1][i]=dp2[n-1][i+1]+a[n-1][i];
		dp3[0][i] = dp3[0][i+1]+a[0][i];
	}
	rep(i,1,n){
		rep(j,1,m){
			dp1[i][j] = max(dp1[i-1][j],dp1[i][j-1])+a[i][j];
		}
	}
	for(int i = n-2;i>=0;i--){
		for(int j = m-2;j>=0;j--){
			dp2[i][j] = max(dp2[i+1][j],dp2[i][j+1])+a[i][j];
		}
	}
	for(int i = 1;i < n;i++){
		for(int j=m-2;j>=0;j--){
			dp3[i][j] = max(dp3[i][j+1],dp3[i-1][j])+a[i][j];
		}
	}
	for(int i=n-2;i>=0;i--){
		for(int j=1;j<m;j++){
			dp4[i][j] = max(dp4[i+1][j],dp4[i][j-1])+a[i][j];
		}
	}
	/*rep(i,0,p.size()){
		ll tmp=0,t2 = 0,t4 = 0;
		int u = p[i].f;
		int v = p[i].s;
		t2+=dp1[u-1][v];
		t2+=dp3[u][v+1];
		t4+=dp1[u][v-1];
		t4+=dp3[u-1][v];
		t2+=dp2[u+1][v];
		t2+=dp4[u][v-1];
		t4+=dp4[u+1][v];
		t4+=dp2[u][v+1];
		tmp = max(t2,t4);
		ans=max(ans,tmp);
	}*/
	rep(i,0,n){
		rep(j,0,m){
			if(i-1>=0 && i+1<=n-1 && j+1<=m && j-1>=0)ans = max(ans,dp1[i-1][j]+dp2[i+1][j]+dp3[i][j+1]+dp4[i][j-1]);
			if(i-1>=0 && i+1<=n-1 && j+1<=m && j-1>=0)ans = max(ans,dp1[i][j-1]+dp2[i][j+1]+dp3[i-1][j]+dp4[i+1][j]);
		}
	}
	plln(ans);
	return 0;
}


//Saatwik Singh Nagpal
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1000005
#define INF 1000000001
#define MOD 1000000007

#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define mset(a,b) memset(a,b,sizeof(a))
#define m2set(a,m,n,b) memset(a,b,sizeof(a[0][0])*m*n)
#define graphclear(g) for(int i=1;i<=n;i++)g[i].clear()

#define inp_s     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define test()     int test;cin>>test;for(int t=1;t<=test;t++)

struct sort_pred {
	bool operator()(const std::pair<int,int> &left, const std::pair<int,int> &right) {
		return left.second < right.second;
	}
};
//sort(v,v+n,sort_pred())

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define pb push_back
#define PB pop_back
#define pf push_front
#define PF pop_front
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef vector < ll > vll;
typedef vector < vll > vvll;
typedef pair < ll,ll > ll2;
typedef vector < ll2 > vll2;
typedef vector < int > vi;
typedef vector < vi > vvi;
typedef pair < int,int > ii;
typedef vector < ii > vii;
typedef vector < vii > vvii;
typedef pair< int,pair<int,int> > iii;
typedef vector < iii > viii;
#define tr(c,it) for(typeof(c.begin()) it = c.begin(); it != c.end(); it++)

#define gc getchar
#define pc putchar
#define endl "\n"
#define si(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define pin(n) printf("%d\n",n)
#define plln(n) printf("%lld\n",n)
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//Works for forward as well as backward iteration

int gcd(int a, int b)	{return (a == 0) ? b : gcd(b%a, a);}
ll powmod(ll a,ll b){if(b==0)return 1;ll x=powmod(a,b/2);ll y=(x*x)%MOD;if(b%2)return (a*y)%MOD;return y%MOD;}ll n,m,a[1001][1001],dp1[1001][1001],dp2[1001][1001],dp3[1001][1001],dp4[1001][1001];
int main()
{
	//inp_s;
	//test()
	sll(n);sll(m);
	ll mn = INF;
	vll2 p;
	rep(i,0,n)rep(j,0,m){
		sll(a[i][j]);
		mn = min(mn,a[i][j]);
	}
	rep(i,0,n)rep(j,0,m)
		if(a[i][j] == mn)p.pb(mp(i,j));
	ll ans = 0;
	dp1[0][0] = a[0][0];
	dp2[n-1][m-1] = a[n-1][m-1];
	dp3[0][m-1] = a[0][m-1];
	dp4[n-1][0] = a[n-1][0];
	rep(i,1,n){
		dp1[i][0] = dp1[i-1][0]+a[i][0];
		dp3[i][m-1] = dp3[i-1][m-1]+a[i][m-1];
	}
	rep(i,1,m){
		dp1[0][i] = dp1[0][i-1]+a[0][i];
		dp4[n-1][i] = dp4[n-1][i-1]+a[n-1][i];
	}
	for(int i=n-2;i>=0;i--){
		dp2[i][m-1]=dp2[i+1][m-1]+a[i][m-1];
		dp4[i][0] = dp4[i+1][0]+a[i][0];
	}
	for(int i=m-2;i>=0;i--){
		dp2[n-1][i]=dp2[n-1][i+1]+a[n-1][i];
		dp3[0][i] = dp3[0][i+1]+a[0][i];
	}
	rep(i,1,n){
		rep(j,1,m){
			dp1[i][j] = max(dp1[i-1][j],dp1[i][j-1])+a[i][j];
		}
	}
	for(int i = n-2;i>=0;i--){
		for(int j = m-2;j>=0;j--){
			dp2[i][j] = max(dp2[i+1][j],dp2[i][j+1])+a[i][j];
		}
	}
	for(int i = 1;i < n;i++){
		for(int j=m-2;j>=0;j--){
			dp3[i][j] = max(dp3[i][j+1],dp3[i-1][j])+a[i][j];
		}
	}
	for(int i=n-2;i>=0;i--){
		for(int j=1;j<m;j++){
			dp4[i][j] = max(dp4[i+1][j],dp4[i][j-1])+a[i][j];
		}
	}
	rep(i,0,p.size()){
		ll tmp = 0;
		int u = p[i].f;
		int v = p[i].s;
		tmp+=max(dp1[u-1][v],dp1[u][v-1]);
		tmp+=dp2[u][v];
		tmp-=a[u][v];
		tmp+=max(dp3[u][v+1],dp3[u-1][v+1]);
		tmp+=dp4[u][v];
		tmp-=a[u][v];
		ans = max(ans,tmp);
	}
	plln(ans);
	return 0;
}


//Saatwik Singh Nagpal
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1000005
#define INF 1000000001
#define MOD 1000000007

#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define mset(a,b) memset(a,b,sizeof(a))
#define m2set(a,m,n,b) memset(a,b,sizeof(a[0][0])*m*n)
#define graphclear(g) for(int i=1;i<=n;i++)g[i].clear()

#define inp_s     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define test()     int test;cin>>test;for(int t=1;t<=test;t++)

struct sort_pred {
	bool operator()(const std::pair<int,int> &left, const std::pair<int,int> &right) {
		return left.second < right.second;
	}
};
//sort(v,v+n,sort_pred())

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define pb push_back
#define PB pop_back
#define pf push_front
#define PF pop_front
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef vector < ll > vll;
typedef vector < vll > vvll;
typedef pair < ll,ll > ll2;
typedef vector < ll2 > vll2;
typedef vector < int > vi;
typedef vector < vi > vvi;
typedef pair < int,int > ii;
typedef vector < ii > vii;
typedef vector < vii > vvii;
typedef pair< int,pair<int,int> > iii;
typedef vector < iii > viii;
#define tr(c,it) for(typeof(c.begin()) it = c.begin(); it != c.end(); it++)

#define gc getchar
#define pc putchar
#define endl "\n"
#define si(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define pin(n) printf("%d\n",n)
#define plln(n) printf("%lld\n",n)
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//Works for forward as well as backward iteration

int gcd(int a, int b)	{return (a == 0) ? b : gcd(b%a, a);}
ll powmod(ll a,ll b){if(b==0)return 1;ll x=powmod(a,b/2);ll y=(x*x)%MOD;if(b%2)return (a*y)%MOD;return y%MOD;}ll n,m,a[1001][1001],dp1[1001][1001],dp2[1001][1001],dp3[1001][1001],dp4[1001][1001];
int main()
{
	//inp_s;
	//test()
	sll(n);sll(m);
	ll mn = INF;
	vll2 p;
	rep(i,0,n)rep(j,0,m){
		sll(a[i][j]);
		mn = min(mn,a[i][j]);
	}
	rep(i,0,n)rep(j,0,m)
		if(a[i][j] == mn)p.pb(mp(i,j));
	ll ans = 0;
	dp1[0][0] = a[0][0];
	dp2[n-1][m-1] = a[n-1][m-1];
	dp3[0][m-1] = a[0][m-1];
	dp4[n-1][0] = a[n-1][0];
	rep(i,1,n){
		dp1[i][0] = dp1[i-1][0]+a[i][0];
		dp3[i][m-1] = dp3[i-1][m-1]+a[i][m-1];
	}
	rep(i,1,m){
		dp1[0][i] = dp1[0][i-1]+a[0][i];
		dp4[n-1][i] = dp4[n-1][i-1]+a[n-1][i];
	}
	for(int i=n-2;i>=0;i--){
		dp2[i][m-1]=dp2[i+1][m-1]+a[i][m-1];
		dp4[i][0] = dp4[i+1][0]+a[i][0];
	}
	for(int i=m-2;i>=0;i--){
		dp2[n-1][i]=dp2[n-1][i+1]+a[n-1][i];
		dp3[0][i] = dp3[0][i+1]+a[0][i];
	}
	rep(i,1,n){
		rep(j,1,m){
			dp1[i][j] = MAX(dp1[i-1][j],dp1[i][j-1])+a[i][j];
		}
	}
	for(int i = n-2;i>=0;i--){
		for(int j = m-2;j>=0;j--){
			dp2[i][j] = MAX(dp2[i+1][j],dp2[i][j+1])+a[i][j];
		}
	}
	for(int i = 1;i < n;i++){
		for(int j=m-2;j>=0;j--){
			dp3[i][j] = MAX(dp3[i][j+1],dp3[i-1][j])+a[i][j];
		}
	}
	for(int i=n-2;i>=0;i--){
		for(int j=1;j<m;j++){
			dp4[i][j] = MAX(dp4[i+1][j],dp4[i][j-1])+a[i][j];
		}
	}
	rep(i,1,n-1){
		rep(j,1,m-1){
			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]);
		}
	}
	plln(ans);
	return 0;
}


//Saatwik Singh Nagpal
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1000005
#define INF 1000000001
#define MOD 1000000007

#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define mset(a,b) memset(a,b,sizeof(a))
#define m2set(a,m,n,b) memset(a,b,sizeof(a[0][0])*m*n)
#define graphclear(g) for(int i=1;i<=n;i++)g[i].clear()

#define inp_s     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define test()     int test;cin>>test;for(int t=1;t<=test;t++)

struct sort_pred {
	bool operator()(const std::pair<int,int> &left, const std::pair<int,int> &right) {
		return left.second < right.second;
	}
};
//sort(v,v+n,sort_pred())

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define pb push_back
#define PB pop_back
#define pf push_front
#define PF pop_front
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef vector < ll > vll;
typedef vector < vll > vvll;
typedef pair < ll,ll > ll2;
typedef vector < ll2 > vll2;
typedef vector < int > vi;
typedef vector < vi > vvi;
typedef pair < int,int > ii;
typedef vector < ii > vii;
typedef vector < vii > vvii;
typedef pair< int,pair<int,int> > iii;
typedef vector < iii > viii;
#define tr(c,it) for(typeof(c.begin()) it = c.begin(); it != c.end(); it++)

#define gc getchar
#define pc putchar
#define endl "\n"
#define si(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define pin(n) printf("%d\n",n)
#define plln(n) printf("%lld\n",n)
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//Works for forward as well as backward iteration

int gcd(int a, int b)	{return (a == 0) ? b : gcd(b%a, a);}
ll powmod(ll a,ll b){if(b==0)return 1;ll x=powmod(a,b/2);ll y=(x*x)%MOD;if(b%2)return (a*y)%MOD;return y%MOD;}ll n,m,a[1001][1001],dp1[1001][1001],dp2[1001][1001],dp3[1001][1001],dp4[1001][1001];
int main()
{
	//inp_s;
	//test()
	sll(n);sll(m);
	ll mn = INF;
	vll2 p;
	rep(i,0,n)rep(j,0,m){
		sll(a[i][j]);
		mn = min(mn,a[i][j]);
	}
	rep(i,0,n)rep(j,0,n)
		if(a[i][j] == mn)p.pb(mp(i,j));
	ll ans = 0;
	dp1[0][0] = a[0][0];
	dp2[n-1][n-1] = a[n-1][n-1];
	dp3[0][n-1] = a[0][n-1];
	dp4[n-1][0] = a[n-1][0];
	rep(i,1,n){
		dp1[0][i] = dp1[0][i-1]+a[0][i];
		dp1[i][0] = dp1[i-1][0]+a[i][0];
		dp3[i][n-1] = dp3[i-1][n-1]+a[i][n-1];
		dp4[n-1][i] = dp4[n-1][i-1]+a[n-1][i];
	}
	for(int i=n-2;i>=0;i--){
		dp2[i][n-1]=dp2[i+1][n-1]+a[i][n-1];
		dp2[n-1][i]=dp2[n-1][i+1]+a[n-1][i];
		dp3[0][i] = dp3[0][i+1]+a[0][i];
		dp4[i][0] = dp4[i+1][0]+a[i][0];
	}
	rep(i,1,n){
		rep(j,1,n){
			dp1[i][j] = max(dp1[i-1][j],dp1[i][j-1])+a[i][j];
		}
	}
	for(int i = n-2;i>=0;i--){
		for(int j = n-2;j>=0;j--){
			dp2[i][j] = max(dp2[i+1][j],dp2[i][j+1])+a[i][j];
		}
	}
	for(int i = 1;i < n;i++){
		for(int j=n-2;j>=0;j--){
			dp3[i][j] = max(dp3[i][j+1],dp3[i-1][j])+a[i][j];
		}
	}
	for(int i=n-2;i>=0;i--){
		for(int j=1;j<n;j++){
			dp4[i][j] = max(dp4[i+1][j],dp4[i][j-1])+a[i][j];
		}
	}
	rep(i,0,p.size()){
		ll tmp = 0;
		int u = p[i].f;
		int v = p[i].s;
		tmp+=max(dp1[u-1][v],dp1[u][v-1]);
		tmp+=dp2[u][v];
		tmp-=a[u][v];
		tmp+=max(dp3[u][v+1],dp3[u-1][v+1]);
		tmp+=dp4[u][v];
		tmp-=a[u][v];
		ans = max(ans,tmp);
	}
	plln(ans);
	return 0;
}


//Saatwik Singh Nagpal
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1000005
#define INF 1000000001
#define MOD 1000000007

#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define mset(a,b) memset(a,b,sizeof(a))
#define m2set(a,m,n,b) memset(a,b,sizeof(a[0][0])*m*n)
#define graphclear(g) for(int i=1;i<=n;i++)g[i].clear()

#define inp_s     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define test()     int test;cin>>test;for(int t=1;t<=test;t++)

struct sort_pred {
	bool operator()(const std::pair<int,int> &left, const std::pair<int,int> &right) {
		return left.second < right.second;
	}
};
//sort(v,v+n,sort_pred())

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define pb push_back
#define PB pop_back
#define pf push_front
#define PF pop_front
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef vector < ll > vll;
typedef vector < vll > vvll;
typedef pair < ll,ll > ll2;
typedef vector < ll2 > vll2;
typedef vector < int > vi;
typedef vector < vi > vvi;
typedef pair < int,int > ii;
typedef vector < ii > vii;
typedef vector < vii > vvii;
typedef pair< int,pair<int,int> > iii;
typedef vector < iii > viii;
#define tr(c,it) for(typeof(c.begin()) it = c.begin(); it != c.end(); it++)

#define gc getchar
#define pc putchar
#define endl "\n"
#define si(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define pin(n) printf("%d\n",n)
#define plln(n) printf("%lld\n",n)
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//Works for forward as well as backward iteration

int gcd(int a, int b)	{return (a == 0) ? b : gcd(b%a, a);}
ll powmod(ll a,ll b){if(b==0)return 1;ll x=powmod(a,b/2);ll y=(x*x)%MOD;if(b%2)return (a*y)%MOD;return y%MOD;}ll n,m,a[1001][1001],dp1[1001][1001],dp2[1001][1001],dp3[1001][1001],dp4[1001][1001];
int main()
{
	//inp_s;
	//test()
	sll(n);sll(m);
	ll mn = INF;
	vll2 p;
	rep(i,0,n)rep(j,0,m){
		sll(a[i][j]);
		mn = min(mn,a[i][j]);
	}
	rep(i,0,n)rep(j,0,m)
		if(a[i][j] == mn)p.pb(mp(i,j));
	ll ans = 0;
	dp1[0][0] = a[0][0];
	dp2[n-1][m-1] = a[n-1][m-1];
	dp3[0][m-1] = a[0][m-1];
	dp4[n-1][0] = a[n-1][0];
	rep(i,1,n){
		dp1[i][0] = dp1[i-1][0]+a[i][0];
		dp3[i][m-1] = dp3[i-1][m-1]+a[i][m-1];
	}
	rep(i,1,m){
		dp1[0][i] = dp1[0][i-1]+a[0][i];
		dp4[n-1][i] = dp4[n-1][i-1]+a[n-1][i];
	}
	for(int i=n-2;i>=0;i--){
		dp2[i][m-1]=dp2[i+1][m-1]+a[i][m-1];
		dp4[i][0] = dp4[i+1][0]+a[i][0];
	}
	for(int i=m-2;i>=0;i--){
		dp2[n-1][i]=dp2[n-1][i+1]+a[n-1][i];
		dp3[0][i] = dp3[0][i+1]+a[0][i];
	}
	rep(i,1,n){
		rep(j,1,m){
			dp1[i][j] = max(dp1[i-1][j],dp1[i][j-1])+a[i][j];
		}
	}
	for(int i = n-2;i>=0;i--){
		for(int j = m-2;j>=0;j--){
			dp2[i][j] = max(dp2[i+1][j],dp2[i][j+1])+a[i][j];
		}
	}
	for(int i = 1;i < n;i++){
		for(int j=m-2;j>=0;j--){
			dp3[i][j] = max(dp3[i][j+1],dp3[i-1][j])+a[i][j];
		}
	}
	for(int i=n-2;i>=0;i--){
		for(int j=1;j<m;j++){
			dp4[i][j] = max(dp4[i+1][j],dp4[i][j-1])+a[i][j];
		}
	}
	rep(i,0,p.size()){
		ll tmp=0,t1 = 0,t2 = 0,t3 = 0,t4 = 0;
		int u = p[i].f;
		int v = p[i].s;
		t1+=dp1[u][v-1];
		t1+=max(dp3[u][v+1],dp3[u-1][v]);
		t2+=dp1[u-1][v];
		t2+=dp3[u][v+1];
		t3+=max(dp1[u][v-1],dp1[u-1][v]);
		t3+=dp3[u][v+1];
		t4+=dp1[u][v-1];
		t4+=dp3[u-1][v];
		tmp = max(max(t1,t4),max(t2,t3));
		t1=t2=t3=t4=0;
		t1+=dp4[u][v-1];
		t1+=max(dp2[u][v+1],dp2[u+1][v]);
		t2+=dp4[u+1][v];
		t2+=dp2[u][v+1];
		t3+=max(dp4[u][v-1],dp4[u+1][v]);
		t3+=dp2[u][v+1];
		t4+=dp4[u][v-1];
		t4+=dp2[u+1][v];
		tmp += max(max(t1,t4),max(t2,t3));
		ans=max(ans,tmp);
	}
	plln(ans);
	return 0;
}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define mod 1000000007

#define pb push_back

#define bpt(x) (__builtin_popcount(x))

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define bit(x, y) (((x)>>(y))&1)

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<class T, class U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<class T, class U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<class T> inline void gn(T &x) {char c,sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-' && c!=-1);if (c==-1) return ;if (c=='-') sg=1,c=getchar(); for (x=0; c>='0' && c<='9'; x=(x<<1)+(x<<3)+c-'0', c=getchar());if (sg) x=-x;}

template<class T> void print(T x) {if (x<0) return putchar('-'), print(-x);if (x<10) {putchar(x+'0');return ;} print(x/10);putchar(x%10+'0');}

template<class U, class V>inline void gn(U &u, V &v){gn(u);gn(v);}

template<class U, class V, class W>inline void gn(U &u, V &v, W &w){gn(u); gn(v, w);}

template<class T> void println(T x) {print(x);putchar('\n');}

template<class T> void printsp(T x) {print(x);putchar(' ');}

template<class U, class V> inline void print(U u, V v) {printsp(u);println(v);}

template<class U, class V, class W> inline void print(U u, V v, W w){printsp(u);print(v, w);}

int power(int a, int b, int m, int ans=1) {

	for (; b; a=(INT)a*a%m,b>>=1) if (b&1) ans=(INT)ans*a%m;

	return ans;

}

#define NN 1111

int judge[NN][NN][5];

int a[NN][NN];

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n,m;

	gn(n,m);

	int ans=0;

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

		for(int j=1;j<=m;j++){

			gn(a[i][j]);

		}

	}

	int bi,ei,bj,ej,dsi,dsj;

	for(int id=0;id<=3;id++){

		if(id==0){

			bi=1,bj=1,ei=n,ej=m,dsi=1,dsj=1;

		}

		if(id==1){

			bi=n,bj=1,ei=1,ej=m,dsi=-1,dsj=1;

		}

		if(id==2){

			bi=n,bj=m,ei=1,ej=1,dsi=-1,dsj=-1;

		}

		if(id==3){

			bi=1,bj=m,ei=n,ej=1,dsi=1,dsj=-1;

		}

		for(int i=bi;i!=ei;i+=dsi){

			for(int j=bj;j!=ej;j+=dsj){

				smax(judge[i][j][id],a[i][j]+judge[i][j-dsj][id]);

				smax(judge[i][j][id],a[i][j]+judge[i-dsi][j][id]);

			}

		}

	}

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

		for(int j=2;j<m;j++){

			smax(ans,judge[i][j-1][0]+judge[i+1][j][1]+judge[i][j+1][2]+judge[i-1][j][3]);

			smax(ans,judge[i-1][j][0]+judge[i][j-1][1]+judge[i+1][j][2]+judge[i][j+1][3]);

		}

	}

	println(ans);

	return 0;

}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define MOD 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define x first

#define y second

#define eps 1e-9

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define sgn(x) (((x)>eps)-((x)<-eps))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<class T>inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}

template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}

template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}

template<class T>inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

template<class T>inline void printsp(T x) {print(x); putchar(' ');}

template<class T>inline void println(T x) {print(x); putchar('\n');}

template<class T, class U>inline void print(T x, U y) {printsp(x); println(y);}

template<class T, class U, class V>inline void print(T x, U y, V z) {printsp(x); printsp(y); println(z);}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#define NN 1010

int a[NN][NN], dp[NN][NN][4];

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n, m;

	gn(n, m);

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

	for (int k=0; k<4; k++) {

		int sti, stj, edi, edj, stpi, stpj;

		if (k==0) {

			sti=1; edi=n; stj=1; edj=m;

			stpi=1; stpj=1;

		}

		if (k==1) {

			sti=1; edi=n; stj=m; edj=1;

			stpi=1; stpj=-1;

		}

		if (k==2) {

			sti=n; edi=1; stj=m; edj=1;

			stpi=-1; stpj=-1;

		}

		if (k==3) {

			sti=n; edi=1; stj=1; edj=m;

			stpi=-1; stpj=1;

		}

		dp[sti][stj][k]=a[sti][stj];

		for (int i=sti; i!=edi; i+=stpi) {

			for (int j=stj; j!=edj; j+=stpj) {

				if (i==sti && j==stj) continue;

				if (i!=sti) smax(dp[i][j][k], dp[i-stpi][j][k]+a[i][j]);

				if (j!=stj) smax(dp[i][j][k], dp[i][j-stpj][k]+a[i][j]);

			}

		}

	}

	int ans=0;

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

		smax(ans, dp[i-1][j][0]+dp[i][j+1][1]+dp[i+1][j][2]+dp[i][j-1][3]);

		smax(ans, dp[i][j-1][0]+dp[i-1][j][1]+dp[i][j+1][2]+dp[i+1][j][3]);

	}

	println(ans);

	return 0;

}


#include <bits/stdc++.h>

#define INF 0x3f3f3f3f

#define mod 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define x first

#define y second

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

#define eps 1e-8

#define squ(a,b) (a)*(a)+(b)*(b)

#define pi 3.1415926535897932384626433832795

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

template<class T>inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if(sg) x=-x;}

template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}

template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}

template<class T>inline void print(T x) {if(x<0) {putchar('-');return print(-x);}if(x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

template<class T>inline void printsp(T x) {print(x); putchar(' ');}

template<class T>inline void println(T x) {print(x); putchar('\n');}

template<class T, class U>inline void print(T x, U y) {printsp(x); println(y);}

template<class T, class U, class V>inline void print(T x, U y, V z) {printsp(x); printsp(y); println(z);}

int power(int a, int b, int m, int ans=1) {

	for(; b; b>>=1, a=1LL*a*a%m) if(b&1) ans=1LL*ans*a%m;

	return ans;

}

const int B=1111;

int dp[B][B][4],a[B][B];

void solve_B(){

	int n,m;gn(n,m);

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

	dp[1][1][0]=a[1][1];

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

		if(i>1) smax(dp[i][j][0],dp[i-1][j][0]+a[i][j]);

		if(j>1) smax(dp[i][j][0],dp[i][j-1][0]+a[i][j]);

	}

	dp[n][m][1]=a[n][m];

	for(int i=n; i>0; i--) for(int j=m; j>0; j--){

		if(i<n) smax(dp[i][j][1],dp[i+1][j][1]+a[i][j]);

		if(j<m) smax(dp[i][j][1],dp[i][j+1][1]+a[i][j]);

	}

	dp[n][1][2]=a[n][1];

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

		if(i<n) smax(dp[i][j][2],dp[i+1][j][2]+a[i][j]);

		if(j>1) smax(dp[i][j][2],dp[i][j-1][2]+a[i][j]);

	}

	dp[1][m][3]=a[1][m];

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

		if(i>1) smax(dp[i][j][3],dp[i-1][j][3]+a[i][j]);

		if(j<m) smax(dp[i][j][3],dp[i][j+1][3]+a[i][j]);

	}

	int ans=0;

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

		smax(ans,dp[i-1][j][0]+dp[i+1][j][1]+dp[i][j-1][2]+dp[i][j+1][3]);

		smax(ans,dp[i-1][j][3]+dp[i+1][j][2]+dp[i][j-1][0]+dp[i][j+1][1]);

	}

	println(ans);

	exit(0);

}

int main(){

#ifndef ONLINE_JUDGE

	freopen("in.in","r",stdin);

	freopen("out.out","w",stdout);

#endif

	solve_B();

	return 0;

}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define MOD 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define x first

#define y second

#define eps 1e-8

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define debug(x) cout << #x << " = " << x << endl

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

#define NN 1010

char c;

void getint(int &x) {

	while (c=getchar(), c>'9' || c<'0');

	for (x=0; c>='0' && c<='9'; c=getchar()) x=(x<<3)+(x<<1)+c-'0';

}

int a[NN][NN];

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

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

int n, m;

int dd[][2]={{0, 1}, {0, 3}, {1, 2}, {2, 3}};

INT dp[4][NN][NN][5];

#define beyond(u, v) (u<0 || v<0 || u>=n || v>=m)

int bfs(int k, int sx, int sy) {

	if (k==0) {

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

			if (i==0 && j==0) dp[k][0][0][4]=a[0][0];

			else {

				if (i) {

					dp[k][i][j][1]=dp[k][i-1][j][4];

				}

				if (j) {

					dp[k][i][j][0]=dp[k][i][j-1][4];

				}

				dp[k][i][j][4]=max(dp[k][i][j][1], dp[k][i][j][0])+a[i][j];

			}

		}

	}

	if (k==1) {

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

			if (i==n-1 && j==0) dp[k][i][j][4]=a[i][j];

			else {

				if (i<n-1) {

					dp[k][i][j][3]=dp[k][i+1][j][4];

				}

				if (j) {

					dp[k][i][j][0]=dp[k][i][j-1][4];

				}

				dp[k][i][j][4]=max(dp[k][i][j][3], dp[k][i][j][0])+a[i][j];

			}

		}

	}

	if (k==2) {

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

			if (i==0 && j==m-1) dp[k][i][j][4]=a[i][j];

			else {

				if (i) {

					dp[k][i][j][1]=dp[k][i-1][j][4];

				}

				if (j<m-1) {

					dp[k][i][j][2]=dp[k][i][j+1][4];

				}

				dp[k][i][j][4]=max(dp[k][i][j][2], dp[k][i][j][1])+a[i][j];

			}

		}

	}

	if (k==3) {

		for (int i=n-1; i>=0; i--) for (int j=m-1; j>=0; j--) {

			if (i==n-1 && j==m-1) dp[k][i][j][4]=a[i][j];

			else {

				if (i<n-1) {

					dp[k][i][j][3]=dp[k][i+1][j][4];

				}

				if (j<m-1) {

					dp[k][i][j][2]=dp[k][i][j+1][4];

				}

				dp[k][i][j][4]=max(dp[k][i][j][2], dp[k][i][j][3])+a[i][j];

			}

		}

	}

}

int main() {

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

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

	bfs(0, 0, 0);

	bfs(1, n-1, 0);

	bfs(2, 0, m-1);

	bfs(3, n-1, m-1);

	int b[9][4]={

		{0, 3, 1, 2},

//		{0, 3, 1, 3},

//		{0, 3, 2, 3},

//		{1, 3, 1, 3},

//		{1, 3, 2, 3},

//		{1, 0, 1, 3},

		{1, 0, 2, 3},

//		{1, 3, 1, 2},

//		{1, 0, 1, 2}

	};

	INT ans=0;

	for (int i=1; i<n-1; i++) {

		for (int j=1; j<m-1; j++) {

			for (int r=0; r<2; r++) {

				smax(ans, dp[0][i][j][b[r][0]]+dp[1][i][j][b[r][1]]+dp[2][i][j][b[r][2]]+dp[3][i][j][b[r][3]]);

			}

		}

	}

	cout << ans << endl;

	return 0;

}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1005;
const ll inf = 1ll<<62;
int n,m,grid[maxn][maxn];
ll dp1[maxn][maxn], dp2[maxn][maxn], dp3[maxn][maxn], dp4[maxn][maxn];
void up(ll &x,ll y){ if (y > x) x= y;}
int main(){
  //  freopen("b.in","r",stdin);
  //  freopen("b.out","w",stdout);
        scanf("%d%d",&n,&m);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j ++)
        scanf("%d",&grid[i][j]);
    //dp1
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp1[i][j] = - inf;
    dp1[1][1] = grid[1][1];
    for (int i = 1; i <= n; i ++)
      for (int j = 1; j <= n; j ++)
        if (i != 1 || j != 1){
              dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]) + grid[i][j];
        }

    //dp2
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp2[i][j] = - inf;
    dp2[n][1]=grid[n][1];
    for (int i = n; i >= 1; i --)
      for (int j = 1; j <= m; j ++)
          if (i != n || j != 1)
            dp2[i][j] = max(dp2[i+1][j], dp2[i][j-1]) + grid[i][j];

    //dp3
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp3[i][j] = - inf;
    dp3[n][m]=grid[n][m];
    for (int i = n; i >= 1; i --)
      for (int j = m; j >= 1; j --)
        if (i != n || j != m)
           dp3[i][j] = max(dp3[i+1][j], dp3[i][j+1]) + grid[i][j];

    //dp4
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
        dp4[i][j] = - inf;
    dp4[1][m] = grid[1][m];
    for (int i = 1; i <= n; i ++)
      for (int j = m; j > 0; j --)
         if (i != 1 || j != m)
             dp4[i][j] = max(dp4[i-1][j], dp4[i][j+1]) + grid[i][j];
    //merge
    ll ans=-inf;
    for (int i = 1; i <= n; i ++)
       for (int j = 1; j <= m; j ++){
           /*
           ll in=-inf;
           up(in, dp1[i-1][j] + dp2[i+1][j]);
           up(in, dp1[i-1][j] + dp2[i][j-1]);
           up(in, dp1[i][j-1] + dp2[i+1][j]);
           ll ou=-inf;
           up(ou, dp3[i][j+1] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i][j+1]);
           if (ou != -inf && in != -inf) up(ans, in + ou);
           */
           up(ans, dp1[i-1][j] + dp2[i][j-1] + dp3[i+1][j] + dp4[i][j+1]);
           up(ans, dp1[i][j-1] + dp2[i+1][j] + dp3[i][j+1] + dp4[i-1][j]);
       }
    printf("%I64d\n",ans);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1005;
const ll inf = 1ll<<60;
int n,m,grid[maxn][maxn];
ll dp1[maxn][maxn], dp2[maxn][maxn], dp3[maxn][maxn], dp4[maxn][maxn];
void up(ll &x,ll y){ if (y > x) x= y;}
int main(){
  //  freopen("b.in","r",stdin);
  //  freopen("b.out","w",stdout);
        scanf("%d%d",&n,&m);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j ++)
        scanf("%d",&grid[i][j]);
    //dp1
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp1[i][j] = - inf;
    dp1[1][1] = grid[1][1];
    for (int i = 1; i <= n; i ++)
      for (int j = 1; j <= m; j ++)
        if (i != 1 || j != 1){
              dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]) + grid[i][j];
        }

    //dp2
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp2[i][j] = - inf;
    dp2[n][1]=grid[n][1];
    for (int i = n; i >= 1; i --)
      for (int j = 1; j <= m; j ++)
          if (i != n || j != 1)
            dp2[i][j] = max(dp2[i+1][j], dp2[i][j-1]) + grid[i][j];

    //dp3
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp3[i][j] = - inf;
    dp3[n][m]=grid[n][m];
    for (int i = n; i >= 1; i --)
      for (int j = m; j >= 1; j --)
        if (i != n || j != m)
           dp3[i][j] = max(dp3[i+1][j], dp3[i][j+1]) + grid[i][j];

    //dp4
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
        dp4[i][j] = - inf;
    dp4[1][m] = grid[1][m];
    for (int i = 1; i <= n; i ++)
      for (int j = m; j > 0; j --)
         if (i != 1 || j != m)
             dp4[i][j] = max(dp4[i-1][j], dp4[i][j+1]) + grid[i][j];
    //merge
    ll ans=-inf;
    for (int i = 1; i <= n; i ++)
       for (int j = 1; j <= m; j ++){
           /*
           ll in=-inf;
           up(in, dp1[i-1][j] + dp2[i+1][j]);
           up(in, dp1[i-1][j] + dp2[i][j-1]);
           up(in, dp1[i][j-1] + dp2[i+1][j]);
           ll ou=-inf;
           up(ou, dp3[i][j+1] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i][j+1]);
           if (ou != -inf && in != -inf) up(ans, in + ou);
           */
           up(ans, dp1[i-1][j] + dp2[i][j-1] + dp3[i+1][j] + dp4[i][j+1]);
           up(ans, dp1[i][j-1] + dp2[i+1][j] + dp3[i][j+1] + dp4[i-1][j]);
       }
    printf("%I64d\n",ans);
    return 0;
}