1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 const int maxn=1010; 5 using namespace std; 6 int n,m; 7 int dp[maxn][maxn]; 8 int a[maxn][maxn]; 9 int b[maxn][maxn]; 10 int sum1[maxn][maxn]; 11 int sum2[maxn][maxn]; 12 int main() 13 { 14 cin>>n>>m; 15 while(m!=0||n!=0) 16 { 17 memset(a,0,sizeof(a)); 18 memset(b,0,sizeof(b)); 19 memset(sum1,0,sizeof(sum1)); 20 memset(sum2,0,sizeof(sum2)); 21 memset(dp,0,sizeof(dp)); 22 for(int i=1;i<=n;i++) 23 { 24 for(int j=1;j<=m;j++) 25 { 26 cin>>b[i][j]; 27 sum1[i][j]=sum1[i][j-1]+b[i][j]; 28 } 29 } 30 for(int i=1;i<=n;i++) 31 { 32 for(int j=1;j<=m;j++) 33 { 34 cin>>a[i][j]; 35 sum2[i][j]=sum2[i-1][j]+a[i][j]; 36 } 37 } 38 for(int i=n;i>=0;i--) 39 { 40 for(int j=m;j>=0;j--) 41 { 42 dp[i][j]=max(dp[i][j+1]+sum2[i][j+1],dp[i+1][j]+sum1[i+1][j]); 43 } 44 } 45 cout<<dp[0][0]<<endl; 46 cin>>n>>m; 47 } 48 return 0; 49 }