#include <bits/stdc++.h>
using namespace std;
vector<string> s;
vector<tuple<int, int, int> > ans;
void upd(int r, int c) { s[r][c] = s[r][c] == '1' ? '0' : '1'; }
void upd(int r, int c, int d) {
  upd(r, c);
  upd(r + (d >> 1 ? -1 : 1), c);
  upd(r, c + (d & 1 ? -1 : 1));
  ans.push_back({r + 1, c + 1, d});
}
void solve2x2(int r0, int c0) {
  vector<pair<int, int> > vec;
  for (int i = (0); i < (2); ++i)
    for (int j = (0); j < (2); ++j)
      if (s[r0 + i][c0 + j] == '1') vec.push_back({i, j});
  vector<pair<int, int> > curAns;
  if ((int)((vec).size()) == 1) {
    for (int i = (0); i < (2); ++i)
      for (int j = (0); j < (2); ++j)
        if (abs(i - vec[0].first) + abs(j - vec[0].second) < 2)
          curAns.push_back({i, j});
  } else if ((int)((vec).size()) == 2) {
    int d =
        abs(vec[0].first - vec[1].first) + abs(vec[0].second - vec[1].second);
    for (int i = (0); i < (2); ++i)
      for (int j = (0); j < (2); ++j) {
        if ((d == 1) ^ (s[r0 + i][c0 + j] == '1')) curAns.push_back({i, j});
      }
  } else if ((int)((vec).size()) == 3) {
    for (int i = (0); i < (2); ++i)
      for (int j = (0); j < (2); ++j) {
        if (s[r0 + i][c0 + j] == '0') curAns.push_back({i ^ 1, j ^ 1});
      }
  } else if ((int)((vec).size()) == 4) {
    for (int i = (0); i < (2); ++i)
      for (int j = (0); j < (2); ++j) curAns.push_back({i, j});
  }
  for (auto [i, j] : curAns) {
    upd(r0 + i, c0 + j, 2 * i + j);
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    ans.clear();
    int n, m;
    cin >> n >> m;
    s.resize(n);
    for (auto& str : s) {
      cin >> str;
    }
    for (int i = (0); i < (n - 2); ++i) {
      for (int j = (0); j < (m - 1); ++j)
        if (s[i][j] == '1') {
          upd(i + 1, j, 2);
        }
      if (s[i][m - 1] == '1') {
        upd(i + 1, m - 1, 3);
      }
    }
    for (int j = (0); j < (m - 2); ++j) {
      if (s[n - 2][j] == '1') upd(n - 2, j, 0);
      if (s[n - 1][j] == '1') upd(n - 1, j + 1, 3);
    }
    solve2x2(n - 2, m - 2);
    for (int i = (0); i < (n); ++i)
      for (int j = (0); j < (m); ++j) assert(s[i][j] == '0');
    assert((int)((ans).size()) <= n * m);
    cout << (int)((ans).size()) << "\n";
    for (auto [r, c, d] : ans) {
      cout << r << " " << c << " " << r + (d >> 1 ? -1 : 1) << " " << c << " "
           << r << " " << c + (d & 1 ? -1 : 1) << "\n";
    }
  }
  return 0;
}
