#include <bits/stdc++.h>
using namespace std;
template <typename... Ts>
void do_nothing(Ts &&...vals) {}
constexpr long long MOD = 1000000007;
constexpr int INF = 0x7f7f7f7f;
constexpr double EPS = 1e-8;
inline bool diffsign(long long a, long long b) { return a ^ b < 0; }
inline bool isodd(long long a) { return a & 1; }
inline bool iseven(long long a) { return !isodd(a); }
inline long long pow2(long long pow, long long n = 1) { return n << pow; }
constexpr long long SZ = 110;
long long mat[SZ][SZ], R, C;
long long buf[10];
long long cost4(int r0, int c0) {
  int r1 = R - 1 - r0;
  int c1 = C - 1 - c0;
  buf[0] = mat[r0][c0];
  buf[1] = mat[r1][c0];
  buf[2] = mat[r0][c1];
  buf[3] = mat[r1][c1];
  sort(buf, buf + 4);
  long long target = buf[1];
  long long ret = abs(target - mat[r0][c0]) + abs(target - mat[r1][c0]) +
                  abs(target - mat[r0][c1]) + abs(target - mat[r1][c1]);
  return ret;
}
void solve() {
  cin >> R >> C;
  for (int i = 0; i < R; ++i) {
    for (int j = 0; j < C; ++j) {
      cin >> mat[i][j];
    }
  }
  long long ans = 0;
  for (int i = 0; i < R / 2; ++i) {
    for (int j = 0; j < C / 2; ++j) {
      ans += cost4(i, j);
    }
  }
  if (R % 2 == 1) {
    long long *row = mat[R / 2];
    for (int i = 0; i < C / 2; ++i) {
      ans += abs(row[i] - row[C - 1 - i]);
    }
  }
  if (C % 2 == 1) {
    long long c = C / 2;
    for (int i = 0; i < R / 2; ++i) {
      ans += abs(mat[i][c] - mat[R - 1 - i][c]);
    }
  }
  std::cout << ans << std::endl;
}
int main(int argn, char **argv) {
  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  cin.tie(nullptr);
  int T = 1;
  cin >> T;
  for (int i = 1; i <= T; ++i) {
    solve();
  }
}
