#include <bits/stdc++.h>
long long a[111][111];
int vis[111][111];
long long b[5];
using namespace std;
long long Read() {
  long long val = 0, opt = 1;
  char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-') opt = -1;
  while (isdigit(ch)) (val *= 10) += ch - '0', ch = getchar();
  return val * opt;
}
int main(void) {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, m;
    long long ans = 0;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) a[i][j] = Read();
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++)
        if (!vis[i][j]) {
          vis[i][j] = 1;
          vis[n - i + 1][j] = 1;
          vis[i][m - j + 1] = 1;
          vis[n - i + 1][m - j + 1] = 1;
          if (i != n - i + 1 && j != m - j + 1) {
            b[1] = a[i][j], b[2] = a[n - i + 1][j], b[3] = a[i][m - j + 1];
            b[4] = a[n - i + 1][m - j + 1];
            sort(b + 1, b + 4 + 1);
            ans += b[2] - b[1] + b[3] - b[2] + b[4] - b[2];
          } else if (i == n - i + 1 && j != m - j + 1) {
            b[1] = a[i][j], b[2] = a[i][m - j + 1];
            ans += abs(b[2] - b[1]);
          } else if (i != n - i + 1 && j == m - j + 1) {
            ans += abs(a[i][j] - a[n - i + 1][j]);
          }
        }
    }
    printf("%lld\n", ans);
    memset(vis, 0, sizeof vis);
  }
}
