Nice Matrix
A matrix of size $$$n \times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \le i \le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him!
In the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5
The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \le t \le 10$$$). The $$$t$$$ tests follow.The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the size of the matrix.Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, j} \le 10^9$$$) — the elements of the matrix.
For each test output the smallest number of operations required to make the matrix nice.
from collections import defaultdict

def main():
    (n, m) = map(int, input().split())
    a = []
    c = 0
    for i in range(n):
        a.append(list(map(int, input().split())))
    for i in range(n // 2):
        for j in range(m // 2):
            l = sorted([a[i][j], a[i][m - j - 1], a[n - i - 1][j], a[n - i - 1][m - j - 1]])
            avg = (l[1] + l[2]) // 2
            c += abs(a[i][j] - avg) + abs(a[i][m - j - 1] - avg) + abs(a[n - i - 1][j] - avg) + abs(a[n - i - 1][m - j - 1] - avg)
    if n % 2 != 0:
        for j in range(m // 2):
            avg = (a[n // 2][j] + a[n // 2][m - j - 1]) // 2
            c += abs(a[n // 2][j] - avg) + abs(a[n // 2][m - j - 1] - avg)
    if m % 2 != 0:
        for i in range(n // 2):
            avg = (a[i][m // 2] + a[n - i - 1][m // 2]) // 2
            c += abs(a[i][m // 2] - avg) + abs(a[n - i - 1][m // 2] - avg)
    print(c)
for i in range(int(input())):
    main()
def matrix(rows):
    res = 0
    for i in range(len(rows) // 2):
        for x in range(len(rows[0]) // 2):
            (one, two, three, four) = (rows[i][x], rows[len(rows) - i - 1][x], rows[i][len(rows[0]) - x - 1], rows[len(rows) - i - 1][len(rows[0]) - x - 1])
            (one, two, three, four) = sorted([one, two, three, four])
            res += two - one + three - two + four - two
        if len(rows[0]) % 2:
            res += abs(rows[i][len(rows[0]) // 2] - rows[len(rows) - i - 1][len(rows[0]) // 2])
    if len(rows) % 2:
        for x in range(len(rows[0]) // 2):
            res += abs(rows[len(rows) // 2][x] - rows[len(rows) // 2][len(rows[0]) - x - 1])
    return res
testcases = range(int(input()))
for case in testcases:
    (rows, cols) = [int(i) for i in input().split(' ')]
    rowss = []
    for i in range(rows):
        rowss.append([int(i) for i in input().split(' ')])
    print(matrix(rowss))
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    (n, m) = map(int, input().split())
    g = [list(map(int, input().split())) for _ in range(n)]
    s = 0
    for i in range(n // 2):
        for j in range(m // 2):
            a = g[i][j]
            b = g[i][m - 1 - j]
            c = g[n - 1 - i][j]
            d = g[n - 1 - i][m - 1 - j]
            (a, b, c, d) = sorted([a, b, c, d])
            s += c + d - a - b
    if n % 2:
        for i in range(m // 2):
            a = g[n // 2][i]
            b = g[n // 2][m - 1 - i]
            x = (a + b) // 2
            s += abs(a - x) + abs(b - x)
    if m % 2:
        for i in range(n // 2):
            a = g[i][m // 2]
            b = g[n - 1 - i][m // 2]
            x = (a + b) // 2
            s += abs(a - x) + abs(b - x)
    print(s)