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, 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][m - j - 1], arr));
					sol = Math.min(sol, getCost(xlist, ylist, arr[i][m - j - 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 p2
{
    public static void main(String[] args)
    {
            Scanner sc=new Scanner(System.in);
            int t=sc.nextInt();
            while (t-->0) {
            int n=sc.nextInt();
            int m=sc.nextInt();
            long[][] arr=new long[n][m];
            long tot=0;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++){
                    arr[i][j]=sc.nextLong();
                }
            }
            long res=0;
            long ans[][]=new long[n][m];
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {

                    long[] med=new long[4];

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


import java.util.*;
import java.io.*;
public class p2
{
    public static void main(String[] args)
    {
            Scanner sc=new Scanner(System.in);
            int t=sc.nextInt();
            while (t-->0) {
            int n=sc.nextInt();
            int m=sc.nextInt();
            int[][] arr=new int[n][m];
            int tot=0;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++){
                    arr[i][j]=sc.nextInt();tot+=arr[i][j];}
            }
            tot/=(n*m);
            int ans=0;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                    ans+=(int)Math.abs(arr[i][j]-tot);
            }
                System.out.println(ans);
            }
    }
}


import java.util.*;
import java.io.*;
public class p2
{
    public static void main(String[] args)
    {
            Scanner sc=new Scanner(System.in);
            int t=sc.nextInt();
            while (t-->0) {
            int n=sc.nextInt();
            int m=sc.nextInt();
            long[][] arr=new long[n][m];
            long tot=0;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++){
                    arr[i][j]=sc.nextLong();tot+=(long)arr[i][j];}
            }
            tot/=((long)n*(long)m);
            int ans=0;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                    ans+=(long)Math.abs(arr[i][j]-tot);
            }
                System.out.println(ans);
            }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class B {
    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
//        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        int t = scan.nextInt();
//        int t= Integer.parseInt(br.readLine());
        while (t-->0)
        {
//            String[] s= br.readLine().split(" ");
//            int n= Integer.parseInt(s[0]);
//            int m= Integer.parseInt(s[1]);
            int n= scan.nextInt();
            int m= scan.nextInt();
            long[][] a= new long[n][m];
            HashSet<Integer> set= new HashSet<>();
            for (int i=0;i<n;i++){
//                String[] s1= br.readLine().split(" ");
                for (int j=0;j<m;j++){
//                    a[i][j]= Integer.parseInt(s1[i]);
                    a[i][j]= scan.nextLong();
                }
            }
            long ans=0;
            for (int i=0;i<(n)/2;i++){
                for (int j=0;j<(m)/2;j++){
                    long num= a[i][j]+a[n-i-1][j]+a[i][m-j-1]+a[n-i-1][m-1-j];
//                    System.out.println(num);
                    long a1= num/4;
                    long a2= (num+3)/4;
                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1)+Math.abs(a[i][m-j-1]-a1)+Math.abs(a[n-i-1][m-j-1]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2)+Math.abs(a[i][m-1-j]-a2)+Math.abs(a[n-i-1][m-j-1]-a2));
                }
            }
//            System.out.println(ans);
            int i= (n+1)/2 -1;
            int j= (m+1)/2 -1;
//            System.out.println(i+" "+j);
            if (n%2==0 && m%2==1){
//                for (j=0;j<(m)/2;j++){
//                    int num= a[i][j]+a[i][m-1-j];
//                    int a1= num/2;
//                    int a2= (num+1)/2;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[i][m-1-j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[i][m-1-j]-a2));
//                }
                for (i=0;i<(n)/2;i++){
                    long num= a[i][j]+a[n-i-1][j];
                    long a1= num/2;
                    long a2= (num+1)/2;
                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2));
                }
//                System.out.println("**");
            } else if (n%2==1 && m%2==0){
                for (j=0;j<(m)/2;j++){
                    long num= a[i][j]+a[i][m-1-j];
                    long a1= num/2;
                    long a2= (num+1)/2;
                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[i][m-1-j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[i][m-1-j]-a2));
                }
//                for (i=0;i<(n)/2;i++){
//                    int num= a[i][j]+a[n-i-1][j];
//                    System.out.println(num+" "+ a[i][j]);
//                    int a1= num/2;
//                    int a2= (num+1)/2;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2));
//                }
//                System.out.println("*");
            }
            else if (n%2==1 && m%2==1){
                for (i=0;i<(n)/2;i++){
                    long num= a[i][j]+a[n-i-1][j];
                    long a1= num/2;
                    long a2= (num+1)/2;
                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2));
                }
                for (j=0;j<(m)/2;j++){
                    long num= a[i][j]+a[i][m-1-j];
                    long a1= num/2;
                    long a2= (num+1)/2;
                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[i][m-1-j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[i][m-1-j]-a2));
                }
            }

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


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class B {
    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
//        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        int t = scan.nextInt();
//        int t= Integer.parseInt(br.readLine());
        while (t-->0)
        {
//            String[] s= br.readLine().split(" ");
//            int n= Integer.parseInt(s[0]);
//            int m= Integer.parseInt(s[1]);
            int n= scan.nextInt();
            int m= scan.nextInt();
            int[][] a= new int[n][m];
            HashSet<Integer> set= new HashSet<>();
            for (int i=0;i<n;i++){
//                String[] s1= br.readLine().split(" ");
                for (int j=0;j<m;j++){
//                    a[i][j]= Integer.parseInt(s1[i]);
                    a[i][j]= scan.nextInt();
                }
            }
            long ans=0;
            for (int i=0;i<(n)/2;i++){
                for (int j=0;j<(m)/2;j++){
                    int num= a[i][j]+a[n-i-1][j]+a[i][m-j-1]+a[n-i-1][m-1-j];
                    int a1= num/4;
                    int a2= (num+3)/4;
                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1)+Math.abs(a[i][m-j-1]-a1)+Math.abs(a[n-i-1][m-j-1]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2)+Math.abs(a[i][m-1-j]-a2)+Math.abs(a[n-i-1][m-j-1]-a2));
                }
            }
//            System.out.println(ans);
            int i= (n+1)/2 -1;
            int j= (m+1)/2 -1;
//            System.out.println(i+" "+j);
            if (n%2==0 && m%2==1){
//                for (j=0;j<(m)/2;j++){
//                    int num= a[i][j]+a[i][m-1-j];
//                    int a1= num/2;
//                    int a2= (num+1)/2;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[i][m-1-j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[i][m-1-j]-a2));
//                }
                for (i=0;i<(n)/2;i++){
                    int num= a[i][j]+a[n-i-1][j];
                    int a1= num/2;
                    int a2= (num+1)/2;
                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2));
                }
//                System.out.println("**");
            } else if (n%2==1 && m%2==0){
                for (j=0;j<(m)/2;j++){
                    int num= a[i][j]+a[i][m-1-j];
                    int a1= num/2;
                    int a2= (num+1)/2;
                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[i][m-1-j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[i][m-1-j]-a2));
                }
//                for (i=0;i<(n)/2;i++){
//                    int num= a[i][j]+a[n-i-1][j];
//                    System.out.println(num+" "+ a[i][j]);
//                    int a1= num/2;
//                    int a2= (num+1)/2;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2));
//                }
//                System.out.println("*");
            }
            else if (n%2==1 && m%2==1){
                for (i=0;i<(n)/2;i++){
                    int num= a[i][j]+a[n-i-1][j];
                    int a1= num/2;
                    int a2= (num+1)/2;
                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2));
                }
                for (j=0;j<(m)/2;j++){
                    int num= a[i][j]+a[i][m-1-j];
                    int a1= num/2;
                    int a2= (num+1)/2;
                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[i][m-1-j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[i][m-1-j]-a2));
                }
            }

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


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class B {
    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
//        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        int t = scan.nextInt();
//        int t= Integer.parseInt(br.readLine());
        while (t-->0)
        {
//            String[] s= br.readLine().split(" ");
//            int n= Integer.parseInt(s[0]);
//            int m= Integer.parseInt(s[1]);
            int n= scan.nextInt();
            int m= scan.nextInt();
            long[][] a= new long[n][m];
            HashSet<Integer> set= new HashSet<>();
            for (int i=0;i<n;i++){
//                String[] s1= br.readLine().split(" ");
                for (int j=0;j<m;j++){
//                    a[i][j]= Integer.parseInt(s1[i]);
                    a[i][j]= scan.nextLong();
                }
            }
            long ans=0;
            for (int i=0;i<n;i++){
                for (int j=0;j<m;j++){
                    long[] b= new long[3];
                    b[0]= a[i][j];
                    b[1]= a[n-1-i][j];
                    b[2]= a[i][m-1-j];
                    Arrays.sort(b);
                    ans+= Math.abs(b[1]-b[0]);
                    ans+= Math.abs(b[1]-b[2]);
                    a[i][j]=a[n-i-1][j]=a[i][m-1-j]= b[1];
                }
            }
            System.out.println(ans);
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class B {
    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
//        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        int t = scan.nextInt();
//        int t= Integer.parseInt(br.readLine());
        while (t-->0)
        {
//            String[] s= br.readLine().split(" ");
//            int n= Integer.parseInt(s[0]);
//            int m= Integer.parseInt(s[1]);
            int n= scan.nextInt();
            int m= scan.nextInt();
            int[][] a= new int[n][m];
            int x=0;
            HashSet<Integer> set= new HashSet<>();
            for (int i=0;i<n;i++){
//                String[] s1= br.readLine().split(" ");
                for (int j=0;j<m;j++){
//                    a[i][j]= Integer.parseInt(s1[i]);
                    a[i][j]= scan.nextInt();
                    x+=a[i][j];
                }
            }
            int y= (x+n*m-1)/(n*m);
            x= x/(n*m);
            int ans1=0, ans2=0;
            for (int i=0;i<n;i++){
//                String[] s1= br.readLine().split(" ");
                for (int j=0;j<m;j++){
//                    a[i][j]= Integer.parseInt(s1[i]);
//                    x+=a[i][j];
                    ans1+= Math.abs(x-a[i][j]);
                }
            }
            for (int i=0;i<n;i++){
//                String[] s1= br.readLine().split(" ");
                for (int j=0;j<m;j++){
//                    a[i][j]= Integer.parseInt(s1[i]);
//                    x+=a[i][j];
                    ans2+= Math.abs(y-a[i][j]);
                }
            }
//            int ans=0;
//            for (int i=0;i<(n)/2;i++){
//                for (int j=0;j<(m)/2;j++){
//                    int num= a[i][j]+a[n-i-1][j]+a[i][m-j-1]+a[n-i-1][m-1-j];
//                    int a1= num/4;
//                    int a2= (num+3)/4;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1)+Math.abs(a[i][m-j-1]-a1)+Math.abs(a[n-i-1][m-j-1]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2)+Math.abs(a[i][m-1-j]-a2)+Math.abs(a[n-i-1][m-j-1]-a2));
//                }
//            }
//            int i= (n+1)/2;
//            int j= (m+1)/2;
//            if (n%2==0 && m%2==1){
//                for (i=0;i<(n)/2;i++){
//                    int num= a[i][j]+a[n-i-1][j];
//                    int a1= num/2;
//                    int a2= (num+1)/2;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2));
//                }
//            } else if (n%2==1 && m%2==0){
//                for (j=0;j<(m)/2;j++){
//                    int num= a[i][j]+a[i][m-1-j];
//                    int a1= num/2;
//                    int a2= (num+1)/2;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[i][m-1-j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[i][m-1-j]-a2));
//                }
//            }
//            else if (n%2==1 && m%2==1){
//                for (i=0;i<(n)/2;i++){
//                    int num= a[i][j]+a[n-i-1][j];
//                    int a1= num/2;
//                    int a2= (num+1)/2;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2));
//                }
//                for (j=0;j<(m)/2;j++){
//                    int num= a[i][j]+a[i][m-1-j];
//                    int a1= num/2;
//                    int a2= (num+1)/2;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[i][m-1-j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[i][m-1-j]-a2));
//                }
//            }

//            System.out.println(ans);
            System.out.println(Math.max(ans1,ans2));
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class B {
    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
//        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        int t = scan.nextInt();
//        int t= Integer.parseInt(br.readLine());
        while (t-->0)
        {
//            String[] s= br.readLine().split(" ");
//            int n= Integer.parseInt(s[0]);
//            int m= Integer.parseInt(s[1]);
            int n= scan.nextInt();
            int m= scan.nextInt();
            int[][] a= new int[n][m];
            long x=0;
            HashSet<Integer> set= new HashSet<>();
            for (int i=0;i<n;i++){
//                String[] s1= br.readLine().split(" ");
                for (int j=0;j<m;j++){
//                    a[i][j]= Integer.parseInt(s1[i]);
                    a[i][j]= scan.nextInt();
                    x+=a[i][j];
                }
            }
            long y= (x+n*m-1)/(n*m);
            x= x/(n*m);
            long ans1=0, ans2=0;
            for (int i=0;i<n;i++){
//                String[] s1= br.readLine().split(" ");
                for (int j=0;j<m;j++){
//                    a[i][j]= Integer.parseInt(s1[i]);
//                    x+=a[i][j];
                    ans1+= Math.abs(x-a[i][j]);
                }
            }
            for (int i=0;i<n;i++){
//                String[] s1= br.readLine().split(" ");
                for (int j=0;j<m;j++){
//                    a[i][j]= Integer.parseInt(s1[i]);
//                    x+=a[i][j];
                    ans2+= Math.abs(y-a[i][j]);
                }
            }
//            int ans=0;
//            for (int i=0;i<(n)/2;i++){
//                for (int j=0;j<(m)/2;j++){
//                    int num= a[i][j]+a[n-i-1][j]+a[i][m-j-1]+a[n-i-1][m-1-j];
//                    int a1= num/4;
//                    int a2= (num+3)/4;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1)+Math.abs(a[i][m-j-1]-a1)+Math.abs(a[n-i-1][m-j-1]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2)+Math.abs(a[i][m-1-j]-a2)+Math.abs(a[n-i-1][m-j-1]-a2));
//                }
//            }
//            int i= (n+1)/2;
//            int j= (m+1)/2;
//            if (n%2==0 && m%2==1){
//                for (i=0;i<(n)/2;i++){
//                    int num= a[i][j]+a[n-i-1][j];
//                    int a1= num/2;
//                    int a2= (num+1)/2;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2));
//                }
//            } else if (n%2==1 && m%2==0){
//                for (j=0;j<(m)/2;j++){
//                    int num= a[i][j]+a[i][m-1-j];
//                    int a1= num/2;
//                    int a2= (num+1)/2;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[i][m-1-j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[i][m-1-j]-a2));
//                }
//            }
//            else if (n%2==1 && m%2==1){
//                for (i=0;i<(n)/2;i++){
//                    int num= a[i][j]+a[n-i-1][j];
//                    int a1= num/2;
//                    int a2= (num+1)/2;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[n-i-1][j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[n-i-1][j]-a2));
//                }
//                for (j=0;j<(m)/2;j++){
//                    int num= a[i][j]+a[i][m-1-j];
//                    int a1= num/2;
//                    int a2= (num+1)/2;
//                    ans+= Math.min(Math.abs(a[i][j]-a1)+Math.abs(a[i][m-1-j]-a1),Math.abs(a[i][j]-a2)+Math.abs(a[i][m-1-j]-a2));
//                }
//            }

//            System.out.println(ans);
            System.out.println(Math.max(ans1,ans2));
        }
    }
}


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

public class competetive  {
    static class tri implements Comparable<tri>{
        int f;
        int s;
        int t;
        int ind;
        tri(int a, int b, int c, int d){
            this.f=a;
            this.s=b;
            this.t=c;
            this.ind=d;
        }
        int getF(){
            return f;
        }
        int getS(){
            return s;
        }
        int getT(){
            return t;
        }
        int getInd(){
            return ind;
        }

        public int compareTo(tri tr){
            if(this.f==tr.f){
                if(this.t==tr.t){
                    return this.s-tr.s;
                }
                return tr.t-this.t;
            }
            return  tr.f-this.f;
        }
    }
    static long c(long[][]mx, int r, int c, int n, int m){
        long[]a=new long[4];
        a[0]=mx[r][c];
        a[1]=mx[n-r+1][c];
        a[2]=mx[r][m-c+1];
        a[3]=mx[n-r+1][m-c+1];
        long count1=0;
        long count2=0;

        long mean1=0;
        long mean2=0;
        Arrays.sort(a);

        mean1=(a[0]+a[3])/2;
        mean2=mean1+1;

        for(int i=0;i<4;i++){
            count1+=Math.abs(mean1-a[i]);
            count2+=Math.abs(mean2-a[i]);
        }
        if(n%2==1&&m%2==1){
            if(r==n/2&&c==m/2) {
                return Math.min(count1 / 4,count2/4);
            }
        }else if(n%2==1){
            if(r==n/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[r][m-c+1]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[r][m-c+1]));
            }
        }else if(m%2==1){
            if(c==m/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[n-r+1][c]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[n-r+1][c]));
            }
        }
        return Math.min(count1,count2);
    }
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);

        int t=Integer.parseInt(br.readLine());
        while (t-->0) {
            String line = br.readLine();
            String[] s = line.trim().split(" ");
            int n=Integer.parseInt(s[0]);
            int m=Integer.parseInt(s[1]);
            long mx[][]=new long[n+1][m+1];
            for(int i=0;i<n;i++){
                String row=br.readLine();
                String[]r=row.trim().split(" ");
                for(int j=0;j<m;j++){
                    mx[i+1][j+1]=Long.parseLong(r[j]);
                }
            }
            long ans=0;
            int is=n/2;
            int js=m/2;

            for(int i=1;i<=n/2+n%2;i++){
                for(int j=1;j<=m/2+m%2;j++){
                    ans+=c(mx,i,j,n,m);
                }
            }
            out.write(String.valueOf(ans));
            out.newLine();

        }

        out.flush();
    }

}


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

public class competetive  {
    static class tri implements Comparable<tri>{
        int f;
        int s;
        int t;
        int ind;
        tri(int a, int b, int c, int d){
            this.f=a;
            this.s=b;
            this.t=c;
            this.ind=d;
        }
        int getF(){
            return f;
        }
        int getS(){
            return s;
        }
        int getT(){
            return t;
        }
        int getInd(){
            return ind;
        }

        public int compareTo(tri tr){
            if(this.f==tr.f){
                if(this.t==tr.t){
                    return this.s-tr.s;
                }
                return tr.t-this.t;
            }
            return  tr.f-this.f;
        }
    }
    static long c(long[][]mx, int r, int c, int n, int m){
        long[]a=new long[4];
        a[0]=mx[r][c];
        a[1]=mx[n-r+1][c];
        a[2]=mx[r][m-c+1];
        a[3]=mx[n-r+1][m-c+1];
        long count1=0;
        long count2=0;

        long mean1=0;
        long mean2=0;
        Arrays.sort(a);

        mean1=a[1];
        mean2=mean1+1;

        for(int i=0;i<4;i++){
            count1+=Math.abs(mean1-a[i]);
            count2+=Math.abs(mean2-a[i]);
        }
        if(n%2==1&&m%2==1){
            if(r==n/2&&c==m/2) {
                return Math.min(count1 / 4,count2/4);
            }
        }else if(n%2==1){
            if(r==n/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[r][m-c+1]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[r][m-c+1]));
            }
        }else if(m%2==1){
            if(c==m/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[n-r+1][c]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[n-r+1][c]));
            }
        }
        return Math.min(count1,count2);
    }
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);

        int t=Integer.parseInt(br.readLine());
        while (t-->0) {
            String line = br.readLine();
            String[] s = line.trim().split(" ");
            int n=Integer.parseInt(s[0]);
            int m=Integer.parseInt(s[1]);
            long mx[][]=new long[n+1][m+1];
            for(int i=0;i<n;i++){
                String row=br.readLine();
                String[]r=row.trim().split(" ");
                for(int j=0;j<m;j++){
                    mx[i+1][j+1]=Long.parseLong(r[j]);
                }
            }
            long ans=0;
            int is=n/2;
            int js=m/2;

            for(int i=1;i<=n/2+n%2;i++){
                for(int j=1;j<=m/2+m%2;j++){
                    ans+=c(mx,i,j,n,m);
                }
            }
            out.write(String.valueOf(ans));
            out.newLine();

        }

        out.flush();
    }

}


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

public class competetive  {
    static class tri implements Comparable<tri>{
        int f;
        int s;
        int t;
        int ind;
        tri(int a, int b, int c, int d){
            this.f=a;
            this.s=b;
            this.t=c;
            this.ind=d;
        }
        int getF(){
            return f;
        }
        int getS(){
            return s;
        }
        int getT(){
            return t;
        }
        int getInd(){
            return ind;
        }

        public int compareTo(tri tr){
            if(this.f==tr.f){
                if(this.t==tr.t){
                    return this.s-tr.s;
                }
                return tr.t-this.t;
            }
            return  tr.f-this.f;
        }
    }
    static long c(long[][]mx, int r, int c, int n, int m){
        long[]a=new long[4];
        a[0]=mx[r][c];
        a[1]=mx[n-r+1][c];
        a[2]=mx[r][m-c+1];
        a[3]=mx[n-r+1][m-c+1];
        long count1=0;
        long count2=0;

        long mean1=0;
        long mean2=0;
        Arrays.sort(a);

        mean1=a[1];
        mean2=mean1+1;

        for(int i=0;i<4;i++){
            count1+=Math.abs(mean1-a[i]);
            count2+=Math.abs(mean2-a[i]);
        }
        if(n%2==1&&m%2==1){
            if(r==n/2+1&&c==m/2+1) {
                return 0;
            }
        }else if(n%2==1){
            if(r==n/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[r][m-c+1]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[r][m-c+1]));
            }
        }else if(m%2==1){
            if(c==m/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[n-r+1][c]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[n-r+1][c]));
            }
        }
        return Math.min(count1,count2);
    }
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);

        int t=Integer.parseInt(br.readLine());
        while (t-->0) {
            String line = br.readLine();
            String[] s = line.trim().split(" ");
            int n=Integer.parseInt(s[0]);
            int m=Integer.parseInt(s[1]);
            long mx[][]=new long[n+1][m+1];
            for(int i=0;i<n;i++){
                String row=br.readLine();
                String[]r=row.trim().split(" ");
                for(int j=0;j<m;j++){
                    mx[i+1][j+1]=Long.parseLong(r[j]);
                }
            }
            long ans=0;
            int is=n/2;
            int js=m/2;

            for(int i=1;i<=n/2+n%2;i++){
                for(int j=1;j<=m/2+m%2;j++){
                    ans+=c(mx,i,j,n,m);
                }
            }
            out.write(String.valueOf(ans));
            out.newLine();

        }

        out.flush();
    }

}


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

public class competetive  {
    static class tri implements Comparable<tri>{
        int f;
        int s;
        int t;
        int ind;
        tri(int a, int b, int c, int d){
            this.f=a;
            this.s=b;
            this.t=c;
            this.ind=d;
        }
        int getF(){
            return f;
        }
        int getS(){
            return s;
        }
        int getT(){
            return t;
        }
        int getInd(){
            return ind;
        }

        public int compareTo(tri tr){
            if(this.f==tr.f){
                if(this.t==tr.t){
                    return this.s-tr.s;
                }
                return tr.t-this.t;
            }
            return  tr.f-this.f;
        }
    }
    static long c(long[][]mx, int r, int c, int n, int m){
        long[]a=new long[4];
        a[0]=mx[r][c];
        a[1]=mx[n-r+1][c];
        a[2]=mx[r][m-c+1];
        a[3]=mx[n-r+1][m-c+1];
        long count=0;

        long mean=0;
        Arrays.sort(a);
        mean=a[1];

        for(int i=0;i<4;i++){
            count+=Math.abs(mean-a[i]);
        }
        if(n%2==1&&m%2==1) {
            if (r == n / 2 + 1 && c == m / 2 + 1) {
                return 0;
            }
        }
        if(n%2==1){
            if(r==n/2+1){
                return count-Math.abs(mean-mx[r][c])-Math.abs(mean-mx[r][m-c+1]);
            }
        }
        if(m%2==1){
            if(c==m/2+1){
                return count-Math.abs(mean-mx[r][c])-Math.abs(mean-mx[n-r+1][c]);
            }
        }
        return count;
    }
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);

        int t=Integer.parseInt(br.readLine());
        while (t-->0) {
            String line = br.readLine();
            String[] s = line.trim().split(" ");
            int n=Integer.parseInt(s[0]);
            int m=Integer.parseInt(s[1]);
            long mx[][]=new long[n+1][m+1];
            for(int i=0;i<n;i++){
                String row=br.readLine();
                String[]r=row.trim().split(" ");
                for(int j=0;j<m;j++){
                    mx[i+1][j+1]=Long.parseLong(r[j]);
                }
            }
            long ans=0;
            int is=n/2;
            int js=m/2;

            for(int i=1;i<=n/2+n%2;i++){
                for(int j=1;j<=m/2+m%2;j++){
                    ans+=c(mx,i,j,n,m);
                }
            }
            out.write(String.valueOf(ans));
            out.newLine();

        }

        out.flush();
    }

}


import java.io.*;

public class competetive  {
    static class tri implements Comparable<tri>{
        int f;
        int s;
        int t;
        int ind;
        tri(int a, int b, int c, int d){
            this.f=a;
            this.s=b;
            this.t=c;
            this.ind=d;
        }
        int getF(){
            return f;
        }
        int getS(){
            return s;
        }
        int getT(){
            return t;
        }
        int getInd(){
            return ind;
        }

        public int compareTo(tri tr){
            if(this.f==tr.f){
                if(this.t==tr.t){
                    return this.s-tr.s;
                }
                return tr.t-this.t;
            }
            return  tr.f-this.f;
        }
    }
    static long c(long[][]mx, int r, int c, int n, int m){
        long[]a=new long[4];
        a[0]=mx[r][c];
        a[1]=mx[n-r+1][c];
        a[2]=mx[r][m-c+1];
        a[3]=mx[n-r+1][m-c+1];
        long count=0;

        long mean=0;
        for(int i=0;i<4;i++){
            mean+=a[i];
        }

        mean/=4;

        for(int i=0;i<4;i++){
            count+=Math.abs(mean-a[i]);
        }
        if(n%2==1&&m%2==1){
            if(r==n/2&&c==m/2) {
                return count / 4;
            }
        }else if(n%2==1){
            if(r==n/2+1){
                return count-Math.abs(mean-mx[r][c])-Math.abs(mean-mx[r][m-c+1]);
            }
        }else if(m%2==1){
            if(c==m/2+1){
                return count-Math.abs(mean-mx[r][c])-Math.abs(mean-mx[n-r+1][c]);
            }
        }
        return count;
    }
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);

        int t=Integer.parseInt(br.readLine());
        while (t-->0) {
            String line = br.readLine();
            String[] s = line.trim().split(" ");
            int n=Integer.parseInt(s[0]);
            int m=Integer.parseInt(s[1]);
            long mx[][]=new long[n+1][m+1];
            for(int i=0;i<n;i++){
                String row=br.readLine();
                String[]r=row.trim().split(" ");
                for(int j=0;j<m;j++){
                    mx[i+1][j+1]=Long.parseLong(r[j]);
                }
            }
            long ans=0;
            int is=n/2;
            int js=m/2;

            for(int i=1;i<=n/2+n%2;i++){
                for(int j=1;j<=m/2+m%2;j++){
                    ans+=c(mx,i,j,n,m);
                }
            }
            out.write(String.valueOf(ans));
            out.newLine();

        }

        out.flush();
    }

}


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

public class competetive  {
    static class tri implements Comparable<tri>{
        int f;
        int s;
        int t;
        int ind;
        tri(int a, int b, int c, int d){
            this.f=a;
            this.s=b;
            this.t=c;
            this.ind=d;
        }
        int getF(){
            return f;
        }
        int getS(){
            return s;
        }
        int getT(){
            return t;
        }
        int getInd(){
            return ind;
        }

        public int compareTo(tri tr){
            if(this.f==tr.f){
                if(this.t==tr.t){
                    return this.s-tr.s;
                }
                return tr.t-this.t;
            }
            return  tr.f-this.f;
        }
    }
    static long c(long[][]mx, int r, int c, int n, int m){
        long[]a=new long[4];
        a[0]=mx[r][c];
        a[1]=mx[n-r+1][c];
        a[2]=mx[r][m-c+1];
        a[3]=mx[n-r+1][m-c+1];
        long count1=0;
        long count2=0;

        long mean1=0;
        long mean2=0;
        Arrays.sort(a);

        mean1=a[1];
        mean2=mean1+1;

        for(int i=0;i<4;i++){
            count1+=Math.abs(mean1-a[i]);
            count2+=Math.abs(mean2-a[i]);
        }
        if(n%2==1&&m%2==1){
            if(r==n/2+1&&c==m/2+1) {
                return Math.min(count1 / 4,count2/4);
            }
        }else if(n%2==1){
            if(r==n/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[r][m-c+1]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[r][m-c+1]));
            }
        }else if(m%2==1){
            if(c==m/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[n-r+1][c]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[n-r+1][c]));
            }
        }
        return Math.min(count1,count2);
    }
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);

        int t=Integer.parseInt(br.readLine());
        while (t-->0) {
            String line = br.readLine();
            String[] s = line.trim().split(" ");
            int n=Integer.parseInt(s[0]);
            int m=Integer.parseInt(s[1]);
            long mx[][]=new long[n+1][m+1];
            for(int i=0;i<n;i++){
                String row=br.readLine();
                String[]r=row.trim().split(" ");
                for(int j=0;j<m;j++){
                    mx[i+1][j+1]=Long.parseLong(r[j]);
                }
            }
            long ans=0;
            int is=n/2;
            int js=m/2;

            for(int i=1;i<=n/2+n%2;i++){
                for(int j=1;j<=m/2+m%2;j++){
                    ans+=c(mx,i,j,n,m);
                }
            }
            out.write(String.valueOf(ans));
            out.newLine();

        }

        out.flush();
    }

}


import java.io.*;

public class competetive  {
    static class tri implements Comparable<tri>{
        int f;
        int s;
        int t;
        int ind;
        tri(int a, int b, int c, int d){
            this.f=a;
            this.s=b;
            this.t=c;
            this.ind=d;
        }
        int getF(){
            return f;
        }
        int getS(){
            return s;
        }
        int getT(){
            return t;
        }
        int getInd(){
            return ind;
        }

        public int compareTo(tri tr){
            if(this.f==tr.f){
                if(this.t==tr.t){
                    return this.s-tr.s;
                }
                return tr.t-this.t;
            }
            return  tr.f-this.f;
        }
    }
    static long c(long[][]mx, int r, int c, int n, int m){
        long[]a=new long[4];
        a[0]=mx[r][c];
        a[1]=mx[n-r+1][c];
        a[2]=mx[r][m-c+1];
        a[3]=mx[n-r+1][m-c+1];
        long count1=0;
        long count2=0;

        long mean1=0;
        long mean2=0;
        for(int i=0;i<4;i++){
            mean1+=a[i];
        }

        mean1/=4;
        mean2=mean1+1;

        for(int i=0;i<4;i++){
            count1+=Math.abs(mean1-a[i]);
            count2+=Math.abs(mean2-a[i]);
        }
        if(n%2==1&&m%2==1){
            if(r==n/2&&c==m/2) {
                return Math.min(count1 / 4,count2/4);
            }
        }else if(n%2==1){
            if(r==n/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[r][m-c+1]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[r][m-c+1]));
            }
        }else if(m%2==1){
            if(c==m/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[n-r+1][c]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[n-r+1][c]));
            }
        }
        return Math.min(count1,count2);
    }
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);

        int t=Integer.parseInt(br.readLine());
        while (t-->0) {
            String line = br.readLine();
            String[] s = line.trim().split(" ");
            int n=Integer.parseInt(s[0]);
            int m=Integer.parseInt(s[1]);
            long mx[][]=new long[n+1][m+1];
            for(int i=0;i<n;i++){
                String row=br.readLine();
                String[]r=row.trim().split(" ");
                for(int j=0;j<m;j++){
                    mx[i+1][j+1]=Long.parseLong(r[j]);
                }
            }
            long ans=0;
            int is=n/2;
            int js=m/2;

            for(int i=1;i<=n/2+n%2;i++){
                for(int j=1;j<=m/2+m%2;j++){
                    ans+=c(mx,i,j,n,m);
                }
            }
            out.write(String.valueOf(ans));
            out.newLine();

        }

        out.flush();
    }

}


package CodeForces;

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

public class competetive  {
    static class tri implements Comparable<tri>{
        int f;
        int s;
        int t;
        int ind;
        tri(int a, int b, int c, int d){
            this.f=a;
            this.s=b;
            this.t=c;
            this.ind=d;
        }
        int getF(){
            return f;
        }
        int getS(){
            return s;
        }
        int getT(){
            return t;
        }
        int getInd(){
            return ind;
        }

        public int compareTo(tri tr){
            if(this.f==tr.f){
                if(this.t==tr.t){
                    return this.s-tr.s;
                }
                return tr.t-this.t;
            }
            return  tr.f-this.f;
        }
    }
    static long c(long[][]mx, int r, int c, int n, int m){
        long[]a=new long[4];
        a[0]=mx[r][c];
        a[1]=mx[n-r+1][c];
        a[2]=mx[r][m-c+1];
        a[3]=mx[n-r+1][m-c+1];
        long count1=0;
        long count2=0;

        long mean1=0;
        long mean2=0;
        Arrays.sort(a);

        mean1=a[1];
        mean2=mean1+1;

        for(int i=0;i<4;i++){
            count1+=Math.abs(mean1-a[i]);
            count2+=Math.abs(mean2-a[i]);
        }
        if(n%2==1&&m%2==1){
            if(r==n/2+1&&c==m/2+1) {
                return 0;
            }
        }else if(n%2==1){
            if(r==n/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[r][m-c+1]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[r][m-c+1]));
            }
        }else if(m%2==1){
            if(c==m/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[n-r+1][c]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[n-r+1][c]));
            }
        }
        return Math.min(count1,count2);
    }
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);

        int t=Integer.parseInt(br.readLine());
        while (t-->0) {
            String line = br.readLine();
            String[] s = line.trim().split(" ");
            int n=Integer.parseInt(s[0]);
            int m=Integer.parseInt(s[1]);
            long mx[][]=new long[n+1][m+1];
            for(int i=0;i<n;i++){
                String row=br.readLine();
                String[]r=row.trim().split(" ");
                for(int j=0;j<m;j++){
                    mx[i+1][j+1]=Long.parseLong(r[j]);
                }
            }
            long ans=0;
            int is=n/2;
            int js=m/2;

            for(int i=1;i<=n/2+n%2;i++){
                for(int j=1;j<=m/2+m%2;j++){
                    ans+=c(mx,i,j,n,m);
                }
            }
            out.write(String.valueOf(ans));
            out.newLine();

        }

        out.flush();
    }

}


import java.io.*;

public class competetive  {
    static class tri implements Comparable<tri>{
        int f;
        int s;
        int t;
        int ind;
        tri(int a, int b, int c, int d){
            this.f=a;
            this.s=b;
            this.t=c;
            this.ind=d;
        }
        int getF(){
            return f;
        }
        int getS(){
            return s;
        }
        int getT(){
            return t;
        }
        int getInd(){
            return ind;
        }

        public int compareTo(tri tr){
            if(this.f==tr.f){
                if(this.t==tr.t){
                    return this.s-tr.s;
                }
                return tr.t-this.t;
            }
            return  tr.f-this.f;
        }
    }
    static int c(int[][]mx, int r, int c, int n, int m){
        int[]a=new int[4];
        a[0]=mx[r][c];
        a[1]=mx[n-r+1][c];
        a[2]=mx[r][m-c+1];
        a[3]=mx[n-r+1][m-c+1];
        int count=0;

        int mean=0;
        for(int i=0;i<4;i++){
            mean+=a[i];
        }

        mean/=4;

        for(int i=0;i<4;i++){
            count+=Math.abs(mean-a[i]);
        }
        if(n%2==1&&m%2==1){
            if(r==n/2&&c==m/2) {
                return count / 4;
            }
        }else if(n%2==1){
            if(r==n/2+1){
                return count-Math.abs(mean-mx[r][c])-Math.abs(mean-mx[r][m-c+1]);
            }
        }else if(m%2==1){
            if(c==m/2+1){
                return count-Math.abs(mean-mx[r][c])-Math.abs(mean-mx[n-r+1][c]);
            }
        }
        return count;
    }
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);

        int t=Integer.parseInt(br.readLine());
        while (t-->0) {
            String line = br.readLine();
            String[] s = line.trim().split(" ");
            int n=Integer.parseInt(s[0]);
            int m=Integer.parseInt(s[1]);
            int mx[][]=new int[n+1][m+1];
            for(int i=0;i<n;i++){
                String row=br.readLine();
                String[]r=row.trim().split(" ");
                for(int j=0;j<m;j++){
                    mx[i+1][j+1]=Integer.parseInt(r[j]);
                }
            }
            int ans=0;
            int is=n/2;
            int js=m/2;

            for(int i=1;i<=n/2+n%2;i++){
                for(int j=1;j<=m/2+m%2;j++){
                    ans+=c(mx,i,j,n,m);
                }
            }
            out.write(String.valueOf(ans));
            out.newLine();

        }

        out.flush();
    }

}


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

public class competetive  {
    static class tri implements Comparable<tri>{
        int f;
        int s;
        int t;
        int ind;
        tri(int a, int b, int c, int d){
            this.f=a;
            this.s=b;
            this.t=c;
            this.ind=d;
        }
        int getF(){
            return f;
        }
        int getS(){
            return s;
        }
        int getT(){
            return t;
        }
        int getInd(){
            return ind;
        }

        public int compareTo(tri tr){
            if(this.f==tr.f){
                if(this.t==tr.t){
                    return this.s-tr.s;
                }
                return tr.t-this.t;
            }
            return  tr.f-this.f;
        }
    }
    static long c(long[][]mx, int r, int c, int n, int m){
        long[]a=new long[4];
        a[0]=mx[r][c];
        a[1]=mx[n-r+1][c];
        a[2]=mx[r][m-c+1];
        a[3]=mx[n-r+1][m-c+1];
        long count1=0;
        long count2=0;

        long mean1=0;
        long mean2=0;
        Arrays.sort(a);

        mean1=(a[1]+a[2])/2;
        mean2=mean1+1;

        for(int i=0;i<4;i++){
            count1+=Math.abs(mean1-a[i]);
            count2+=Math.abs(mean2-a[i]);
        }
        if(n%2==1&&m%2==1){
            if(r==n/2&&c==m/2) {
                return Math.min(count1 / 4,count2/4);
            }
        }else if(n%2==1){
            if(r==n/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[r][m-c+1]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[r][m-c+1]));
            }
        }else if(m%2==1){
            if(c==m/2+1){
                return Math.min(count1-Math.abs(mean1-mx[r][c])-Math.abs(mean1-mx[n-r+1][c]),count2-Math.abs(mean2-mx[r][c])-Math.abs(mean2-mx[n-r+1][c]));
            }
        }
        return Math.min(count1,count2);
    }
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);

        int t=Integer.parseInt(br.readLine());
        while (t-->0) {
            String line = br.readLine();
            String[] s = line.trim().split(" ");
            int n=Integer.parseInt(s[0]);
            int m=Integer.parseInt(s[1]);
            long mx[][]=new long[n+1][m+1];
            for(int i=0;i<n;i++){
                String row=br.readLine();
                String[]r=row.trim().split(" ");
                for(int j=0;j<m;j++){
                    mx[i+1][j+1]=Long.parseLong(r[j]);
                }
            }
            long ans=0;
            int is=n/2;
            int js=m/2;

            for(int i=1;i<=n/2+n%2;i++){
                for(int j=1;j<=m/2+m%2;j++){
                    ans+=c(mx,i,j,n,m);
                }
            }
            out.write(String.valueOf(ans));
            out.newLine();

        }

        out.flush();
    }

}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0,res;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    ll p = ar[i][j] + ar[i][m-j-1] + ar[n-i-1][j] + ar[n-i-1][m-j-1];
                    p /= 4;
                    res = abs(p-ar[i][j]) + abs(p-ar[i][m-j-1]) + abs(p-ar[n-i-1][j]) + abs(p-ar[n-i-1][m-j-1]);
                    if((i!=n-i-1)&&(j!=m-j-1))
                        ans += res;
                    else if(((i==n-i-1)&&(j!=m-j-1))||((i!=n-i-1)&&(j==m-j-1)))
                        ans += res/2;
                    arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0,res,res2;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    ll p = ar[i][j] + ar[i][m-j-1] + ar[n-i-1][j] + ar[n-i-1][m-j-1];
                    p /= 4;
                    res = abs(p - ar[i][j]) + abs(p - ar[i][m-j-1]) + abs(p - ar[n-i-1][j]) + abs(p - ar[n-i-1][m-j-1]);
                    res2 = abs(p - ar[i][j] + 1) + abs(p - ar[i][m-j-1] + 1) + abs(p - ar[n-i-1][j] + 1) + abs(p - ar[n-i-1][m-j-1] + 1);
                    res = min(res,res2);
                    if((2*i!=n-1)&&(2*j!=m-1))
                    {
                        ans += res;
                    }
                    else if(((2*i==n-1)&&(2*j!=m-1))||((2*i!=n-1)&&(2*j==m-1)))
                    {
                        res /= 2;
                        ans += res;
                    }
                    arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0,res;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    ll p = ar[i][j] + ar[i][m-j-1] + ar[n-i-1][j] + ar[n-i-1][m-j-1];
                    p /= 4;
                    res = abs(p - ar[i][j]) + abs(p - ar[i][m-j-1]) + abs(p - ar[n-i-1][j]) + abs(p - ar[n-i-1][m-j-1]);
                    if((2*i!=n-1)&&(2*j!=m-1))
                    {
                        ans += res;
                    }
                    else if(((2*i==n-1)&&(2*j!=m-1))||((2*i!=n-1)&&(2*j==m-1)))
                    {
                        res /= 2;
                        ans += res;
                    }
                    arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0,res,res2;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        ll br[4];
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    br[0] = ar[i][j];
                    br[1] = ar[i][m-j-1];
                    br[2] = ar[n-i-1][j];
                    br[3] = ar[n-i-1][m-j-1];
                    sort(br,br+3);
                    ans += br[3] - br[0] + br[2] - br[1];
                    //arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans/4<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0,res,res2;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    res = 0,res2 = 0;
                    ll p = ar[i][j] + ar[i][m-j-1] + ar[n-i-1][j] + ar[n-i-1][m-j-1];
                    p /= 4;
                    res = abs(p - ar[i][j]) + abs(p - ar[i][m-j-1]) + abs(p - ar[n-i-1][j]) + abs(p - ar[n-i-1][m-j-1]);
                    res2 = abs(p - ar[i][j] + 1) + abs(p - ar[i][m-j-1] + 1) + abs(p - ar[n-i-1][j] + 1) + abs(p - ar[n-i-1][m-j-1] + 1);
                    res = min(res,res2);
                    if((2*i!=n-1)&&(2*j!=m-1))
                    {
                        ans += res;
                    }
                    else
                    {
                        res /= 2;
                        ans += res;
                    }
                    arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    ll p = ar[i][j] + ar[i][m-j-1] + ar[n-i-1][j] + ar[n-i-1][m-j-1];
                    ll res;
                    double h = (double)p/4;
                    ll k = (floor)(h);
                    ll u = (ceil)(h);
                    res = min((abs(k-ar[i][j]) + abs(k-ar[i][m-j-1]) + abs(k-ar[n-i-1][j]) + abs(k-ar[n-i-1][m-j-1])),
                            (abs(u-ar[i][j]) + abs(u-ar[i][m-j-1]) + abs(u-ar[n-i-1][j]) + abs(u-ar[n-i-1][m-j-1])));
                    if(i!=n-i-1&&j!=m-j-1)
                        ans += res;
                    else if((i==n-i-1&&j!=m-j-1)||(i!=n-i-1&&j==m-j-1))
                        ans += res/2;
                    else
                        ans += res/4;
                            arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    ll p = ar[i][j] + ar[i][m-j-1] + ar[n-i-1][j] + ar[n-i-1][m-j-1];
                    ll res = 0;
                    double h = (double)p/4;
                    //cout<<i<<" "<<j<<endl;
                    ll k = (floor)(h);
                    ll u = (ceil)(h);
                    //cout<<k<<" "<<u<<endl;
                    res = min((abs(k-ar[i][j]) + abs(k-ar[i][m-j-1]) + abs(k-ar[n-i-1][j]) + abs(k-ar[n-i-1][m-j-1])),
                            (abs(u-ar[i][j]) + abs(u-ar[i][m-j-1]) + abs(u-ar[n-i-1][j]) + abs(u-ar[n-i-1][m-j-1])));
                    if(i!=n-i-1&&j!=m-j-1)
                        ans += res;
                    else if((i==n-i-1&&j!=m-j-1)||(i!=n-i-1&&j==m-j-1))
                        ans += res/2;
                            arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0,res,res2;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        ll br[4];
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    br[0] = ar[i][j];
                    br[1] = ar[i][m-j-1];
                    br[2] = ar[n-i-1][j];
                    br[3] = ar[n-i-1][m-j-1];
                    sort(br,br+3);
                    ans += ar[i][j] - br[1];
                    //arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0,res,res2;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]!=1)
                {
                    res = 0,res2 = 0;
                    ll p = ar[i][j] + ar[i][m-j-1] + ar[n-i-1][j] + ar[n-i-1][m-j-1];
                    p /= 4;
                    res = abs(p - ar[i][j]) + abs(p - ar[i][m-j-1]) + abs(p - ar[n-i-1][j]) + abs(p - ar[n-i-1][m-j-1]);
                    res2 = abs(p - ar[i][j] + 1) + abs(p - ar[i][m-j-1] + 1) + abs(p - ar[n-i-1][j] + 1) + abs(p - ar[n-i-1][m-j-1] + 1);
                    res = min(res,res2);
                    if((2*i!=n-1)&&(2*j!=m-1))
                    {
                        ans += res;
                    }
                    else if(((2*i==n-1)&&(2*j!=m-1))||((2*i!=n-1)&&(2*j==m-1)))
                    {
                        res /= 2;
                        ans += res;
                    }
                    else
                    {
                        ans += 0;
                    }
                    arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0,res,res2;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    res = 0,res2 = 0;
                    ll p = ar[i][j] + ar[i][m-j-1] + ar[n-i-1][j] + ar[n-i-1][m-j-1];
                    p /= 4;
                    res = abs(p - ar[i][j]) + abs(p - ar[i][m-j-1]) + abs(p - ar[n-i-1][j]) + abs(p - ar[n-i-1][m-j-1]);
                    res2 = abs(p - ar[i][j] + 1) + abs(p - ar[i][m-j-1] + 1) + abs(p - ar[n-i-1][j] + 1) + abs(p - ar[n-i-1][m-j-1] + 1);
                    res = max(res,res2);
                    if((2*i!=n-1)&&(2*j!=m-1))
                    {
                        ans += res;
                    }
                    else
                    {
                        res /= 2;
                        ans += res;
                    }
                    arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    ll p = ar[i][j] + ar[i][m-j-1] + ar[n-i-1][j] + ar[n-i-1][m-j-1];
                    ll res = 0;
                    p /= 4;
                    res = abs(p-ar[i][j]) + abs(p-ar[i][m-j-1]) + abs(p-ar[n-i-1][j]) + abs(p-ar[n-i-1][m-j-1]);
                    if((i!=n-i-1)&&(j!=m-j-1))
                        ans += res;
                    else if(((i==n-i-1)&&(j!=m-j-1))||((i!=n-i-1)&&(j==m-j-1)))
                        ans += res/2;
                    arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0,res,res2;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        ll br[4];
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    br[0] = ar[i][j];
                    br[1] = ar[i][m-j-1];
                    br[2] = ar[n-i-1][j];
                    br[3] = ar[n-i-1][m-j-1];
                    sort(br,br+3);
                    ans += abs(ar[i][j] - br[1]);
                    //arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0,res,res2;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        ll br[4];
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    br[0] = ar[i][j];
                    br[1] = ar[i][m-j-1];
                    br[2] = ar[n-i-1][j];
                    br[3] = ar[n-i-1][m-j-1];
                    sort(br,br+3);
                    ans += br[3] - br[0] + br[2] - br[1];
                    arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans/4<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m];
        ll ans = 0;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
            }
        }
        ll br[4];
        f(i,0,n)
        {
            f(j,0,m)
            {
                br[0] = ar[i][j];
                br[1] = ar[i][m-j-1];
                br[2] = ar[n-i-1][j];
                br[3] = ar[n-i-1][m-j-1];
                sort(br,br+4);
                ans += abs(ar[i][j] - br[1]);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


//                                      CPP
//                                      Mind_flayer404

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector

#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
#define mod1 998244353

ll const N = 1e+6;

ll power(ll a,ll b,ll m)
{
    ll res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = ((res%m)*(a%m))%m;
        }
        a = ((a%m)*(a%m))%m;
        b /= 2;
    }
    return res;
}

ll modi(ll a,ll p)
{
    return power(a,p-2,p);
}

ll max(ll a,ll b)
{
    return a>b?a:b;
}

ll min(ll a,ll b)
{
    return a>b?b:a;
}

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll bins(ll ar[],ll l,ll r,ll x)
{
    if(l<=r)
    {
        ll mid = l + (r-1)/2;
        if(ar[mid]==x)
            return mid;
        else if(ar[mid]>x)
        {
            bins(ar,l,mid-1,x);
        }
        else if(ar[mid]<x)
        {
            bins(ar,mid+1,r,x);
        }
    }
    return -1;
}

int main()
{
    fast;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        ll ar[n][m],arr[n][m];;
        ll ans = 0,res,res2;
        f(i,0,n)
        {
            f(j,0,m)
            {
                cin>>ar[i][j];
                arr[i][j] = 0;
            }
        }
        f(i,0,n)
        {
            f(j,0,m)
            {
                if(arr[i][j]==0)
                {
                    res = 0,res2 = 0;
                    ll p = ar[i][j] + ar[i][m-j-1] + ar[n-i-1][j] + ar[n-i-1][m-j-1];
                    p /= 4;
                    res = abs(p - ar[i][j]) + abs(p - ar[i][m-j-1]) + abs(p - ar[n-i-1][j]) + abs(p - ar[n-i-1][m-j-1]);
                    res2 = abs(p - ar[i][j] + 1) + abs(p - ar[i][m-j-1] + 1) + abs(p - ar[n-i-1][j] + 1) + abs(p - ar[n-i-1][m-j-1] + 1);
                    res = min(res,res2);
                    if((2*i!=n-1)&&(2*j!=m-1))
                    {
                        ans += res;
                    }
                    else if(((2*i==n-1)&&(2*j!=m-1))||((2*i!=n-1)&&(2*j==m-1)))
                    {
                        res /= 2;
                        ans += res;
                    }
                    arr[i][j] = 1,arr[i][m-j-1] = 1,arr[n-i-1][j] = 1,arr[n-i-1][m-j-1] = 1;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<math.h>
using namespace std;

int main() {
    int i,j,t,n,m;
    long long int s;
    long int a[100][100];
    long long int count = 0;
    cin>>t;
    while(t--) {
        count=0;
        s=0;
        cin>>n>>m;
        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                cin>>a[i][j];
            }
        }

        for(i = 0; i < n/2; i++) {
            for(j = 0; j < m/2; j++) {
                s = round((a[i][j] + a[i][m-j-1] + a[n-i-1][j] + a[n-i-1][m-j-1])/4);
                count += abs(a[i][j]-s) + abs(a[i][m-j-1]-s) + abs(a[n-i-1][j]-s) + abs(a[n-i-1][m-j-1]-s);
            }
        }

        if(n%2 != 0) {
            s = 0;
            for(i = 0; i < m; i++) {
                s += a[n/2][i];
            }
            s = round(s/4);
            for(i = 0; i < m; i++) {
                count += abs(a[n/2][i]-s);
            }
        }

        if(m%2 != 0) {
            s = 0;
            for(i = 0; i < n; i++) {
                s += a[i][m/2];
            }
            s = round(s/4);
            for(i = 0; i < n; i++) {
                count += abs(a[i][m/2]-s);
            }
        }

        cout<<count<<endl;
    }

}


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<math.h>
using namespace std;

int main() {
    int i,j,t,n,m;
    int s;
    long int a[100][100];
    long long int count = 0;
    cin>>t;
    while(t--) {
        count=0;
        cin>>n>>m;
        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                cin>>a[i][j];
            }
        }

        for(i = 0; i < n/2; i++) {
            for(j = 0; j < m/2; j++) {
                s = round((a[i][j] + a[i][m-j-1] + a[n-i-1][j] + a[n-i-1][m-j-1])/4);
                count += abs(a[i][j]-s) + abs(a[i][m-j-1]-s) + abs(a[n-i-1][j]-s) + abs(a[n-i-1][m-j-1]-s);
            }
        }

        if(n%2 != 0) {
            s = 0;
            for(i = 0; i < m; i++) {
                s += a[n/2][i];
            }
            s = round(s/4);
            for(i = 0; i < m; i++) {
                count += abs(a[n/2][i]-s);
            }
        }

        if(m%2 != 0) {
            s = 0;
            for(i = 0; i < n; i++) {
                s += a[i][m/2];
            }
            s = round(s/4);
            for(i = 0; i < n; i++) {
                count += abs(a[i][m/2]-s);
            }
        }

        cout<<count<<endl;
    }

}


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<math.h>
using namespace std;

int main() {
    int i,j,t,n,m;
    long long int s = 0,count=0;
    long long int a[100][100];
    cin>>t;
    while(t--) {
        count=0;
        s=0;
        cin>>n>>m;
        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                cin>>a[i][j];
            }
        }

        for(i = 0; i < n/2; i++) {
            for(j = 0; j < m/2; j++) {
                s = round((a[i][j] + a[i][m-j-1] + a[n-i-1][j] + a[n-i-1][m-j-1])/4);
                count += abs(a[i][j]-s) + abs(a[i][m-j-1]-s) + abs(a[n-i-1][j]-s) + abs(a[n-i-1][m-j-1]-s);
            }
        }

        if(n%2 != 0) {
            s = 0;
            for(i = 0; i < m; i++) {
                s += a[n/2][i];
            }
            s = round(s/4);
            for(i = 0; i < m; i++) {
                count += abs(a[n/2][i]-s);
            }
        }

        if(m%2 != 0) {
            s = 0;
            for(i = 0; i < n; i++) {
                s += a[i][m/2];
            }
            s = round(s/4);
            for(i = 0; i < n; i++) {
                count += abs(a[i][m/2]-s);
            }
        }

        cout<<count<<endl;
    }

}


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<math.h>
using namespace std;

int main() {
    int i,j,t,n,m;
    long long int s = 0,count=0;
    cin>>t;
    while(t--) {
        count=0;
        s=0;
        cin>>n>>m;
        long long int a[n][m];
        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                cin>>a[i][j];
            }
        }

        for(i = 0; i < n/2; i++) {
            for(j = 0; j < m/2; j++) {
                s = round((a[i][j] + a[i][m-j-1] + a[n-i-1][j] + a[n-i-1][m-j-1])/4);
                count += abs(a[i][j]-s) + abs(a[i][m-j-1]-s) + abs(a[n-i-1][j]-s) + abs(a[n-i-1][m-j-1]-s);
            }
        }

        if(n%2 != 0) {
            s = 0;
            for(i = 0; i < m; i++) {
                s += a[n/2][i];
            }
            s = round(s/4);
            for(i = 0; i < m; i++) {
                count += abs(a[n/2][i]-s);
            }
        }

        if(m%2 != 0) {
            s = 0;
            for(i = 0; i < n; i++) {
                s += a[i][m/2];
            }
            s = round(s/4);
            for(i = 0; i < n; i++) {
                count += abs(a[i][m/2]-s);
            }
        }

        cout<<count<<endl;
    }

}


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<math.h>
using namespace std;

int main() {
    int i,j,t,n,m;
    long long int s;
    long int a[100][100];
    long long int count = 0;
    cin>>t;
    while(t--) {
        count=0;
        cin>>n>>m;
        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                cin>>a[i][j];
            }
        }

        for(i = 0; i < n/2; i++) {
            for(j = 0; j < m/2; j++) {
                s = round((a[i][j] + a[i][m-j-1] + a[n-i-1][j] + a[n-i-1][m-j-1])/4);
                count += abs(a[i][j]-s) + abs(a[i][m-j-1]-s) + abs(a[n-i-1][j]-s) + abs(a[n-i-1][m-j-1]-s);
            }
        }

        if(n%2 != 0) {
            s = 0;
            for(i = 0; i < m; i++) {
                s += a[n/2][i];
            }
            s = round(s/4);
            for(i = 0; i < m; i++) {
                count += abs(a[n/2][i]-s);
            }
        }

        if(m%2 != 0) {
            s = 0;
            for(i = 0; i < n; i++) {
                s += a[i][m/2];
            }
            s = round(s/4);
            for(i = 0; i < n; i++) {
                count += abs(a[i][m/2]-s);
            }
        }

        cout<<count<<endl;
    }

}


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<math.h>
using namespace std;

int main() {
    int i,j,t,n,m;
    unsigned long long int s = 0,count=0;
    long long int a[100][100];
    cin>>t;
    while(t--) {
        count=0;
        s=0;
        cin>>n>>m;
        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                cin>>a[i][j];
            }
        }

        for(i = 0; i < n/2; i++) {
            for(j = 0; j < m/2; j++) {
                s = round((a[i][j] + a[i][m-j-1] + a[n-i-1][j] + a[n-i-1][m-j-1])/4);
                count += abs(a[i][j]-s) + abs(a[i][m-j-1]-s) + abs(a[n-i-1][j]-s) + abs(a[n-i-1][m-j-1]-s);
            }
        }

        if(n%2 != 0) {
            s = 0;
            for(i = 0; i < m; i++) {
                s += a[n/2][i];
            }
            s = round(s/4);
            for(i = 0; i < m; i++) {
                count += abs(a[n/2][i]-s);
            }
        }

        if(m%2 != 0) {
            s = 0;
            for(i = 0; i < n; i++) {
                s += a[i][m/2];
            }
            s = round(s/4);
            for(i = 0; i < n; i++) {
                count += abs(a[i][m/2]-s);
            }
        }

        cout<<count<<endl;
    }

}


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<math.h>
using namespace std;

int main() {
    int i,j,t,n,m;
    long long int s = 0,count=0;
    long long int b[4];
    cin>>t;
    while(t--) {
        count=0;
        s=0;
        cin>>n>>m;
        long long int a[n][m];
        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                cin>>a[i][j];
            }
        }

        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                b[0] = a[i][j];
                b[1] = a[i][m-j-1];
                b[2] = a[n-i-1][j];
                b[3] = a[n-i-1][m-j-1];
                sort(b,b+4);
                count += abs(a[i][j]-b[2]);
            }
        }
        cout<<count<<endl;
    }

}