#include <bits/stdc++.h>
using namespace std;
int arr[100][100];
int n, m;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int TC;
  cin >> TC;
  for (int tc = 1; tc <= TC; ++tc) {
    long long ans = 0;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        cin >> arr[i][j];
      }
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        int a[4];
        a[0] = arr[i][j];
        a[1] = arr[n - 1 - i][j];
        a[2] = arr[i][m - 1 - j];
        a[3] = arr[n - 1 - i][m - 1 - j];
        sort(a, a + 4);
        int avg = a[1];
        ans += abs(arr[i][j] - avg) + abs(arr[n - 1 - i][j] - avg) +
               abs(arr[i][m - 1 - j] - avg) +
               abs(arr[n - 1 - i][m - 1 - j] - avg);
      }
    }
    cout << (ans >> 2) << "\n";
  }
  return 0;
}
