stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <iostream>

#include <cstring>

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];

  

  int ans=max(y[0][n-1],y[n-1][0]);

  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

      ans=max(ans,max(y[j][i],x[i][j]));

    }



  int sum[302][302],K[2][302][302]={};

  for(int i=0;i<=n;i++)

    for(int j=0;j<=n;j++) sum[i][j]=-100000000;





  int t=0,f=1;

  while(f){

    t++,f=0;



    for(int i=0;i<n;i++) //yoko

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

	int a=K[t%2][i][j];

	K[(t+1)%2][i][j]=a;

	sum[i][j]=-100000000;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum[i][j]<=X+Y)sum[i][j]=X+Y,K[(t+1)%2][i][j]=k;

	  ans=max(ans,sum[i][j]);

	}

	if(K[t%2][i][j]!=n-1)f=1;

	for(int k=K[t%2][i][j]+1;k<K[(t+1)%2][i][j];k++){//tate

	  sum[i][j]+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum[i][j]);

	}

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 1.1784963187967996x):
#include <iostream>

#define INF 10000000

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];



  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }

  

  int ans=y[0][n-1],sum,K[302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[i][j],nk=a;

	sum=-INF;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum<=X+Y)sum=X+Y,nk=k;

	}

	ans=max(ans,sum);

	

	if(nk!=n-1)f=1;

	for(int k=a+1;k<nk;k++){//tate

	  sum+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum);

	  }

	K[i][j]=nk;

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}
// Slower C++ solution:
#include <iostream>

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];

  

  int ans=max(y[0][n-1],y[n-1][0]);

  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

      ans=max(ans,max(y[j][i],x[i][j]));

    }



  

  int sum[302][302],K[2][302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[t%2][i][j],&b=K[(t+1)%2][i][j];



	b=a;

	sum[i][j]=-10000000;



	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum[i][j]<=X+Y)sum[i][j]=X+Y,b=k;

	}

	

	if(b!=n-1)f=1;

	ans=max(ans,sum[i][j]);

	for(int k=a+1;k<b;k++){//tate

	  sum[i][j]+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum[i][j]);

	}

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 1.1747040075772373x):
#include <iostream>

#define INF 10000000

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];



  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }

  

  int ans=y[0][n-1],sum,K[302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[i][j],nk=a;

	sum=-INF;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum<=X+Y)sum=X+Y,nk=k;

	}

	ans=max(ans,sum);

	

	if(nk!=n-1)f=1;

	for(int k=a+1;k<nk;k++){//tate

	  sum+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum);

	  }

	K[i][j]=nk;

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}
// Slower C++ solution:
#include <iostream>

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];

  

  int ans=max(y[0][n-1],y[n-1][0]);

  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }



  

  int sum[302][302],K[2][302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[t%2][i][j],&b=K[(t+1)%2][i][j];

	b=a+1;

	sum[i][j]=-10000000;



	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum[i][j]<=X+Y)sum[i][j]=X+Y,b=k;

	}

	

	if(b<=n-1)f=1;

	ans=max(ans,sum[i][j]);

	for(int k=a+1;k<b;k++){//tate

	  sum[i][j]+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum[i][j]);

	}

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 1.1744916824602705x):
#include <iostream>

#define INF 10000000

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];



  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }

  

  int ans=y[0][n-1],sum,K[302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[i][j],nk=a;

	sum=-INF;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum<=X+Y)sum=X+Y,nk=k;

	}

	ans=max(ans,sum);

	

	if(nk!=n-1)f=1;

	for(int k=a+1;k<nk;k++){//tate

	  sum+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum);

	  }

	K[i][j]=nk;

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}
// Slower C++ solution:
#include <iostream>

#define INF 10000000

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];

  

  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }



  int ans=max(y[0][n-1],y[n-1][0]),sum[302][302],K[2][302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[t%2][i][j],&b=K[(t+1)%2][i][j];

	b=a;

	sum[i][j]=-INF;



	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum[i][j]<=X+Y)sum[i][j]=X+Y,b=k;

	}

	ans=max(ans,sum[i][j]);



	if(b!=n-1)f=1;

	for(int k=a+1;k<b;k++){//tate

	  sum[i][j]+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum[i][j]);

	}

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 1.174521750861928x):
#include <iostream>

#define INF 10000000

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];



  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }

  

  int ans=y[0][n-1],sum,K[302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[i][j],nk=a;

	sum=-INF;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum<=X+Y)sum=X+Y,nk=k;

	}

	ans=max(ans,sum);

	

	if(nk!=n-1)f=1;

	for(int k=a+1;k<nk;k++){//tate

	  sum+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum);

	  }

	K[i][j]=nk;

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

using namespace std;



int main(){

  int n,mp[302][302],x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],mp[i][j]=x[i][j]=y[i][j];

  

  int ans=max(y[0][n-1],y[n-1][0]);

  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

      if(ans<x[i][j])ans=x[i][j];

      if(ans<y[j][i])ans=y[j][i];

    }





  int sum[302][302],K[2][302][302]={};





  for(int i=0;i<=n;i++)

    for(int j=0;j<=n;j++) sum[i][j]=-100000000;



  int t=0;

  while(1){

    t++;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[t%2][i][j];

	K[(t+1)%2][i][j]=a;

	sum[i][j]=-100000000;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum[i][j]<=X+Y)sum[i][j]=X+Y,K[(t+1)%2][i][j]=k;

	  ans=max(ans,sum[i][j]);

	}

      }



    int f=1;

    for(int i=0;i<n;i++) //yoko

      for(int j=i+1;j<n;j++) //yoko

	for(int k=K[t%2][i][j]+1;k<K[(t+1)%2][i][j];k++){

	  f=0;

	  int X=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  sum[i][j]+=X;

	  ans=max(ans,sum[i][j]);

	}

    if(f)break;

  }

  cout <<ans<<endl;

	  

  return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 1.1781922146342596x):
#include <iostream>

#include <time.h>

#define INF 10000000

using namespace std;



int main(){

  clock_t start=clock();

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];





  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }

  

  int ans=y[0][n-1],sum,K[302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[i][j],nk=a;

	sum=-INF;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum<=X+Y)sum=X+Y,nk=k;

	}

	ans=max(ans,sum);

	

	if(nk<n-1)f=1;

	for(int k=a+1;k<nk;k++){//tate

	  sum+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum);

	}

	K[i][j]=nk+(nk==a+1);

      }

  }

  clock_t end=clock();

  //  cout << (double)(end-start)/CLOCKS_PER_SEC <<endl;

  cout <<ans<<endl;

	  

  return 0; 

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];

  

  int ans=max(y[0][n-1],y[n-1][0]);

  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

      ans=max(ans,max(y[j][i],x[i][j]));

    }



  int sum[302][302],K[2][302][302]={};

  for(int i=0;i<=n;i++)

    for(int j=0;j<=n;j++) sum[i][j]=-100000000;





  int t=0,f=1;

  while(f){

    t++,f=0;



    for(int i=0;i<n;i++) //yoko

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

	int a=K[t%2][i][j];

	K[(t+1)%2][i][j]=a;

	sum[i][j]=-100000000;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum[i][j]<=X+Y)sum[i][j]=X+Y,K[(t+1)%2][i][j]=k;

	  ans=max(ans,sum[i][j]);

	}

	if(K[t%2][i][j]!=n-1)f=1;

	for(int k=K[t%2][i][j]+1;k<K[(t+1)%2][i][j];k++){//tate

	  sum[i][j]+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum[i][j]);

	}

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 1.1783960821567654x):
#include <iostream>

#include <time.h>

#define INF 10000000

using namespace std;



int main(){

  clock_t start=clock();

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];





  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }

  

  int ans=y[0][n-1],sum,K[302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[i][j],nk=a;

	sum=-INF;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum<=X+Y)sum=X+Y,nk=k;

	}

	ans=max(ans,sum);

	

	if(nk<n-1)f=1;

	for(int k=a+1;k<nk;k++){//tate

	  sum+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum);

	}

	K[i][j]=nk+(nk==a+1);

      }

  }

  clock_t end=clock();

  //  cout << (double)(end-start)/CLOCKS_PER_SEC <<endl;

  cout <<ans<<endl;

	  

  return 0; 

}
// Slower C++ solution:
#include <iostream>

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];

  

  int ans=max(y[0][n-1],y[n-1][0]);

  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

      ans=max(ans,max(y[j][i],x[i][j]));

    }



  

  int sum[302][302],K[2][302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[t%2][i][j],&b=K[(t+1)%2][i][j];



	b=a;

	sum[i][j]=-10000000;



	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum[i][j]<=X+Y)sum[i][j]=X+Y,b=k;

	}

	

	if(b!=n-1)f=1;

	ans=max(ans,sum[i][j]);

	for(int k=a+1;k<b;k++){//tate

	  sum[i][j]+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum[i][j]);

	}

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 1.1746040934910613x):
#include <iostream>

#include <time.h>

#define INF 10000000

using namespace std;



int main(){

  clock_t start=clock();

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];





  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }

  

  int ans=y[0][n-1],sum,K[302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[i][j],nk=a;

	sum=-INF;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum<=X+Y)sum=X+Y,nk=k;

	}

	ans=max(ans,sum);

	

	if(nk<n-1)f=1;

	for(int k=a+1;k<nk;k++){//tate

	  sum+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum);

	}

	K[i][j]=nk+(nk==a+1);

      }

  }

  clock_t end=clock();

  //  cout << (double)(end-start)/CLOCKS_PER_SEC <<endl;

  cout <<ans<<endl;

	  

  return 0; 

}
// Slower C++ solution:
#include <iostream>

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];

  

  int ans=max(y[0][n-1],y[n-1][0]);

  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }



  

  int sum[302][302],K[2][302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[t%2][i][j],&b=K[(t+1)%2][i][j];

	b=a+1;

	sum[i][j]=-10000000;



	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum[i][j]<=X+Y)sum[i][j]=X+Y,b=k;

	}

	

	if(b<=n-1)f=1;

	ans=max(ans,sum[i][j]);

	for(int k=a+1;k<b;k++){//tate

	  sum[i][j]+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum[i][j]);

	}

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 1.1743917864333417x):
#include <iostream>

#include <time.h>

#define INF 10000000

using namespace std;



int main(){

  clock_t start=clock();

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];





  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }

  

  int ans=y[0][n-1],sum,K[302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[i][j],nk=a;

	sum=-INF;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum<=X+Y)sum=X+Y,nk=k;

	}

	ans=max(ans,sum);

	

	if(nk<n-1)f=1;

	for(int k=a+1;k<nk;k++){//tate

	  sum+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum);

	}

	K[i][j]=nk+(nk==a+1);

      }

  }

  clock_t end=clock();

  //  cout << (double)(end-start)/CLOCKS_PER_SEC <<endl;

  cout <<ans<<endl;

	  

  return 0; 

}
// Slower C++ solution:
#include <iostream>

#define INF 10000000

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];

  

  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }



  int ans=max(y[0][n-1],y[n-1][0]),sum[302][302],K[2][302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[t%2][i][j],&b=K[(t+1)%2][i][j];

	b=a;

	sum[i][j]=-INF;



	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum[i][j]<=X+Y)sum[i][j]=X+Y,b=k;

	}

	ans=max(ans,sum[i][j]);



	if(b!=n-1)f=1;

	for(int k=a+1;k<b;k++){//tate

	  sum[i][j]+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum[i][j]);

	}

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 1.1744218522775405x):
#include <iostream>

#include <time.h>

#define INF 10000000

using namespace std;



int main(){

  clock_t start=clock();

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];





  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }

  

  int ans=y[0][n-1],sum,K[302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[i][j],nk=a;

	sum=-INF;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum<=X+Y)sum=X+Y,nk=k;

	}

	ans=max(ans,sum);

	

	if(nk<n-1)f=1;

	for(int k=a+1;k<nk;k++){//tate

	  sum+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum);

	}

	K[i][j]=nk+(nk==a+1);

      }

  }

  clock_t end=clock();

  //  cout << (double)(end-start)/CLOCKS_PER_SEC <<endl;

  cout <<ans<<endl;

	  

  return 0; 

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

using namespace std;



int main(){

  int n,mp[302][302],x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],mp[i][j]=x[i][j]=y[i][j];

  

  int ans=max(y[0][n-1],y[n-1][0]);

  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

      if(ans<x[i][j])ans=x[i][j];

      if(ans<y[j][i])ans=y[j][i];

    }





  int sum[302][302],K[2][302][302]={};





  for(int i=0;i<=n;i++)

    for(int j=0;j<=n;j++) sum[i][j]=-100000000;



  int t=0;

  while(1){

    t++;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[t%2][i][j];

	K[(t+1)%2][i][j]=a;

	sum[i][j]=-100000000;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum[i][j]<=X+Y)sum[i][j]=X+Y,K[(t+1)%2][i][j]=k;

	  ans=max(ans,sum[i][j]);

	}

      }



    int f=1;

    for(int i=0;i<n;i++) //yoko

      for(int j=i+1;j<n;j++) //yoko

	for(int k=K[t%2][i][j]+1;k<K[(t+1)%2][i][j];k++){

	  f=0;

	  int X=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  sum[i][j]+=X;

	  ans=max(ans,sum[i][j]);

	}

    if(f)break;

  }

  cout <<ans<<endl;

	  

  return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 1.1783970220143978x):
#include <iostream>

#include <time.h>

#define INF 10000000

using namespace std;



int main(){

  clock_t start=clock();

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];

  /*

  n=300;

  for(int i=0;i<n;i++)

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

  */



  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }

  

  int ans=y[0][n-1],sum,K[302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[i][j],nk=a;

	sum=-INF;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum<=X+Y)sum=X+Y,nk=k;

	}

	ans=max(ans,sum);

	

	if(nk<n-1)f=1;

	for(int k=a+1;k<nk;k++){//tate

	  sum+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum);

	}

	if(a+1==nk&&j-i!=1) nk=n-1;

	K[i][j]=nk;

      }

  }

  clock_t end=clock();

  //  cout << (double)(end-start)/CLOCKS_PER_SEC <<endl;

  cout <<ans<<endl;

	  

  return 0; 

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

using namespace std;



int main(){

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];

  

  int ans=max(y[0][n-1],y[n-1][0]);

  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

      ans=max(ans,max(y[j][i],x[i][j]));

    }



  int sum[302][302],K[2][302][302]={};

  for(int i=0;i<=n;i++)

    for(int j=0;j<=n;j++) sum[i][j]=-100000000;





  int t=0,f=1;

  while(f){

    t++,f=0;



    for(int i=0;i<n;i++) //yoko

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

	int a=K[t%2][i][j];

	K[(t+1)%2][i][j]=a;

	sum[i][j]=-100000000;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum[i][j]<=X+Y)sum[i][j]=X+Y,K[(t+1)%2][i][j]=k;

	  ans=max(ans,sum[i][j]);

	}

	if(K[t%2][i][j]!=n-1)f=1;

	for(int k=K[t%2][i][j]+1;k<K[(t+1)%2][i][j];k++){//tate

	  sum[i][j]+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum[i][j]);

	}

      }

  }

  cout <<ans<<endl;

	  

  return 0; 

}

// Faster C++ solution for the same problem (measured speedup: 1.1786009249755804x):
#include <iostream>

#include <time.h>

#define INF 10000000

using namespace std;



int main(){

  clock_t start=clock();

  int n,x[302][302]={},y[302][302]={};

  cin>>n;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)cin>>y[i][j],x[i][j]=y[i][j];

  /*

  n=300;

  for(int i=0;i<n;i++)

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

  */



  for(int i=0;i<n;i++)

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

      x[i][j]+=x[i][j+1];

      y[j][i]+=y[j+1][i];

    }

  

  int ans=y[0][n-1],sum,K[302][302]={},t=0,f=1;

  while(f){

    t++,f=0;

    for(int i=0;i<n;i++) //yoko

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

	int a=K[i][j],nk=a;

	sum=-INF;

	for(int k=a+1;k<n;k++) { //tate

	  int X=(x[a][i]-x[a][j+1])+(x[k][i]-x[k][j+1]);

	  int Y=(y[a+1][i]-y[k][i])+(y[a+1][j]-y[k][j]);

	  if(sum<=X+Y)sum=X+Y,nk=k;

	}

	ans=max(ans,sum);

	

	if(nk<n-1)f=1;

	for(int k=a+1;k<nk;k++){//tate

	  sum+=(x[k][i+1]-x[k][j])-(x[k-1][i]-x[k-1][j+1]);

	  ans=max(ans,sum);

	}

	if(a+1==nk&&j-i!=1) nk=n-1;

	K[i][j]=nk;

      }

  }

  clock_t end=clock();

  //  cout << (double)(end-start)/CLOCKS_PER_SEC <<endl;

  cout <<ans<<endl;

	  

  return 0; 

}