#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
void speed() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int32_t main() {
  speed();
  long long zzz;
  cin >> zzz;
  while (zzz--) {
    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 change = 0;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) {
        vector<long long> v;
        v.push_back(a[i][j]);
        v.push_back(a[i][m - j - 1]);
        v.push_back(a[n - i - 1][j]);
        sort(v.begin(), v.end());
        a[i][j] = a[i][m - j - 1] = a[n - i - 1][j] = v[1];
        change += (v[2] - v[0]);
      }
    }
    cout << change << '\n';
    ;
  }
  return 0;
}
