#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e10;
const long long MOD = 228228227;
inline void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> field(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> field[i][j];
    }
  }
  long long answer = 0;
  set<pair<int, int>> bullshits;
  for (int i = 0; i <= n - i - 1; i++) {
    int i2 = n - i - 1;
    for (int j = 0; j <= m - j - 1; j++) {
      int j2 = m - j - 1;
      bullshits.clear();
      bullshits.insert({i, j});
      bullshits.insert({i, j2});
      bullshits.insert({i2, j});
      bullshits.insert({i2, j2});
      long long current_shit, best_shit = INF;
      for (auto &[i3, j3] : bullshits) {
        current_shit = 0;
        for (auto &[i4, j4] : bullshits) {
          current_shit += abs(field[i4][j4] - field[i3][j3]);
        }
        best_shit = min(best_shit, current_shit);
      }
      answer += best_shit;
    }
  }
  cout << answer << '\n';
}
signed main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) solve();
}
