#include <bits/stdc++.h>
using namespace std;
const long long int MAX = 100;
long long int a[MAX + 1][MAX + 1] = {0};
long long int f(vector<long long int> v) {
  sort(v.begin(), v.end());
  long long int res = 0;
  long long int med;
  long long int sz = v.size();
  if (sz == 1) {
    med = v[0];
  } else if (sz == 2) {
    med = v[0];
  } else if (sz == 3) {
    med = v[1];
  } else
    med = v[2];
  for (auto i : v) res += abs(med - i);
  return res;
}
void solve() {
  long long int n, m;
  cin >> n >> m;
  for (long long int j = 1; j <= n; j++) {
    for (long long int i = 1; i <= m; i++) cin >> a[j][i];
  }
  long long int cost = 0;
  for (long long int j = 1; j <= (n + 1) / 2; j++) {
    for (long long int i = 1; i <= (m + 1) / 2; i++) {
      vector<long long int> seq;
      seq.push_back(a[j][i]);
      bool ok = true;
      if (j != n - j + 1) {
        seq.push_back(a[n - j + 1][i]);
      }
      if (i != m - i + 1) {
        seq.push_back(a[j][m - i + 1]);
      }
      if (j < n - j + 1 && i < m - i + 1) {
        seq.push_back(a[n - j + 1][m - i + 1]);
      }
      cost += f(seq);
    }
  }
  cout << cost << '\n';
  return;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
