#include <bits/stdc++.h>
using namespace std;
const int N = 505;
long long MOD = 1e9 + 7;
long long INF = -1e9;
long long a[105][105];
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < (n); i++) {
    for (int j = 0; j < (m); j++) {
      cin >> a[i][j];
    }
  }
  long long ans = 0;
  for (int i = 0; i < (n); i++) {
    for (int j = 0; j < (m); j++) {
      int dx = n - i - 1;
      int dy = m - j - 1;
      long long T1 = a[i][j];
      long long T2 = a[dx][j];
      long long t3 = a[i][dy];
      long long t4 = a[dx][dy];
      if ((T1 == T2) && (T2 == t3) && (t3 == t4)) {
        continue;
      }
      vector<long long> p;
      if (i != dx) p.push_back(T1);
      if (j != dy) p.push_back(T2);
      p.push_back(t3);
      p.push_back(t4);
      sort(p.begin(), p.end());
      int sz = p.size();
      int k = sz / 2;
      long long sum = p[k];
      if (j != dy) {
        ans += abs(T2 - sum);
      }
      if (i != dx) {
        ans += abs(T1 - sum);
      }
      ans += abs(t4 - sum);
      ans = ans + abs(t3 - sum);
      a[i][j] = sum;
      a[dx][j] = sum;
      a[i][dy] = sum;
      a[dx][dy] = sum;
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
