#include <bits/stdc++.h>
using namespace std;
void flip(char &x) { x = '0' + '1' - x; }
void solve() {
  int n, m;
  cin >> n >> m;
  vector<string> ls(n);
  for (auto &s : ls) cin >> s;
  vector<vector<int> > ans;
  for (int i = (n); i-- > (2);) {
    for (int j = (m); j-- > (0);) {
      if (ls[i][j] == '1') {
        vector<int> cur = {i, j, i - 1, j, i - 1, abs(j - 1)};
        for (int t = (0); t < (3); ++t) flip(ls[cur[2 * t]][cur[2 * t + 1]]);
        ans.emplace_back(cur);
      }
    }
  }
  for (int j = (m); j-- > (2);) {
    for (int i = (0); i < (2); ++i) {
      if (ls[i][j] == '1') {
        vector<int> cur = {i, j, 0, j - 1, 1, j - 1};
        for (int t = (0); t < (3); ++t) flip(ls[cur[2 * t]][cur[2 * t + 1]]);
        ans.emplace_back(cur);
      }
    }
  }
  for (int i = (0); i < (2); ++i)
    for (int j = (0); j < (2); ++j) {
      vector<int> cur = {1 ^ i, 0 ^ j, 0 ^ i, 1 ^ j, 1 ^ i, 1 ^ j};
      int sum = 0;
      for (int t = (0); t < (3); ++t)
        sum += (ls[cur[2 * t]][cur[2 * t + 1]] == '1');
      if (sum % 2) ans.emplace_back(cur);
    }
  cout << int(ans.size()) << endl;
  for (auto &l : ans) {
    for (auto &x : l) cout << x + 1 << " ";
    cout << endl;
  }
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n;
  cin >> n;
  for (int _ = (0); _ < (n); ++_) solve();
}
