#include <bits/stdc++.h>
using namespace std;
long long s[105][105];
long long a[10];
int main() {
  int t, n, m;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> s[i][j];
      }
    }
    long long ans = 0, sum;
    for (int i = 1; i <= n / 2; i++) {
      for (int j = 1; j <= m / 2; j++) {
        a[0] = s[i][j], a[1] = s[i][m - j + 1], a[2] = s[n - i + 1][j],
        a[3] = s[n - i + 1][m - j + 1];
        sort(a, a + 4);
        sum = (a[1] + a[2]) / 2;
        ans +=
            (abs(s[i][j] - sum) + abs(s[i][m - j + 1] - sum) +
             abs(s[n - i + 1][j] - sum) + abs(s[n - i + 1][m - j + 1] - sum));
      }
    }
    if (n % 2 == 1) {
      int i = n / 2 + 1;
      for (int j = 1; j <= m / 2; j++) {
        sum = s[i][j];
        ans += abs(sum - s[i][j]) + abs(sum - s[i][m - j + 1]);
      }
    }
    if (m % 2 == 1) {
      int j = m / 2 + 1;
      for (int i = 1; i <= n / 2; i++) {
        sum = s[i][j];
        ans += abs(sum - s[i][j]) + abs(sum - s[n - i + 1][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
