#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
long long a[150][150];
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long chu(long long x, long long y) {
  if (x % y == 0)
    return x / y;
  else
    return x / y + 1;
}
signed main() {
  long long T = read();
  while (T--) {
    long long n = read(), m = read();
    for (long long i = 1; i <= n; i++)
      for (long long j = 1; j <= m; j++) a[i][j] = read();
    long long ans = 0;
    for (long long i = 1; i <= chu(n, 2); i++) {
      for (long long j = 1; j <= chu(m, 2); j++) {
        if (i == n + 1 - i && j == m + 1 - j)
          ;
        else if (i != n + 1 - i && j != m + 1 - j) {
          long long mn = 9223372036854775807;
          int val;
          if (mn > abs(a[i][j] - a[i][m + 1 - j]) +
                       abs(a[i][j] - a[n + 1 - i][j]) +
                       abs(a[i][j] - a[n + 1 - i][m + 1 - j])) {
            mn = abs(a[i][j] - a[i][m + 1 - j]) +
                 abs(a[i][j] - a[n + 1 - i][j]) +
                 abs(a[i][j] - a[n + 1 - i][m + 1 - j]);
            val = a[i][j];
          }
          if (mn > abs(a[i][m + 1 - j] - a[i][j]) +
                       abs(a[i][m + 1 - j] - a[n + 1 - i][j]) +
                       abs(a[i][m + 1 - j] - a[n + 1 - i][m + 1 - j])) {
            mn = abs(a[i][m + 1 - j] - a[i][j]) +
                 abs(a[i][m + 1 - j] - a[n + 1 - i][j]) +
                 abs(a[i][m + 1 - j] - a[n + 1 - i][m + 1 - j]);
            val = a[i][m + 1 - j];
          }
          if (mn > abs(a[n + 1 - i][j] - a[i][j]) +
                       abs(a[n + 1 - i][j] - a[i][m + 1 - j]) +
                       abs(a[n + 1 - i][j] - a[n + 1 - i][m + 1 - j])) {
            mn = abs(a[n + 1 - i][j] - a[i][j]) +
                 abs(a[n + 1 - i][j] - a[i][m + 1 - j]) +
                 abs(a[n + 1 - i][j] - a[n + 1 - i][m + 1 - j]);
            val = a[n + 1 - i][j];
          }
          if (mn > abs(a[n + 1 - i][m + 1 - j] - a[i][j]) +
                       abs(a[n + 1 - i][m + 1 - j] - a[n + 1 - i][j]) +
                       abs(a[n + 1 - i][m + 1 - j] - a[i][m + 1 - j])) {
            mn = abs(a[n + 1 - i][m + 1 - j] - a[i][j]) +
                 abs(a[n + 1 - i][m + 1 - j] - a[n + 1 - i][j]) +
                 abs(a[n + 1 - i][m + 1 - j] - a[i][m + 1 - j]);
            val = a[n + 1 - i][m + 1 - j] - a[i][j];
          }
          a[i][j] = a[i][m + 1 - j] = a[n + 1 - i][j] =
              a[n + 1 - i][m + 1 - j] = val;
          ans += mn;
        } else if (i == n + 1 - i || j != m + 1 - j) {
          long long mn = 9223372036854775807;
          mn = min(abs(a[i][j] - a[i][m + 1 - j]), mn);
          a[i][j] = a[i][m + 1 - j];
          ans += mn;
        } else {
          long long mn = 9223372036854775807;
          mn = min(abs(a[i][j] - a[n + 1 - i][j]), mn);
          a[i][j] = a[n + 1 - i][j];
          ans += mn;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
