#include <bits/stdc++.h>
using namespace std;
long long int max(long long int a, long long int b) { return (a > b) ? a : b; }
long long int min(long long int a, long long int b) { return (a < b) ? a : b; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    long long int a[n][m];
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < m; j++) {
        cin >> a[i][j];
      }
    }
    long long int f[4], final = 0;
    for (long long int i = 0; i < n / 2; i++) {
      for (long long int j = 0; j < m / 2; j++) {
        f[0] = a[i][j];
        f[1] = a[i][m - 1 - j];
        f[2] = a[n - 1 - i][j];
        f[3] = a[n - 1 - i][m - 1 - j];
        sort(f, f + 4);
        final += f[3] + f[2] - f[1] - f[0];
      }
      if (m % 2) {
        long long int c1 = a[i][m / 2];
        long long int c2 = a[n - 1 - i][m / 2];
        final = final + (max(c1, c2) - min(c1, c2));
      }
    }
    if (n % 2) {
      for (long long int j = 0; j < m / 2; j++) {
        long long int c1 = a[n / 2][j];
        long long int c2 = a[n / 2][m - 1 - j];
        final = final + (max(c1, c2) - min(c1, c2));
      }
    }
    cout << final << endl;
  }
  return 0;
}
