#include <bits/stdc++.h>
using namespace std;
long long a[105][105];
bool cmp(long long x, long long y) { return x < y; }
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    memset(a, 0, sizeof(a));
    long long n, m, ans = 0;
    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 zz[4];
    for (long long i = 1; i <= n / 2; i++) {
      for (long long j = 1; j <= m / 2; j++) {
        long long p, q, x, y;
        p = a[i][j], x = a[i][m - j + 1];
        q = a[n - i + 1][j], y = a[n - i + 1][m - j + 1];
        if (p < q) swap(p, q);
        if (x < y) swap(x, y);
        if (p < x) swap(p, x);
        if (q > y) swap(q, y);
        ans += abs(p - q) + abs(x - y);
      }
    }
    if (n & 1) {
      for (long long j = 1; j <= m / 2; j++) {
        ans += abs(a[(n + 1) / 2][j] - a[(n + 1) / 2][m - j + 1]);
      }
    }
    if (m & 1) {
      for (long long i = 1; i <= n / 2; i++) {
        ans += abs(a[i][(m + 1) / 2] - a[n - i + 1][(m + 1) / 2]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
