As long as this problem is selected i,j then either the horizontal, or the column is the i,j before the row all selected, cannot turn.
So each update corresponds to a length or width extension of 1 from an existing rectangle, then the state equation can be defined as Dp[i][j]=max (Dp[i-1][j]+row[i][j],dp[i][j-1]+col[i][j]).
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=500+10;
#define REP (i,a,b) for (int i= (a), i<= (b); i++)
#define RET (I,A,B) for (int i= (a); i>= (b); i--)
#define SS (x) scanf ("%d", &x)
int ROW[MAXN][MAXN],COL[MAXN][MAXN];
int n,m;
int F[MAXN][MAXN];
int main ()
{
while (true)
{
int x;
SS (N); SS (M); if (n+m==0) break;
Rep (I,1,n) Rep (j,1,m) {SS (x); row[i][j]=row[i][j-1]+x;}
Rep (I,1,n) Rep (j,1,m) {SS (x); col[i][j]=col[i-1][j]+x;}
Rep (i,1,n) {
Rep (j,1,m) {
F[i][j]=max (f[i][j-1]+col[i][j],f[i-1][j]+row[i][j]);
}
}
cout<<f[n][m]<<endl;
}
return 0;
}