#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  long long** arr = new long long*[n];
  for (int i = 0; i < n; i++) {
    arr[i] = new long long[m];
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
    }
  }
  long long ans = 0;
  for (int i = 0; i < (n + n % 2) / 2; i++) {
    for (int j = 0; j < (m + m % 2) / 2; j++) {
      vector<long long> temp;
      temp.push_back(arr[i][j]);
      temp.push_back(arr[n - 1 - i][j]);
      temp.push_back(arr[n - 1 - i][m - 1 - j]);
      temp.push_back(arr[i][m - 1 - j]);
      sort(temp.begin(), temp.end());
      long long diff = temp[3] + temp[2] - temp[1] - temp[0];
      if (n % 2 == 1 && i == (n - n % 2) / 2) {
        diff /= 2;
      }
      if (m % 2 == 1 && j == (m - m % 2) / 2) {
        diff /= 2;
      }
      ans += diff;
    }
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
