#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long N = 2e5 + 7;
const int mod = 1e9 + 7;
bool cmp(const pair<long long, long long>& a,
         const pair<long long, long long>& b) {
  return a.first < b.first;
}
long long median(long long a, long long b, long long c, long long d) {
  vector<long long> v(4);
  v[0] = a;
  v[1] = b;
  v[2] = c;
  v[3] = d;
  sort(v.begin(), v.end());
  return ((v[1] + v[2]) / 2);
}
void solve() {
  long long n, m;
  cin >> n >> m;
  long long a[n][m];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      long long k = median(a[i][j], a[i][m - 1 - j], a[n - 1 - i][j],
                           a[n - 1 - i][m - 1 - j]);
      ans += abs(k - a[i][j]);
    }
  }
  cout << ans << '\n';
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
