#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);
   }
   return 0;
}