#include <bits/stdc++.h>
using namespace std;
void faster() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
long long n, m, a[200][200];
void solve() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> a[i][j];
    }
  }
  long long ans = 0;
  for (int i = 0; i <= (n - i - 1); ++i) {
    for (int j = 0; j <= (m - j - 1); ++j) {
      vector<long long> val = {a[i][j], a[i][m - j - 1], a[n - i - 1][j],
                               a[n - i - 1][m - j - 1]};
      sort(val.begin(), val.end());
      long long res = 0;
      for (int k = 0; k < 4; ++k) {
        res += abs(val[k] - val[1]);
      }
      if (i == n - i - 1 && j == m - j - 1) {
        res /= 4;
      } else if (i == n - i - 1 || j == m - j - 1) {
        res /= 2;
      } else {
        res /= 1;
      }
      ans += res;
    }
  }
  cout << ans << '\n';
}
int main() {
  faster();
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
