    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.util.*;

    public class d1 extends PrintWriter {

        static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
        // static Scanner s=new Scanner(System.in);
        d1 () { super(System.out); }
        public static void main(String[] args) throws IOException{
            d1 d1=new d1();d1.main();d1.flush();
        }
        void main() throws IOException {
            BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
            StringBuffer sb = new StringBuffer();
            StringBuffer sb1 = new StringBuffer();
            PrintWriter out = new PrintWriter(System.out);
           String[] s1 = s();
           int t=i(s1[0]);
          while(t-->0){
              String[] s2 = s();
               int n=i(s2[0]);
               int m=i(s2[1]);
               long[] [] a=new long[n][m];
               for(int i=0;i<n;i++){
                   String[] s3=s();
                   for(int j=0;j<m;j++){
                       a[i][j]=i(s3[j]);
                   }
               }long ans=0;
               for(int i=0;i<n/2;i++){
                   for(int j=0;j<m/2;j++){
                       long x=(a[i][j]+(a[n-1-i][j]+(a[i][m-1-j]+a[n-1-i][m-1-j])))/4;
//                       System.out.println(i+" "+j+" "+x);
                       ans+=(Math.abs(x-a[i][j])+Math.abs(x-a[n-1-i][j])+Math.abs(x-a[n-1-i][m-1-j])+Math.abs(x-a[i][m-1-j]));
                   }
               }
               if(n%2!=0){
                   int i=n/2;
                   for(int j=0;j<m/2;j++){
                       long x=(a[i][j]+((a[i][m-1-j])))/2;
//                       System.out.println(i+" "+j+" "+x);
                       ans+=(Math.abs(x-a[i][j])+Math.abs(x-x)+Math.abs(x-x)+Math.abs(x-a[i][m-1-j]));
                   }
               }
              if(m%2!=0){
                  int j=m/2;
                  for(int i=0;i<n/2;i++){
                      long x=(a[i][j]+((a[n-1-i][j])))/2;
//                      System.out.println(i+" "+j+" "+x);
                      ans+=(Math.abs(x-x)+Math.abs(x-a[i][j])+Math.abs(x-a[n-1-i][j])+Math.abs(x-x));
                  }
              }
              System.out.println(ans);
               //int[] a=new int[n];arr(a,n);
//              System.out.println(Math.min(a,Math.min(b,c)));
          }

            System.out.println(sb);
        }
        static long powerwithmod(long  x, long y, long p) {
            long  res = 1;
            x = x % p;
            while (y > 0) {
                if ((y & 1) == 1)
                    res = (res * x) % p;
                y = y >> 1;
                x = (x * x) % p;
            }
            return res;
        }
        public void arr(int[] a,int n) throws IOException{
    //        BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
            String[] s2=s();
            for(int i=0;i<n;i++){
                a[i]=i(s2[i]);
            }
        }
        public void sort(int[] a,int l,int h){
            if(l==h) return;
            int mid=(l+h)/2;

            sort(a,l,mid);
            sort(a,mid+1,h);
            merge(a,l,(l+h)/2,h);
        }
        void merge(int arr[], int l, int m, int r)
        {
            int n1 = m - l + 1;
            int n2 = r - m;
            int L[] = new int[n1];
            int R[] = new int[n2];
            for (int i = 0; i < n1; ++i)
                L[i] = arr[l + i];
            for (int j = 0; j < n2; ++j)
                R[j] = arr[m + 1 + j];
            int i = 0, j = 0;
            int k = l;
            while (i < n1 && j < n2) {
                if (L[i] <= R[j]) {
                    arr[k] = L[i];
                    i++;
                }
                else {
                    arr[k] = R[j];
                    j++;
                }
                k++;
            }

            while (i < n1) {
                arr[k] = L[i];
                i++;
                k++;
            }
            while (j < n2) {
                arr[k] = R[j];
                j++;
                k++;
            }
        }

        static long gcd(long a, long b) {
            if (b == 0)
                return a;
            return gcd(b, a % b);
        }
        static String[] s() throws IOException {
            return s.readLine().trim().split("\\s+");
        }

        static int i(String ss) {
            return Integer.parseInt(ss);
        }
        static long l(String ss) {
            return Long.parseLong(ss);
        }
    }
    class Student {
        long  a;int b;int c;
        public Student(int a,int b) {
            this.a=a;this.c=c;this.b=b;
        }
    }
    class Pair {
        int  a,b,c;
        public Pair(int  a,int b,int c){
            this.a=a;this.b=b;this.c=c;}
    }
    class Sortbyroll implements Comparator<Student> {

        public int compare(Student a, Student b){
          if(a.b==b.b) return (int)b.a-(int)a.a;
            return   a.b-b.b;}
    }


import java.util.Arrays;
import java.util.Scanner;

public class nice_matrix {

	public static long find_changes(int a, int b, int c, int d) {

		int[] arr= new int[4];

		arr[0] = a;
		arr[1] = b;
		arr[2] = c;
		arr[3] = d;

		Arrays.sort(arr);
		return (long)Math.abs(arr[3]+arr[2]-arr[1]-arr[0]);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner s =new Scanner(System.in);
		int t = s.nextInt();

		while(t-->0) {

			int n = s.nextInt();
			int m = s.nextInt();

			int[][] arr = new int[n][m];

			for(int i=0; i<n ;i++) {
				for(int j=0; j<m; j++) {
					arr[i][j] = s.nextInt();
				}
			}

			long ans =0;
			for(int i=0; i< n/2 ; i++) {
				for(int j=0; j< m/2 ; j++) {

					int a = arr[i][j];
					int b = arr[i][m-j-1];
					int c = arr[n-i-1][j];
					int d = arr[n-i-1][m-j-1];

					ans += find_changes(a, b, c, d);
				}
			}

			if(n%2==1) {

				int i = n/2;
				for(int j=0; j< m/2 ; j++) {
					ans += Math.abs(arr[i][j]-arr[i][m-j-1]);
				}
			}
			if(m%2==1) {

				int j = m/2;
				for(int i=0; i< n/2 ; i++) {
					ans += Math.abs(arr[i][j]-arr[n-i-1][j]);
				}
			}
			System.out.println(ans);
		}
	}
}


import java.util.Arrays;
import java.util.Scanner;

public class nice_matrix {

	public static long find_changes(int a, int b, int c, int d) {

		int[] arr= new int[4];

		arr[0] = a;
		arr[1] = b;
		arr[2] = c;
		arr[3] = d;

		Arrays.sort(arr);
		return (long)Math.abs(arr[3]+arr[2]-arr[1]-arr[0]);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner s =new Scanner(System.in);
		int t = s.nextInt();

		while(t-->0) {

			int n = s.nextInt();
			int m = s.nextInt();

			int[][] arr = new int[n][m];

			for(int i=0; i<n ;i++) {
				for(int j=0; j<m; j++) {
					arr[i][j] = s.nextInt();
				}
			}

			long ans =0;
			for(int i=0; i< n/2 ; i++) {
				for(int j=0; j< m/2 ; j++) {

					int a = arr[i][j];
					int b = arr[i][m-j-1];
					int c = arr[n-i-1][j];
					int d = arr[n-i-1][m-j-1];

					ans += find_changes(a, b, c, d);
				}
			}

			if(n%2==1) {

				int i = n/2;
				for(int j=0; j< m/2 ; j++) {
					ans += Math.abs(arr[i][j]-arr[i][m-j-1]);
				}
			}
			if(m%2==1) {

				int j = m/2;
				for(int i=0; j< n/2 ; i++) {
					ans += Math.abs(arr[i][j]-arr[n-i-1][j]);
				}
			}
			System.out.println(ans);
		}
	}
}


import java.util.*;
public class asdasdasaaa {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t>0) {
	int n=s.nextInt();
	int m=s.nextInt();
	long[][] a=new long[n+1][m+1];
	ArrayList<Long> um=new ArrayList<>();
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=m;j++) {
			a[i][j]=s.nextLong();
		}
	}
	long res=0;
	long[][] b=new long[n+1][m+1];
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=m;j++) {
			um.add(a[i][j]);
			um.add(a[i][m+1-j]);
			um.add(a[n-i+1][j]);
			um.add(a[n-i+1][m-j+1]);
		Collections.sort(um);
		b[i][j]=(um.get(1)+um.get(2))/2;
		um.clear();
		}
	}
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=m;j++) {
			res+=Math.abs(a[i][j]-b[i][j]);
		}
	}

	System.out.println(res);
t--;
}
	}

}


import java.util.*;
public class asdasdasaaa {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t>0) {
	int n=s.nextInt();
	int m=s.nextInt();
	ArrayList<Long> um=new ArrayList<>();
	for(int i=0;i<n*m;i++) {
	um.add(s.nextLong());
	}
	long count=0;
	Collections.sort(um);
	if(um.size()%2==0) {
		long m1=um.get(um.size()/2);
		long m2=um.get(um.size()/2)-1;
		long count1=0;
		long count2=0;
		for(int i=0;i<um.size();i++) {
			if(i!=m1) {
				count1+=Math.abs(um.get(i)-um.get((int) m1));
			}
		}
		for(int i=0;i<um.size();i++) {
			if(i!=m2) {
				count2+=Math.abs(um.get(i)-um.get((int) m2));
			}
		}
		count=Math.min(count1, count2);
	}else {
		for(int i=0;i<um.size();i++) {
			if(i!=um.size()/2) {
				count+=Math.abs(um.get(i)-um.get(um.size()/2));
			}
		}
	}

	System.out.println(count);
t--;
}
	}

}


import java.util.*;
public class asdasdasaaa {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t>0) {
	int n=s.nextInt();
	int m=s.nextInt();
	ArrayList<Long> um=new ArrayList<>();
	for(int i=0;i<n*m;i++) {
	um.add(s.nextLong());
	}
	long count=0;
	Collections.sort(um);
	if(um.size()%2==0) {
		long m1=(um.size()/2);
		long m2=(um.size()/2)-1;
		long count1=0;
		long count2=0;
		for(int i=0;i<um.size();i++) {
			if(i!=m1) {
				count1+=Math.abs(um.get(i)-um.get((int) m1));
			}
		}
		for(int i=0;i<um.size();i++) {
			if(i!=m2) {
				count2+=Math.abs(um.get(i)-um.get((int) m2));
			}
		}
		count=Math.min(count1, count2);
	}else {
		for(int i=0;i<um.size();i++) {
			if(i!=um.size()/2) {
				count+=Math.abs(um.get(i)-um.get(um.size()/2));
			}
		}
	}

	System.out.println(count);
t--;
}
	}

}


import java.util.*;
public class asdasdasaaa {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t>0) {
	long count=0;
	int n=s.nextInt();
	int m=s.nextInt();
	int[][] a=new int[n][m];
	for(int i=0;i<n;i++) {
		for(int j=0;j<m;j++) {
			a[i][j]=s.nextInt();
		}
	}
	if(m%2==0) {
		int k=n/2;
		for(int j=0;j<m;j++) {
			count+=Math.abs(a[k][m-j-1]-a[k][j]);
			a[k][m-j-1]=a[k][j];
		}
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				count+=Math.abs(a[i][j]-a[k][j]);
			}
		}
	}else {
		int k=n/2+1;
		for(int j=0;j<m;j++) {
			count+=Math.abs(a[k][m-j-1]-a[k][j]);
			a[k][m-j-1]=a[k][j];
		}
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				count+=Math.abs(a[i][j]-a[k][j]);
			}
		}

	}
	System.out.println(count);
t--;
}
	}

}


import java.util.*;
public class asdasdasaaa {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t>0) {
	int n=s.nextInt();
	int m=s.nextInt();
	ArrayList<Integer> um=new ArrayList<>();
	for(int i=0;i<n*m;i++) {
	um.add(s.nextInt());
	}
	long count=0;
	Collections.sort(um);
	if(um.size()%2==0) {
		int m1=um.get(um.size()/2);
		int m2=um.get(um.size()/2)-1;
		long count1=0;
		long count2=0;
		for(int i=0;i<um.size();i++) {
			if(i!=m1) {
				count1+=Math.abs(um.get(i)-um.get(m1));
			}
		}
		for(int i=0;i<um.size();i++) {
			if(i!=m2) {
				count2+=Math.abs(um.get(i)-um.get(m2));
			}
		}
		count=Math.min(count1, count2);
	}else {
		for(int i=0;i<um.size();i++) {
			if(i!=um.size()/2) {
				count+=Math.abs(um.get(i)-um.get(um.size()/2));
			}
		}
	}

	System.out.println(count);
t--;
}
	}

}


import java.util.*;
public class asdasdasaaa {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t>0) {
	int n=s.nextInt();
	int m=s.nextInt();
	ArrayList<Integer> um=new ArrayList<>();
	HashMap<Integer,Integer> um1=new HashMap<>();
	int max=Integer.MIN_VALUE;
	int max1=0;
	for(int i=0;i<n*m;i++) {
	int v=s.nextInt();
		um.add(v);
	if(!um1.containsKey(v)) {
		um1.put(v, 1);
	}else {
		um1.put(v, um1.get(v)+1);
	}
	if(um1.get(v)>=max) {
		max=um1.get(v);
		max1=v;
	}
	}
	long count=0;
	Collections.sort(um);
	for(int i=0;i<um.size();i++) {
			count+=Math.abs(um.get(i)-max1);

	}
	System.out.println(count);
t--;
}
	}

}


import java.util.*;
public class asdasdasaaa {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t>0) {
	int n=s.nextInt();
	int m=s.nextInt();
	ArrayList<Integer> um=new ArrayList<>();
	for(int i=0;i<n*m;i++) {
	um.add(s.nextInt());
	}
	long count=0;
	Collections.sort(um);
	for(int i=0;i<um.size();i++) {
		if(i!=um.size()/2) {
			count+=Math.abs(um.get(i)-um.get(um.size()/2));
		}
	}
	System.out.println(count);
t--;
}
	}

}


import java.util.*;
import java.io.*;

public final class Codechef {
	static Scanner sc=new Scanner(System.in);
	static FastReader re=new FastReader();
	static PrintWriter writer=new PrintWriter(System.out);

	public static void main(String[] args){
		int t=sc.nextInt();
		while(t-->0) {
			int n=sc.nextInt(), m=sc.nextInt();
			long[][] a=new long[n][m];
			for(int i=0;i<n;i++) {
				for(int j=0;j<m;j++) {
					a[i][j]=sc.nextLong();
				}
			}
			long ans=0;
			for(int j=m/2-1;j>=0;j--) {
				for(int i=n/2-1;i>=0;i--) {
					long avg=(long)Math.floor((a[i][j]+a[i][m-j-1]+a[n-i-1][j]+a[n-i-1][m-j-1])/4);
					ans+=Math.abs(avg-a[i][j]);
					ans+=Math.abs(avg-a[i][m-j-1]);
					ans+=Math.abs(avg-a[n-i-1][j]);
					ans+=Math.abs(avg-a[n-i-1][m-j-1]);
				}
			}
			if(n%2!=0) {
				for(int i=0;i<m/2;i++) {
					ans+=Math.abs(a[n/2][i]-a[n/2][m-i-1]);
				}
			}
			if(m%2!=0) {
				for(int i=0;i<n/2;i++) {
					ans+=Math.abs(a[i][m/2]-a[n-i-1][m/2]);
				}
			}
			System.out.println(ans);
		}
		writer.flush();
		writer.close();
  }

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}


import java.util.*;
import java.io.*;

public final class Codechef {
	static Scanner sc=new Scanner(System.in);
	static FastReader re=new FastReader();
	static PrintWriter writer=new PrintWriter(System.out);

	public static void main(String[] args){
		int t=sc.nextInt();
		while(t-->0) {
			int n=sc.nextInt(), m=sc.nextInt();
			long[][] a=new long[n][m];
			for(int i=0;i<n;i++) {
				for(int j=0;j<m;j++) {
					a[i][j]=sc.nextLong();
				}
			}
			long ans=0;
			for(int j=m/2-1;j>=0;j--) {
				for(int i=n/2-1;i>=0;i--) {
					long avg=(a[i][j]+a[i][m-j-1]+a[n-i-1][j]+a[n-i-1][m-j-1])/4;
					ans+=Math.abs(avg-a[i][j]);
					ans+=Math.abs(avg-a[i][m-j-1]);
					ans+=Math.abs(avg-a[n-i-1][j]);
					ans+=Math.abs(avg-a[n-i-1][m-j-1]);
				}
			}
			if(n%2!=0) {
				for(int i=0;i<m/2;i++) {
					ans+=Math.abs(a[n/2][i]-a[n/2][m-i-1]);
				}
			}
			if(m%2!=0) {
				for(int i=0;i<n/2;i++) {
					ans+=Math.abs(a[i][m/2]-a[n-i-1][m/2]);
				}
			}
			System.out.println(ans);
		}
		writer.flush();
		writer.close();
  }

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}


import java.util.*;
import java.io.*;

public final class Codechef {
	static Scanner sc=new Scanner(System.in);
	static FastReader re=new FastReader();
	static PrintWriter writer=new PrintWriter(System.out);

	public static void main(String[] args){
		int t=sc.nextInt();
		while(t-->0) {
			int n=sc.nextInt(), m=sc.nextInt();
			long[][] a=new long[n][m];
			for(int i=0;i<n;i++) {
				for(int j=0;j<m;j++) {
					a[i][j]=sc.nextLong();
				}
			}
			long ans=0, tmp=0;
			for(int j=m/2-1;j>=0;j--) {
				for(int i=n/2-1;i>=0;i--) {
					long avg=a[i][j];
					ans+=Math.abs(avg-a[i][j]);
					ans+=Math.abs(avg-a[i][m-j-1]);
					ans+=Math.abs(avg-a[n-i-1][j]);
					ans+=Math.abs(avg-a[n-i-1][m-j-1]);
				}
			}
			tmp=ans;
			ans=0;
			for(int j=m/2-1;j>=0;j--) {
				for(int i=n/2-1;i>=0;i--) {
					long avg=a[i][m-j-1];
					ans+=Math.abs(avg-a[i][j]);
					ans+=Math.abs(avg-a[i][m-j-1]);
					ans+=Math.abs(avg-a[n-i-1][j]);
					ans+=Math.abs(avg-a[n-i-1][m-j-1]);
				}
			}
			tmp=Math.min(tmp, ans);
			ans=0;
			for(int j=m/2-1;j>=0;j--) {
				for(int i=n/2-1;i>=0;i--) {
					long avg=a[n-i-1][j];
					ans+=Math.abs(avg-a[i][j]);
					ans+=Math.abs(avg-a[i][m-j-1]);
					ans+=Math.abs(avg-a[n-i-1][j]);
					ans+=Math.abs(avg-a[n-i-1][m-j-1]);
				}
			}
			tmp=Math.min(tmp, ans);
			ans=0;
			for(int j=m/2-1;j>=0;j--) {
				for(int i=n/2-1;i>=0;i--) {
					long avg=a[n-i-1][m-j-1];
					ans+=Math.abs(avg-a[i][j]);
					ans+=Math.abs(avg-a[i][m-j-1]);
					ans+=Math.abs(avg-a[n-i-1][j]);
					ans+=Math.abs(avg-a[n-i-1][m-j-1]);
				}
			}
			if(n%2!=0) {
				for(int i=0;i<m/2;i++) {
					tmp+=Math.abs(a[n/2][i]-a[n/2][m-i-1]);
				}
			}
			if(m%2!=0) {
				for(int i=0;i<n/2;i++) {
					tmp+=Math.abs(a[i][m/2]-a[n-i-1][m/2]);
				}
			}
			System.out.println(tmp);
		}
		writer.flush();
		writer.close();
  }

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}


import java.util.*;
import java.io.*;

public final class Codechef {
	static Scanner sc=new Scanner(System.in);
	static FastReader re=new FastReader();
	static PrintWriter writer=new PrintWriter(System.out);

	public static void main(String[] args){
		int t=sc.nextInt();
		while(t-->0) {
			int n=sc.nextInt(), m=sc.nextInt();
			long[][] a=new long[n][m];
			for(int i=0;i<n;i++) {
				for(int j=0;j<m;j++) {
					a[i][j]=sc.nextLong();
				}
			}
			long ans=0;
			for(int j=m/2-1;j>=0;j--) {
				for(int i=n/2-1;i>=0;i--) {
					long x=a[i][j], y=a[i][m-j-1], z=a[n-i-1][j], w=a[n-i-1][m-j-1];
					long min=x+y-Math.min(x, y), max=w+z-Math.max(w, z);
					long avg=Math.min(min, max);
					ans+=Math.abs(avg-a[i][j]);
					ans+=Math.abs(avg-a[i][m-j-1]);
					ans+=Math.abs(avg-a[n-i-1][j]);
					ans+=Math.abs(avg-a[n-i-1][m-j-1]);
				}
			}
			if(n%2!=0) {
				for(int i=0;i<m/2;i++) {
					ans+=Math.abs(a[n/2][i]-a[n/2][m-i-1]);
				}
			}
			if(m%2!=0) {
				for(int i=0;i<n/2;i++) {
					ans+=Math.abs(a[i][m/2]-a[n-i-1][m/2]);
				}
			}
			System.out.println(ans);
		}
		writer.flush();
		writer.close();
  }

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}


import java.util.*;
import java.io.*;

public final class Codechef {
	static Scanner sc=new Scanner(System.in);
	static FastReader re=new FastReader();
	static PrintWriter writer=new PrintWriter(System.out);

	public static void main(String[] args){
		int t=sc.nextInt();
		while(t-->0) {
			int n=sc.nextInt(), m=sc.nextInt();
			long[][] a=new long[n][m];
			for(int i=0;i<n;i++) {
				for(int j=0;j<m;j++) {
					a[i][j]=sc.nextLong();
				}
			}
			long ans=0;
			for(int j=m/2-1;j>=0;j--) {
				for(int i=n/2-1;i>=0;i--) {
					long avg=(a[i][j]+a[i][m-j-1]+a[n-i-1][j]+a[n-i-1][m-j-1])/4;
					ans+=Math.abs(avg-a[i][j]);
					ans+=Math.abs(avg-a[i][m-j-1]);
					ans+=Math.abs(avg-a[n-i-1][j]);
					ans+=Math.abs(avg-a[n-i-1][m-j-1]);
				}
			}
			if(n%2!=0) {
				for(int i=0;i<m/2;i++) {
					ans+=Math.abs(a[n/2][i]-a[n/2][m-i-1]);
				}
			}
			System.out.println(ans);
		}
		writer.flush();
		writer.close();
  }

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}


import java.util.*;
import java.io.*;

public final class Codechef {
	static Scanner sc=new Scanner(System.in);
	static FastReader re=new FastReader();
	static PrintWriter writer=new PrintWriter(System.out);

	public static void main(String[] args){
		int t=sc.nextInt();
		while(t-->0) {
			int n=sc.nextInt(), m=sc.nextInt();
			long[][] a=new long[n][m];
			for(int i=0;i<n;i++) {
				for(int j=0;j<m;j++) {
					a[i][j]=sc.nextLong();
				}
			}
			long ans=0;
			for(int j=m/2-1;j>=0;j--) {
				long tmp=0, temp=0, avg=0;
				for(int i=n/2-1;i>=0;i--) {
					avg=a[i][j];
					tmp+=Math.abs(avg-a[i][j]);
					tmp+=Math.abs(avg-a[i][m-j-1]);
					tmp+=Math.abs(avg-a[n-i-1][j]);
					tmp+=Math.abs(avg-a[n-i-1][m-j-1]);
					temp=tmp;
					tmp=0;

					avg=a[i][m-j-1];
					tmp+=Math.abs(avg-a[i][j]);
					tmp+=Math.abs(avg-a[i][m-j-1]);
					tmp+=Math.abs(avg-a[n-i-1][j]);
					tmp+=Math.abs(avg-a[n-i-1][m-j-1]);
					temp=Math.min(temp, tmp);
					tmp=0;

					avg=a[n-i-1][m-j-1];
					tmp+=Math.abs(avg-a[i][j]);
					tmp+=Math.abs(avg-a[i][m-j-1]);
					tmp+=Math.abs(avg-a[n-i-1][j]);
					tmp+=Math.abs(avg-a[n-i-1][m-j-1]);
					temp=Math.min(temp, tmp);
					tmp=0;

					avg=a[n-i-1][j];
					tmp+=Math.abs(avg-a[i][j]);
					tmp+=Math.abs(avg-a[i][m-j-1]);
					tmp+=Math.abs(avg-a[n-i-1][j]);
					tmp+=Math.abs(avg-a[n-i-1][m-j-1]);
					temp=Math.min(temp, tmp);

					ans+=temp;
				}
			}
			if(n%2!=0) {
				for(int i=0;i<m/2;i++) {
					ans+=Math.abs(a[n/2][i]-a[n/2][m-i-1]);
				}
			}
			if(m%2!=0) {
				for(int i=0;i<n/2;i++) {
					ans+=Math.abs(a[i][m/2]-a[n-i-1][m/2]);
				}
			}
			System.out.println(ans);
		}
		writer.flush();
		writer.close();
  }

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

}


import java.io.*;
import java.util.*;

public class A
{
    static int n, m;
    static int[][] arr;
    static char[] s;
    public static void main(String[] args) throws IOException
    {
        Flash f = new Flash();
        int T = f.ni();
        for(int tc = 1; tc <= T; tc++){
            n = f.ni(); m = f.ni();
            arr = new int[n][m];
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    arr[i][j] = f.ni();
                }
            }

            System.out.println(fn());
        }
    }

    static int fn()
    {
        int ans = 0;
        for(int i = 0; i < n/2; i++){
            for(int j = 0; j < m/2; j++){
                int v1 = arr[i][j], v2 = arr[n-i-1][j], v3 = arr[i][m-j-1], v4 = arr[n-i-1][m-j-1];
                List<Integer> A = new ArrayList<>();
                A.add(v1); A.add(v2); A.add(v3); A.add(v4);
                int med = findmed(A);
                ans += Math.abs(v1-med);
                ans += Math.abs(v2-med);
                ans += Math.abs(v3-med);
                ans += Math.abs(v4-med);
            }
        }

        if((n&1) == 1){
            for(int j = 0; j < m/2; j++){
                int v1 = arr[n/2][j], v2 = arr[n/2][m-j-1];
                int med = (v1+v2)/2;
                ans += Math.abs(v1-med);
                ans += Math.abs(v2-med);
            }
        }

        if((m&1) == 1){
            for(int i = 0; i < n/2; i++){
                int v1 = arr[i][m/2], v2 = arr[n-i-1][m/2];
                int med = (v1+v2)/2;
                ans += Math.abs(v1-med);
                ans += Math.abs(v2-med);
            }
        }

        return ans;
    }

    static int findmed(List<Integer> A){
        Collections.sort(A);
        if(A.size() % 2 == 1) return A.get(A.size()/2);
        else return (A.get(A.size()/2-1) + A.get(A.size()/2) ) / 2;
    }

    static void sort(int[] a){
        List<Integer> A = new ArrayList<>();
        for(int i : a) A.add(i);
        Collections.sort(A);
        for(int i = 0; i < A.size(); i++) a[i] = A.get(i);
    }

    static int swap(int itself, int dummy){
        return itself;
    }

    static void print(int[] a){
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < a.length; i++) sb.append(a[i] + " ");
        System.out.println(sb);
    }

    static class Flash
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        String next(){
            while(!st.hasMoreTokens()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch(IOException e){
                    e.printStackTrace();
                }
            }

            return st.nextToken();
        }

        String nextLine(){
            String s = new String();
            try{
                s = br.readLine().trim();
            }catch(IOException e){
                e.printStackTrace();
            }

            return s;
        }

        int ni(){
            return Integer.parseInt(next());
        }

        int[] arr(int n){
            int[] a = new int[n];
            for(int i = 0; i < n; i++) a[i] = ni();
            return a;
        }

        long nl(){
            return Long.parseLong(next());
        }
    }
}


import java.io.*;
import java.util.*;

public class A
{
    static int n, m;
    static int[][] arr;
    static char[] s;
    public static void main(String[] args) throws IOException
    {
        Flash f = new Flash();
        int T = f.ni();
        for(int tc = 1; tc <= T; tc++){
            n = f.ni(); m = f.ni();
            arr = new int[n][m];
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    arr[i][j] = f.ni();
                }
            }

            System.out.println(fn());
        }
    }

    static long fn()
    {
        long ans = 0;
        for(int i = 0; i < n/2; i++){
            for(int j = 0; j < m/2; j++){
                int v1 = arr[i][j], v2 = arr[n-i-1][j], v3 = arr[i][m-j-1], v4 = arr[n-i-1][m-j-1];
                List<Integer> A = new ArrayList<>();
                A.add(v1); A.add(v2); A.add(v3); A.add(v4);
                int med = findmed(A);
                ans += Math.abs(v1-med);
                ans += Math.abs(v2-med);
                ans += Math.abs(v3-med);
                ans += Math.abs(v4-med);
            }
        }

        if((n&1) == 1){
            for(int j = 0; j < m/2; j++){
                int v1 = arr[n/2][j], v2 = arr[n/2][m-j-1];
                int med = (v1+v2)/2;
                ans += Math.abs(v1-med);
                ans += Math.abs(v2-med);
            }
        }

        if((m&1) == 1){
            for(int i = 0; i < n/2; i++){
                int v1 = arr[i][m/2], v2 = arr[n-i-1][m/2];
                int med = (v1+v2)/2;
                ans += Math.abs(v1-med);
                ans += Math.abs(v2-med);
            }
        }

        return ans;
    }

    static int findmed(List<Integer> A){
        Collections.sort(A);
        if(A.size() % 2 == 1) return A.get(A.size()/2);
        else return (A.get(A.size()/2-1) + A.get(A.size()/2) ) / 2;
    }

    static void sort(int[] a){
        List<Integer> A = new ArrayList<>();
        for(int i : a) A.add(i);
        Collections.sort(A);
        for(int i = 0; i < A.size(); i++) a[i] = A.get(i);
    }

    static int swap(int itself, int dummy){
        return itself;
    }

    static void print(int[] a){
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < a.length; i++) sb.append(a[i] + " ");
        System.out.println(sb);
    }

    static class Flash
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        String next(){
            while(!st.hasMoreTokens()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch(IOException e){
                    e.printStackTrace();
                }
            }

            return st.nextToken();
        }

        String nextLine(){
            String s = new String();
            try{
                s = br.readLine().trim();
            }catch(IOException e){
                e.printStackTrace();
            }

            return s;
        }

        int ni(){
            return Integer.parseInt(next());
        }

        int[] arr(int n){
            int[] a = new int[n];
            for(int i = 0; i < n; i++) a[i] = ni();
            return a;
        }

        long nl(){
            return Long.parseLong(next());
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public final class B {

    public static void main(String[] args) {
        final FastScanner fs = new FastScanner();
        final int t = fs.nextInt();
        for (int test = 0; test < t; test++) {
            final int n = fs.nextInt();
            final int m = fs.nextInt();
            final int[] arr = new int[n * m];
            Arrays.sort(arr);
            for (int i = 0; i < n * m; i++) {
                arr[i] = fs.nextInt();
            }
            final long curr = arr[n * m / 2];
            long res = 0;
            for (int i = 0; i < n * m; i++) {
                res += Math.abs(curr - arr[i]);
            }
            System.out.println(res);
        }
    }

    static final class Utils {
        public static void shuffleSort(int[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffleSort(long[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffle(int[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void shuffle(long[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void swap(int[] x, int i, int j) {
            final int t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        public static void swap(long[] x, int i, int j) {
            final long t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        private Utils() {}
    }

    static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        private String next() {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    //noinspection CallToPrintStackTrace
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int[] nextIntArray(int n) {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++) { a[i] = nextInt(); }
            return a;
        }

        long[] nextLongArray(int n) {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++) { a[i] = nextLong(); }
            return a;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public final class B {

    public static void main(String[] args) {
        final FastScanner fs = new FastScanner();
        final int t = fs.nextInt();
        for (int test = 0; test < t; test++) {
            final int n = fs.nextInt();
            final int m = fs.nextInt();
            final int[][] grid = new int[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    grid[i][j] = fs.nextInt();
                }
            }
            long res = 0;
            for (int i = 0, upR = 0, botR = n - 1; i < n / 2; i++, upR++, botR--) {
                for (int j = 0, lC = 0, rC = m - 1; j < m / 2; j++, lC++, rC--) {
                    final int[] curr = new int[4];
                    curr[0] = grid[upR][lC];
                    curr[1] = grid[upR][rC];
                    curr[2] = grid[botR][lC];
                    curr[3] = grid[botR][rC];
                    res += getRes(curr);
                }
            }
            if (n % 2 != 0) {
                for (int i = 0, j = m - 1; i < j; i++, j--) {
                    res += Math.abs(grid[n / 2][i] - grid[n / 2][j]);
                }
            }
            if (m % 2 != 0) {
                for (int i = 0, j = n - 1; i < j; i++, j--) {
                    res += Math.abs(grid[i][m / 2] - grid[j][m / 2]);
                }
            }
            System.out.println(res);
        }
    }

    private static long getRes(int[] arr) {
        Arrays.sort(arr);
        final long curr = arr[arr.length / 2];
        long res = 0;
        for (int value : arr) {
            res += Math.abs(curr - value);
        }
        return res;
    }

    static final class Utils {
        public static void shuffleSort(int[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffleSort(long[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffle(int[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void shuffle(long[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void swap(int[] x, int i, int j) {
            final int t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        public static void swap(long[] x, int i, int j) {
            final long t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        private Utils() {}
    }

    static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        private String next() {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    //noinspection CallToPrintStackTrace
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int[] nextIntArray(int n) {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++) { a[i] = nextInt(); }
            return a;
        }

        long[] nextLongArray(int n) {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++) { a[i] = nextLong(); }
            return a;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public final class B {

    public static void main(String[] args) {
        final FastScanner fs = new FastScanner();
        final int t = fs.nextInt();
        for (int test = 0; test < t; test++) {
            final int n = fs.nextInt();
            final int m = fs.nextInt();
            if (n == 1) {
                final int[] arr = fs.nextIntArray(m);
                System.out.println(getRes(arr));
            } else if (m == 1) {
                final int[] arr = fs.nextIntArray(n);
                System.out.println(getRes(arr));
            } else {
                final int[][] grid = new int[n][m];
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < m; j++) {
                        grid[i][j] = fs.nextInt();
                    }
                }
                long res = 0;
                for (int i = 0, upR = 0, botR = n - 1; i < n / 2; i++, upR++, botR--) {
                    for (int j = 0, lC = 0, rC = m - 1; j < m / 2; j++, lC++, rC--) {
                        final int[] curr = new int[4];
                        curr[0] = grid[upR][lC];
                        curr[1] = grid[upR][rC];
                        curr[2] = grid[botR][lC];
                        curr[3] = grid[botR][rC];
                        res += getRes(curr);
                    }
                }
                if (n % 2 != 0) {
                    res += getRes(grid[n / 2]);
                    final int median = grid[n / 2][m / 2];
                    for (int i = 0; i < m; i++) {
                        grid[n / 2][i] = median;
                    }
                }
                if (m % 2 != 0) {
                    final int[] columnRow = new int[n];
                    for (int i = 0; i < n; i++) {
                        columnRow[i] = grid[i][m / 2];
                    }
                    res += getRes(columnRow);
                }
                System.out.println(res);
            }

        }
    }

    private static long getRes(int[] arr) {
        Arrays.sort(arr);
        final long curr = arr[arr.length / 2];
        long res = 0;
        for (int value : arr) {
            res += Math.abs(curr - value);
        }
        return res;
    }

    static final class Utils {
        public static void shuffleSort(int[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffleSort(long[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffle(int[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void shuffle(long[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void swap(int[] x, int i, int j) {
            final int t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        public static void swap(long[] x, int i, int j) {
            final long t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        private Utils() {}
    }

    static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        private String next() {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    //noinspection CallToPrintStackTrace
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int[] nextIntArray(int n) {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++) { a[i] = nextInt(); }
            return a;
        }

        long[] nextLongArray(int n) {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++) { a[i] = nextLong(); }
            return a;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public final class B {

    public static void main(String[] args) {
        final FastScanner fs = new FastScanner();
        final int t = fs.nextInt();
        for (int test = 0; test < t; test++) {
            final int n = fs.nextInt();
            final int m = fs.nextInt();
            final int[] arr = new int[n * m];
            for (int i = 0; i < n * m; i++) {
                arr[i] = fs.nextInt();
            }
            Arrays.sort(arr);
            final long curr = arr[n * m / 2];
            long res = 0;
            for (int i = 0; i < n * m; i++) {
                res += Math.abs(curr - arr[i]);
            }
            System.out.println(res);
        }
    }

    static final class Utils {
        public static void shuffleSort(int[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffleSort(long[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffle(int[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void shuffle(long[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void swap(int[] x, int i, int j) {
            final int t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        public static void swap(long[] x, int i, int j) {
            final long t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        private Utils() {}
    }

    static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        private String next() {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    //noinspection CallToPrintStackTrace
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int[] nextIntArray(int n) {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++) { a[i] = nextInt(); }
            return a;
        }

        long[] nextLongArray(int n) {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++) { a[i] = nextLong(); }
            return a;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public final class B {

    public static void main(String[] args) {
        final FastScanner fs = new FastScanner();
        final int t = fs.nextInt();
        for (int test = 0; test < t; test++) {
            final int n = fs.nextInt();
            final int m = fs.nextInt();
            final int[] arr = new int[n * m];
            for (int i = 0; i < n * m; i++) {
                arr[i] = fs.nextInt();
            }
            long res = (long) 1e9;
            for (int i = 0; i < n * m; i++) {
                final int mid = arr[i];
                long curr = 0;
                for (int num : arr) {
                    curr += Math.abs(num - mid);
                }
                res = Math.min(res, curr);
            }
            System.out.println(res);
        }
    }

    static final class Utils {
        public static void shuffleSort(int[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffleSort(long[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffle(int[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void shuffle(long[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void swap(int[] x, int i, int j) {
            final int t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        public static void swap(long[] x, int i, int j) {
            final long t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        private Utils() {}
    }

    static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        private String next() {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    //noinspection CallToPrintStackTrace
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int[] nextIntArray(int n) {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++) { a[i] = nextInt(); }
            return a;
        }

        long[] nextLongArray(int n) {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++) { a[i] = nextLong(); }
            return a;
        }
    }
}


import java.io.*;
import java.util.*;

public class B {
    static class Scan {
        BufferedReader br;
        StringTokenizer st;

        public Scan() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public Scan(String f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (Exception ignored) {
            }
        }

        public String next() {
            if (st == null || !st.hasMoreElements())
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception ignored) {
                }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            try {
                return br.readLine();
            } catch (Exception ignored) {
                return "";
            }
        }
    }
    public static void main(String[] args) {
        Scan scan = new Scan();
        int Tee = scan.nextInt();
        for(int tee = 0; tee < Tee; tee++){
            int n = scan.nextInt();
            int m = scan.nextInt();
            int[][] arr = new int[n][m];
            int[][] nice = new int[n][m];
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++)
                    arr[i][j] = scan.nextInt();
            }
            long out = 0;
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    int[] pal = new int[] {arr[i][j],arr[n-1-i][j],arr[i][m-1-j],arr[n-1-i][m-1-j]};
                    Arrays.sort(pal);
                    nice[i][j] = (pal[2]+pal[1])/2;
                }
            }
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    out+=Math.abs(nice[i][j]-arr[i][j]);
                }
            }
            System.out.println(out);
        }
    }
}


import java.io.*;
import java.util.*;

public class B {
    static class Scan {
        BufferedReader br;
        StringTokenizer st;

        public Scan() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public Scan(String f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (Exception ignored) {
            }
        }

        public String next() {
            if (st == null || !st.hasMoreElements())
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception ignored) {
                }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            try {
                return br.readLine();
            } catch (Exception ignored) {
                return "";
            }
        }
    }
    public static void main(String[] args) {
        Scan scan = new Scan();
        int Tee = scan.nextInt();
        for(int tee = 0; tee < Tee; tee++){
            int n = scan.nextInt();
            int m = scan.nextInt();
            int[][] arr = new int[n][m];
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++)
                    arr[i][j] = scan.nextInt();
            }
            int out = 0;
            for(int i = 0; i < n/2; i++){
                for(int j = 0; j < m/2; j++){
                    int[] pal = new int[] {arr[i][j],arr[n-1-i][j],arr[i][m-1-j],arr[n-1-i][m-1-j]};
                    Arrays.sort(pal);
                    int med = (pal[2]+pal[1])/2;
                    for(int k : pal){
                        out+=Math.abs(k-med);
                    }
                }
            }
            if(n%2==1){
                for(int i = 0; i < m/2; i++){
                    out+=Math.abs(arr[n/2][i]-arr[n/2][m-i-1]);
                }
            }
            if(m%2==1) {
                for(int i = 0; i < n/2; i++){
                    out+=Math.abs(arr[i][m/2]-arr[n-1-i][m/2]);
                }
            }
            System.out.println(out);
        }
    }
}


import java.io.*;
import java.util.*;

public class B{

    static long f(long x, ArrayList<Integer> arr){
        long res=0;
        for(int ele: arr) res+=Math.abs(x-ele);
        return res;
    }
    static long changeToPalindrome(ArrayList<Integer> arr){
        long lo = -1, hi = 1000;
        while (hi - lo > 1){
            long mid = (hi + lo)>>1;
            if (f(mid, arr) > f(mid + 1, arr))
                 lo = mid;
            else
                 hi = mid;
        }

        return f(hi+1, arr);
    }
    public static void main(String[] args){
        FastReader sc=new FastReader();
        int t=sc.nextInt();
        while(t-->0){
            int n=sc.nextInt(), m=sc.nextInt();
            int[][] mat=new int[n][m];
            boolean[][] done=new boolean[n][m];
            for(int i=0; i<n; ++i)
                for(int j=0; j<m; ++j)
                    mat[i][j]=sc.nextInt();
            long ops=0;
            for(int i=0; i<n; ++i){
                for(int j=0; j<m; ++j){
                    if(!done[i][j]){
                        ArrayList<Integer> arr=new ArrayList<>();
                        arr.add(mat[i][j]);
                        int rowPal=n-i-1;
                        int colPal=m-j-1;
                        if(rowPal!=i) arr.add(mat[rowPal][j]);
                        if(colPal!=j) arr.add(mat[i][colPal]);
                        if(rowPal!=i && colPal!=j) arr.add(mat[rowPal][colPal]);
                        ops+=changeToPalindrome(arr);
                        done[i][j]=true;
                        done[rowPal][j]=true;
                        done[i][colPal]=true;
                        done[rowPal][colPal]=true;
                    }
                }
            }
            System.out.println(ops);
        }
    }

    static class FastReader{
            BufferedReader br; StringTokenizer st;
            public FastReader(){
                br = new BufferedReader(new InputStreamReader(System.in));
            }
            String next(){
                while (st == null || !st.hasMoreElements()) {
                    try {
                        st = new StringTokenizer(br.readLine());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            }
            int nextInt() {
                return Integer.parseInt(next());
            }
            long nextLong() {
                return Long.parseLong(next());
            }
            double nextDouble() {
                return Double.parseDouble(next());
            }
            String nextLine() {
                String str = "";
                try {
                    str = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return str;
            }
        }
}


import java.io.*;
import java.util.*;

public class B{

    static long f(long x, ArrayList<Integer> arr){
        long res=0;
        for(int ele: arr) res+=Math.abs(x-ele);
        return -res;
    }
    static long changeToPalindrome(ArrayList<Integer> arr){
        long lo = -1, hi = 1000;
        while (hi - lo > 1){
            long mid = (hi + lo)>>1;
            if (f(mid, arr) > f(mid + 1, arr))
                 hi = mid;
            else
                 lo = mid;
        }

        return f(lo+1, arr);
    }
    public static void main(String[] args){
        FastReader sc=new FastReader();
        int t=sc.nextInt();
        while(t-->0){
            int n=sc.nextInt(), m=sc.nextInt();
            int[][] mat=new int[n][m];
            boolean[][] done=new boolean[n][m];
            for(int i=0; i<n; ++i)
                for(int j=0; j<m; ++j)
                    mat[i][j]=sc.nextInt();
            long ops=0;
            for(int i=0; i<n; ++i){
                for(int j=0; j<m; ++j){
                    if(!done[i][j]){
                        ArrayList<Integer> arr=new ArrayList<>();
                        arr.add(mat[i][j]);
                        int rowPal=n-i-1;
                        int colPal=m-j-1;
                        if(rowPal!=i) arr.add(mat[rowPal][j]);
                        if(colPal!=j) arr.add(mat[i][colPal]);
                        if(rowPal!=i && colPal!=j) arr.add(mat[rowPal][colPal]);
                        ops+=changeToPalindrome(arr);
                        done[i][j]=true;
                        done[rowPal][j]=true;
                        done[i][colPal]=true;
                        done[rowPal][colPal]=true;
                    }
                }
            }
            System.out.println(-ops);
        }
    }

    static class FastReader{
            BufferedReader br; StringTokenizer st;
            public FastReader(){
                br = new BufferedReader(new InputStreamReader(System.in));
            }
            String next(){
                while (st == null || !st.hasMoreElements()) {
                    try {
                        st = new StringTokenizer(br.readLine());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            }
            int nextInt() {
                return Integer.parseInt(next());
            }
            long nextLong() {
                return Long.parseLong(next());
            }
            double nextDouble() {
                return Double.parseDouble(next());
            }
            String nextLine() {
                String str = "";
                try {
                    str = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return str;
            }
        }
}


import java.io.*;
import java.util.*;

public class B{

    static long f(long x, ArrayList<Integer> arr){
        long res=0;
        for(int ele: arr) res+=Math.abs(x-ele);
        return -res;
    }
    static long changeToPalindrome(ArrayList<Integer> arr){
        long lo = -1, hi = (int)1e9+500;
        while (hi - lo > 1){
            long mid = (hi + lo)>>1;
            if (f(mid, arr) > f(mid + 1, arr))
                 hi = mid;
            else
                 lo = mid;
        }

        return f(lo+1, arr);
    }
    public static void main(String[] args){
        FastReader sc=new FastReader();
        int t=sc.nextInt();
        while(t-->0){
            int n=sc.nextInt(), m=sc.nextInt();
            int[][] mat=new int[n][m];
            boolean[][] done=new boolean[n][m];
            for(int i=0; i<n; ++i)
                for(int j=0; j<m; ++j)
                    mat[i][j]=sc.nextInt();
            long ops=0;
            for(int i=0; i<n; ++i){
                for(int j=0; j<m; ++j){
                    if(!done[i][j]){
                        ArrayList<Integer> arr=new ArrayList<>();
                        arr.add(mat[i][j]);
                        int rowPal=n-i-1;
                        int colPal=m-j-1;
                        if(rowPal!=i) arr.add(mat[rowPal][j]);
                        if(colPal!=j) arr.add(mat[i][colPal]);
                        if(rowPal!=i && colPal!=j) arr.add(mat[rowPal][colPal]);
                        ops+=changeToPalindrome(arr);
                        done[i][j]=true;
                        done[rowPal][j]=true;
                        done[i][colPal]=true;
                        done[rowPal][colPal]=true;
                    }
                }
            }
            System.out.println(-ops);
        }
    }

    static class FastReader{
            BufferedReader br; StringTokenizer st;
            public FastReader(){
                br = new BufferedReader(new InputStreamReader(System.in));
            }
            String next(){
                while (st == null || !st.hasMoreElements()) {
                    try {
                        st = new StringTokenizer(br.readLine());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            }
            int nextInt() {
                return Integer.parseInt(next());
            }
            long nextLong() {
                return Long.parseLong(next());
            }
            double nextDouble() {
                return Double.parseDouble(next());
            }
            String nextLine() {
                String str = "";
                try {
                    str = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return str;
            }
        }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;

public class NiceMatrix {
    class Pair implements Comparable<Pair> {
        public int a;
        public int b;

        public Pair() {
            this.a = 0;
            this.b = 0;
        }

        public Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public int compareTo(Pair p) {
            if (this.a == p.a) {
                return this.b - p.b;
            }
            return this.a - p.a;
        }

        @Override
        public String toString() {
            return "a = " + this.a + " b = " + this.b;

        }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readArray(int n, int size) {
            int[] a = new int[size];
            for (int i = 0; i < n; i++) {
                a[i] = this.nextInt();
            }

            return a;
        }

        long[] readLongArray(int n, int size) {
            long[] a = new long[size];
            for (int i = 0; i < n; i++) {
                a[i] = this.nextLong();
            }
            return a;
        }
    }

    static void ruffleSort(long[] a) {
        int n = a.length;//shuffle, then sort
        for (int i = 0; i < n; i++) {
            int oi = random.nextInt(n);
            long temp = a[oi];
            a[oi] = a[i];
            a[i] = temp;
        }
        Arrays.sort(a);
    }

    static final Random random = new Random();

    static void ruffleSort(int[] a) {
        int n = a.length;//shuffle, then sort
        for (int i = 0; i < n; i++) {
            int oi = random.nextInt(n), temp = a[oi];
            a[oi] = a[i];
            a[i] = temp;
        }
        Arrays.sort(a);
    }

    public static long process(long[][] mat, int n, int m, int topRow, int bottomRow) {
        int lc = 0;
        int rc = m - 1;
        long result = 0;
        while(lc <= rc) {
            long sum = 0;
            sum += mat[topRow][lc] + mat[bottomRow][lc] + ((lc != rc && topRow != bottomRow) ? mat[bottomRow][rc] + mat[topRow][rc] : 0);
            long req = (sum / ((lc != rc && topRow != bottomRow) ? 4 : 2));
            result += Math.abs(mat[topRow][lc] - req);
            if(lc != rc && topRow != bottomRow) result += Math.abs(mat[topRow][rc] - req);
            if(lc != rc && topRow != bottomRow) result += Math.abs(mat[bottomRow][lc] - req);
            result += Math.abs(mat[bottomRow][rc] - req);
            //System.out.print("LC: " + lc + " rc: "  +  rc + "  topRow: " + topRow + " " + bottomRow + "");
            lc++;
            rc--;
        }

        return result;
    }
    public static void main(String[] args) {
        FastReader fs = new FastReader();
        int t = fs.nextInt();

        for (int z = 0; z < t; z++) {
            int n = fs.nextInt();
            int m = fs.nextInt();
            long[][] mat = new long[n][m];
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++) {
                    mat[i][j] = (long)fs.nextInt();
                }
            }

            int topRow = 0;
            int BottomRow = n - 1;

            long result = 0;

            while(topRow <= BottomRow) {
                result += process(mat, n, m, topRow, BottomRow);
                topRow++;
                BottomRow--;
            }
            System.out.println(result);
        }
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;

public class NiceMatrix {
    class Pair implements Comparable<Pair> {
        public int a;
        public int b;

        public Pair() {
            this.a = 0;
            this.b = 0;
        }

        public Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public int compareTo(Pair p) {
            if (this.a == p.a) {
                return this.b - p.b;
            }
            return this.a - p.a;
        }

        @Override
        public String toString() {
            return "a = " + this.a + " b = " + this.b;

        }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readArray(int n, int size) {
            int[] a = new int[size];
            for (int i = 0; i < n; i++) {
                a[i] = this.nextInt();
            }

            return a;
        }

        long[] readLongArray(int n, int size) {
            long[] a = new long[size];
            for (int i = 0; i < n; i++) {
                a[i] = this.nextLong();
            }
            return a;
        }
    }

    static void ruffleSort(long[] a) {
        int n = a.length;//shuffle, then sort
        for (int i = 0; i < n; i++) {
            int oi = random.nextInt(n);
            long temp = a[oi];
            a[oi] = a[i];
            a[i] = temp;
        }
        Arrays.sort(a);
    }

    static final Random random = new Random();

    static void ruffleSort(int[] a) {
        int n = a.length;//shuffle, then sort
        for (int i = 0; i < n; i++) {
            int oi = random.nextInt(n), temp = a[oi];
            a[oi] = a[i];
            a[i] = temp;
        }
        Arrays.sort(a);
    }

    public static long process(long[][] mat, int n, int m, int topRow, int bottomRow) {
        int lc = 0;
        int rc = m - 1;
        int count = 0;
        long result = 0;
        while(lc <= rc) {
            long sum = 0;
            sum += mat[topRow][lc] + mat[bottomRow][lc] + ((lc != rc && topRow != bottomRow) ? mat[bottomRow][rc] + mat[topRow][rc] : 0);
            long req = (sum / ((lc != rc && topRow != bottomRow) ? 4 : 2));
            result += Math.abs(mat[topRow][lc] - req);
            if(lc != rc && topRow != bottomRow) result += Math.abs(mat[topRow][rc] - req);
            if(lc != rc && topRow != bottomRow) result += Math.abs(mat[bottomRow][lc] - req);
            result += Math.abs(mat[bottomRow][rc] - req);
            //System.out.print("LC: " + lc + " rc: "  +  rc + "  topRow: " + topRow + " " + bottomRow + "");
            lc++;
            rc--;
        }

        return result;
    }
    public static void main(String[] args) {
        FastReader fs = new FastReader();
        int t = fs.nextInt();

        for (int z = 0; z < t; z++) {
            int n = fs.nextInt();
            int m = fs.nextInt();
            long[][] mat = new long[n][m];
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++) {
                    mat[i][j] = (long)fs.nextInt();
                }
            }

            int topRow = 0;
            int BottomRow = n - 1;

            int result = 0;

            while(topRow <= BottomRow) {
                result += process(mat, n, m, topRow, BottomRow);
                topRow++;
                BottomRow--;
            }
            System.out.println(result);
        }
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;

public class NiceMatrix {
    class Pair implements Comparable<Pair> {
        public int a;
        public int b;

        public Pair() {
            this.a = 0;
            this.b = 0;
        }

        public Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public int compareTo(Pair p) {
            if (this.a == p.a) {
                return this.b - p.b;
            }
            return this.a - p.a;
        }

        @Override
        public String toString() {
            return "a = " + this.a + " b = " + this.b;

        }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readArray(int n, int size) {
            int[] a = new int[size];
            for (int i = 0; i < n; i++) {
                a[i] = this.nextInt();
            }

            return a;
        }

        long[] readLongArray(int n, int size) {
            long[] a = new long[size];
            for (int i = 0; i < n; i++) {
                a[i] = this.nextLong();
            }
            return a;
        }
    }

    static void ruffleSort(long[] a) {
        int n = a.length;//shuffle, then sort
        for (int i = 0; i < n; i++) {
            int oi = random.nextInt(n);
            long temp = a[oi];
            a[oi] = a[i];
            a[i] = temp;
        }
        Arrays.sort(a);
    }

    static final Random random = new Random();

    static void ruffleSort(int[] a) {
        int n = a.length;//shuffle, then sort
        for (int i = 0; i < n; i++) {
            int oi = random.nextInt(n), temp = a[oi];
            a[oi] = a[i];
            a[i] = temp;
        }
        Arrays.sort(a);
    }

    public static long process(long[][] mat, int n, int m, int topRow, int bottomRow) {
        int lc = 0;
        int rc = m - 1;
        long result = 0;
        while(lc < rc) {
            long sum = 0;
            sum += mat[topRow][lc] + mat[bottomRow][lc] + mat[bottomRow][rc] + mat[topRow][rc];
            long req = (sum / (long)4);
            result += Math.abs(mat[topRow][lc] - req);
            result += Math.abs(mat[topRow][rc] - req);
            result += Math.abs(mat[bottomRow][lc] - req);
            result += Math.abs(mat[bottomRow][rc] - req);
            //System.out.print("LC: " + lc + " rc: "  +  rc + "  topRow: " + topRow + " " + bottomRow + "");
            lc++;
            rc--;
        }
        if(lc == rc) {
            long sum = mat[topRow][lc] + mat[bottomRow][lc];
            sum /= (long)2;
            result += Math.abs(mat[topRow][lc] - sum) + Math.abs(mat[bottomRow][lc] - sum);
        }

        return result;
    }
    public static void main(String[] args) {
        FastReader fs = new FastReader();
        int t = fs.nextInt();

        for (int z = 0; z < t; z++) {
            int n = fs.nextInt();
            int m = fs.nextInt();
            long[][] mat = new long[n][m];
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++) {
                    mat[i][j] = (long)fs.nextInt();
                }
            }

            int topRow = 0;
            int BottomRow = n - 1;

            long result = 0;

            while(topRow < BottomRow) {
                result += process(mat, n, m, topRow, BottomRow);
                topRow++;
                BottomRow--;
            }
            if(topRow == BottomRow) {
                int i = 0;
                int j = m-1;

                while(i < j) {
                    long sum = mat[topRow][i] + mat[topRow][j];
                    sum /= (long)2;
                    result += Math.abs(mat[topRow][i] - sum);
                    result += Math.abs(mat[topRow][j] - sum);
                    i++;
                    j--;
                }
            }
            System.out.println(result);
        }
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;

public class NiceMatrix {
    class Pair implements Comparable<Pair> {
        public int a;
        public int b;

        public Pair() {
            this.a = 0;
            this.b = 0;
        }

        public Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public int compareTo(Pair p) {
            if (this.a == p.a) {
                return this.b - p.b;
            }
            return this.a - p.a;
        }

        @Override
        public String toString() {
            return "a = " + this.a + " b = " + this.b;

        }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readArray(int n, int size) {
            int[] a = new int[size];
            for (int i = 0; i < n; i++) {
                a[i] = this.nextInt();
            }

            return a;
        }

        long[] readLongArray(int n, int size) {
            long[] a = new long[size];
            for (int i = 0; i < n; i++) {
                a[i] = this.nextLong();
            }
            return a;
        }
    }

    static void ruffleSort(long[] a) {
        int n = a.length;//shuffle, then sort
        for (int i = 0; i < n; i++) {
            int oi = random.nextInt(n);
            long temp = a[oi];
            a[oi] = a[i];
            a[i] = temp;
        }
        Arrays.sort(a);
    }

    static final Random random = new Random();

    static void ruffleSort(int[] a) {
        int n = a.length;//shuffle, then sort
        for (int i = 0; i < n; i++) {
            int oi = random.nextInt(n), temp = a[oi];
            a[oi] = a[i];
            a[i] = temp;
        }
        Arrays.sort(a);
    }

    public static long median(List<Long> values) {
        Collections.sort(values);
        return values.get(values.size()/2);
    }
    public static long process(long[][] mat, int n, int m, int topRow, int bottomRow) {
        int lc = 0;
        int rc = m - 1;
        long result = 0;
        while(lc < rc) {
            long sum = 0;
            List<Long> values = new ArrayList<>();
            values.add(mat[topRow][lc]);
            values.add(mat[bottomRow][lc]);
            values.add(mat[bottomRow][rc]);
            values.add(mat[topRow][rc]);
            long req = median(values);
            result += Math.abs(mat[topRow][lc] - req);
            result += Math.abs(mat[topRow][rc] - req);
            result += Math.abs(mat[bottomRow][lc] - req);
            result += Math.abs(mat[bottomRow][rc] - req);
            //System.out.print("LC: " + lc + " rc: "  +  rc + "  topRow: " + topRow + " " + bottomRow + "");
            lc++;
            rc--;
        }
        if(lc == rc) {
            result += Math.abs(mat[topRow][lc] - mat[bottomRow][lc]);
        }

        return result;
    }
    public static void main(String[] args) {
        FastReader fs = new FastReader();
        int t = fs.nextInt();

        for (int z = 0; z < t; z++) {
            int n = fs.nextInt();
            int m = fs.nextInt();
            long[][] mat = new long[n][m];
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < m; j++) {
                    mat[i][j] = (long)fs.nextInt();
                }
            }

            int topRow = 0;
            int BottomRow = n - 1;

            long result = 0;

            while(topRow < BottomRow) {
                result += process(mat, n, m, topRow, BottomRow);
                topRow++;
                BottomRow--;
            }
            if(topRow == BottomRow) {
                int i = 0;
                int j = m-1;

                while(i < j) {
                    result += Math.abs(mat[topRow][i] - mat[topRow][j]);
                    i++;
                    j--;
                }
            }
            System.out.println(result);
        }
    }

}


import java.util.*;
import java.io.*;

public class Nice_Matrix{
	public static void main(String[] args)throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int t = Integer.parseInt(br.readLine());

		while(t-->0){
			String nm[] = br.readLine().split(" ");

			int n = Integer.parseInt(nm[0]);
			int m = Integer.parseInt(nm[1]);

			int mat[][] = new int[n][m];

			for(int i=0;i<n;i++){
				nm = br.readLine().split(" ");
				int k=0;
				for(String itr:nm) mat[i][k++] = Integer.parseInt(itr);
			}
			long count=0L;

			for(int i=0;i<n/2;i++){
				for(int j=0;j<m/2;j++){
					int t1 = mat[i][j];
					int t2 = mat[n-i-1][j];
					int t3 = mat[i][m-j-1];
					int t4 = mat[n-i-1][m-j-1];

					int m1 = Math.abs(t1-t2)+Math.abs(t1-t3)+Math.abs(t1-t4);
					int m2 = Math.abs(t1-t2)+Math.abs(t2-t3)+Math.abs(t2-t4);
					int m3 = Math.abs(t3-t2)+Math.abs(t3-t1)+Math.abs(t3-t4);
					int m4 = Math.abs(t4-t2)+Math.abs(t4-t3)+Math.abs(t1-t4);

					int min = Math.min(Math.min(m1,m2),Math.min(m3,m4));

					 count += min;
				}
			}

			if(n%2!=0){
				int fl=0;
				for(int i=0;i<m/2;i++){
					if(mat[n/2][i]!=mat[n/2][m-1-i]) {fl=1; break;}
				}
				if(fl!=0){
				for(int i=0;i<m/2;i++)
					count += Math.abs(mat[n/2][i]-mat[n/2][m-i-1]);
				}

			}

			if(m%2!=0){
				int fl=0;
				for(int i=0;i<n/2;i++){
					if(mat[n-i-1][m/2]!=mat[i][m/2]) {fl=1; break;}
				}
				if(fl!=0){
				for(int i=0;i<n/2;i++){
					count += Math.abs(mat[i][m/2]-mat[n-i-1][m/2]);
				}

				}
			}

			System.out.println(count);
		}

	}
}


import java.util.*;
import java.io.*;

public class Nice_Matrix{
	public static void main(String[] args)throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int t = Integer.parseInt(br.readLine());

		while(t-->0){
			String nm[] = br.readLine().split(" ");

			int n = Integer.parseInt(nm[0]);
			int m = Integer.parseInt(nm[1]);

			long mat[][] = new long[n][m];

			for(int i=0;i<n;i++){
				nm = br.readLine().split(" ");
				int k=0;
				for(String itr:nm) mat[i][k++] = Long.parseLong(itr);
			}
			long count=0L;

			for(int i=0;i<n/2;i++){
				for(int j=0;j<m/2;j++){
					long t1 = mat[i][j];
					long t2 = mat[n-i-1][j];
					long t3 = mat[i][m-j-1];
					long t4 = mat[n-i-1][m-j-1];

					long m1 = Math.abs(t1-t2)+Math.abs(t1-t3)+Math.abs(t1-t4);
					long m2 = Math.abs(t1-t2)+Math.abs(t2-t3)+Math.abs(t2-t4);
					long m3 = Math.abs(t3-t2)+Math.abs(t3-t1)+Math.abs(t3-t4);
					long m4 = Math.abs(t4-t2)+Math.abs(t4-t3)+Math.abs(t1-t4);

					long min = (long)Math.min((long)Math.min(m1,m2),(long)Math.min(m3,m4));

					count += min;
				}
			}

			if(n%2!=0){
				int fl=0;
				for(int i=0;i<m/2;i++){
					if(mat[n/2][i]!=mat[n/2][m-1-i]) {fl=1; break;}
				}
				if(fl!=0){
				for(int i=0;i<m/2;i++)
					count += Math.abs(mat[n/2][i]-mat[n/2][m-i-1]);
				}

			}

			if(m%2!=0){
				int fl=0;
				for(int i=0;i<n/2;i++){
					if(mat[n-i-1][m/2]!=mat[i][m/2]) {fl=1; break;}
				}
				if(fl!=0){
				for(int i=0;i<n/2;i++){
					count += Math.abs(mat[i][m/2]-mat[n-i-1][m/2]);
				}

				}
			}

			System.out.println(count);
		}

	}
}


import java.util.*;
import java.io.*;

public class Nice_Matrix{
	public static void main(String[] args)throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int t = Integer.parseInt(br.readLine());

		while(t-->0){
			String nm[] = br.readLine().split(" ");

			int n = Integer.parseInt(nm[0]);
			int m = Integer.parseInt(nm[1]);

			int mat[][] = new int[n][m];

			for(int i=0;i<n;i++){
				nm = br.readLine().split(" ");
				int k=0;
				for(String itr:nm) mat[i][k++] = Integer.parseInt(itr);
			}
			long count=0L;

			for(int i=0;i<n/2;i++){
				for(int j=0;j<m/2;j++){
					int t1 = mat[i][j];
					int t2 = mat[n-i-1][j];
					int t3 = mat[i][m-j-1];
					int t4 = mat[n-i-1][m-j-1];

					int m1 = Math.abs(t1-t2)+Math.abs(t1-t3)+Math.abs(t1-t4);
					int m2 = Math.abs(t1-t2)+Math.abs(t2-t3)+Math.abs(t2-t4);
					int m3 = Math.abs(t3-t2)+Math.abs(t3-t1)+Math.abs(t3-t4);
					int m4 = Math.abs(t4-t2)+Math.abs(t4-t3)+Math.abs(t1-t4);

					int min = Math.min(Math.min(m1,m2),Math.min(m3,m4));

					 count += min;
				}
			}

			if(n%2!=0){
				int fl=0;
				for(int i=0;i<m/2;i++){
					if(mat[n/2][i]!=mat[n/2][m-1-i]) {fl=1; break;}
				}
				if(fl!=0){
				int min=Integer.MAX_VALUE;
				for(int i=0;i<m;i++){
					int temp = mat[n/2][i];
					int sum = 0;
					for(int j=0;j<m;j++){
						sum +=Math.abs(temp-mat[n/2][j]);
					}
					min = Math.min(min,sum);
				}
				if(min!=Integer.MAX_VALUE)
					count+=min;
				}
			}

			if(m%2!=0){
				int fl=0;
				for(int i=0;i<n/2;i++){
					if(mat[n-i-1][m/2]!=mat[i][m/2]) {fl=1; break;}
				}

				if(fl!=0){
				int min=Integer.MAX_VALUE;
				for(int i=0;i<n;i++){
					int temp = mat[i][m/2];
					int sum = 0;
					for(int j=0;j<n;j++){
						sum +=Math.abs(temp-mat[j][m/2]);
					}
					min = Math.min(min,sum);
				}
				if(min!=Integer.MAX_VALUE)
					count+=min;
				}
			}

			System.out.println(count);
		}

	}
}


import java.util.*;
import java.io.*;

public class Nice_Matrix{
	public static void main(String[] args)throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int t = Integer.parseInt(br.readLine());

		while(t-->0){
			String nm[] = br.readLine().split(" ");

			int n = Integer.parseInt(nm[0]);
			int m = Integer.parseInt(nm[1]);

			int mat[][] = new int[n][m];

			for(int i=0;i<n;i++){
				nm = br.readLine().split(" ");
				int k=0;
				for(String itr:nm) mat[i][k++] = Integer.parseInt(itr);
			}
			long count=0L;

			for(int i=0;i<n/2;i++){
				for(int j=0;j<m/2;j++){
					int t1 = mat[i][j];
					int t2 = mat[n-i-1][j];
					int t3 = mat[i][m-j-1];
					int t4 = mat[n-i-1][m-j-1];

					int m1 = Math.abs(t1-t2)+Math.abs(t1-t3)+Math.abs(t1-t4);
					int m2 = Math.abs(t1-t2)+Math.abs(t2-t3)+Math.abs(t2-t4);
					int m3 = Math.abs(t3-t2)+Math.abs(t3-t1)+Math.abs(t3-t4);
					int m4 = Math.abs(t4-t2)+Math.abs(t4-t3)+Math.abs(t1-t4);

					int min = Math.min(Math.min(m1,m2),Math.min(m3,m4));

					 count += min;
				}
			}

			if(n%2!=0){
				int min=Integer.MAX_VALUE;
				for(int i=0;i<m;i++){
					int temp = mat[n/2][i];
					int sum = 0;
					for(int j=0;j<m;j++){
						sum +=Math.abs(temp-mat[n/2][j]);
					}
					min = Math.min(min,sum);
				}
				if(min!=Integer.MAX_VALUE)
					count+=min;
			}
			if(m%2!=0){

				int min=Integer.MAX_VALUE;
				for(int i=0;i<n;i++){
					int temp = mat[i][m/2];
					int sum = 0;
					for(int j=0;j<n;j++){
						sum +=Math.abs(temp-mat[j][m/2]);
					}
					min = Math.min(min,sum);
				}
				if(min!=Integer.MAX_VALUE)
					count+=min;
			}

			System.out.println(count);
		}

	}
}


import java.util.*;
import java.io.*;

public class Question2 {

	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int t = sc.nextInt();
		while(t-->0) {
			solve();
		}
	}

	public static void solve() {
		int n = sc.nextInt();
		int m = sc.nextInt();

		long[][] arr = new long[n][m];

		for(int i =0;i < n;i++) {
			for(int j= 0;j < m;j++)
				arr[i][j] = sc.nextLong();
		}

		boolean[][] vis = new boolean[n][m];

		long ans = 0;
		for(int i= 0;i < n;i++) {
			for(int j = 0;j < m;j++) {
				if(!vis[i][j]) {
					long a = 1;
					long total = arr[i][j];

					ArrayList<Integer> xlist, ylist;
					xlist = new ArrayList<Integer>();
					ylist = new ArrayList<Integer>();

					xlist.add(i);
					ylist.add(j);

					vis[i][j] = true;

					if(!vis[n - i - 1][j]) {
						a++;
						vis[n - i - 1][j] = true;
						total += arr[n - i - 1][j];

						xlist.add(n - i - 1);
						ylist.add(j);
					}

					if(!vis[i][m - j - 1]) {
						a++;
						vis[i][m - j - 1] = true;
						total += arr[i][m - j - 1];

						xlist.add(i);
						ylist.add(m - j - 1);

					}

					if(!vis[n - i - 1][m - j - 1]) {
						a++;
						vis[n - i - 1][m - j - 1] = true;
						total += arr[n - i - 1][m - j - 1];

						xlist.add(n - i - 1);
						ylist.add(m - j - 1);

					}
//					System.out.println("together : ");
//					for(int ix = 0;ix < xlist.size();ix++) {
//						System.out.print(arr[xlist.get(ix)][ylist.get(ix)] + " ");
//					}System.out.println();

					long mean = total/a;

					long sol = getCost(xlist, ylist, mean - 10, arr);
					for(int iv = -9;iv <= +10;iv++) {

						sol = Math.min(sol, getCost(xlist, ylist, mean + iv, arr));
					}

					sol = Math.min(sol, getCost(xlist, ylist, 0, arr));
					sol = Math.min(sol, getCost(xlist, ylist, arr[i][j], arr));
					sol = Math.min(sol, getCost(xlist, ylist, arr[n - i - 1][j], arr));
					sol = Math.min(sol, getCost(xlist, ylist, arr[n - i - 1][j - m - 1], arr));
					sol = Math.min(sol, getCost(xlist, ylist, arr[i][j - m - 1], arr));

					ans += sol;

				}
			}
		}

		System.out.println(ans);

	}

	public static long getCost(ArrayList<Integer> xList, ArrayList<Integer> yList, long mean, long[][] arr) {

		long ans = 0;

		for(int i = 0;i < xList.size();i++) {
			int x = xList.get(i);
			int y = yList.get(i);

			ans += Math.abs(mean - arr[x][y]);

		}
		return ans;
	}
}


import java.util.*;
import java.io.*;

public class Question2 {

	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int t = sc.nextInt();
		while(t-->0) {
			solve();
		}
	}

	public static void solve() {
		int n = sc.nextInt();
		int m = sc.nextInt();

		long[][] arr = new long[n][m];

		for(int i =0;i < n;i++) {
			for(int j= 0;j < m;j++)
				arr[i][j] = sc.nextLong();
		}

		boolean[][] vis = new boolean[n][m];

		long ans = 0;
		for(int i= 0;i < n;i++) {
			for(int j = 0;j < m;j++) {
				if(!vis[i][j]) {
					long a = 1;
					long total = arr[i][j];

					ArrayList<Integer> xlist, ylist;
					xlist = new ArrayList<Integer>();
					ylist = new ArrayList<Integer>();

					xlist.add(i);
					ylist.add(j);

					vis[i][j] = true;

					if(!vis[n - i - 1][j]) {
						a++;
						vis[n - i - 1][j] = true;
						total += arr[n - i - 1][j];

						xlist.add(n - i - 1);
						ylist.add(j);
					}

					if(!vis[i][m - j - 1]) {
						a++;
						vis[i][m - j - 1] = true;
						total += arr[i][m - j - 1];

						xlist.add(i);
						ylist.add(m - j - 1);

					}

					if(!vis[n - i - 1][m - j - 1]) {
						a++;
						vis[n - i - 1][m - j - 1] = true;
						total += arr[n - i - 1][m - j - 1];

						xlist.add(n - i - 1);
						ylist.add(m - j - 1);

					}
//					System.out.println("together : ");
//					for(int ix = 0;ix < xlist.size();ix++) {
//						System.out.print(arr[xlist.get(ix)][ylist.get(ix)] + " ");
//					}System.out.println();

					long mean = total/a;

					long sol = getCost(xlist, ylist, mean - 10, arr);
					for(int iv = -9;iv <= +10;iv++) {

						sol = Math.min(sol, getCost(xlist, ylist, mean + iv, arr));
					}

					ans += sol;

				}
			}
		}

		System.out.println(ans);

	}

	public static long getCost(ArrayList<Integer> xList, ArrayList<Integer> yList, long mean, long[][] arr) {

		long ans = 0;

		for(int i = 0;i < xList.size();i++) {
			int x = xList.get(i);
			int y = yList.get(i);

			ans += Math.abs(mean - arr[x][y]);

		}
		return ans;
	}
}


import java.util.*;
import java.io.*;

public class Question2 {

	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int t = sc.nextInt();
		while(t-->0) {
			solve();
		}
	}

	public static void solve() {
		int n = sc.nextInt();
		int m = sc.nextInt();

		int[][] arr = new int[n][m];

		for(int i =0;i < n;i++) {
			for(int j= 0;j < m;j++)
				arr[i][j] = sc.nextInt();
		}

		boolean[][] vis = new boolean[n][m];

		long ans = 0;
		for(int i= 0;i < n;i++) {
			for(int j = 0;j < m;j++) {
				if(!vis[i][j]) {
					int a = 1;
					long total = arr[i][j];

					ArrayList<Integer> xlist, ylist;
					xlist = new ArrayList<Integer>();
					ylist = new ArrayList<Integer>();

					xlist.add(i);
					ylist.add(j);

					vis[i][j] = true;

					if(!vis[n - i - 1][j]) {
						a++;
						vis[n - i - 1][j] = true;
						total += arr[n - i - 1][j];
						xlist.add(n - i - 1);
						ylist.add(j);
					}

					if(!vis[i][m - j - 1]) {
						a++;
						vis[i][m - j - 1] = true;
						total += arr[i][m - j - 1];
						xlist.add(i);
						ylist.add(m - j - 1);

					}

					if(!vis[n - i - 1][m - j - 1]) {
						a++;
						vis[n - i - 1][m - j - 1] = true;
						total += arr[n - i - 1][m - j - 1];
						xlist.add(n - i - 1);
						ylist.add(m - j - 1);

					}

					long mean = total/a;

					long sol = getCost(xlist, ylist, mean - 1, arr);
					sol = Math.min(sol, getCost(xlist, ylist, mean, arr));
					sol = Math.min(sol, getCost(xlist, ylist, mean + 1, arr));

					ans += sol;

				}
			}
		}

		System.out.println(ans);

	}

	public static long getCost(ArrayList<Integer> xList, ArrayList<Integer> yList, long mean, int[][] arr) {

		long ans = 0;

		for(int i = 0;i < xList.size();i++) {
			int x = xList.get(i);
			int y = yList.get(i);

			ans += Math.abs(mean - arr[x][y]);

		}
		return ans;
	}
}