#include <bits/stdc++.h>
using namespace std;
int mat[100][100];
int main() {
  int tests;
  cin >> tests;
  while (tests--) {
    int rows, cols, f[100][2] = {0}, ans = 0;
    cin >> rows >> cols;
    for (int x = 0; x < rows; x++) {
      for (int y = 0; y < cols; y++) {
        cin >> mat[x][y];
        f[x + y][mat[x][y]]++;
      }
    }
    for (int itr = 0; itr < (rows + cols - 1) / 2; itr++)
      ans += min(f[itr][0] + f[rows + cols - 2 - itr][0],
                 f[itr][1] + f[rows + cols - 2 - itr][1]);
    cout << ans << "\n";
  }
}
