#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int N = 1e5 + 90;
int n, m;
int a[200][200];
vector<int> b;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        scanf("%d", &a[i][j]);
      }
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (i >= n - 1 - i) continue;
        if (j >= m - 1 - j) continue;
        b.clear();
        b.push_back(a[i][j]);
        b.push_back(a[n - 1 - i][j]);
        b.push_back(a[i][m - 1 - j]);
        b.push_back(a[n - 1 - i][m - 1 - j]);
        sort(b.begin(), b.end());
        ans = ans + abs(a[i][j] - b[1]) + abs(a[n - 1 - i][j] - b[1]) +
              abs(a[i][m - 1 - j] - b[1]) + abs(a[n - 1 - i][m - 1 - j] - b[1]);
        a[i][j] = a[n - 1 - i][j] = a[i][m - 1 - j] = a[n - 1 - i][m - 1 - j] =
            b[1];
      }
    }
    if (n & 1) {
      int mid = n / 2;
      for (int j = 0; j <= m / 2; j++) {
        ans = ans + abs(a[mid][j] - a[mid][m - 1 - j]);
        a[mid][m - 1 - j] = a[mid][j];
      }
    }
    if (m & 1) {
      int mid = m / 2;
      for (int i = 0; i <= n / 2; i++) {
        ans = ans + abs(a[i][mid] - a[n - 1 - i][mid]);
        a[n - 1 - i][mid] = a[i][mid];
      }
    }
    cout << ans << endl;
  }
}
