#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
int t;
int n, m;
int arr[MAXN][MAXN];
int main() {
  scanf("%d", &t);
  while (t--) {
    long long ans = 0;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) scanf("%d", &arr[i][j]);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        vector<int> v;
        v.push_back(arr[n - i - 1][m - j - 1]);
        v.push_back(arr[i][j]);
        v.push_back(arr[n - i - 1][j]);
        v.push_back(arr[i][m - j - 1]);
        sort(v.begin(), v.end());
        ans += abs(v[1] - arr[n - i - 1][m - j - 1]);
        arr[n - i - 1][m - j - 1] = v[1];
        ans += abs(v[1] - arr[i][j]);
        arr[i][j] = v[1];
        ans += abs(v[1] - arr[n - i - 1][j]);
        arr[n - i - 1][j] = v[1];
        ans += abs(v[1] - arr[i][m - j - 1]);
        arr[i][m - j - 1] = v[1];
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
