#include <bits/stdc++.h>
using namespace std;
long long dist(long long m, long long a, long long b, long long c,
               long long d) {
  return (abs(m - a) + abs(m - b) + abs(m - c) + abs(m - d));
}
long long dist2(long long m, long long a, long long b) {
  return (abs(m - a) + abs(m - b));
}
long long v[110][110];
void solve() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) cin >> v[i][j];
  long long ans = 0;
  long long a, b, c, d;
  for (long long i = 0; i < n / 2; i++) {
    for (long long j = 0; j < m / 2; j++) {
      a = v[i][j];
      b = v[i][m - j - 1];
      c = v[n - i - 1][j];
      d = v[n - i - 1][m - j - 1];
      long long mean = (a + b + c + d) / 4;
      long long count = dist(mean, a, b, c, d);
      vector<int> v;
      v.push_back(a);
      v.push_back(b);
      v.push_back(c);
      v.push_back(d);
      sort(v.begin(), v.end());
      ans += (v[3] - v[0]);
      ans += (v[2] - v[1]);
    }
  }
  if (m % 2) {
    for (long long i = 0; i < n / 2; i++) {
      long long j = m / 2;
      a = v[i][j];
      b = v[n - i - 1][j];
      if (a > b) swap(a, b);
      ans += (b - a);
    }
  }
  if (n % 2) {
    for (long long j = 0; j < m / 2; j++) {
      long long i = n / 2;
      a = v[i][j];
      b = v[i][m - j - 1];
      if (a > b) swap(a, b);
      ans += (b - a);
    }
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long T;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
