#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; int dp[310][310]; int maxsub(int n,int a[]) { int i,sum=0,b=0; for(i=1;i<=n;i++) { if(b>0) b+=a[i]; else b=a[i]; if(b>sum) sum=b; } return sum; } int main() { int b[310]; int n,i,k,j,sum,maxsum=0,t,m; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { scanf("%d",&dp[i][j]); } } maxsum=0; for(i=1;i<=n;i++) { memset(b,0,sizeof(b)); for(j=i;j<=n;j++) { for(k=1;k<=m;k++) { b[k]+=dp[j][k]; } sum=maxsub(m,b); if(sum>maxsum) maxsum=sum; } } printf("%d ",maxsum); } return 0; }
求从i到n的最优子矩阵的最优解