#include <bits/stdc++.h>
using namespace std;
long long int a[101][101] = {0};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < m; j++) {
        cin >> a[i][j];
      }
    }
    long long int ans = 0;
    for (long long int i = 0; i < n / 2 + (n % 2 == 1); i++) {
      for (long long int j = 0; j < m / 2 + (m % 2 == 1); j++) {
        long long int f[4];
        f[0] = a[i][j];
        f[1] = a[i][m - j - 1];
        f[2] = a[n - i - 1][m - j - 1];
        f[3] = a[n - i - 1][j];
        if (i == n / 2 || j == m / 2) {
          if (i == n / 2 && j == m / 2) {
            continue;
          } else if (i == n / 2) {
            ans += abs(f[0] - f[1]);
          } else if (j == m / 2) {
            ans += abs(f[1] - f[2]);
          }
        } else {
          sort(f, f + 4);
          ans += abs(f[0] - f[1]) + abs(f[1] - f[1]) + abs(f[2] - f[1]) +
                 abs(f[3] - f[1]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
