#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;
char grid[105][105];
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
    for (int j = 0; j < m; j++) {
      grid[i][j] -= '0';
    }
  }
  vector<VI> ops;
  for (int i = n - 1; i >= 2; i--) {
    for (int j = 0; j < m; j++) {
      if (grid[i][j] == 1) {
        int jj = j == 0 ? 1 : j - 1;
        ops.push_back({i, j, i - 1, j, i - 1, jj});
        grid[i][j] = 0;
        grid[i - 1][j] ^= 1;
        grid[i - 1][jj] ^= 1;
      }
    }
  }
  for (int j = m - 1; j >= 2; j--) {
    for (int i = 0; i < 2; i++) {
      if (grid[i][j] == 1) {
        ops.push_back({i, j, 0, j - 1, 1, j - 1});
        grid[i][j] = 0;
        grid[0][j - 1] ^= 1;
        grid[1][j - 1] ^= 1;
      }
    }
  }
  vector<PII> ze, on;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      (grid[i][j] == 1 ? ze : on).emplace_back(i, j);
    }
  }
  if (ze.size() == 4) {
    ops.emplace_back();
    while (ze.size() > 1) {
      auto [x, y] = ze.back();
      ze.pop_back();
      ops.back().push_back(x);
      ops.back().push_back(y);
      on.emplace_back(x, y);
    }
  }
  if (ze.size() == 1) {
    ops.emplace_back();
    ops.back().push_back(ze[0].first);
    ops.back().push_back(ze[0].second);
    ops.back().push_back(on[1].first);
    ops.back().push_back(on[1].second);
    ops.back().push_back(on[2].first);
    ops.back().push_back(on[2].second);
    auto e = ze[0];
    ze.clear();
    ze.push_back(on[1]);
    ze.push_back(on[2]);
    on.pop_back();
    on[1] = e;
  }
  if (ze.size() == 2) {
    ops.emplace_back();
    ops.back().push_back(on[0].first);
    ops.back().push_back(on[0].second);
    ops.back().push_back(on[1].first);
    ops.back().push_back(on[1].second);
    ops.back().push_back(ze[1].first);
    ops.back().push_back(ze[1].second);
    auto e = ze[1];
    ze.pop_back();
    ze.push_back(on[0]);
    ze.push_back(on[1]);
    on.clear();
    on.push_back(e);
  }
  if (ze.size() == 3) {
    ops.emplace_back();
    for (auto &[x, y] : ze) {
      ops.back().push_back(x);
      ops.back().push_back(y);
    }
  }
  cout << ops.size() << '\n';
  for (auto &v : ops) {
    for (int x : v) cout << (x + 1) << ' ';
    cout << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
