#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int mp[N][N];
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> mp[i][j];
    }
  }
  long long ans = 0;
  int lr = n / 2, lc = m / 2;
  int ax = 1, ay = 1, bx = 1, by = m;
  int cx = n, cy = 1, dx = n, dy = m;
  long long tt[4];
  for (int i = 0; i < lr; i++) {
    for (int j = 0; j < lc; j++) {
      if (!i && !j) continue;
      tt[0] = mp[ax + i][ay + j], tt[1] = mp[bx + i][by - j];
      tt[2] = mp[cx - i][cy + j], tt[3] = mp[dx - i][dy - j];
      long long t = 1e18;
      for (int x = 0; x < 4; x++) {
        long long s = 0;
        for (int k = 0; k < 4; k++) {
          if (k == x) continue;
          s += abs(tt[k] - tt[x]);
        }
        t = min(t, s);
      }
      ans += t;
    }
  }
  long long t = 1e18;
  tt[0] = mp[ax][ay], tt[1] = mp[bx][by], tt[2] = mp[cx][cy],
  tt[3] = mp[dx][dy];
  for (int x = 0; x < 4; x++) {
    long long s = 0;
    for (int k = 0; k < 4; k++) {
      if (k == x) continue;
      s += abs(tt[k] - tt[x]);
    }
    t = min(t, s);
  }
  ans += t;
  if (n % 2) {
    ax = n / 2 + 1, ay = 1, bx = n / 2 + 1, by = m;
    for (int i = 0; i < lc; i++) {
      ans += abs(mp[ax][ay + i] - mp[bx][by - i]);
    }
  }
  if (m % 2) {
    ax = 1, ay = m / 2 + 1, bx = n, by = m / 2 + 1;
    for (int i = 0; i < lr; i++) {
      ans += abs(mp[ax + i][ay] - mp[bx - i][by]);
    }
  }
  cout << ans << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
