#include <bits/stdc++.h>
using namespace std;
const int MAX = (int)1e2 + 5;
long long int median(long long int a, long long int b, long long int c,
                     long long int d) {
  vector<int> v(4);
  v[0] = a, v[1] = b, v[2] = c, v[3] = d;
  sort(v.begin(), v.end());
  return (v[1] + v[2]) / 2;
}
long long int arr[MAX][MAX];
void solveTestCases() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
    }
  }
  long long int res = 0;
  int iter_x = n / 2;
  bool ex_x = false;
  if (n % 2) {
    ex_x = true;
  }
  int iter_y = m / 2;
  bool ex_y = false;
  if (m % 2) {
    ex_y = true;
  }
  for (int i = 0; i < iter_x; i++) {
    for (int j = 0; j < iter_y; j++) {
      long long int a = arr[i][j], b = arr[n - 1 - i][j],
                    c = arr[n - 1 - i][m - 1 - j], d = arr[i][m - 1 - j];
      long long int mean = median(a, b, c, d);
      res += abs(mean - a) + abs(mean - b) + abs(mean - c) + abs(mean - d);
    }
  }
  if (ex_x) {
    for (int j = 0; j < iter_y; j++) {
      long long int a = arr[n / 2][j], b = arr[n / 2][m - 1 - j];
      long long int mean = (a + b) / 2;
      res += abs(mean - a) + abs(mean - b);
    }
  }
  if (ex_y) {
    for (int i = 0; i < iter_x; i++) {
      long long int a = arr[i][m / 2], b = arr[n - 1 - i][m / 2];
      long long int mean = (a + b) / 2;
      res += abs(mean - a) + abs(mean - b);
    }
  }
  cout << res << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    solveTestCases();
  }
  return 0;
}
