#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 9;
const long long MX = 1e5 + 5;
long long n, m;
long long inp[105][105];
long long mark[105][105];
int main() {
  long long tt;
  cin >> tt;
  while (tt--) {
    cin >> n >> m;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++) {
        cin >> inp[i][j];
        mark[i][j] = 0;
      }
    }
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++) {
        if (mark[i][j] == 1) continue;
        set<pair<long long, long long>> st;
        st.insert({i, j});
        st.insert({i, m - j + 1});
        st.insert({n - i + 1, j});
        st.insert({n - i + 1, m - j + 1});
        mark[i][j] = 1;
        mark[i][m - j + 1] = 1;
        mark[n - i + 1][j] = 1;
        mark[n - i + 1][m - j + 1] = 1;
        vector<long long> val;
        for (auto it : st) {
          long long x = it.first;
          long long y = it.second;
          val.push_back(inp[x][y]);
        }
        if (st.size() == 1) continue;
        if (st.size() == 2) {
          long long ret = abs(val[0] - val[1]);
          ans += ret;
          continue;
        }
        sort(val.begin(), val.end());
        long long m1 = (val[1] + val[2]) / 2;
        long long m2 = (val[1] + val[2] + 1) / 2;
        long long c1 = 0;
        long long c2 = 0;
        for (long long i = 0; i < val.size(); i++) {
          c1 += abs(m1 - val[i]);
          c2 += abs(m2 - val[i]);
        }
        ans += min(c1, c2);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
