#include <bits/stdc++.h>
using namespace std;
void solve() {
  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 c = 0;
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      long long int b[3] = {0};
      b[0] = a[i][j];
      b[1] = a[i][m - j - 1];
      b[2] = a[n - i - 1][j];
      sort(b, b + 3);
      c += b[2] - b[1];
      c += b[1] - b[0];
      a[i][m - j - 1] = b[1];
      a[n - i - 1][j] = b[1];
      a[i][j] = b[1];
    }
  }
  cout << c << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
