#include <bits/stdc++.h>
using namespace std;
const long long maxn = 110 + 1;
long long a[maxn][maxn];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++) {
        cin >> a[i][j];
      }
    }
    long long ans = 0;
    for (long long i = 1; i <= n / 2; i++) {
      for (long long j = 1; j <= m / 2; j++) {
        vector<long long> temp;
        temp.push_back(a[i][j]);
        temp.push_back(a[i][m - j + 1]);
        temp.push_back(a[n - i + 1][j]);
        temp.push_back(a[n - i + 1][m - j + 1]);
        sort(temp.begin(), temp.end());
        for (auto i : temp) {
          ans += abs(temp[1] - i);
        }
      }
    }
    if (n & 1) {
      for (long long i = 1; i <= m / 2; i++) {
        ans += abs(a[n / 2 + 1][i] - a[n / 2 + 1][m - i + 1]);
      }
    }
    if (m & 1) {
      for (long long i = 1; i <= n / 2; i++) {
        ans += abs(a[i][m / 2 + 1] - a[n - i + 1][m / 2 + 1]);
      }
    }
    cout << ans << endl;
  }
}
