#include <bits/stdc++.h>
using namespace std;
const long long int M = 1000000007;
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res % p;
}
long long int modinver(long long int a, long long int b) {
  return (power(a, 1, M) * power(b, M - 2, M)) % M;
}
void SieveOfEratosthenes(int n) {
  bool prime[10005];
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    long long int a[n + 1][m + 1];
    long long int vis[n + 1][m + 1];
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        cin >> a[i][j];
        vis[i][j] = -1;
      }
    }
    long long int ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; j++) {
        if (vis[i][j] != -1) continue;
        vector<int> xx;
        xx.push_back(a[i][j]);
        if (n - 1 - i != i) {
          xx.push_back(a[n - 1 - i][j]);
          vis[n - 1 - i][j] = 1;
        }
        if (m - 1 - j != j) {
          vis[i][m - 1 - j] = 1;
          xx.push_back(a[i][m - 1 - j]);
        }
        if (n - 1 - i != i && m - 1 - j != j) {
          vis[n - 1 - i][m - 1 - j] = 1;
          xx.push_back(a[n - 1 - i][m - 1 - j]);
        }
        if (xx.size() == 2) {
          ans += abs(xx[0] - xx[1]);
        }
        if (xx.size() == 4) {
          sort(xx.begin(), xx.end());
          for (int i = 0; i < xx.size(); ++i) {
            ans += abs(xx[i] - xx[1]);
          }
        }
      }
    }
    cout << ans << '\n';
  }
}
