#include <bits/stdc++.h>
using namespace std;
void init() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
int main() {
  init();
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    long long arr[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> arr[i][j];
      }
    }
    long long ops = 0;
    for (long long i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        long long a[4];
        a[0] = arr[i][j];
        a[1] = arr[n - 1 - i][m - 1 - j];
        a[2] = arr[i][m - 1 - j];
        a[3] = arr[n - 1 - i][j];
        sort(a, a + 4);
        long long m = (a[1] + a[2]) / 2;
        for (int ii = 0; ii < 4; ii++) {
          ops += abs(a[ii] - (m));
        }
      }
    }
    ops /= 4;
    cout << ops << endl;
  }
}
