#include<iostream>
#include<stdio.h>
//#include<conio.h>
using namespace std;
int r1,r2,c1,c2,m,n;
long long int A[50][50],C[50],R[50],bob,alice;
void game_alice();
void game_bob();
void game_bob()
{
//cout<<"\nInside bob bob="<<bob<<" alice="<<alice;
int option,i;
if(r1>r2 || c1>c2)
return;
else
{
if(R[r1]>=R[r2] && R[r1]>=C[c1] && R[r1]>=C[c2])
option=1;
else if(R[r2]>R[r1] && R[r2]>=C[c1] && R[r2]>=C[c2])
option=2;
else if(C[c1]>R[r1] && C[c1]>R[r2] && C[c1]>=C[c2])
option=3;
else if(C[c2]>R[r1] && C[c2]>R[r2] && C[c2]>C[c2])
option=4;
}
if(option==1)
{
bob+=R[r1]; //cout<<"\nbob="<<bob<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
for(i=c1;i<=c2;++i)
C[i]-=A[r1][i];
++r1;
game_alice();
}
else if(option==2)
{
bob+=R[r2]; //cout<<"\nbob="<<bob<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
for(i=c1;i<=c2;++i)
C[i]-=A[r2][i];
--r2;
game_alice();
}
else if(option==3)
{
bob+=C[c1]; //cout<<"\nbob="<<bob<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
for(i=r1;i<=r2;++i)
R[i]-=A[i][c1];
++c1;
game_alice();
}
else if(option==4)
{
bob+=C[c2]; //cout<<"\nbob="<<bob<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
for(i=r1;i<=r2;++i)
R[i]-=A[i][c2];
--c2;
game_alice();
}
}
void game_alice()
{
//cout<<"\nInside alice bob="<<bob<<" alice="<<alice;
int option,i;
if(r1>r2 || c1>c2)
return;
else
{
if(R[r1]<=R[r2] && R[r1]<=C[c1] && R[r1]<=C[c2])
option=1;
else if(R[r2]<R[r1] && R[r2]<=C[c1] && R[r2]<=C[c2])
option=2;
else if(C[c1]<R[r1] && C[c1]<R[r2] && C[c1]<=C[c2])
option=3;
else if(C[c2]<R[r1] && C[c2]<R[r2] && C[c2]<C[c1])
option=4;
}
if(option==1)
{
alice+=R[r1]; //cout<<"\nalice="<<alice<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
for(i=c1;i<=c2;++i)
C[i]-=A[r1][i];
++r1;
game_bob();
}
else if(option==2)
{
alice+=R[r2]; //cout<<"\nalice="<<alice<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
for(i=c1;i<=c2;++i)
C[i]-=A[r2][i];
--r2;
game_bob();
}
else if(option==3)
{
alice+=C[c1]; //cout<<"\nalice="<<alice<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
for(i=r1;i<=r2;++i)
R[i]-=A[i][c1];
++c1;
game_bob();
}
else if(option==4)
{
alice+=C[c2]; //cout<<"\nalice="<<alice<<" : option="<<option;cout<<"\nr1="<<r1<<" r2="<<r2<<" c1="<<c1<<" c2="<<c2;getch();
for(i=r1;i<=r2;++i)
R[i]-=A[i][c2];
--c2;
game_bob();
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
//scanf("%d%d",&m,&n);
cin>>m>>n;
int i,j;
r1=0;r2=m-1;
c1=0;c2=n-1;
bob=0;alice=0;
for(i=0;i<50;++i)
R[i]=0;
for(j=0;j<50;++j)
C[j]=0;
for(i=0;i<m;++i)
for(j=0;j<n;++j)
{
cin>>A[i][j];
R[i]+=A[i][j];
C[j]+=A[i][j];
}
//cout<<"\nalice="<<alice<<"\nbob="<<bob;
game_alice();
//cout<<"\nalice="<<alice<<"\nbob="<<bob;
if(alice==bob)
cout<<alice+bob<<endl;
else if(alice>bob)
cout<<alice<<endl;
else cout<<bob<<endl;
}
}
I2luY2x1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