#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (size_t i_t = 0; i_t < t; ++i_t) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n);
    for (auto& row : a) {
      row.resize(m);
      for (auto& v : row) cin >> v;
    }
    using pos = pair<int, int>;
    set<pos> start, finish;
    start.insert({0, 0});
    finish.insert({n - 1, m - 1});
    int ans = 0;
    for (int i = 0; i < (n + m - 1) / 2; ++i) {
      int c1s = 0, c0s = 0, c1f = 0, c0f = 0;
      set<pos> st, fn;
      for (const auto& sp : start) {
        if (sp.first >= n || sp.second >= m) continue;
        if (a[sp.first][sp.second])
          c1s++;
        else
          c0s++;
        st.insert({sp.first + 1, sp.second});
        st.insert({sp.first, sp.second + 1});
      }
      for (const auto& fp : finish) {
        if (fp.first < 0 || fp.second < 0) continue;
        if (a[fp.first][fp.second])
          c1f++;
        else
          c0f++;
        fn.insert({fp.first - 1, fp.second});
        fn.insert({fp.first, fp.second - 1});
      }
      start = std::move(st);
      finish = std::move(fn);
      ans += min(c1s + c1f, c0s + c0f);
    }
    cout << ans << endl;
  }
}
