#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  long long a[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  long long ans = 0;
  long long row_ele, col_ele, max_ele, mid, min_ele;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      row_ele = a[i][m - j - 1];
      col_ele = a[n - i - 1][j];
      max_ele = max({a[i][j], row_ele, col_ele});
      min_ele = min({a[i][j], row_ele, col_ele});
      mid = (a[i][j] + row_ele + col_ele) - (max_ele + min_ele);
      ans += (max_ele - mid) + (mid - min_ele);
      a[i][j] = mid;
      a[i][m - j - 1] = mid;
      a[n - i - 1][j] = mid;
    }
  }
  cout << ans << "\n";
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
