#include <bits/stdc++.h>
using namespace std;
int m, n, t, q, yy[35][35];
int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> m >> n;
    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j) cin >> yy[i][j];
    int b = (m + n - 1) / 2, sum = 0;
    if (m < n) {
      for (int i = 0; i < b; ++i) {
        int j = 0, k = i, l = 0, r = 0;
        while (k >= 0 && j < m) {
          if (yy[j][k])
            l++;
          else
            r++;
          if (yy[m - j - 1][n - k - 1])
            l++;
          else
            r++;
          k--;
          j++;
        }
        sum += min(l, r);
      }
    } else {
      for (int i = 0; i < b; ++i) {
        int j = 0, k = i, l = 0, r = 0;
        while (k >= 0 && j < n) {
          if (yy[k][j])
            l++;
          else
            r++;
          if (yy[m - k - 1][n - j - 1])
            l++;
          else
            r++;
          k--;
          j++;
        }
        sum += min(l, r);
      }
    }
    cout << sum << endl;
    memset(yy, 0, sizeof(yy));
  }
  return 0;
}
