#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e6 + 10;
long long N, M, T, g[110][110], d[4];
int main() {
  scanf("%lld", &T);
  while (T--) {
    long long ans = 0;
    scanf("%lld%lld", &N, &M);
    for (long long i = 1; i <= N; i++) {
      for (long long j = 1; j <= M; j++) {
        scanf("%lld", &g[i][j]);
      }
    }
    for (long long i = 1, ii = N; i <= ii; i++, ii--) {
      for (long long j = 1, jj = M; j <= jj; j++, jj--) {
        d[0] = g[i][j], d[1] = g[i][jj], d[2] = g[ii][j], d[3] = g[ii][jj];
        sort(d, d + 4);
        long long tem = d[2] + d[3] - d[0] - d[1];
        if (ii == i || j == jj) tem /= 2;
        ans += tem;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
