#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long int solve() {
  long long int n, m;
  cin >> n >> m;
  long long int arr[n][m];
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      cin >> arr[i][j];
    }
  }
  set<pair<long long int, long long int>> s1;
  set<pair<long long int, long long int>> s2;
  s1.insert({0, 0});
  s2.insert({n - 1, m - 1});
  long long int x, y;
  long long int ans = 0;
  for (long long int i = 0; i < (m - 1 + n - 1) / 2 + (m + n - 2) % 2; i++) {
    vector<pair<long long int, long long int>> s1t, s2t;
    long long int one = 0;
    long long int zero = 0;
    for (auto j : s1) {
      x = j.first;
      y = j.second;
      zero += (arr[x][y] == 0);
      one += (arr[x][y] == 1);
      if (x + 1 < n && y < m) s1t.push_back({x + 1, y});
      if (x < n && y + 1 < m) s1t.push_back({x, y + 1});
    }
    for (auto j : s2) {
      x = j.first;
      y = j.second;
      zero += (arr[x][y] == 0);
      one += (arr[x][y] == 1);
      if (x - 1 >= 0 && y >= 0) s2t.push_back({x - 1, y});
      if (x >= 0 && y - 1 >= 0) s2t.push_back({x, y - 1});
    }
    ans += min(one, zero);
    s1.clear();
    s2.clear();
    for (auto j : s1t) {
      s1.insert(j);
    }
    for (auto j : s2t) {
      s2.insert(j);
    }
  }
  cout << ans << endl;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t) {
    solve();
    t--;
  }
  return 0;
}
