#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
long long int pow1(long long int n, long long int p) {
  if (p == 0) return 1;
  long long int x = pow1(n, p / 2);
  x = (x * x);
  if (p % 2 == 0)
    return x;
  else
    return (x * n);
}
long long int pd(long long int x, long long int y) {
  if (x % y == 0)
    return x / y;
  else
    return x / y + 1;
}
long long int abss(long long int x) { return max(x, -x); }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int n, m, j, k, l, o;
    cin >> n >> m;
    long long int ans = 0;
    long long int a[n][m];
    for (long long int i = 0; i < n; i++)
      for (long long int j = 0; j < m; j++) cin >> a[i][j];
    for (long long int i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        long long int D = 1000000007 * 2;
        long long int K = 0;
        K = a[i][j];
        D = min(D, abss(a[i][j] - K) + abss(a[n - 1 - i][j] - K) +
                       abss(a[i][m - 1 - j] - K) +
                       abss(a[n - 1 - i][m - 1 - j] - K));
        K = a[n - 1 - i][j];
        D = min(D, abss(a[i][j] - K) + abss(a[n - 1 - i][j] - K) +
                       abss(a[i][m - 1 - j] - K) +
                       abss(a[n - 1 - i][m - 1 - j] - K));
        K = a[i][m - 1 - j];
        D = min(D, abss(a[i][j] - K) + abss(a[n - 1 - i][j] - K) +
                       abss(a[i][m - 1 - j] - K) +
                       abss(a[n - 1 - i][m - 1 - j] - K));
        K = a[n - 1 - i][m - 1 - j];
        D = min(D, abss(a[i][j] - K) + abss(a[n - 1 - i][j] - K) +
                       abss(a[i][m - 1 - j] - K) +
                       abss(a[n - 1 - i][m - 1 - j] - K));
        long long int B[4];
        B[0] = a[i][j];
        B[1] = a[n - 1 - i][j];
        B[2] = a[i][m - 1 - j];
        B[3] = a[n - 1 - i][m - 1 - j];
        for (long long int g = 0; g < 4; g++)
          for (long long int h = 0; h < 4; h++) {
            K = B[h] + B[g];
            D = min(D, abss(a[i][j] - K) + abss(a[n - 1 - i][j] - K) +
                           abss(a[i][m - 1 - j] - K) +
                           abss(a[n - 1 - i][m - 1 - j] - K));
          }
        ans += D;
      }
    }
    cout << ans / 4;
    cout << "\n";
  }
  return 0;
}
