#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
const ll inf64 = 1e18 + 18;
const int inf32 = 1e9 + 9;
const int N = 102;
int ar[N][N];
void Solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> ar[i][j];
    }
  }
  ll res = 0;
  for (int i = 0; i < n / 2; ++i) {
    for (int j = 0; j < m / 2; ++j) {
      vector<ll> sum(4);
      vector<int> tmp = {ar[i][j], ar[i][m - j - 1], ar[n - i - 1][j],
                         ar[n - i - 1][m - j - 1]};
      for (int k = 0; k < 4; ++k) {
        for (int l = 0; l < 4; ++l) {
          sum[k] += abs(tmp[l] - tmp[k]);
        }
      }
      ll mn = inf64;
      for (auto& x : sum) {
        mn = min(mn, x);
      }
      res += mn;
    }
  }
  if (n & 1) {
    for (int i = 0; i < m / 2; ++i) {
      res += abs(ar[n / 2][i] - ar[n / 2][m - i - 1]);
    }
  }
  if (m & 1) {
    for (int i = 0; i < n / 2; ++i) {
      res += abs(ar[i][m / 2] - ar[n - i - 1][m / 2]);
    }
  }
  cout << res << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    Solve();
  }
}
