#include <bits/stdc++.h>
using namespace std;
using cd = complex<double>;
const double PI = acos(-1);
int main() {
  ios_base::sync_with_stdio(false);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    long long** a = new long long*[n];
    for (int i = 0; i < n; i++) {
      a[i] = new long long[m];
      for (int j = 0; j < m; j++) {
        cin >> a[i][j];
      }
    }
    long long sum = 0;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) {
        vector<long long> V;
        V.push_back(a[i][j]);
        V.push_back(a[n - i - 1][j]);
        V.push_back(a[i][m - j - 1]);
        V.push_back(a[n - i - 1][m - j - 1]);
        sort(V.begin(), V.end());
        long long ans = (V[1] + V[2]) / 2;
        long long cnt = abs(V[0] - ans) + abs(V[1] - ans) + abs(V[2] - ans) +
                        abs(V[3] - ans);
        sum += cnt;
      }
    }
    sum /= 4;
    cout << sum << endl;
  }
}
