#include <bits/stdc++.h>
using namespace std;
const int M = 1e2 + 7;
long long mat[M][M];
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) cin >> mat[i][j];
  }
  long long res = 0;
  for (int i = 1; i <= n / 2; i++) {
    long long mn = 1e14L;
    long long a[4];
    for (int j = 1; j <= m / 2; j++) {
      a[0] = mat[i][j];
      a[1] = mat[n - i + 1][j];
      a[2] = mat[i][m - j + 1];
      a[3] = mat[n - i + 1][m - j + 1];
      sort(a, a + 4);
      long long val = (a[1] + a[2]) / 2;
      res += abs(mat[i][j] - val);
      res += abs(mat[n - i + 1][j] - val);
      res += abs(mat[i][m - j + 1] - val);
      res += abs(mat[n - i + 1][m - j + 1] - val);
    }
  }
  if (n & 1) {
    int mid = (n + 1) / 2;
    for (int i = 1; i <= m / 2; i++)
      res += abs(mat[mid][i] - mat[mid][m - i + 1]);
  }
  if (m & 1) {
    int mid = (m + 1) / 2;
    for (int i = 1; i <= n / 2; i++)
      res += abs(mat[i][mid] - mat[n - i + 1][mid]);
  }
  cout << res << "\n";
  ;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
