#include <bits/stdc++.h>
using namespace std;
int n, m;
bool in_bounds(int x, int y) { return (x > -1 && x < n && y > -1 && y < m); }
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    int a[n][m];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) cin >> a[i][j];
    int levels[61][2];
    memset(levels, 0, sizeof(levels));
    int mx = 0;
    int vis[n][m];
    memset(vis, -1, sizeof(vis));
    queue<pair<int, int>> q;
    q.push({0, 0});
    int dx[] = {0, 1};
    int dy[] = {1, 0};
    vis[0][0] = 0;
    while (!q.empty()) {
      int x = q.front().first, y = q.front().second;
      q.pop();
      levels[vis[x][y]][a[x][y]]++;
      mx = max(mx, vis[x][y]);
      for (int i = 0; i < 2; i++)
        if (in_bounds(x + dx[i], y + dy[i]) && vis[x + dx[i]][y + dy[i]] == -1)
          vis[x + dx[i]][y + dy[i]] = vis[x][y] + 1,
                             q.push({x + dx[i], y + dy[i]});
    }
    int ans = 0;
    for (int i = 0; i < (mx + 1) / 2; i++)
      ans += min(levels[i][0] + levels[mx - i][0],
                 levels[i][1] + levels[mx - i][1]);
    cout << ans << "\n";
  }
  return 0;
}
