#include <bits/stdc++.h>
using namespace std;
const long long mod = (0 ? 998244353 : 1000000007);
const long long inf = 1 << 30;
const long long llf = 9e18;
long long powmod(long long a, long long b, long long pp = mod) {
  long long res = 1;
  a %= pp;
  assert(b >= 0);
  for (; b; b >>= 1, a = a * a % pp)
    if (b & 1) res = res * a % pp;
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long mul(long long a, long long b, long long pp = mod) {
  long long ans = 0;
  for (; b; b >>= 1) {
    if (b & 1) ans = (ans + a) % pp;
    a = a * 2 % pp;
  }
  return ans;
}
struct node {
  long long x, y, z, time;
  long long no, w, h;
  long long pre, nxt;
  friend bool operator<(const node& a, const node& b) { return a.w < b.w; }
};
bool cmp(long long x, long long y) { return x > y; }
inline long long min(long long a, long long b) {
  if (a > b)
    return b;
  else
    return a;
}
inline long long max(long long a, long long b) {
  if (a < b)
    return b;
  else
    return a;
}
inline void upmax(long long& a, long long b) {
  if (a < b) a = b;
};
inline void upmin(long long& a, long long b) {
  if (a > b) a = b;
};
template <class T>
inline void pt(T x) {
  static long long top, stk[105];
  if (x < 0) x = -x, putchar('-');
  if (x == 0) putchar('0');
  while (x) stk[++top] = x % 10, x /= 10;
  while (top) putchar(stk[top--] + '0');
}
long long dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
long long dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};
const long long N = 200005;
const double eps = 1e-12;
long long a[200][200];
signed main() {
  ios::sync_with_stdio(false);
  long long _ = 1;
  cin >> _;
  while (_--) {
    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) {
        long long b[4] = {a[i][j], a[i][m - j + 1], a[n - i + 1][j],
                          a[n - i + 1][m - j + 1]};
        sort(b, b + 4);
        for (long long k = 0; k <= 3; ++k) ans += abs(b[k] - b[1]);
      }
    }
    if (n % 2) {
      long long i = (n + 1) / 2;
      for (long long j = 1; j <= m / 2; ++j) {
        long long b[4] = {a[i][j], a[i][m - j + 1]};
        sort(b, b + 2);
        for (long long k = 0; k <= 1; ++k) ans += abs(b[k] - b[1]);
      }
    }
    if (m % 2) {
      long long j = (m + 1) / 2;
      for (long long i = 1; i <= n / 2; ++i) {
        long long b[2] = {a[i][j], a[n - i + 1][j]};
        sort(b, b + 2);
        for (long long k = 0; k <= 1; ++k) ans += abs(b[k] - b[1]);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
