#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 3;
const long long MOD = 1e9 + 7;
const int oo = 1e9;
int n, m;
long long a[N][N];
long long Calc(int ar1, int ar2, int ar3, int ar4) {
  long long ar[] = {ar1, ar2, ar3, ar4};
  sort(ar, ar + 4);
  return ar[1];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) cin >> a[i][j];
    long long res = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        int tv = Calc(a[i][j], a[i][m + 1 - j], a[n + 1 - i][j],
                      a[n + 1 - i][m + 1 - j]);
        res += abs(tv - a[i][j]);
        a[i][j] = tv;
        res += abs(tv - a[i][m + 1 - j]);
        a[i][m + 1 - j] = tv;
        res += abs(tv - a[n + 1 - i][j]);
        a[n + 1 - i][j] = tv;
        res += abs(tv - a[n + 1 - i][m + 1 - j]);
        a[n + 1 - i][m + 1 - j] = tv;
      }
    cout << res << '\n';
  }
  return 0;
}
