#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
  long long x = 0;
  char o, f = 1;
  while (o = getchar(), o < 48)
    if (o == 45) f = -f;
  do x = (x << 3) + (x << 1) + (o ^ 48);
  while (o = getchar(), o > 47);
  return x * f;
}
const int maxn = 1e2 + 5;
const long long mod = 998244353;
int _;
int n, m, a[maxn][maxn];
long long ans;
void solve(int x, int y) {
  vector<int> v;
  v.push_back(a[x][y]);
  if (n - x + 1 != x) v.push_back(a[n - x + 1][y]);
  if (m - y + 1 != y) v.push_back(a[x][m - y + 1]);
  if (n - x + 1 != x && m - y + 1 != y) v.push_back(a[n - x + 1][m - y + 1]);
  sort(v.begin(), v.end());
  int len = v.size();
  len /= 2;
  for (auto u : v) {
    ans += abs(v[len] - u);
  }
}
int main() {
  for (_ = rd(); _; _--) {
    ans = 0;
    n = rd();
    m = rd();
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) a[i][j] = rd();
    for (int i = 1; i <= (n + 1) / 2; i++) {
      for (int j = 1; j <= (m + 1) / 2; j++) solve(i, j);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
