#include<stdio.h>
int findmin(int (*arr)[51],int rlow,int rhigh, int clow, int chigh)
{
if(rlow==rhigh||clow==chigh)
return 0;
int i,min,p=0,q=0,r=0,s=0;
for(i=clow;i<chigh;i++)
p=p+arr[rlow][i];
for(i=clow;i<chigh;i++)
q=q+arr[rhigh-1][i];
for(i=rlow;i<rhigh;i++)
r=r+arr[i][clow];
for(i=rlow;i<rhigh;i++)
s=s+arr[i][chigh-1];
min=p;
if(q<min)
min=q;
if(r<min)
min=r;
if(s<min)
min=s;
return(min);
}
int deletearr(int (*arr)[51],int rlow,int rhigh,int clow,int chigh,int a,int b,int atotal, int btotal)
{
if(clow==chigh||rlow==rhigh)
{
if(atotal>btotal)
return (atotal);
else if(btotal>atotal)
return(btotal);
else
return(atotal+btotal);
}
int p=0,q=0,r=0,s=0,i,min,max,temp,count,value;
char MAX;
if(a==1)
{
for(i=clow;i<chigh;i++)
p=p+arr[rlow][i];
for(i=clow;i<chigh;i++)
q=q+arr[rhigh-1][i];
for(i=rlow;i<rhigh;i++)
r=r+arr[i][clow];
for(i=rlow;i<rhigh;i++)
s=s+arr[i][chigh-1];
min=p;
if(q<min)
min=q;
if(r<min)
min=r;
if(s<min)
min=s;
if(min==p)
rlow=rlow+1;
else if(min==q)
rhigh=rhigh-1;
else if(min==r)
clow=clow+1;
else
chigh=chigh-1;
deletearr(arr,rlow,rhigh,clow,chigh,0,1,atotal+min,btotal);
}
else
{
for(i=clow;i<chigh;i++)
p=p+arr[rlow][i];
for(i=clow;i<chigh;i++)
q=q+arr[rhigh-1][i];
for(i=rlow;i<rhigh;i++)
r=r+arr[i][clow];
for(i=rlow;i<rhigh;i++)
s=s+arr[i][chigh-1];
max=p;
if(q>=max)
max=q;
if(r>=max)
max=r;
if(s>=max)
max=s;
count=0;
if(max==p)
{
temp=findmin(arr,rlow+1,rhigh,clow,chigh);
min=temp;MAX='p';count++;
}
if(max==q)
{
temp=findmin(arr,rlow,rhigh-1,clow,chigh);
if(count==0)
{min=temp;MAX='q';count++;}
else if(temp<min)
{min=temp;MAX='q';}
}
if(max==r)
{
temp=findmin(arr,rlow,rhigh,clow+1,chigh);
if(count==0)
{min=temp;MAX='r';}
else if(temp<min)
{min=temp;MAX='r';}
}
if(max==s)
{
temp=findmin(arr,rlow,rhigh,clow,chigh-1);
if(count==0)
{min=temp;MAX='s';}
else if(temp<min)
{min=temp;MAX='s';}
}
if(MAX=='p')
{rlow=rlow+1;value=p;}
else if(MAX=='q')
{rhigh=rhigh-1;value=q;}
else if(MAX=='r')
{clow=clow+1;value=r;}
else if(MAX=='s')
{chigh=chigh-1;value=s;}
deletearr(arr,rlow,rhigh,clow,chigh,1,0,atotal,btotal+value);
}
}
int main()
{
int t,i,j,k,r,c,arr[51][51],res;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d%d",&r,&c);
for(j=0;j<r;j++)
for(k=0;k<c;k++)
scanf("%d",&arr[j][k]);
res=deletearr(arr,0,r,0,c,1,0,0,0);
printf("%d\n",res);
}
}
I2luY2x1ZGU8c3RkaW8uaD4KCmludCBmaW5kbWluKGludCAoKmFycilbNTFdLGludCBybG93LGludCByaGlnaCwgaW50IGNsb3csIGludCBjaGlnaCkKewogICAgaWYocmxvdz09cmhpZ2h8fGNsb3c9PWNoaWdoKQogICAgcmV0dXJuIDA7CiAgICBpbnQgaSxtaW4scD0wLHE9MCxyPTAscz0wOwogICAgZm9yKGk9Y2xvdztpPGNoaWdoO2krKykKICAgICAgIHA9cCthcnJbcmxvd11baV07CiAgICAgICBmb3IoaT1jbG93O2k8Y2hpZ2g7aSsrKQogICAgICAgcT1xK2FycltyaGlnaC0xXVtpXTsKICAgICAgIGZvcihpPXJsb3c7aTxyaGlnaDtpKyspCiAgICAgICByPXIrYXJyW2ldW2Nsb3ddOwogICAgICAgZm9yKGk9cmxvdztpPHJoaWdoO2krKykKICAgICAgIHM9cythcnJbaV1bY2hpZ2gtMV07CgogICAgICAgbWluPXA7CiAgICAgICBpZihxPG1pbikKICAgICAgIG1pbj1xOwogICAgICAgaWYocjxtaW4pCiAgICAgICBtaW49cjsKICAgICAgIGlmKHM8bWluKQogICAgICAgbWluPXM7CgogICAgICAgcmV0dXJuKG1pbik7Cn0KCmludCBkZWxldGVhcnIoaW50ICgqYXJyKVs1MV0saW50IHJsb3csaW50IHJoaWdoLGludCBjbG93LGludCBjaGlnaCxpbnQgYSxpbnQgYixpbnQgYXRvdGFsLCBpbnQgYnRvdGFsKQp7CiAgIGlmKGNsb3c9PWNoaWdofHxybG93PT1yaGlnaCkKICAgewogICAgICBpZihhdG90YWw+YnRvdGFsKQogICAgICByZXR1cm4gKGF0b3RhbCk7CiAgICAgIGVsc2UgaWYoYnRvdGFsPmF0b3RhbCkKICAgICAgcmV0dXJuKGJ0b3RhbCk7CiAgICAgIGVsc2UKICAgICAgcmV0dXJuKGF0b3RhbCtidG90YWwpOwogICB9CiAgIGludCBwPTAscT0wLHI9MCxzPTAsaSxtaW4sbWF4LHRlbXAsY291bnQsdmFsdWU7CiAgIGNoYXIgTUFYOwogICBpZihhPT0xKQogICB7CiAgICAgICBmb3IoaT1jbG93O2k8Y2hpZ2g7aSsrKQogICAgICAgcD1wK2FycltybG93XVtpXTsKICAgICAgIGZvcihpPWNsb3c7aTxjaGlnaDtpKyspCiAgICAgICBxPXErYXJyW3JoaWdoLTFdW2ldOwogICAgICAgZm9yKGk9cmxvdztpPHJoaWdoO2krKykKICAgICAgIHI9cithcnJbaV1bY2xvd107CiAgICAgICBmb3IoaT1ybG93O2k8cmhpZ2g7aSsrKQogICAgICAgcz1zK2FycltpXVtjaGlnaC0xXTsKCiAgICAgICBtaW49cDsKICAgICAgIGlmKHE8bWluKQogICAgICAgbWluPXE7CiAgICAgICBpZihyPG1pbikKICAgICAgIG1pbj1yOwogICAgICAgaWYoczxtaW4pCiAgICAgICBtaW49czsKICAgICAgCiAgICAgICBpZihtaW49PXApCiAgICAgICBybG93PXJsb3crMTsKICAgICAgIGVsc2UgaWYobWluPT1xKQogICAgICAgcmhpZ2g9cmhpZ2gtMTsKICAgICAgIGVsc2UgaWYobWluPT1yKQogICAgICAgY2xvdz1jbG93KzE7CiAgICAgICBlbHNlCiAgICAgICBjaGlnaD1jaGlnaC0xOwoKICAgICAgIGRlbGV0ZWFycihhcnIscmxvdyxyaGlnaCxjbG93LGNoaWdoLDAsMSxhdG90YWwrbWluLGJ0b3RhbCk7CiAgICAgICAKICAgfQogICBlbHNlCiAgIHsKICAgICAgIGZvcihpPWNsb3c7aTxjaGlnaDtpKyspCiAgICAgICBwPXArYXJyW3Jsb3ddW2ldOwogICAgICAgZm9yKGk9Y2xvdztpPGNoaWdoO2krKykKICAgICAgIHE9cSthcnJbcmhpZ2gtMV1baV07CiAgICAgICBmb3IoaT1ybG93O2k8cmhpZ2g7aSsrKQogICAgICAgcj1yK2FycltpXVtjbG93XTsKICAgICAgIGZvcihpPXJsb3c7aTxyaGlnaDtpKyspCiAgICAgICBzPXMrYXJyW2ldW2NoaWdoLTFdOwoKICAgICAgIG1heD1wOwogICAgICAgaWYocT49bWF4KQogICAgICAgbWF4PXE7CiAgICAgICBpZihyPj1tYXgpCiAgICAgICBtYXg9cjsKICAgICAgIGlmKHM+PW1heCkKICAgICAgIG1heD1zOwogICAgICAKICAgICAgIGNvdW50PTA7CiAgICAgICBpZihtYXg9PXApCiAgICAgICB7CiAgICAgICAgICAgdGVtcD1maW5kbWluKGFycixybG93KzEscmhpZ2gsY2xvdyxjaGlnaCk7CiAgICAgICAgICAgbWluPXRlbXA7TUFYPSdwJztjb3VudCsrOwogICAgICAgfQogICAgICAgaWYobWF4PT1xKQogICAgICAgewogICAgICAgICAgIHRlbXA9ZmluZG1pbihhcnIscmxvdyxyaGlnaC0xLGNsb3csY2hpZ2gpOwogICAgICAgICAgIGlmKGNvdW50PT0wKQogICAgICAgICAgIHttaW49dGVtcDtNQVg9J3EnO2NvdW50Kys7fQogICAgICAgICAgIGVsc2UgaWYodGVtcDxtaW4pCiAgICAgICAgICAge21pbj10ZW1wO01BWD0ncSc7fQogICAgICAgfQogICAgICAgaWYobWF4PT1yKQogICAgICAgewogICAgICAgICAgdGVtcD1maW5kbWluKGFycixybG93LHJoaWdoLGNsb3crMSxjaGlnaCk7CiAgICAgICAgICAgaWYoY291bnQ9PTApCiAgICAgICAgICAge21pbj10ZW1wO01BWD0ncic7fQogICAgICAgICAgIGVsc2UgaWYodGVtcDxtaW4pCiAgICAgICAgICAge21pbj10ZW1wO01BWD0ncic7fSAKICAgICAgIH0KICAgICAgIGlmKG1heD09cykKICAgICAgIHsKICAgICAgICAgICB0ZW1wPWZpbmRtaW4oYXJyLHJsb3cscmhpZ2gsY2xvdyxjaGlnaC0xKTsKICAgICAgICAgICBpZihjb3VudD09MCkKICAgICAgICAgICB7bWluPXRlbXA7TUFYPSdzJzt9CiAgICAgICAgICAgZWxzZSBpZih0ZW1wPG1pbikKICAgICAgICAgICB7bWluPXRlbXA7TUFYPSdzJzt9CiAgICAgICB9CgogICAgICAgaWYoTUFYPT0ncCcpCiAgICAgICB7cmxvdz1ybG93KzE7dmFsdWU9cDt9CiAgICAgICBlbHNlIGlmKE1BWD09J3EnKQogICAgICAge3JoaWdoPXJoaWdoLTE7dmFsdWU9cTt9CiAgICAgICBlbHNlIGlmKE1BWD09J3InKQogICAgICAge2Nsb3c9Y2xvdysxO3ZhbHVlPXI7fQogICAgICAgZWxzZSBpZihNQVg9PSdzJykKICAgICAgIHtjaGlnaD1jaGlnaC0xO3ZhbHVlPXM7fQoKICAgICAgIGRlbGV0ZWFycihhcnIscmxvdyxyaGlnaCxjbG93LGNoaWdoLDEsMCxhdG90YWwsYnRvdGFsK3ZhbHVlKTsKICAgICAgIAogICB9Cn0KCmludCBtYWluKCkKewogICBpbnQgdCxpLGosayxyLGMsYXJyWzUxXVs1MV0scmVzOwogICBzY2FuZigiJWQiLCZ0KTsKICAgZm9yKGk9MDtpPHQ7aSsrKQogICB7CiAgICAgIHNjYW5mKCIlZCVkIiwmciwmYyk7CiAgICAgIGZvcihqPTA7ajxyO2orKykKICAgICAgZm9yKGs9MDtrPGM7aysrKQogICAgICBzY2FuZigiJWQiLCZhcnJbal1ba10pOwoKICAgICAgcmVzPWRlbGV0ZWFycihhcnIsMCxyLDAsYywxLDAsMCwwKTsKICAgICAgcHJpbnRmKCIlZFxuIixyZXMpOwogICB9Cn0=