#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int tc;
  cin >> tc;
  while (tc--) {
    int arr[103][103];
    long long cnt = 0;
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) cin >> arr[i][j];
    for (int i = 1; i <= n / 2; i++) {
      vector<int> a;
      for (int j = 1; j <= m / 2; j++) {
        a.push_back(arr[i][j]);
        a.push_back(arr[n + 1 - i][j]);
        a.push_back(arr[i][m + 1 - j]);
        a.push_back(arr[n + 1 - i][m + 1 - j]);
        sort(a.begin(), a.end());
        for (int tmp : a) cnt += abs(tmp - a[1]);
        a.clear();
      }
      if (m % 2) cnt += abs(arr[i][m / 2 + 1] - arr[n + 1 - i][m / 2 + 1]);
    }
    if (n % 2) {
      for (int j = 1; j <= m / 2; j++)
        cnt += abs(arr[n / 2 + 1][j] - arr[n / 2 + 1][m + 1 - j]);
    }
    cout << cnt << "\n";
  }
  return 0;
}
