#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  int a[105][105];
  while (t--) {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> a[i][j];
      }
    }
    long long int total = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int other1 = a[i][m - j - 1];
        int other2 = a[n - i - 1][j];
        vector<int> b;
        b.push_back(a[i][j]);
        b.push_back(other1);
        b.push_back(other2);
        sort(b.begin(), b.end());
        a[i][j] = a[i][m - j - 1] = a[n - i - 1][j] = b[1];
        total += (long long int)(b[2] - b[1]) + (b[1] - b[0]);
      }
    }
    cout << total << endl;
  }
}
