#include <bits/stdc++.h>
using namespace std;
vector<long> dp;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> arr(n, vector<int>(m, 0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> arr[i][j];
  long diagonals = n + m - 1;
  int count = 1;
  int row = 0, col = 0, rev_row = n - 1, rev_col = m - 1;
  unordered_map<int, int> mp;
  int change = 0;
  while (count <= diagonals / 2) {
    mp.clear();
    if (count <= m) {
      for (int i = row, j = col; i < n && j >= 0; i++, j--) {
        mp[arr[i][j]]++;
      }
      col++;
    } else {
      row = count - m;
      col = m - 1;
      for (int i = row, j = col; i < n && j >= 0; i++, j--) {
        mp[arr[i][j]]++;
      }
    }
    if (count <= n) {
      int tr = n - count, tc = m - 1;
      for (int i = tr, j = tc; i < n && j >= 0; i++, j--) {
        mp[arr[i][j]]++;
      }
    } else {
      int tr = 0, tc = m - (count - n) - 1;
      for (int i = tr, j = tc; i < n && j >= 0; i++, j--) {
        mp[arr[i][j]]++;
      }
    }
    auto it = mp.begin();
    long temp_count = 0, max = 0;
    while (it != mp.end()) {
      temp_count += it->second;
      if (it->second > max) max = it->second;
      it++;
    }
    change += (temp_count - max);
    count++;
  }
  cout << change << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
