n=int(input());s=raw_input().split(" ");l=[int(x) for x in s]
m=max(l)
if((m-n)%2==0):
	print "Bob"
else:
	print "Alice"


from fractions import gcd
n = input()
v = list(map(int, raw_input().split()))
M = max(v)

cmmdc = v[0]
for i in range(1, len(v)):
    cmmdc = gcd(cmmdc, v[i])

sol = M // cmmdc - n
if sol % 2 == 1: print 'Alice'
else: print 'Bob'


def gcd(a, b) :
	return a if not b else gcd(b, a%b);

n = input();
a = map(int, raw_input().split());
m = max(a);
g = a[0];
for i in xrange(1, n) :
	g = gcd(g, a[i]);
le = len(a);
print "Alice" if (m / g - le) % 2 else "Bob";


n = input();
a = map(int, raw_input().split());
le = len(set(a))+1 if len(set(a))!=len(a) else len(a);
print "Alice" if (max(a) - le) % 2 else "Bob";


n = input();
a = map(int, raw_input().split());
le = len(set(a))+1 if len(set(a))!=len(a) else len(a);
print "Alice" if (max(a) - le)) % 2 else "Bob";


n = input();
a = map(int, raw_input().split());
print "Alice" if (max(a) - len(set(a))) % 2 else "Bob";


n = input();
a = map(int, raw_input().split());
print "Alice" if (max(a) - n) % 2 else "Bob";


from fractions import gcd
n=input()
a=map(int,raw_input().split())
d=reduce(gcd,a)
print ["Bob","Alice"][(max(a)/d-n)%2]


import fractions
n = int(raw_input())
a = map(int, raw_input().split())
mx = max(a)
gcd = reduce(fractions.gcd, a)
ans = mx / gcd - n
print "Alice" if ans % 2 == 1 else "Bob"


from fractions import gcd
n=int(raw_input())
l=map(int,raw_input().split())
g=l[0]
for i in l:
    g=gcd(g,i)
if (max(l)/g-n)%2==0:
    print "Bob"
else:
    print "Alice"


from fractions import gcd
n = input()
L = sorted(map(int, raw_input().split()))
d = reduce(gcd, L)
L = [x/d for x in L]
if (max(L)-n)%2 == 1:
	print "Alice"
else:
	print "Bob"


import fractions
n = input()
a = map(int, raw_input().split())
gcd = reduce(fractions.gcd, a)
moves = max(a) / gcd - n
print(['Bob', 'Alice'][moves % 2])


n=input()
a=map(int,raw_input().split())
print ["Bob","Alice"][(max(a)-n)%2]


def gcd(a,b):
    while b:
        a,b=b,a%b
    return a
n=input()
a=sorted(map(int,raw_input().split()))
g=reduce(gcd, a)
print ["Alice","Bob"][((a[-1])/g + 1 - n) % 2]


#coding:utf-8
#comentário para reenvio

def gcd(x,y):
	while y!=0:
		temp = y
		y = x % y
		x = temp
	return x

n = int(raw_input())

lista = map(int, raw_input().split())

maximum = max(lista)

d = gcd(lista[0], lista[1])

for num in range(2, len(lista)):
	d = gcd(d, lista[num])

ans = (maximum/d) - n

if ans % 2 == 0:
	print "Bob"
else:
	print "Alice"


def gcd(x,y):
	while y!=0:
		temp = y
		y = x % y
		x = temp
	return x

n = int(raw_input())

lista = map(int, raw_input().split())

maximum = max(lista)

d = gcd(lista[0], lista[1])

for num in range(2, len(lista)):
	d = gcd(d, lista[num])

ans = (maximum/d) - n

if ans % 2 == 0:
	print "Bob"
else:
	print "Alice"


#comentário para reenvio

def gcd(x,y):
	while y!=0:
		temp = y
		y = x % y
		x = temp
	return x

n = int(raw_input())

lista = map(int, raw_input().split())

maximum = max(lista)

d = gcd(lista[0], lista[1])

for num in range(2, len(lista)):
	d = gcd(d, lista[num])

ans = (maximum/d) - n

if ans % 2 == 0:
	print "Bob"
else:
	print "Alice"


import fractions
n = int(raw_input())
s =  map(int, raw_input().split())
mcd = reduce(fractions.gcd, s)
if (max(s)/mcd - n) % 2 == 0:
    print "Bob"
else:
    print "Alice"


n = int(raw_input())
s =  map(int, raw_input().split())
if (max(s) - n) % 2 == 0:
    print "Bob"
else:
    print "Alice"


#to namiri daram python mizanam =)
def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

n = int(raw_input())
inp = raw_input()
div = 0
mx = 0
for stri in inp.split():
	num = int(stri)
	div = gcd(div, num)
	mx = max(mx, num)

mx /= div
mx -= n
if mx % 2 == 1:
	print "Alice"
else:
	print "Bob"


#coding=utf-8

def gcd(a, b):
  if b == 0:
    return a
  return gcd(b, a % b)

n = int(raw_input())
a = map(int, raw_input().split())
a = sorted(a)
g = reduce(gcd, a)

m = a[-1] / g - n
if m % 2 == 1:
  print "Alice"
else:
  print "Bob"


n = int(raw_input())

a = map(int, raw_input().split())

mx = max(a)

if ((mx - len(a))%2 == 1):
	print "Alice"
else:
	print "Bob"


def gcd(a, b):
	if a == 0:
		return b
	else:
		return gcd(b%a, a)

n = int(raw_input())

a = sorted(map(int, raw_input().split()))

b = [a[0]] + [a[i] - a[i-1] for i in xrange(1,len(a))]

num = reduce(lambda x,y: gcd(x,y), b)

if ((max(a)/num - len(a))%2 == 1):
	print "Alice"
else:
	print "Bob"


from fractions import *
n = input()
a = map(int,raw_input().split())
print 'Alice' if (max(a)/reduce(gcd,a)-n)%2 else 'Bob'


def read(): return map(int, raw_input().split())
n, a = input(), read()
cen, flag = 1, 0
while(flag == 0):
    for i in a:
        for j in a:
            if(i != j):
                t = abs(i-j)
                if(not (t in a)):
                    a, cen, flag = a+[t], 3-cen, 1
                    break
    flag -= 1

if(cen == 1): print 'Bob'
else: print 'Alice'


from fractions import gcd
n, a = input(), sorted(map(int, raw_input().split()))
d = reduce(gcd, a)
if (max(a)/d-n)%2 == 1: print "Alice"
else: print "Bob"


from fractions import gcd

n=int(raw_input())
a=map(int,raw_input().split())
num=max(a)/reduce(gcd,a)-n
if num%2:
    print "Alice"
else:
    print "Bob"


from fractions import gcd

n=int(raw_input())
a=map(int,raw_input().split())
num=reduce(gcd,a)-n
if num%2:
    print "Alice"
else:
    print "Bob"


import fractions
n = int(input())
a = list(map(int, raw_input().split()))
gcd = reduce(fractions.gcd, a)
moves = max(a) / gcd - n
print(['Bob', 'Alice'][moves % 2])


import fractions
n = input()
a = map(int, raw_input().split())
gcd = reduce(fractions.gcd, a)
moves = max(a) / gcd - n
print(['Bob', 'Alice'][moves % 2])


l=lambda: map(int,raw_input().split())
def gcd(a, b):
    if b==0: return a
    return gcd(b,a%b)
n=input()
a=l()
a.sort()
g=reduce(gcd, a)
m=(a[-1]/g)-n
if m%2==1:
    print "Alice"
else:
    print "Bob"


#coding: utf-8
# Codeforces - 346A
# Alice and Bob

primos = [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]

n = int(raw_input())
a = map(int, raw_input().split())

maior = max(a)
p = 0

#verificar se tem algum primo em comum
for i in primos:
    failed = False

    for j in range(n):
        if a[j]%i != 0:
            failed = True
            break

    if not failed:
        p = i
        break

if (maior/p-n)%2 == 0:
    print "Bob"
else:
    print "Alice"


#coding: utf-8
# Codeforces - 346A
# Alice and Bob

primos = [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]

n = int(raw_input())
a = map(int, raw_input().split())

maior = max(a)
universo = []
p = 0

#verificar se tem algum primo em comum
for i in primos:
    universo = range(0, maior+1, i)
    failed = False

    for j in range(n):
        if a[j] not in universo:
            failed = True
            break

    if not failed:
        p = i
        break

if (len(universo)-1-n)%2 == 0:
    print "Bob"
else:
    print "Alice"


#coding: utf-8
# Codeforces - 346A
# Alice and Bob

n = int(raw_input())
a = map(int, raw_input().split())
if (max(a)-n)%2 == 0:
    print "Bob"
else:
    print "Alice"


#coding: utf-8
# Codeforces - 346A
# Alice and Bob

def mdc(a, b):
    if b == 0:
        return a
    else:
        return mdc(b, a%b)

n = int(raw_input())
a = map(int, raw_input().split())

p = 0
maior = 0

#verificar se tem algum primo em comum
for i in a:
    p = mdc(p, i)
    maior = max(maior, i)

if (maior/p-n)%2 == 0:
    print "Bob"
else:
    print "Alice"


I=lambda:map(int, raw_input().split())
def gcd(x, y):
    if x == 0: return y
    return gcd(y%x, x)
n = input()
a = I()
x = 0
for y in a:
    x = gcd(x, y)
print 'Alice' if (max(a)/x - n) % 2 == 1 else 'Bob'


n = input()
a = map(int, raw_input().split())

def gcd(x, y):
    return x == 0 and y or gcd(y % x, x)

p = max(a) / reduce(gcd, a) - n

print p & 1 and 'Alice' or 'Bob'


def euclid(a, b):
    while b != 0:
        a, b = b, a % b
    return a

n = input()
s = [int(x) for x in raw_input().split()]

nod = euclid(s[0], s[1])
for i in s:
    nod = euclid(nod, i)
    if nod == 1:
        break

for i in xrange(n):
    s[i] /= nod

if (max(s) - n) % 2 == 0:
    print "Bob"
else:
    print "Alice"


from fractions import gcd
n=input()
a=map(int,raw_input().split())
d=reduce(gcd,a)
print ["Bob","Alice"][(max(a)/d-n)%2]


import fractions
n,a=input(),map(int,raw_input().split())
gcd=reduce(fractions.gcd,a)
print (['Bob','Alice'][(max(a)/gcd-n)%2])


import math
import fractions
def gcd(a,b):
    if (b==0):
        return a
    else:
        return gcd(b,a%b)
n=input()
a=map(int,raw_input().split())
a.sort() #������
g=reduce(gcd,a)
'''reduce(����(a,b),����) ��ʾ����������ִ�к��� ��redece(gcd,a) ��ʾ���������gcd'''
print ['Bob','Alice'][(max(a)/g-n)&1]


import math
import fractions
def gcd(a,b):
    if (b==0):
        return a
    else:
        return gcd(b,a%b)
n=input()
a=map(int,raw_input().split())
a.sort() #排序函数
g=reduce(gcd,a)
#'''reduce(函数(a,b),数列) 表示对数列两两执行函数 如redece(gcd,a) 表示求所有数的gcd'''
print ['Bob','Alice'][(max(a)/g-n)&1]


def gcd(a,b):
    if (b==0):
        return a
    else:
        return gcd(b,a%b)

n=input()
a=map(int,raw_input().split())
a.sort()
g=reduce(gcd,a)
print (['Bob','Alice'][(max(a)/g-n)&1])


from fractions import gcd

ni = lambda: int(raw_input())
nai = lambda: map(int, raw_input().split())

n = ni()
a = nai()
p = max(a) / reduce(gcd, a) - n
print "Alice" if (p & 1) == 1 else "Bob"


# -*- coding: utf-8 -*-

import fractions

if __name__ == '__main__':
    n = int(raw_input())
    aa = map(int, raw_input().split())

    d = reduce(fractions.gcd, aa)
    val = max(aa) / d - n
    if val % 2 == 0:
        result = 'Bob'
    else:
        result = 'Alice'

    print(str(result))


#include <stdio.h>
#include <stdlib.h>

int main()
{
	int n,i=1,item,r;
	scanf("%d",&n);
	scanf("%d",&item);
	int max=item,gcd=item;
	while(i<n)
	{
		scanf("%d",&item);
		if(item>max)
			max=item;

		while(item!=0)
		{
			r=gcd%item;
			gcd=item;
			item=r;
		}

		i++;
	}
	((max-n)/gcd%2!=0)?printf("Alice"):printf("Bob");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
	int n,i=1,item,r;
	scanf("%d",&n);
	scanf("%d",&item);
	int max=item,gcd=item;
	while(i<n)
	{
		scanf("%d",&item);
		if(item>max)
			max=item;

		while(item!=0)
		{
			r=gcd%item;
			gcd=item;
			item=r;
		}

		i++;
	}
	((max/gcd-n)%2!=0)?printf("Alice"):printf("Bob");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
#define mod 1000000007
#define ms(a,x) memset(a,x,sizeof(a))
//*************************** mekch ********************************************\\

int main()
{
    run;
    ll t;
    cin>>t;
    while(t--)
    {
       ll i,j,n,m,ans[4],res,res1,ans1=0,k;
       cin>>n>>m;
      ll 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++)
           {
               ans[0]=a[i][j];
               ans[1]=a[n-1-i][j];
               ans[2]=a[n-1-i][m-1-j];
               ans[3]=a[i][m-1-j];

               sort(ans,ans+4);
               res=ans[2];

               for(k=0;k<4;k++)
               ans1+= abs(ans[k]-res);

           }

       }
       cout<<ans1/4<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
#define mod 1000000007
#define ms(a,x) memset(a,x,sizeof(a))
//*************************** mekch ********************************************\\

int main()
{
    run;
    ll t;
    cin>>t;
    while(t--)
    {
       ll i,j,n,m,ans=0,res,res1,ans1=0,ans2=0,ans3=0,real,real1,real2,res2,res3;
       cin>>n>>m;
      ll 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++)
           {

             res=a[i][j]+a[n-1-i][j]+a[i][m-1-j]+a[n-1-i][m-1-j];
             res=res/4;
             res1=res+1;
             res2=res+2;
             res3=res+3;

            ans+=abs(a[i][j]-res);
            ans+=abs(a[n-1-i][j]-res);
            ans+=abs(a[i][m-1-j]-res);
            ans+=abs(a[n-1-i][m-1-j]-res);

             ans1+=abs(a[i][j]-res1);
            ans1+=abs(a[n-1-i][j]-res1);
            ans1+=abs(a[i][m-1-j]-res1);
            ans1+=abs(a[n-1-i][m-1-j]-res1);

            ans2+=abs(a[i][j]-res2);
            ans2+=abs(a[n-1-i][j]-res2);
            ans2+=abs(a[i][m-1-j]-res2);
            ans2+=abs(a[n-1-i][m-1-j]-res2);

            ans3+=abs(a[i][j]-res3);
            ans3+=abs(a[n-1-i][j]-res3);
            ans3+=abs(a[i][m-1-j]-res3);
            ans3+=abs(a[n-1-i][m-1-j]-res3);

           real=min(ans,ans1);
           real1=min(real,ans2);
           real2=min(real1,ans3);

           if(ans1==real2)
             res=res1;
            else if(ans2==real2)
             res=res2;
             else if(ans3==real2)
             res=res3;

           ans1=real2;
           ans=real2;
           ans2=real2;
           ans3=real2;

            a[i][j]=res;
            a[n-i-1][j]=res;
            a[i][m-1-j]=res;
            a[n-1-i][m-1-j]=res;

        }
       }
      //cout<<ans<<endl;

         res=0;

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

               res=a[i][j]+a[i][m-1-j];

                  res=res/2;

            ans+=abs(a[i][j]-res);
            ans+=abs(a[i][m-1-j]-res);
            a[i][j]=res;
            a[i][m-1-j]=res;

           }
       }

       for(i=0;i<n/2;i++)
       {
           for(j=0;j<m;j++)
           {
               res=a[i][j]+a[n-i-1][j];
               res=res/2;

                ans+=abs(a[i][j]-res);
             ans+=abs(a[n-i-1][j]-res);
             a[i][j]=res;
             a[n-i-1][j]=res;

           }
       }

       cout<<ans<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define pb push_back

int main(){
	int t;
	cin>>t;

	while(t--){
		int n, m;
		cin>>n>>m;

		int a[n][m];
		for(int i=0; i<n; i++)
			for(int j=0; j<m; j++)
				cin>>a[i][j];

		ll ans {0};
		for(int i=0; i<n; i++)
			for(int j=0; j<m; j++){
				ll v[4];
				v[0] = a[i][j]; v[1] = a[i][m-1-j];
				v[2] = a[n-1-i][j]; v[3] = a[n-1-i][m-1-j];
				sort(v, v+4);
				ans += abs(a[i][j]-v[1]);
			}

		cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define pb push_back

int main(){
	int t;
	cin>>t;

	while(t--){
		int n, m;
		cin>>n>>m;

		int a[n][m];
		for(int i=0; i<n; i++)
			for(int j=0; j<m; j++)
				cin>>a[i][j];

		ll avg, ans {0};
		for(int i=0; i<n/2; i++)
			for(int j=0; j<m/2; j++){
				ll tr, tl, br, bl;
				tl = a[i][j]; tr = a[i][m-1-j];
				bl = a[n-1-i][j]; br = a[n-1-i][m-1-j];
				avg = (tl+tr+bl+br)/4;
				ans += abs(tl-avg)+abs(tr-avg)+abs(bl-avg)+abs(br-avg);
			}

		if(n%2)
			for(int i=0; i<m/2; i++){
				ll l, r;
				l = a[n/2][i]; r = a[n/2][m-1-i];
				avg = (l+r)/2;
				ans += abs(l-avg)+abs(r-avg);
			}
		if(m%2)
			for(int i=0; i<n/2; i++){
				ll t, b;
				t = a[i][m/2]; b = a[n-i-1][m/2];
				avg = (t+b)/2;
				ans += abs(t-avg)+abs(b-avg);
			}

		cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define pb push_back

int main(){
	int t;
	cin>>t;

	while(t--){
		int n, m;
		cin>>n>>m;

		int a[n][m];
		for(int i=0; i<n; i++)
			for(int j=0; j<m; j++)
				cin>>a[i][j];

		ll avg, ans {0};
		for(int i=0; i<n/2; i++)
			for(int j=0; j<m/2; j++){
				int tr, tl, br, bl;
				tl = a[i][j]; tr = a[i][m-1-j];
				bl = a[n-1-i][j]; br = a[n-1-i][m-1-j];
				avg = (tl+tr+bl+br)/4;
				ans += abs(tl-avg)+abs(tr-avg)+abs(bl-avg)+abs(br-avg);
			}

		if(n%2)
			for(int i=0; i<m/2; i++){
				int l, r;
				l = a[n/2][i]; r = a[n/2][m-1-i];
				avg = (l+r)/2;
				ans += abs(l-avg)+abs(r-avg);
			}
		if(m%2)
			for(int i=0; i<n/2; i++){
				int t, b;
				t = a[i][m/2]; b = a[n-i-1][m/2];
				avg = (t+b)/2;
				ans += abs(t-avg)+abs(b-avg);
			}

		cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define pb push_back

int main(){
	int t;
	cin>>t;

	while(t--){
		int n, m;
		cin>>n>>m;

		int a[n][m];
		for(int i=0; i<n; i++)
			for(int j=0; j<m; j++)
				cin>>a[i][j];

		ll avg, ans {0};
		for(int i=0; i<n/2; i++)
			for(int j=0; j<m/2; j++){
				int tr, tl, br, bl;
				tl = a[i][j]; tr = a[i][m-1-j];
				bl = a[n-1-i][j]; br = a[n-1-i][m-1-j];
				avg = (tl+tr+bl+br)/4;
				ans += abs(tl-avg)+abs(tr-avg)+abs(bl-avg)+abs(br-avg);
			}

		if(n%2)
			for(int i=0; i<m/2; i++){
				ll l, r;
				l = a[n/2][i]; r = a[n/2][m-1-i];
				avg = (l+r)/2;
				ans += abs(l-avg)+abs(r-avg);
			}
		if(m%2)
			for(int i=0; i<n/2; i++){
				ll t, b;
				t = a[i][m/2]; b = a[n-i-1][m/2];
				avg = (t+b)/2;
				ans += abs(t-avg)+abs(b-avg);
			}

		cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define pb push_back

int main(){
	int t;
	cin>>t;

	while(t--){
		int n, m;
		cin>>n>>m;

		int a[n][m];
		for(int i=0; i<n; i++)
			for(int j=0; j<m; j++)
				cin>>a[i][j];

		ll avg1, avg2, ans {0};
		for(int i=0; i<n/2; i++)
			for(int j=0; j<m/2; j++){
				ll tr, tl, br, bl;
				tl = a[i][j]; tr = a[i][m-1-j];
				bl = a[n-1-i][j]; br = a[n-1-i][m-1-j];
				avg1 = (tl+tr+bl+br)/4;
				avg2 = (tl+tr+bl+br+3)/4;
				ans += min(abs(tl-avg1)+abs(tr-avg1)+abs(bl-avg1)+abs(br-avg1),
				abs(tl-avg2)+abs(tr-avg2)+abs(bl-avg2)+abs(br-avg2));
			}

		if(n%2)
			for(int i=0; i<m/2; i++){
				ll l, r;
				l = a[n/2][i]; r = a[n/2][m-1-i];
				avg1 = (l+r)/2;
				avg2 = (l+r+1)/2;
				ans += min(abs(l-avg1)+abs(r-avg1),
				abs(l-avg2)+abs(r-avg2));
			}
		if(m%2)
			for(int i=0; i<n/2; i++){
				ll t, b;
				t = a[i][m/2]; b = a[n-i-1][m/2];
				avg1 = (t+b)/2;
				avg2 = (t+b+1)/2;
				ans += min(abs(t-avg1)+abs(b-avg1),
				abs(t-avg2)+abs(b-avg2));
			}

		cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define lowbit(x) x & -x
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2e5+5;
int a[105][105];
int main(){
#ifdef Mizp
    freopen("in.txt","r",stdin);
#endif
    int T;
    cin>>T;
    while(T--){
        int n,m;
        cin>>n>>m;

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%d",&a[i][j]);
            }
        }
        ll ans=0;
        for(int i=1;i<=n/2;i++){
            for(int j=1;j<=m/2;j++){
                ll sum=(1ll*a[i][j]+a[n-i+1][j]+a[i][m-j+1]+a[n-i+1][m-j+1]);
                ll avg=sum/4;
                //if(sum%4>2)
                 //   avg++;
                ans=ans+abs(a[i][j]-avg)+abs(a[n-i+1][j]-avg)+abs(a[i][m-j+1]-avg)+abs(a[n-i+1][m-j+1]-avg);
                //cout<<ans<<endl;
            }
        }
        if(m%2){
            int t=m/2+1;
            for(int i=1;i<=n/2;i++){
                ll sum=1ll*a[i][t]+a[n-i+1][t];
                ll avg=sum/2;
                ans=ans+abs(a[i][t]-avg)+abs(a[n-i+1][t]-avg);
            }
        }
        if(n%2){
            int t=n/2+1;
            for(int i=1;i<=m/2;i++){
                ll sum=1ll*a[t][i]+a[t][m-i+1];
                ll avg=sum/2;
                ans+=abs(a[t][i]-avg)+abs(a[t][m-i+1]-avg);
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define lowbit(x) x & -x
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2e5+5;
int a[105][105];
int main(){
#ifdef Mizp
    freopen("in.txt","r",stdin);
#endif
    int T;
    cin>>T;
    while(T--){
        int n,m;
        cin>>n>>m;

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%d",&a[i][j]);
            }
        }
        ll ans=0;
        for(int i=1;i<=n/2;i++){
            for(int j=1;j<=m/2;j++){
                ll sum=(1ll*a[i][j]+a[n-i+1][j]+a[i][m-j+1]+a[n-i+1][m-j+1]);
                ll avg=sum/4;
                if(sum%4>2)
                    avg++;
                ans=ans+abs(a[i][j]-avg)+abs(a[n-i+1][j]-avg)+abs(a[i][m-j+1]-avg)+abs(a[n-i+1][m-j+1]-avg);
                //cout<<ans<<endl;
            }
        }
        if(m%2){
            int t=m/2+1;
            for(int i=1;i<=n/2;i++){
                ll sum=1ll*a[i][t]+a[n-i+1][t];
                ll avg=sum/2;
                ans=ans+abs(a[i][t]-avg)+abs(a[n-i+1][t]-avg);
            }
        }
        if(n%2){
            int t=n/2+1;
            for(int i=1;i<=m/2;i++){
                ll sum=1ll*a[t][i]+a[t][m-i+1];
                ll avg=sum/2;
                ans+=abs(a[t][i]-avg)+abs(a[t][m-i+1]-avg);
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define lowbit(x) x & -x
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2e5+5;
int main(){
#ifdef Mizp
    freopen("in.txt","r",stdin);
#endif
    int T;
    scanf("%d",&T);
    while(T--){
        int a,b,c;
        cin>>a>>b>>c;
        if(b==c){
            cout<<a<<'\n';
        }else{
            ll tmp=abs(b-c);
            tmp=sqrt(1ll*a*a+tmp*tmp)+1;
            cout<<tmp<<'\n';
        }
    }
    return 0;
}


    #include<bits/stdc++.h>
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #define INF 0x3f3f3f3f
    #define lowbit(x) x & -x
    #define lson root<<1,l,mid
    #define rson root<<1|1,mid+1,r
    using namespace std;
    typedef long long ll;
    const int mod=1e9+7;
    const int N=2e5+5;
    ll a[105][105];
    int main(){
    #ifdef Mizp
        freopen("in.txt","r",stdin);
    #endif
        int T;
        cin>>T;
        while(T--){
            int n,m;
            cin>>n>>m;

            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    scanf("%d",&a[i][j]);
                }
            }
            ll ans=0;
            for(int i=1;i<=n/2;i++){
                for(int j=1;j<=m/2;j++){
                    ll sum=(a[i][j]+a[n-i+1][j]+a[i][m-j+1]+a[n-i+1][m-j+1]);
                    ll avg=sum/4;
                    if(sum%4>2)
                    avg++;
                    ans=ans+abs(a[i][j]-avg)+abs(a[n-i+1][j]-avg)+abs(a[i][m-j+1]-avg)+abs(a[n-i+1][m-j+1]-avg);
                    //cout<<ans<<endl;
                }
            }
            if(m%2){
                int t=m/2+1;
                for(int i=1;i<=n/2;i++){
                    ll sum=1ll*a[i][t]+a[n-i+1][t];
                    ll avg=sum/2;
                    ans=ans+abs(a[i][t]-avg)+abs(a[n-i+1][t]-avg);
                }
            }
            if(n%2){
                int t=n/2+1;
                for(int i=1;i<=m/2;i++){
                    ll sum=1ll*a[t][i]+a[t][m-i+1];
                    ll avg=sum/2;
                    ans=ans+abs(a[t][i]-avg)+abs(a[t][m-i+1]-avg);
                }
            }
            cout<<ans<<'\n';
        }
        return 0;
    }


    #include<bits/stdc++.h>
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #define INF 0x3f3f3f3f
    #define lowbit(x) x & -x
    #define lson root<<1,l,mid
    #define rson root<<1|1,mid+1,r
    using namespace std;
    typedef long long ll;
    const int mod=1e9+7;
    const int N=2e5+5;
    ll a[105][105];
    int main(){
    #ifdef Mizp
        freopen("in.txt","r",stdin);
    #endif
        int T;
        cin>>T;
        while(T--){
            int n,m;
            cin>>n>>m;

            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    scanf("%d",&a[i][j]);
                }
            }
            ll ans=0;
            for(int i=1;i<=n/2;i++){
                for(int j=1;j<=m/2;j++){
                    ll temp[4]={a[i][j],a[n-i+1][j],a[i][m-j+1],a[n-i+1][m-j+1]};
                    sort(temp,temp+4);
                    ans=ans+abs(a[i][j]-temp[2])+abs(a[n-i+1][j]-temp[2])+abs(a[i][m-j+1]-temp[2])+abs(a[n-i+1][m-j+1]-temp[2]);
                }
            }
            if(m%2){
                int t=m/2+1;
                for(int i=1;i<=n/2;i++){
                    ans=ans+abs(a[i][t]-a[n-i+1][t]);
                }
            }
            if(n%2){
                int t=n/2+1;
                for(int i=1;i<=m/2;i++){
                    ans=ans+abs(a[t][i]-a[t][m-i+1]);
                }
            }
            cout<<ans<<'\n';
        }
        return 0;
    }


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define lowbit(x) x & -x
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2e5+5;
int a[105][105];
int main(){
#ifdef Mizp
    freopen("in.txt","r",stdin);
#endif
    int T;
    cin>>T;
    while(T--){
        int n,m;
        cin>>n>>m;

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%d",&a[i][j]);
            }
        }
        ll ans=0;
        for(int i=1;i<=n/2;i++){
            for(int j=1;j<=m/2;j++){
                ll sum=(1ll*a[i][j]+a[n-i+1][j]+a[i][m-j+1]+a[n-i+1][m-j+1]);
                ll avg=sum/4;
                if(sum%4>2)
                   avg++;
                ans=ans+abs(a[i][j]-avg)+abs(a[n-i+1][j]-avg)+abs(a[i][m-j+1]-avg)+abs(a[n-i+1][m-j+1]-avg);
                //cout<<ans<<endl;
            }
        }
        if(m%2){
            int t=m/2+1;
            for(int i=1;i<=n/2;i++){
                ll sum=1ll*a[i][t]+a[n-i+1][t];
                ll avg=sum/2;
                ans=ans+abs(a[i][t]-avg)+abs(a[n-i+1][t]-avg);
            }
        }
        if(n%2){
            int t=n/2+1;
            for(int i=1;i<=m/2;i++){
                ll sum=1ll*a[t][i]+a[t][m-i+1];
                ll avg=sum/2;
                ans=ans+abs(a[t][i]-avg)+abs(a[t][m-i+1]-avg);
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(LL i=(j); i<(k); ++i)
#define pb push_back
#define PII pair<int,int>
#define PLL pair<long long, long long>
#define ini(a,j) memset(a,j,sizeof a)
#define rrep(i,j,k) for(LL i=j; i>=k; --i)
#define fi first
#define se second
#define LL long long
#define beg begin()
#define ed end()
#define all(x) x.begin(),x.end()

LL mod = 1e9+7;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int _n;
	cin>>_n;
	while(_n--){
		int n,m;
		cin>>n>>m;
		vector<vector<int> > a(n, vector<int>(m));
		vector<vector<int> > used(n, vector<int>(m,false));
		rep(i,0,n)
			rep(j,0,m)
				cin>>a[i][j];

		LL ans = 0;
		map<int, int> mp;
		bool flag = false;
		for(int i=0; i<n; i++){
			for(int j=0; j<m; j++){
				mp.clear();
				LL cnt = 0;
				LL ma = 0;
				flag = false;
				if(!used[i][j]){
					used[i][j]=1;
					mp[a[i][j]]++;
					flag = true;
				}
				if(!used[i][m-j-1]&&m-j-1!=j){

					used[i][m-j-1]=1;
					mp[a[i][m-j-1]] += 1;
					flag = true;
				}
				if(!used[n-i-1][j]&&n-i-1!=i){
					used[n-i-1][j]=1;
					mp[a[n-i-1][j]]++;
					flag = true;
				}
				if(!used[n-i-1][m-j-1]&&m-j-1!=j&&n-i-1!=i){
					flag = true;
					used[n-i-1][m-j-1]=1;
					mp[a[n-i-1][m-j-1]]++;
				}
				for(auto e:mp){
					ma += (LL)e.se*e.fi;
					cnt += (LL)e.se;
				}
				if(flag){
					ma = floor(1.0*ma/cnt+0.5);
					for(auto e:mp){
						ans += (LL)(abs(e.fi-ma))*e.se;

					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(LL i=(j); i<(k); ++i)
#define pb push_back
#define PII pair<int,int>
#define PLL pair<long long, long long>
#define ini(a,j) memset(a,j,sizeof a)
#define rrep(i,j,k) for(LL i=j; i>=k; --i)
#define fi first
#define se second
#define LL long long
#define beg begin()
#define ed end()
#define all(x) x.begin(),x.end()

LL mod = 1e9+7;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int _n;
	cin>>_n;
	while(_n--){
		int n,m;
		cin>>n>>m;
		vector<vector<int> > a(n, vector<int>(m));
		vector<vector<int> > used(n, vector<int>(m,false));
		rep(i,0,n)
			rep(j,0,m)
				cin>>a[i][j];

		LL ans = 0;
		map<int, int> mp;
		bool flag = false;
		for(int i=0; i<n; i++){
			for(int j=0; j<m; j++){
				mp.clear();
				int cnt = 0;
				int ma = 0;
				flag = false;
				if(!used[i][j]){
					used[i][j]=1;
					mp[a[i][j]]++;
					flag = true;
				}
				if(!used[i][m-j-1]){
					used[i][m-j-1]=1;
					mp[a[i][m-j-1]] += 1;
					flag = true;
				}
				if(!used[n-i-1][j]){
					used[n-i-1][j]=1;
					mp[a[n-i-1][j]]++;
					flag = true;
				}
				if(!used[n-i-1][m-j-1]){
					flag = true;
					used[n-i-1][m-j-1]=1;
					mp[a[n-i-1][m-j-1]]++;
				}
				for(auto e:mp){
					ma += e.se*e.fi;
					cnt += e.se;
				}
				if(flag){
					ma = floor(1.0*ma/cnt+0.5);
					for(auto e:mp){
						ans += (LL)(abs(e.fi-ma))*e.se;

					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(LL i=(j); i<(k); ++i)
#define pb push_back
#define PII pair<int,int>
#define PLL pair<long long, long long>
#define ini(a,j) memset(a,j,sizeof a)
#define rrep(i,j,k) for(LL i=j; i>=k; --i)
#define fi first
#define se second
#define LL long long
#define beg begin()
#define ed end()
#define all(x) x.begin(),x.end()

LL mod = 1e9+7;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int _n;
	cin>>_n;
	while(_n--){
		int n,m;
		cin>>n>>m;
		vector<vector<int> > a(n, vector<int>(m));
		vector<vector<int> > used(n, vector<int>(m,false));
		rep(i,0,n)
			rep(j,0,m)
				cin>>a[i][j];

		LL ans = 0;
		map<int, int> mp;
		bool flag = false;
		for(int i=0; i<n; i++){
			for(int j=0; j<m; j++){
				mp.clear();
				int cnt = 0;
				int ma = 0;
				flag = false;
				if(!used[i][j]){
					used[i][j]=1;
					mp[a[i][j]]++;
					flag = true;
				}
				if(!used[i][m-j-1]&&m-j-1!=j){

					used[i][m-j-1]=1;
					mp[a[i][m-j-1]] += 1;
					flag = true;
				}
				if(!used[n-i-1][j]&&n-i-1!=i){
					used[n-i-1][j]=1;
					mp[a[n-i-1][j]]++;
					flag = true;
				}
				if(!used[n-i-1][m-j-1]&&m-j-1!=j&&n-i-1!=i){
					flag = true;
					used[n-i-1][m-j-1]=1;
					mp[a[n-i-1][m-j-1]]++;
				}
				for(auto e:mp){
					ma += e.se*e.fi;
					cnt += e.se;
				}
				if(flag){
					ma = floor(1.0*ma/cnt+0.5);
					for(auto e:mp){
						ans += (LL)(abs(e.fi-ma))*e.se;

					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(LL i=(j); i<(k); ++i)
#define pb push_back
#define PII pair<int,int>
#define PLL pair<long long, long long>
#define ini(a,j) memset(a,j,sizeof a)
#define rrep(i,j,k) for(LL i=j; i>=k; --i)
#define fi first
#define se second
#define LL long long
#define beg begin()
#define ed end()
#define all(x) x.begin(),x.end()

LL mod = 1e9+7;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int _n;
	cin>>_n;
	while(_n--){
		int n,m;
		cin>>n>>m;
		vector<vector<int> > a(n, vector<int>(m));
		vector<vector<int> > used(n, vector<int>(m,false));
		rep(i,0,n)
			rep(j,0,m)
				cin>>a[i][j];

		LL ans = 0;
		map<int, int> mp;
		bool flag = false;
		for(int i=0; i<n; i++){
			for(int j=0; j<m; j++){
				mp.clear();
				int cnt = 0;
				int ma = 0;
				flag = false;
				if(!used[i][j]){
					used[i][j]=1;
					mp[a[i][j]]++;
					flag = true;
				}
				if(!used[i][m-j-1]){
					used[i][m-j-1]=1;
					mp[a[i][m-j-1]] += 1;
					flag = true;
				}
				if(!used[n-i-1][j]){
					used[n-i-1][j]=1;
					mp[a[n-i-1][j]]++;
					flag = true;
				}
				if(!used[n-i-1][m-j-1]){
					flag = true;
					used[n-i-1][m-j-1]=1;
					mp[a[n-i-1][m-j-1]]++;
				}
				for(auto e:mp){
					ma += e.se*e.fi;
					cnt += e.se;
				}
				if(flag){
					ma /= cnt;
					for(auto e:mp){
						ans += (LL)(abs(e.fi-ma))*e.se;

					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(LL i=(j); i<(k); ++i)
#define pb push_back
#define PII pair<int,int>
#define PLL pair<long long, long long>
#define ini(a,j) memset(a,j,sizeof a)
#define rrep(i,j,k) for(LL i=j; i>=k; --i)
#define fi first
#define se second
#define LL long long
#define beg begin()
#define ed end()
#define all(x) x.begin(),x.end()

LL mod = 1e9+7;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int _n;
	cin>>_n;
	while(_n--){
		int n,m;
		cin>>n>>m;
		vector<vector<int> > a(n, vector<int>(m));
		vector<vector<int> > used(n, vector<int>(m,0));
		rep(i,0,n)
			rep(j,0,m)
				cin>>a[i][j];

		LL ans = 0;
		map<int, int> mp;
		bool flag = false;
		for(int i=0; i<(n+1)/2; i++){
			for(int j=0; j<(m+1)/2; j++){
				mp.clear();
				LL cnt = 0;
				LL ma = 0;
				flag = false;
				if(!used[i][j]){
					used[i][j]=1;
					mp[a[i][j]]++;
					flag = true;
				}
				if(!used[i][m-j-1]&&m-j-1!=j){
					used[i][m-j-1]=1;
					mp[a[i][m-j-1]]++;
					flag = true;
				}
				if(!used[n-i-1][j]&&n-i-1!=i){
					used[n-i-1][j]=1;
					mp[a[n-i-1][j]]++;
					flag = true;
				}
				if(!used[n-i-1][m-j-1]&&m-j-1!=j&&n-i-1!=i){
					flag = true;
					used[n-i-1][m-j-1]=1;
					mp[a[n-i-1][m-j-1]]++;
				}
				for(auto e:mp){
					ma += (LL)e.fi*e.se;
					cnt += (LL)e.se;
				}
				if(flag&&cnt>1){
					LL rem = ma%cnt;
					ma = ma/cnt;
					if(2*rem>=cnt)
						ma++;
					for(auto e:mp){
						ans += (LL)(abs(e.fi-ma))*e.se;
					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(LL i=(j); i<(k); ++i)
#define pb push_back
#define PII pair<int,int>
#define PLL pair<long long, long long>
#define ini(a,j) memset(a,j,sizeof a)
#define rrep(i,j,k) for(LL i=j; i>=k; --i)
#define fi first
#define se second
#define LL long long
#define beg begin()
#define ed end()
#define all(x) x.begin(),x.end()

LL mod = 1e9+7;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int _n;
	cin>>_n;
	while(_n--){
		int n,m;
		cin>>n>>m;
		vector<vector<int> > a(n, vector<int>(m));
		vector<vector<int> > used(n, vector<int>(m,0));
		rep(i,0,n)
			rep(j,0,m)
				cin>>a[i][j];

		LL ans = 0;
		map<int, int> mp;
		bool flag = false;
		for(int i=0; i<(n+1)/2; i++){
			for(int j=0; j<(m+1)/2; j++){
				mp.clear();
				LL cnt = 0;
				LL ma = 0;
				flag = false;
				if(!used[i][j]){
					used[i][j]=1;
					mp[a[i][j]]++;
					flag = true;
				}
				if(!used[i][m-j-1]&&m-j-1!=j){
					used[i][m-j-1]=1;
					mp[a[i][m-j-1]]++;
					flag = true;
				}
				if(!used[n-i-1][j]&&n-i-1!=i){
					used[n-i-1][j]=1;
					mp[a[n-i-1][j]]++;
					flag = true;
				}
				if(!used[n-i-1][m-j-1]&&m-j-1!=j&&n-i-1!=i){
					flag = true;
					used[n-i-1][m-j-1]=1;
					mp[a[n-i-1][m-j-1]]++;
				}
				for(auto e:mp){

					cnt += e.se;
				}
				if(flag&&mp.size()>1){
					auto pos = mp.begin();
					if((*pos).se*2<cnt)
						++pos;
					ma = (*pos).fi;
					for(auto e:mp){
						ans += (LL)(abs(e.fi-ma))*e.se;
					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(LL i=(j); i<(k); ++i)
#define pb push_back
#define PII pair<int,int>
#define PLL pair<long long, long long>
#define ini(a,j) memset(a,j,sizeof a)
#define rrep(i,j,k) for(LL i=j; i>=k; --i)
#define fi first
#define se second
#define LL long long
#define beg begin()
#define ed end()
#define all(x) x.begin(),x.end()

LL mod = 1e9+7;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int _n;
	cin>>_n;
	while(_n--){
		int n,m;
		cin>>n>>m;
		vector<vector<int> > a(n, vector<int>(m));
		vector<vector<int> > used(n, vector<int>(m,0));
		rep(i,0,n)
			rep(j,0,m)
				cin>>a[i][j];

		LL ans = 0;
		map<int, int> mp;
		bool flag = false;
		for(int i=0; i<n; i++){
			for(int j=0; j<m; j++){
				mp.clear();
				LL cnt = 0;
				LL ma = 0;
				flag = false;
				if(!used[i][j]){
					used[i][j]=1;
					mp[a[i][j]]++;
					flag = true;
				}
				if(!used[i][m-j-1]&&m-j-1!=j){
					used[i][m-j-1]=1;
					mp[a[i][m-j-1]]++;
					flag = true;
				}
				if(!used[n-i-1][j]&&n-i-1!=i){
					used[n-i-1][j]=1;
					mp[a[n-i-1][j]]++;
					flag = true;
				}
				if(!used[n-i-1][m-j-1]&&m-j-1!=j&&n-i-1!=i){
					flag = true;
					used[n-i-1][m-j-1]=1;
					mp[a[n-i-1][m-j-1]]++;
				}
				for(auto e:mp){
					ma += (LL)e.fi*e.se;
					cnt += (LL)e.se;
				}
				if(flag&&cnt>1){
					LL rem = ma%cnt;
					ma = ma/cnt;
					if(2*rem>=cnt)
						ma++;
					for(auto e:mp){
						ans += (LL)(abs(e.fi-ma))*e.se;
					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(LL i=(j); i<(k); ++i)
#define pb push_back
#define PII pair<int,int>
#define PLL pair<long long, long long>
#define ini(a,j) memset(a,j,sizeof a)
#define rrep(i,j,k) for(LL i=j; i>=k; --i)
#define fi first
#define se second
#define LL long long
#define beg begin()
#define ed end()
#define all(x) x.begin(),x.end()

LL mod = 1e9+7;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int _n;
	cin>>_n;
	while(_n--){
		int n,m;
		cin>>n>>m;
		vector<vector<int> > a(n, vector<int>(m));
		vector<vector<int> > used(n, vector<int>(m,0));
		rep(i,0,n)
			rep(j,0,m)
				cin>>a[i][j];

		LL ans = 0;
		map<int, int> mp;
		bool flag = false;
		for(int i=0; i<(n+1)/2; i++){
			for(int j=0; j<(m+1)/2; j++){
				mp.clear();
				LL cnt = 0;
				LL ma = 0;
				flag = false;
				if(!used[i][j]){
					used[i][j]=1;
					mp[a[i][j]]++;
					flag = true;
				}
				if(!used[i][m-j-1]&&m-j-1!=j){
					used[i][m-j-1]=1;
					mp[a[i][m-j-1]]++;
					flag = true;
				}
				if(!used[n-i-1][j]&&n-i-1!=i){
					used[n-i-1][j]=1;
					mp[a[n-i-1][j]]++;
					flag = true;
				}
				if(!used[n-i-1][m-j-1]&&m-j-1!=j&&n-i-1!=i){
					flag = true;
					used[n-i-1][m-j-1]=1;
					mp[a[n-i-1][m-j-1]]++;
				}
				for(auto e:mp){
					cnt++;
				}
				if(flag&&cnt>1){
					auto pos = mp.begin();
					++pos;
					ma = (*pos).fi;
					for(auto e:mp){
						ans += (LL)(abs(e.fi-ma))*e.se;
					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair

//const int fx[]= {+1,-1,+0,+0};
//const int fy[]= {+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

int main()
{

   //  #ifndef ONLINE_JUDGE
   //    freopen("input.txt","r",stdin);
   //    freopen("output.txt","w",stdout);
  //   #endif
    int n,m;
    cin>>n>>m;
    int arr[n+5][m+5];
    ll sum=0;
    ll total=n*m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            sum+=arr[i][j];
        }
    }
    int avg=sum/total;
    ll ans=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            ans+=abs(arr[i][j]-avg);
        }
    }
    cout<<ans<<endl;

///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair

//const int fx[]= {+1,-1,+0,+0};
//const int fy[]= {+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

int main()
{

   //  #ifndef ONLINE_JUDGE
   //    freopen("input.txt","r",stdin);
   //    freopen("output.txt","w",stdout);
  //   #endif
  int t;
  cin>>t;
  while(t--)
  {

    int n,m;
    cin>>n>>m;
    int arr[n+5][m+5];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
        }
    }
    int ans=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            vector<int>v;
            v.pb(arr[i][j]);
            v.pb(arr[i][m-j-1]);
            v.pb(arr[n-i-1][j]);
            sort(v.begin(),v.end());
            int sum1=0,sum2=0,sum3=0;
            for(int k=0;k<v.size();k++)
            {
                sum1+=abs(v[k]-v[0]);
                sum2+=abs(v[k]-v[1]);
                sum3+=abs(v[k]-v[2]);
            }
          //  cout<<sum1<<" "<<sum2<<" "<<sum3<<endl;
            if(sum1<=sum2 && sum1<=sum3)
            {
                ans+=sum1;
                arr[i][j]=v[0];
                arr[i][m-j-1]=v[0];
                arr[n-i-1][j]=v[0];
            }
            else if(sum2<=sum3 && sum2<=sum1)
            {
                ans+=sum2;
                 arr[i][j]=v[1];
                arr[i][m-j-1]=v[1];
                arr[n-i-1][j]=v[1];
            }
            else
            {
                ans+=sum3;
                 arr[i][j]=v[2];
                arr[i][m-j-1]=v[2];
                arr[n-i-1][j]=v[2];
            }
        }
    }
    cout<<ans<<endl;

  }
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair

//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

vector<ll>v;

ll myfunc(int l,int h)
{
    ll low=0;
    ll high=2*h+1;
    ll ans=1e9;
    ll bal=0;
    while(high>=low)
    {
        ll mid=(high+low+1)/2;
        ll diff=0;
        //cout<<mid<<endl;
        for(ll i=0;i<v.size();i++)
        {
            diff+=llabs(v[i]-mid);
        }
       // cout<<mid<<" "<<diff<<endl;
        if(diff<ans)
        {
            bal=mid;
            ans=diff;
            high=mid-1;
        }
        else
        {
            low=mid+1;
        }
    }
    return low;
}

int main()
{

   //  #ifndef ONLINE_JUDGE
   //    freopen("input.txt","r",stdin);
   //    freopen("output.txt","w",stdout);
  //   #endif
  ll t;
  cin>>t;
  while(t--)
  {

    ll n,m;
    cin>>n>>m;
    ll arr[n+5][m+5];
    ll sum=0;
    ll total=n*m;
    ll brr[n+5][m+5];
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            cin>>arr[i][j];
            brr[i][j]=arr[i][j];
        }
    }
    ll ans=0;
//    v.pb(9);
//    v.pb(9);
//    v.pb(0);
//    v.pb(9);
////    for(int i=0;i<m;i++)
////    {
////        v.pb(arr[n-1][i]);
////    }
//    sort(v.begin(),v.end());
//    int val=myfunc();
//    cout<<val<<endl;
    for(ll i=0;i<n;i++)
    {

        for(ll j=0;j<m;j++)
        {
            v.clear();
            v.pb(arr[i][j]);
            v.pb(arr[i][m-j-1]);
            v.pb(arr[n-i-1][j]);
            v.pb(arr[n-i-1][m-j-1]);
            sort(v.begin(),v.end());
            ll val=myfunc(v[0],v[v.size()-1]);

          //  cout<<val<<endl;
            arr[i][j]=val;
            arr[i][m-j-1]=val;
            arr[n-i-1][j]=val;
            arr[n-i-1][m-j-1]=val;
        }
    }
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
           // cout<<arr[i][j]<<" ";
            ans+=abs(arr[i][j]-brr[i][j]);
        }
       // cout<<endl;
    }
    cout<<ans<<endl;
  }

///Before submit=>
///    *check for lleger overflow,array bounds
///    *check for n=1
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair

//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

vector<ll>v;

ll myfunc(ll l,ll h)
{
    ll low=l;
    ll high=h;
    ll ans=1e9;
    ll bal=0;
    while(high>=low)
    {
        ll mid=(high+low+1)/2;
        ll diff=0;
       // cout<<mid<<endl;
        for(ll i=0;i<v.size();i++)
        {
            diff+=abs(v[i]-mid);
        }
        //cout<<mid<<" "<<diff<<endl;
        if(diff<ans)
        {
            bal=mid;
            ans=diff;
            high=mid-1;
        }
        else
        {
            low=mid+1;
        }
    }
    return low;
}

int main()
{

   //  #ifndef ONLINE_JUDGE
   //    freopen("input.txt","r",stdin);
   //    freopen("output.txt","w",stdout);
  //   #endif
  ll t;
  cin>>t;
  while(t--)
  {

    ll n,m;
    cin>>n>>m;
    ll arr[n+5][m+5];
    ll sum=0;
    ll total=n*m;
    ll brr[n+5][m+5];
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            cin>>arr[i][j];
            brr[i][j]=arr[i][j];
        }
    }
    ll ans=0;
    for(ll i=0;i<n;i++)
    {

        for(ll j=0;j<m;j++)
        {
            v.pb(arr[i][j]);
            v.pb(arr[i][m-j-1]);
            v.pb(arr[n-i-1][j]);
            v.pb(arr[n-i-1][m-j-1]);
            sort(v.begin(),v.end());
            ll val=myfunc(v[0],v[v.size()-1]);
            v.clear();
          //  cout<<val<<endl;
            arr[i][j]=val;
            arr[i][m-j-1]=val;
            arr[n-i-1][j]=val;
            arr[n-i-1][m-j-1]=val;
        }
    }
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            ans+=abs(arr[i][j]-brr[i][j]);
        }
    }
    cout<<ans<<endl;
  }
    //cout<<ans<<endl;
///Before submit=>
///    *check for lleger overflow,array bounds
///    *check for n=1
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair

//const int fx[]= {+1,-1,+0,+0};
//const int fy[]= {+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

vector<int>v;

ll myfunc(int l,int h)
{
    int low=l;
    int high=h;
    ll ans=1e9;
    int bal=0;
    while(high>=low)
    {
        int mid=(high+low+1)/2;
        ll diff=0;
       // cout<<mid<<endl;
        for(int i=0;i<v.size();i++)
        {
            diff+=abs(v[i]-mid);
        }
        //cout<<mid<<" "<<diff<<endl;
        if(diff<ans)
        {
            bal=mid;
            ans=diff;
            high=mid-1;
        }
        else
        {
            low=mid+1;
        }
    }
    return low;
}

int main()
{

   //  #ifndef ONLINE_JUDGE
   //    freopen("input.txt","r",stdin);
   //    freopen("output.txt","w",stdout);
  //   #endif
  int t;
  cin>>t;
  while(t--)
  {

    int n,m;
    cin>>n>>m;
    int arr[n+5][m+5];
    ll sum=0;
    ll total=n*m;
    int brr[n+5][m+5];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            brr[i][j]=arr[i][j];
        }
    }
    ll ans=0;
    sort(v.begin(),v.end());
//    int val=myfunc(v[0],v[v.size()-1]);
//    cout<<val<<endl;
//    return 0;
        for(int i=0;i<n;i++)
    {

        for(int j=0;j<m;j++)
        {
            v.pb(arr[i][j]);
            v.pb(arr[i][m-j-1]);
            v.pb(arr[n-i-1][j]);
            v.pb(arr[n-i-1][m-j-1]);
            ll sum=0;
            sort(v.begin(),v.end());
            int val=myfunc(v[0],v[v.size()-1]);
            v.clear();
          //  cout<<val<<endl;
            for(int k=0;k<v.size();k++)
            {
                ans+=abs(val-v[k]);
            }
            arr[i][j]=val;
            arr[i][m-j-1]=val;
            arr[n-i-1][j]=val;
            arr[n-i-1][m-j-1]=val;
        }
        v.clear();
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            ans+=abs(arr[i][j]-brr[i][j]);
        }
    }
    cout<<ans<<endl;
  }
    //cout<<ans<<endl;
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair

//const int fx[]= {+1,-1,+0,+0};
//const int fy[]= {+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

vector<int>v;

ll myfunc(int l,int h)
{
    int low=l;
    int high=h;
    ll ans=1e9;

    while(high>=low)
    {
        int mid=(high+low)/2;
        ll diff=0;
      //  cout<<mid<<endl;
        for(int i=0;i<v.size();i++)
        {
            diff+=abs(v[i]-mid);
        }
        //cout<<mid<<" "<<diff<<endl;
        if(diff<ans)
        {
            ans=diff;
            high=mid-1;
        }
        else
        {
            low=mid+1;
        }
    }
    return low;
}

int main()
{

   //  #ifndef ONLINE_JUDGE
   //    freopen("input.txt","r",stdin);
   //    freopen("output.txt","w",stdout);
  //   #endif
  int t;
  cin>>t;
  while(t--)
  {

    int n,m;
    cin>>n>>m;
    int arr[n+5][m+5];
    ll sum=0;
    ll total=n*m;
    int brr[n+5][m+5];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            brr[i][j]=arr[i][j];
        }
    }
    ll ans=0;
//    for(int i=0;i<m;i++)
//    {
//        v.pb(arr[n-1][i]);
//    }
//    sort(v.begin(),v.end());
//    int val=myfunc(v[0],v[v.size()-1]);
//    cout<<val<<endl;
//    return 0;
        for(int i=0;i<n;i++)
    {

        for(int j=0;j<m;j++)
        {
            v.pb(arr[i][j]);
            v.pb(arr[i][m-j-1]);
            v.pb(arr[n-i-1][j]);
            v.pb(arr[n-i-1][m-j-1]);
            ll sum=0;
            sort(v.begin(),v.end());
            int val=myfunc(v[0],v[v.size()-1]);
            //cout<<val<<endl;
//            for(int k=0;k<v.size();k++)
//            {
//                ans+=abs(val-v[k]);
//            }
            arr[i][j]=val;
            arr[i][m-j-1]=val;
            arr[n-i-1][j]=val;
            arr[n-i-1][m-j-1]=val;
        }
        v.clear();
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            ans+=abs(arr[i][j]-brr[i][j]);
        }
    }
    cout<<ans<<endl;
  }
    //cout<<ans<<endl;
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair

//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

vector<ll>v;

ll myfunc(int l,int h)
{
    ll low=0;
    ll high=2*h+1;
    ll ans=1e9;
    ll bal=0;
    while(high>=low)
    {
        ll mid=(high+low)/2;
        ll diff=0;
        //cout<<mid<<endl;
        for(ll i=0;i<v.size();i++)
        {
            diff+=llabs(v[i]-mid);
        }
       // cout<<mid<<" "<<diff<<endl;
        if(diff<ans)
        {
            bal=mid;
            ans=diff;
            high=mid-1;
        }
        else
        {
            low=mid+1;
        }
    }
    return low;
}

int main()
{

   //  #ifndef ONLINE_JUDGE
   //    freopen("input.txt","r",stdin);
   //    freopen("output.txt","w",stdout);
  //   #endif
  ll t;
  cin>>t;
  while(t--)
  {

    ll n,m;
    cin>>n>>m;
    ll arr[n+5][m+5];
    ll sum=0;
    ll total=n*m;
    ll brr[n+5][m+5];
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            cin>>arr[i][j];
            brr[i][j]=arr[i][j];
        }
    }
    ll ans=0;
//    v.pb(9);
//    v.pb(9);
//    v.pb(0);
//    v.pb(9);
////    for(int i=0;i<m;i++)
////    {
////        v.pb(arr[n-1][i]);
////    }
//    sort(v.begin(),v.end());
//    int val=myfunc();
//    cout<<val<<endl;
    for(ll i=0;i<n;i++)
    {

        for(ll j=0;j<m;j++)
        {
            v.clear();
            v.pb(arr[i][j]);
//            if(j!=m-j-1)
                v.pb(arr[i][m-j-1]);
//            if(i!=n-i-1)
                v.pb(arr[n-i-1][j]);
//            if(i!=n-i-1 && j!=m-j-1)
                v.pb(arr[n-i-1][m-j-1]);
            sort(v.begin(),v.end());
            ll val=myfunc(v[0],v[v.size()-1]);
           // cout<<val<<endl;
            arr[i][j]=val;
            arr[i][m-j-1]=val;
            arr[n-i-1][j]=val;
            arr[n-i-1][m-j-1]=val;
        }
    }
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
           // cout<<arr[i][j]<<" ";
            ans+=abs(arr[i][j]-brr[i][j]);
        }
       // cout<<endl;
    }
    cout<<ans<<endl;
  }

///Before submit=>
///    *check for lleger overflow,array bounds
///    *check for n=1
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair

//const int fx[]= {+1,-1,+0,+0};
//const int fy[]= {+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

int main()
{

   //  #ifndef ONLINE_JUDGE
   //    freopen("input.txt","r",stdin);
   //    freopen("output.txt","w",stdout);
  //   #endif
  int t;
  cin>>t;
  while(t--)
  {

    int n,m;
    cin>>n>>m;
    int arr[n+5][m+5];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
        }
    }
    ll ans=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            vector<int>v;
            v.pb(arr[i][j]);
            v.pb(arr[i][m-j-1]);
            v.pb(arr[n-i-1][j]);
            sort(v.begin(),v.end());
            int sum1=0,sum2=0,sum3=0;
            for(int k=0;k<v.size();k++)
            {
                sum1+=abs(v[k]-v[0]);
                sum2+=abs(v[k]-v[1]);
                sum3+=abs(v[k]-v[2]);
            }
          //  cout<<sum1<<" "<<sum2<<" "<<sum3<<endl;
            if(sum1<=sum2 && sum1<=sum3)
            {
                ans+=sum1;
                arr[i][j]=v[0];
                arr[i][m-j-1]=v[0];
                arr[n-i-1][j]=v[0];
            }
            else if(sum2<=sum3 && sum2<=sum1)
            {
                ans+=sum2;
                 arr[i][j]=v[1];
                arr[i][m-j-1]=v[1];
                arr[n-i-1][j]=v[1];
            }
            else
            {
                ans+=sum3;
                 arr[i][j]=v[2];
                arr[i][m-j-1]=v[2];
                arr[n-i-1][j]=v[2];
            }
        }
    }
    cout<<ans<<endl;

  }
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair

//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

vector<ll>v;

ll myfunc(int l,int h)
{
    ll low=0;
    ll high=2*h+1;
    ll ans=1e9;
    ll bal=0;
    while(high>=low)
    {
        ll mid=(high+low)/2;
        ll diff=0;
        //cout<<mid<<endl;
        for(ll i=0;i<v.size();i++)
        {
            diff+=llabs(v[i]-mid);
        }
       // cout<<mid<<" "<<diff<<endl;
        if(diff<ans)
        {
            bal=mid;
            ans=diff;
            high=mid-1;
        }
        else
        {
            low=mid+1;
        }
    }
    return low;
}

int main()
{

   //  #ifndef ONLINE_JUDGE
   //    freopen("input.txt","r",stdin);
   //    freopen("output.txt","w",stdout);
  //   #endif
  ll t;
  cin>>t;
  while(t--)
  {

    ll n,m;
    cin>>n>>m;
    ll arr[n+5][m+5];
    ll sum=0;
    ll total=n*m;
    ll brr[n+5][m+5];
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            cin>>arr[i][j];
            brr[i][j]=arr[i][j];
        }
    }
    ll ans=0;
//    v.pb(9);
//    v.pb(9);
//    v.pb(0);
//    v.pb(9);
////    for(int i=0;i<m;i++)
////    {
////        v.pb(arr[n-1][i]);
////    }
//    sort(v.begin(),v.end());
//    int val=myfunc();
//    cout<<val<<endl;
    for(ll i=0;i<n;i++)
    {

        for(ll j=0;j<m;j++)
        {
            v.clear();
            v.pb(arr[i][j]);
            if(j!=m-j-1)
                v.pb(arr[i][m-j-1]);
            if(i!=n-i-1)
                v.pb(arr[n-i-1][j]);
            if(i!=n-i-1 && j!=m-j-1)
                v.pb(arr[n-i-1][m-j-1]);
            sort(v.begin(),v.end());
            ll val=myfunc(v[0],v[v.size()-1]);
           // cout<<val<<endl;
            arr[i][j]=val;
            arr[i][m-j-1]=val;
            arr[n-i-1][j]=val;
            arr[n-i-1][m-j-1]=val;
        }
    }
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
           // cout<<arr[i][j]<<" ";
            ans+=llabs(arr[i][j]-brr[i][j]);
        }
       // cout<<endl;
    }
    cout<<ans<<endl;
  }

///Before submit=>
///    *check for lleger overflow,array bounds
///    *check for n=1
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair

//const int fx[]= {+1,-1,+0,+0};
//const int fy[]= {+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

int main()
{

   //  #ifndef ONLINE_JUDGE
   //    freopen("input.txt","r",stdin);
   //    freopen("output.txt","w",stdout);
  //   #endif
  int t;
  cin>>t;
  while(t--)
  {

    int n,m;
    cin>>n>>m;
    int arr[n+5][m+5];
    ll sum=0;
    ll total=n*m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            sum+=arr[i][j];
        }
    }
    int avg=sum/total;
    ll ans=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            ans+=abs(arr[i][j]-avg);
        }
    }
    cout<<ans<<endl;

  }

///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1
}


//In the Name of Allah Most Gracious, Most Merciful//
/*If you want something you've never had, you have  to do something you never did.*/

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define M 100007
#define INF 1e9
#define INFL 1e18
#define PI acos(-1)
#define mp make_pair

//const ll fx[]= {+1,-1,+0,+0};
//const ll fy[]= {+0,+0,+1,-1};
//const ll fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const ll fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const ll fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const ll fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

vector<ll>v;

ll myfunc(int l,int h)
{
    ll low=0;
    ll high=2*h+1;
    ll ans=1e9;
    ll bal=0;
    while(high>=low)
    {
        ll mid=(high+low)/2;
        ll diff=0;
        //cout<<mid<<endl;
        for(ll i=0;i<v.size();i++)
        {
            diff+=llabs(v[i]-mid);
        }
       // cout<<mid<<" "<<diff<<endl;
        if(diff<ans)
        {
            bal=mid;
            ans=diff;
            high=mid-1;
        }
        else
        {
            low=mid+1;
        }
    }
    return low;
}

int main()
{

   //  #ifndef ONLINE_JUDGE
   //    freopen("input.txt","r",stdin);
   //    freopen("output.txt","w",stdout);
  //   #endif
  ll t;
  cin>>t;
  while(t--)
  {

    ll n,m;
    cin>>n>>m;
    ll arr[n+5][m+5];
    ll sum=0;
    ll total=n*m;
    ll brr[n+5][m+5];
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            cin>>arr[i][j];
            brr[i][j]=arr[i][j];
        }
    }
    ll ans=0;
//    v.pb(9);
//    v.pb(9);
//    v.pb(0);
//    v.pb(9);
////    for(int i=0;i<m;i++)
////    {
////        v.pb(arr[n-1][i]);
////    }
//    sort(v.begin(),v.end());
//    int val=myfunc();
//    cout<<val<<endl;
    for(ll i=0;i<n;i++)
    {

        for(ll j=0;j<m;j++)
        {
            v.clear();
            v.pb(arr[i][j]);
//            if(j!=m-j-1)
                v.pb(arr[i][m-j-1]);
//            if(i!=n-i-1)
                v.pb(arr[n-i-1][j]);
//            if(i!=n-i-1 && j!=m-j-1)
                v.pb(arr[n-i-1][m-j-1]);
            sort(v.begin(),v.end());
            ll val=myfunc(v[0],v[v.size()-1]);
           // cout<<val<<endl;
            arr[i][j]=val;
            arr[i][m-j-1]=val;
            arr[n-i-1][j]=val;
            arr[n-i-1][m-j-1]=val;
        }
    }
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
           // cout<<arr[i][j]<<" ";
            ans+=llabs(arr[i][j]-brr[i][j]);
        }
       // cout<<endl;
    }
    cout<<ans<<endl;
  }

///Before submit=>
///    *check for lleger overflow,array bounds
///    *check for n=1
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	long long t, n, m, a[101][101], res, s[3], cur;
	cin >> t;
	while(t--){
		cin >> n >> m;
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < m; j++){
				cin >> a[i][j];
			}
		}
		res = 0;
		for(int i = 0; i < n / 2 + n%2; i ++){
			for(int j = 0; j < m / 2 + m%2; j++){
				cur = 0;
				s[cur ++] = a[i][j];
				if(n - 1 - i != i)
					s[cur ++] = a[n - 1 - i][j];
				if(m - 1 - j != j)
					s[cur ++] = a[i][m - 1 - j];
				if(n - 1 - i != i && m - 1 - j != j)
					s[cur ++] = a[n - 1 - i][m - 1 - j];
				sort(s, s + cur);
				for(int k = 0; k < cur; k++){
					res += abs(s[k] - s[1]);
				}
			}
		}
		cout << res << endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int t, n, m, a[101][101], res, s[3], cur;
	cin >> t;
	while(t--){
		cin >> n >> m;
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < m; j++){
				cin >> a[i][j];
			}
		}
		res = 0;
		for(int i = 0; i < n / 2 + n%2; i ++){
			for(int j = 0; j < m / 2 + m%2; j++){
				cur = 0;
				s[cur ++] = a[i][j];
				if(n - 1 - i != i)
					s[cur ++] = a[n - 1 - i][j];
				if(m - 1 - j != j)
					s[cur ++] = a[i][m - 1 - j];
				if(n - 1 - i != i && m - 1 - j != j)
					s[cur ++] = a[n - 1 - i][m - 1 - j];
				sort(s, s + cur);
				for(int k = 0; k < cur; k++){
					res += abs(s[k] - s[1]);
				}
			}
		}
		cout << res << endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	long long t, n, m, a[101][101], res, s[5], cur;
	cin >> t;
	while(t--){
		cin >> n >> m;
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < m; j++){
				cin >> a[i][j];
			}
		}
		res = 0;
		for(int i = 0; i < n / 2 + n%2; i ++){
			for(int j = 0; j < m / 2 + m%2; j++){
				cur = 0;
				s[cur ++] = a[i][j];
				if(n - 1 - i != i)
					s[cur ++] = a[n - 1 - i][j];
				if(m - 1 - j != j)
					s[cur ++] = a[i][m - 1 - j];
				if(n - 1 - i != i && m - 1 - j != j)
					s[cur ++] = a[n - 1 - i][m - 1 - j];
				sort(s, s + cur);
				for(int k = 0; k < cur; k++){
					res += abs(s[k] - s[min(cur - 1, 1ll)]);
				}
			}
		}
		cout << res << endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int t, n, m, a[101][101], res, s[4], cur;
	cin >> t;
	while(t--){
		cin >> n >> m;
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < m; j++){
				cin >> a[i][j];
			}
		}
		res = 0;
		for(int i = 0; i < n / 2 + n%2; i ++){
			for(int j = 0; j < m / 2 + m%2; j++){
				cur = 0;
				s[cur ++] = a[i][j];
				if(n - 1 - i != i)
					s[cur ++] = a[n - 1 - i][j];
				if(m - 1 - j != j)
					s[cur ++] = a[i][m - 1 - j];
				if(n - 1 - i != i && m - 1 - j != j)
					s[cur ++] = a[n - 1 - i][m - 1 - j];
				sort(s, s + cur);
				for(int k = 0; k < cur; k++){
					res += abs(s[k] - s[1]);
				}
			}
		}
		cout << res << endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	long t, n, m, a[101][101], res, s[4], cur;
	cin >> t;
	while(t--){
		cin >> n >> m;
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < m; j++){
				cin >> a[i][j];
			}
		}
		res = 0;
		for(int i = 0; i < n / 2 + n%2; i ++){
			for(int j = 0; j < m / 2 + m%2; j++){
				cur = 0;
				s[cur ++] = a[i][j];
				if(n - 1 - i != i)
					s[cur ++] = a[n - 1 - i][j];
				if(m - 1 - j != j)
					s[cur ++] = a[i][m - 1 - j];
				if(n - 1 - i != i && m - 1 - j != j)
					s[cur ++] = a[n - 1 - i][m - 1 - j];
				sort(s, s + cur);
				for(int k = 0; k < cur; k++){
					res += abs(s[k] - s[1]);
				}
			}
		}
		cout << res << endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	long long t, n, m, a[101][101], res, s[5], cur;
	cin >> t;
	while(t--){
		cin >> n >> m;
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < m; j++){
				cin >> a[i][j];
			}
		}
		res = 0;
		for(int i = 0; i < n / 2 + n%2; i ++){
			for(int j = 0; j < m / 2 + m%2; j++){
				cur = 0;
				s[cur ++] = a[i][j];
				if(n - 1 - i != i)
					s[cur ++] = a[n - 1 - i][j];
				if(m - 1 - j != j)
					s[cur ++] = a[i][m - 1 - j];
				if(n - 1 - i != i && m - 1 - j != j)
					s[cur ++] = a[n - 1 - i][m - 1 - j];
				sort(s, s + cur);
				for(int k = 0; k < cur; k++){
					res += abs(s[k] - s[1]);
				}
			}
		}
		cout << res << endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
typedef long long ll;
ll mp[101][101];
ll change[101][101];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a,b;
		cin>>a>>b;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				scanf("%lld",&mp[i][j]);
				change[i][j] = mp[i][j];
			}
		}
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b/2;j++)
			{
				if((mp[i][j]+mp[i][b-j+1])%2==0)
				change[i][j] = change[i][b-j+1] = (mp[i][j]+mp[i][b-j+1])/2;
				else
				change[i][j] = change[i][b-j+1] = (mp[i][j]+mp[i][b-j+1])/2+1;
			}
		}
		for(int j=1;j<=b;j++)
		{
			for(int i=1;i<=a/2;i++)
			{
				if((mp[i][j]+mp[a-i+1][j])%2==0)
				change[i][j] = change[a-i+1][j] = (mp[i][j]+mp[a-i+1][j])/2;
				else change[i][j] = change[a-i+1][j] = (mp[i][j]+mp[a-i+1][j])/2+1;
			}
		}
		/*for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				cout<<change[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<endl;*/
		ll ans = 0;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				ans += abs(change[i][j]-mp[i][j]);
			}
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
typedef long long ll;
ll mp[101][101];
ll change[101][101];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a,b;
		cin>>a>>b;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				scanf("%lld",&mp[i][j]);
				change[i][j] = mp[i][j];
			}
		}
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				int count = 0;
				if((mp[i][j] + mp[i][b-j+1] + mp[a-i+1][j] + mp[a-i+1][b-j+1])/4>=mp[i][j]) count++;
				if((mp[i][j] + mp[i][b-j+1] + mp[a-i+1][j] + mp[a-i+1][b-j+1])/4>=mp[i][b-j+1]) count++;
				if((mp[i][j] + mp[i][b-j+1] + mp[a-i+1][j] + mp[a-i+1][b-j+1])/4>=mp[a-i+1][j]) count++;
				if((mp[i][j] + mp[i][b-j+1] + mp[a-i+1][j] + mp[a-i+1][b-j+1])/4>=mp[a-i+1][b-j+1]) count++;
				if(count == 2)
				{
				change[i][j] = (mp[i][j] + mp[i][b-j+1] + mp[a-i+1][j] + mp[a-i+1][b-j+1])/4;
				change[i][b-j+1] = (mp[i][j] + mp[i][b-j+1] + mp[a-i+1][j] + mp[a-i+1][b-j+1])/4;
				change[a-i+1][j] = (mp[i][j] + mp[i][b-j+1] + mp[a-i+1][j] + mp[a-i+1][b-j+1])/4;
				change[a-i+1][b-j+1] = (mp[i][j] + mp[i][b-j+1] + mp[a-i+1][j] + mp[a-i+1][b-j+1])/4;
			    }
			    else if(count == 3)
			    {
			    	change[i][j] = change[i][b-j+1] = change[a-i+1][j] = change[a-i+1][b-j+1] = min(min(change[i][j],change[i][b-j+1]),min(change[a-i+1][j],change[a-i+1][b-j+1]));
				}
				else
				{
					change[i][j] = change[i][b-j+1] = change[a-i+1][j] = change[a-i+1][b-j+1] = max(max(change[i][j],change[i][b-j+1]),max(change[a-i+1][j],change[a-i+1][b-j+1]));

				}
			}
		}
		/*for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				cout<<change[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<endl;*/
		ll ans = 0;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				ans += abs(change[i][j]-mp[i][j]);
			}
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
typedef long long ll;
int mp[101][101];
int change[101][101];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a,b;
		cin>>a>>b;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				scanf("%d",&mp[i][j]);
				change[i][j] = mp[i][j];
			}
		}
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b/2;j++)
			{
				change[i][j] = change[i][b-j+1] = (change[i][j]+change[i][b-j+1])/2;
			}
		}
		for(int j=1;j<=b;j++)
		{
			for(int i=1;i<=a/2;i++)
			{
				change[i][j] = change[a-i+1][j] = (change[i][j]+change[a-i+1][j])/2;
			}
		}
		/*for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				cout<<change[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<endl;*/
		int ans = 0;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				ans += abs(change[i][j]-mp[i][j]);
			}
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
typedef long long ll;
ll mp[101][101];
ll change[101][101];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a,b;
		cin>>a>>b;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				scanf("%lld",&mp[i][j]);
				change[i][j] = mp[i][j];
			}
		}
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b/2;j++)
			{
				change[i][j] = change[i][b-j+1] = (change[i][j]+change[i][b-j+1])/2;
			}
		}
		for(int j=1;j<=b;j++)
		{
			for(int i=1;i<=a/2;i++)
			{
				change[i][j] = change[a-i+1][j] = (change[i][j]+change[a-i+1][j])/2;
			}
		}
		/*for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				cout<<change[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<endl;*/
		ll ans = 0;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				ans += abs(change[i][j]-mp[i][j]);
			}
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
//#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
typedef long long ll;
ll mp[101][101];
ll change[101][101];
ll q[6];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a,b;
		cin>>a>>b;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				scanf("%lld",&mp[i][j]);
				change[i][j] = mp[i][j];
			}
		}
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				q[1] = mp[i][j] ;q[2] = mp[i][b-j+1] ;q[3] = mp[a-i+1][j] ;q[4] = mp[a-i+1][b-j+1];
				sort(q+1,q+5);
				change[i][j] = q[2];
				change[i][b-j+1] = q[2];
				change[a-i+1][j] = q[2];
				change[a-i+1][b-j+1] = q[2];
			}
		}
		/*for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				cout<<change[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<endl;*/
		ll ans = 0;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				ans += abs(change[i][j]-mp[i][j]);
			}
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
typedef long long ll;
ll mp[101][101];
ll change[101][101];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a,b;
		cin>>a>>b;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				scanf("%lld",&mp[i][j]);
				change[i][j] = mp[i][j];
			}
		}
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b/2;j++)
			{
				if((change[i][j]+change[i][b-j+1])%2==0)
				change[i][j] = change[i][b-j+1] = (change[i][j]+change[i][b-j+1])/2;
				else
				change[i][j] = change[i][b-j+1] = (change[i][j]+change[i][b-j+1])/2+1;
			}
		}
		for(int j=1;j<=b;j++)
		{
			for(int i=1;i<=a/2;i++)
			{
				if((change[i][j]+change[a-i+1][j])%2==0)
				change[i][j] = change[a-i+1][j] = (change[i][j]+change[a-i+1][j])/2;
				else change[i][j] = change[a-i+1][j] = (change[i][j]+change[a-i+1][j])/2+1;
			}
		}
		/*for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				cout<<change[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<endl;*/
		ll ans = 0;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				ans += abs(change[i][j]-mp[i][j]);
			}
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
typedef long long ll;
int mp[101][101];
int change[101][101];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a,b;
		cin>>a>>b;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				scanf("%d",&mp[i][j]);
				change[i][j] = mp[i][j];
			}
		}
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				change[i][j] = change[i][b-j+1] = (change[i][j]+change[i][b-j+1])/2;
			}
		}
		for(int j=1;j<=b;j++)
		{
			for(int i=1;i<=a;i++)
			{
				change[i][j] = change[a-i+1][j] = (change[i][j]+change[a-i+1][j])/2;
			}
		}
		/*for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				cout<<change[i][j]<<" ";
			}
			cout<<endl;
		}*/
		int ans = 0;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				ans += abs(change[i][j]-mp[i][j]);
			}
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
typedef long long ll;
ll mp[101][101];
ll change[101][101];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a,b;
		cin>>a>>b;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				scanf("%lld",&mp[i][j]);
				change[i][j] = mp[i][j];
			}
		}
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				change[i][j] = (mp[i][j] + mp[i][b-j+1] + mp[a-i+1][j] + mp[a-i+1][b-j+1])/4;
				change[i][b-j+1] = (mp[i][j] + mp[i][b-j+1] + mp[a-i+1][j] + mp[a-i+1][b-j+1])/4;
				change[a-i+1][j] = (mp[i][j] + mp[i][b-j+1] + mp[a-i+1][j] + mp[a-i+1][b-j+1])/4;
				change[a-i+1][b-j+1] = (mp[i][j] + mp[i][b-j+1] + mp[a-i+1][j] + mp[a-i+1][b-j+1])/4;
			}
		}
		/*for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				cout<<change[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<endl;*/
		ll ans = 0;
		for(int i=1;i<=a;i++)
		{
			for(int j=1;j<=b;j++)
			{
				ans += abs(change[i][j]-mp[i][j]);
			}
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<time.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int MOD=1e9+7;
const int INF = 0x3f3f3f3f;
ll a[200][200];
ll temp[100];

int main()
{
//freopen("D:input.txt", "r", stdin);
clock_t start_time=clock();
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                cin>>a[i][j];
                //cout<<a[i][j]<<endl;
            }
            ll ans=0;

        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                temp[1]=a[i][j];
                temp[2]=a[i][m-j+1];
                temp[3]=a[n-i+1][j];

                sort(temp+1,temp+4);
                ll sum=temp[2];
                //cout<<1111<<endl;
                ans+=abs(sum-a[i][j]);
                ans+=abs(sum-a[i][m-j+1]);
                ans+=abs(sum-a[n-i+1][j]);
                a[i][j]=a[i][m-j+1]=a[n-i+1][j]=sum;
               // cout<<a[i][j]<<endl;
            }
            cout<<ans<<endl;
    }
}
clock_t end_time=clock();
//cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//�������ʱ��
return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<time.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int MOD=1e9+7;
const int INF = 0x3f3f3f3f;
ll a[200][200];

int main()
{
//freopen("D:input.txt", "r", stdin);
clock_t start_time=clock();
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                cin>>a[i][j];
                //cout<<a[i][j]<<endl;
            }
            ll ans=0;

        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                ll sum=(a[i][j]+a[i][m-j+1]+a[n-i+1][j]+a[n-i+1][m-j+1])/4;
                //cout<<1111<<endl;
                ans+=abs(sum-a[i][j]);
                ans+=abs(sum-a[i][m-j+1]);
                ans+=abs(sum-a[n-i+1][j]);
                ans+=abs(sum-a[n-i+1][m-j+1]);
            }
            cout<<ans/4<<endl;
    }
}
clock_t end_time=clock();
//cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//�������ʱ��
return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<time.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int MOD=1e9+7;
const int INF = 0x3f3f3f3f;
int a[200][200];

int main()
{
freopen("D:input.txt", "r", stdin);
clock_t start_time=clock();
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                cin>>a[i][j];
                //cout<<a[i][j]<<endl;
            }
            ll ans=0;

        for(int i=1;i<n/2+1;i++)
            for(int j=1;j<m/2+1;j++)
            {
                ll sum=(a[i][j]+a[i][m-j+1]+a[n-i+1][j]+a[n-i+1][m-j+1]+1)/3;
                //cout<<1111<<endl;
                ans+=abs(sum-a[i][j]);
                ans+=abs(sum-a[i][m-j+1]);
                ans+=abs(sum-a[n-i+1][j]);
                ans+=abs(sum-a[n-i+1][m-j+1]);
            }
            cout<<ans<<endl;
    }
}
clock_t end_time=clock();
//cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//�������ʱ��
return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<time.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int MOD=1e9+7;
const int INF = 0x3f3f3f3f;
ll a[200][200];

int main()
{
//freopen("D:input.txt", "r", stdin);
clock_t start_time=clock();
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                cin>>a[i][j];
                //cout<<a[i][j]<<endl;
            }
            ll ans=0;

        for(int i=1;i<n/2+1;i++)
            for(int j=1;j<m/2+1;j++)
            {
                ll sum=(a[i][j]+a[i][m-j+1]+a[n-i+1][j]+a[n-i+1][m-j+1]+1)/3;
                //cout<<1111<<endl;
                ans+=abs(sum-a[i][j]);
                ans+=abs(sum-a[i][m-j+1]);
                ans+=abs(sum-a[n-i+1][j]);
                ans+=abs(sum-a[n-i+1][m-j+1]);
            }
            cout<<ans<<endl;
    }
}
clock_t end_time=clock();
//cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//�������ʱ��
return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<time.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int MOD=1e9+7;
const int INF = 0x3f3f3f3f;
ll a[200][200];

int main()
{
freopen("D:input.txt", "r", stdin);
clock_t start_time=clock();
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                cin>>a[i][j];
                //cout<<a[i][j]<<endl;
            }
            ll ans=0;

        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                ll sum=(a[i][j]+a[i][m-j+1]+a[n-i+1][j]+a[n-i+1][m-j+1])/4;
                //cout<<1111<<endl;
                ans+=abs(sum-a[i][j]);
                ans+=abs(sum-a[i][m-j+1]);
                ans+=abs(sum-a[n-i+1][j]);
                ans+=abs(sum-a[n-i+1][m-j+1]);
            }
            cout<<ans/4<<endl;
    }
}
clock_t end_time=clock();
//cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//�������ʱ��
return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<time.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int MOD=1e9+7;
const int INF = 0x3f3f3f3f;
int a[200][200];

int main()
{
//freopen("D:input.txt", "r", stdin);
clock_t start_time=clock();
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                cin>>a[i][j];
                //cout<<a[i][j]<<endl;
            }
            ll ans=0;

        for(int i=1;i<n/2+1;i++)
            for(int j=1;j<m/2+1;j++)
            {
                ll sum=(a[i][j]+a[i][m-j+1]+a[n-i+1][j]+a[n-i+1][m-j+1]+1)/3;
                //cout<<1111<<endl;
                ans+=abs(sum-a[i][j]);
                ans+=abs(sum-a[i][m-j+1]);
                ans+=abs(sum-a[n-i+1][j]);
                ans+=abs(sum-a[n-i+1][m-j+1]);
            }
            cout<<ans<<endl;
    }
}
clock_t end_time=clock();
//cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//�������ʱ��
return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<time.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int MOD=1e9+7;
const int INF = 0x3f3f3f3f;
ll a[200][200];

int main()
{
//freopen("D:input.txt", "r", stdin);
clock_t start_time=clock();
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                cin>>a[i][j];
                //cout<<a[i][j]<<endl;
            }
            ll ans=0;

        for(int i=1;i<=n/2+1;i++)
            for(int j=1;j<=m/2+1;j++)
            {
                ll sum=(a[i][j]+a[i][m-j+1]+a[n-i+1][j]+a[n-i+1][m-j+1]+1)/3;
                //cout<<1111<<endl;
                ans+=abs(sum-a[i][j]);
                ans+=abs(sum-a[i][m-j+1]);
                ans+=abs(sum-a[n-i+1][j]);
                ans+=abs(sum-a[n-i+1][m-j+1]);
            }
            cout<<ans<<endl;
    }
}
clock_t end_time=clock();
//cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//�������ʱ��
return 0;
}


#include <bits/stdc++.h>

using namespace std;

int m, n, t;
long long a[102][102], ans;

inline int64_t f(int64_t med, int64_t a, int64_t b, int64_t c, int64_t d) {
    return abs(med - a) + abs(med - b) + abs(med - c) + abs(med - d);
}

int64_t findmin(int64_t a, int64_t b, int64_t c, int64_t d) {
    int64_t l = 0, r = 1000000000000LL;
    while (r - l > 2) {
        int64_t v1 = l + (r - l) / 3;
        int64_t v2 = r - (r - l) / 3;
        // cout << l << ' ' << r << ' ' << v1 << ' ' << v2 << endl;
        int64_t sum1 = f(v1, a, b, c, d);
        int64_t sum2 = f(v2, a, b, c, d);
        if (sum1 > sum2) {
            l = v1;
        } else {
            r = v2;
        }
    }
    int64_t ret = l;
    int64_t sum1 = f(l, a, b, c, d);
    int64_t sum2 = f(r, a, b, c, d);
    int64_t sum3 = f((r + l) / 2, a, b, c, d);
    if (sum1 > sum2) {
        ret = r;
    }
    if (sum2 > sum3) {
        ret = (r + l) / 2;
    }
    return ret;
}

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

        ans = 0;

        int halfm = m >> 1;
        int halfn = n >> 1;

        for (int i = 0; i < halfm; ++i) {
            for (int j = 0; j < halfn; ++j) {
                long long avg = findmin(a[i][j], a[m - i - 1][j], a[i][n - j - 1], a[m - i - 1][n - j - 1]);
                ans += abs(avg - a[i][j]) + abs(avg - a[m - i - 1][j]) + abs(avg - a[i][n - j - 1]) + abs(avg - a[m - i - 1][n - j - 1]);
            }
        }

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

        if (m % 2) {
            for (int j = 0; j < n/2; ++j) {
                ans += abs(a[halfm][j] - a[halfm][n - j - 1]);
            }
        }
        if (ans)
            cout << ans << endl;
        else
            cout << m*n << endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

int m, n, t;
long long a[102][102], ans;

inline int64_t f(int64_t med, int64_t a, int64_t b, int64_t c, int64_t d) {
    return abs(med - a) + abs(med - b) + abs(med - c) + abs(med - d);
}

int64_t findmin(int64_t a, int64_t b, int64_t c, int64_t d) {
    int64_t l = 0, r = 1000000000000LL;
    while (r - l > 2) {
        int64_t v1 = l + (r - l) / 3;
        int64_t v2 = r - (r - l) / 3;
        // cout << l << ' ' << r << ' ' << v1 << ' ' << v2 << endl;
        int64_t sum1 = f(v1, a, b, c, d);
        int64_t sum2 = f(v2, a, b, c, d);
        if (sum1 > sum2) {
            l = v1;
        } else {
            r = v2;
        }
    }
    int64_t ret = l;
    int64_t sum1 = f(l, a, b, c, d);
    int64_t sum2 = f(r, a, b, c, d);
    int64_t sum3 = f((r + l) / 2, a, b, c, d);
    if (sum1 > sum2) {
        ret = r;
        sum1 = sum2;
    }
    if (sum1 > sum3) {
        ret = (r + l) / 2;
    }
    // cout << sum1 << ' ' << sum2 << ' ' << sum3 << endl;
    return ret;
}

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

        ans = 0;

        int halfm = m >> 1;
        int halfn = n >> 1;

        for (int i = 0; i < halfm; ++i) {
            for (int j = 0; j < halfn; ++j) {
                long long avg = findmin(a[i][j], a[m - i - 1][j], a[i][n - j - 1], a[m - i - 1][n - j - 1]);
                ans += abs(avg - a[i][j]) + abs(avg - a[m - i - 1][j]) + abs(avg - a[i][n - j - 1]) + abs(avg - a[m - i - 1][n - j - 1]);
            }
        }

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

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

        cout << ans << endl;

    }

}


#include <bits/stdc++.h>

using namespace std;

int m, n, t;
long long a[102][102], ans;

inline int64_t f(int med, int a, int b, int c, int d) {
    return abs(med - a) + abs(med - b) + abs(med - c) + abs(med - d);
}

int64_t findmin(int64_t a, int64_t b, int64_t c, int64_t d) {
    int64_t l = min(min(a, b), min(c, d)), r = max(max(a, b), max(c, d));
    while (r - l > 2) {
        int64_t v1 = l + (r - l) / 3;
        int64_t v2 = r - (r - l) / 3;
        // cout << l << ' ' << r << ' ' << v1 << ' ' << v2 << endl;
        int64_t sum1 = f(v1, a, b, c, d);
        int64_t sum2 = f(v2, a, b, c, d);
        if (sum1 > sum2) {
            l = v1;
        } else {
            r = v2;
        }
    }
    int64_t ret = l;
    int64_t sum1 = f(l, a, b, c, d);
    int64_t sum2 = f(r, a, b, c, d);
    int64_t sum3 = f((r + l) / 2, a, b, c, d);
    if (sum1 > sum2) {
        ret = r;
    }
    if (sum2 > sum3) {
        ret = (r + l) / 2;
    }
    return ret;
}

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

        ans = 0;

        int halfm = m >> 1;
        int halfn = n >> 1;

        for (int i = 0; i < halfm; ++i) {
            for (int j = 0; j < halfn; ++j) {
                long long avg = findmin(a[i][j], a[m - i - 1][j], a[i][n - j - 1], a[m - i - 1][n - j - 1]);
                ans += abs(avg - a[i][j]) + abs(avg - a[m - i - 1][j]) + abs(avg - a[i][n - j - 1]) + abs(avg - a[m - i - 1][n - j - 1]);
            }
        }

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

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

        cout << ans << endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

int m, n, t;
long long a[100][100], ans;

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

        ans = 0;

        int halfm = m >> 1;
        int halfn = n >> 1;

        for (int i = 0; i < halfm; ++i) {
            for (int j = 0; j < halfn; ++j) {
                long long avg = a[i][j] + a[m - i - 1][j] + a[i][n - j - 1] + a[m - i - 1][n - j - 1];
                avg >>= 2;
                long long res = 0x00AABBCCDDEEFF00LL;
                for (int k = -1; k <= 1; ++k) {
                    long long tmp =
                        abs(avg + k - a[i][j]) +
                        abs(avg + k - a[m - i - 1][j]) +
                        abs(avg + k - a[i][n - j - 1]) +
                        abs(avg + k - a[m - i - 1][n - j - 1]);
                    res = min(res, tmp);
                }
                ans += res;
            }
        }

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

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

        cout << ans << endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

int m, n, t;
long long a[100][100], ans;

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

        ans = 0;

        for (int i = 0; i < m/2; ++i) {
            for (int j = 0; j < n/2; ++j) {
                long long avg = a[i][j] + a[m - i - 1][j] + a[i][n - j - 1] + a[m - i - 1][n - j - 1];
                avg >>= 2;
                long long res = 100000000000000LL;
                for (int k = -1; k <= 1; ++k) {
                    long long tmp =
                        abs(avg + k - a[i][j]) +
                        abs(avg + k - a[m - i - 1][j]) +
                        abs(avg + k - a[i][n - j - 1]) +
                        abs(avg + k - a[m - i - 1][n - j - 1]);
                    res = min(res, tmp);
                }
                ans += res;
            }
        }

        if (n % 2) {
            int halfn = n / 2;
            for (int i = 0; i < m/2; ++i) {
                int avg = a[i][halfn] + a[m - i - 1][halfn];

                ans += abs(avg - a[i][halfn]) + abs(avg - a[m - i - 1][halfn]);
            }
        }

        if (m % 2) {
            int halfm = m / 2;
            for (int j = 0; j < n/2; ++j) {
                int avg = a[halfm][j] + a[halfm][n - j - 1];
                avg >>= 1;
                ans += abs(avg - a[halfm][j]) + abs(avg - a[halfm][n - j - 1]);
            }
        }

        cout << ans << endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

int m, n, t;
long long a[102][102], ans;

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

        ans = 0;

        int halfm = m >> 1;
        int halfn = n >> 1;

        for (int i = 0; i < halfm; ++i) {
            for (int j = 0; j < halfn; ++j) {
                long long avg = a[i][j] + a[m - i - 1][j] + a[i][n - j - 1] + a[m - i - 1][n - j - 1];
                avg >>= 2;
                long long res = 0x00AABBCCDDEEFF00LL;
                for (int k = 0; k <= 1; ++k) {
                    long long tmp =
                        abs(avg + k - a[i][j]) +
                        abs(avg + k - a[m - i - 1][j]) +
                        abs(avg + k - a[i][n - j - 1]) +
                        abs(avg + k - a[m - i - 1][n - j - 1]);
                    res = min(res, tmp);
                }
                ans += res;
            }
        }

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

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

        cout << ans << endl;
    }

}