#include <bits/stdc++.h>
using namespace std;
const char newl = '\n';
void DEBUGLOCAL() { cerr << ")" << newl; }
template <class A, class... B>
void DEBUGLOCAL(A argc, B... argv) {
  cerr << argc;
  if (sizeof...(argv)) cerr << ", ";
  DEBUGLOCAL(argv...);
}
int ts;
int n, m;
long long a[105][105];
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 / 2); ++i)
    for (int j = 0; j < (m / 2); ++j) {
      long long sum = LLONG_MAX;
      long long tmp = abs(a[i][j] - a[n - i - 1][j]) +
                      abs(a[i][j] - a[i][m - j - 1]) +
                      abs(a[i][j] - a[n - i - 1][m - j - 1]);
      sum = min(sum, tmp);
      tmp = abs(a[n - i - 1][j] - a[i][j]) +
            abs(a[n - i - 1][j] - a[i][m - j - 1]) +
            abs(a[n - i - 1][j] - a[n - i - 1][m - j - 1]);
      sum = min(sum, tmp);
      tmp = abs(a[i][m - j - 1] - a[i][j]) +
            abs(a[i][m - j - 1] - a[n - i - 1][j]) +
            abs(a[i][m - j - 1] - a[n - i - 1][m - j - 1]);
      sum = min(sum, tmp);
      tmp = abs(a[n - i - 1][m - j - 1] - a[i][j]) +
            abs(a[n - i - 1][m - j - 1] - a[n - i - 1][j]) +
            abs(a[n - i - 1][m - j - 1] - a[i][m - j - 1]);
      sum = min(sum, tmp);
      ans += sum;
    }
  if (n & 1) {
    int i = n / 2;
    for (int j = 0; j < m / 2; ++j) {
      ans += abs(a[i][j] - a[i][m - j - 1]);
    }
  }
  if (m & 1) {
    int j = m / 2;
    for (int i = 0; i < n / 2; ++i) {
      ans += abs(a[i][j] - a[n - i - 1][j]);
    }
  }
  cout << ans << newl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> ts;
  while (ts--) solve();
}
