#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
ll solve() {
  int n, m;
  cin >> n >> m;
  ll a[n + 1][m + 1];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> a[i][j];
  bool seen[n + 1][m + 1];
  for (int i = 0; i <= n; i++) fill_n(seen[i], m + 1, false);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!seen[i][j]) {
        pii pos[4] = {
            {i, j}, {i, m - j + 1}, {n - i + 1, j}, {n - i + 1, m - j + 1}};
        vector<ll> opts;
        for (auto& p : pos) {
          opts.push_back(a[p.first][p.second]);
        }
        sort(opts.begin(), opts.end());
        for (auto& p : pos) {
          if (!seen[p.first][p.second])
            ans += abs(a[p.first][p.second] - opts[1]);
          seen[p.first][p.second] = true;
        }
      }
    }
  }
  return ans;
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) cout << solve() << '\n';
}
