#include <bits/stdc++.h>
using namespace std;
long long t, a[205][305], b, c, n, m;
int main() {
  cin >> t;
  while (t--) {
    long long ans = 0;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) cin >> a[i][j];
    for (int i = 1; i <= (n + 1) / 2; i++)
      for (int j = 1; j <= (m + 1) / 2; j++) {
        long long mn = 1000000000;
        vector<long long> x;
        if (2 * j != (m + 1) && 2 * i != (n + 1)) {
          x.push_back(a[i][j]);
          x.push_back(a[n + 1 - i][j]);
          x.push_back(a[i][m + 1 - j]);
          x.push_back(a[n + 1 - i][m + 1 - j]);
          sort(x.begin(), x.end());
          ans += (x[3] + x[2] - x[1] - x[0]);
          x.clear();
        }
        if (2 * i == (n + 1) && 2 * j != (m + 1))
          ans += abs(a[i][j] - a[i][m + 1 - j]);
        if (2 * j == (m + 1) && 2 * i != (n + 1))
          ans += abs(a[i][j] - a[n + 1 - i][j]);
      }
    cout << ans << "\n";
  }
  return 0;
}
