#include <bits/stdc++.h>
using namespace std;
void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void one();
int main() {
  fastio();
  int t = 1;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    one();
  }
  return 0;
}
int a[128][128];
void one() {
  int n;
  cin >> n;
  int m;
  cin >> 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 / 2 + (n % 2 > 0 ? 1 : 0); ++i) {
    for (int j = 0; j < m / 2 + (m % 2 > 0 ? 1 : 0); ++j) {
      set<pair<int, int>> s;
      s.insert({i, j});
      s.insert({n - 1 - i, j});
      s.insert({i, m - 1 - j});
      s.insert({n - 1 - i, m - 1 - j});
      if (s.size() < 2) continue;
      vector<long long> v;
      for (auto p : s) {
        v.push_back(a[p.first][p.second]);
      }
      sort(v.begin(), v.end());
      long long cur = 0;
      for (auto x : v) cur += x > v[1] ? x - v[1] : v[1] - x;
      ans += cur;
    }
  }
  cout << ans << endl;
}
