#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int N = 105;
int a[N][N];
int main() {
  int T;
  scanf("%d", &T);
  for (; T--;) {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        scanf("%d", &a[i][j]);
      }
    long long ans = 0;
    for (int i = 1; i <= n / 2; i++)
      for (int j = 1; j <= m / 2; j++) {
        int d[4];
        d[0] = a[i][j], d[1] = a[n - i + 1][j], d[2] = a[i][m - j + 1],
        d[3] = a[n - i + 1][m - j + 1];
        sort(d, d + 4);
        for (int i = 0; i < 4; i++) ans += abs(d[i] - d[1]);
      }
    if (n & 1) {
      for (int j = 1; j <= m / 2; j++)
        ans += abs(a[n / 2 + 1][j] - a[n / 2 + 1][m - j + 1]);
    }
    if (m & 1) {
      for (int j = 1; j <= n / 2; j++)
        ans += abs(a[j][m / 2 + 1] - a[n - j + 1][m / 2 + 1]);
    }
    printf("%lld\n", ans);
  }
}
