#include <bits/stdc++.h>
using namespace std;
const long long N = 105;
long long n, m;
long long a[N][N];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++) cin >> a[i][j];
    }
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++) {
        vector<long long> v = {a[i][j], a[i][m - j + 1], a[n - i + 1][j],
                               a[n - i + 1][m - j + 1]};
        sort(v.begin(), v.end());
        long long val = v[1];
        ans += abs(a[i][j] - val);
        a[i][j] = val;
        ans += abs(a[i][m - j + 1] - val);
        a[i][m - j + 1] = val;
        ans += abs(a[n - i + 1][j] - val);
        a[n - i + 1][j] = val;
        ans += abs(a[i][j] - val);
        a[i][m - j + 1] = val;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
