#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int i, j, k, n, m, t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    bool a[n][m];
    for (i = 0; i < n; ++i)
      for (j = 0; j < m; ++j) cin >> a[i][j];
    int ans = 0;
    int s1 = 0, s2 = n - 1 + m - 1;
    while (s1 < s2) {
      int c1 = 0, c2 = 0;
      for (i = 0; i < n; ++i) {
        for (j = 0; j < m; ++j) {
          if (i + j == s1 or i + j == s2) {
            if (a[i][j] == 0)
              c1++;
            else
              c2++;
          }
        }
      }
      ans += min(c1, c2);
      s1++, s2--;
    }
    cout << ans << "\n";
  }
  return 0;
}
