#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const int maxn = 1e2 + 10;
const int INF = 1000000000;
int T;
int dx[] = {1, 1, 0, 0};
int dy[] = {1, 0, 0, 1};
vector<pair<int, int>> vv;
int n, m;
int a[maxn][maxn];
void tr(int id, int x, int y, vector<pair<int, int>>& vvv = vv) {
  for (int i = (0); i < (2); ++i)
    for (int j = (0); j < (2); ++j) {
      if (i == dx[id] && j == dy[id]) continue;
      vvv.push_back({x + i, y + j});
      a[x + i][y + j] ^= 1;
    }
}
map<string, vector<pair<int, int>>> sol;
int main() {
  for (int i = (0); i < (1 << 4); ++i) {
    string s;
    for (int j = (0); j < (2); ++j)
      for (int k = (0); k < (2); ++k) {
        int u = j * 2 + k;
        if (i & (1 << u))
          s += '1', a[j][k] = 1;
        else
          s += '0', a[j][k] = 0;
      }
    for (int ch = (0); ch < (1 << 4); ++ch) {
      for (int j = (0); j < (2); ++j)
        for (int k = (0); k < (2); ++k) {
          int u = j * 2 + k;
          if (i & (1 << u))
            a[j][k] = 1;
          else
            a[j][k] = 0;
        }
      for (int o = (0); o < (4); ++o) {
        if (ch & (1 << o)) {
          tr(o, 0, 0);
        }
      }
      bool ok = 1;
      for (int i = (0); i < (2); ++i)
        for (int j = (0); j < (2); ++j)
          if (a[i][j]) ok = 0;
      if (ok) {
        vector<pair<int, int>> vv;
        for (int o = (0); o < (4); ++o) {
          if (ch & (1 << o)) {
            tr(o, 0, 0, vv);
          }
        }
        sol[s] = vv;
        break;
      }
    }
  }
  cin >> T;
  while (T--) {
    cin >> n >> m;
    vv.clear();
    for (int i = (0); i < (n); ++i)
      for (int j = (0); j < (m); ++j) {
        char c;
        cin >> c;
        a[i][j] = c - '0';
      }
    for (int i = (0); i < (n - 2); ++i)
      for (int j = (0); j < (m); ++j) {
        if (!a[i][j]) continue;
        if (j + 1 == m)
          tr(2, i, j - 1);
        else
          tr(0, i, j);
      }
    auto &fi = a[n - 2], &se = a[n - 1];
    for (int j = (0); j < (m - 2); ++j) {
      if (!fi[j] && !se[j]) continue;
      if (fi[j] && se[j]) {
        tr(0, n - 2, j);
        continue;
      }
      if (fi[j]) {
        tr(1, n - 2, j);
        continue;
      }
      tr(2, n - 2, j);
    }
    string s;
    for (int i = (0); i < (2); ++i)
      for (int j = (0); j < (2); ++j) {
        int d = n - 2;
        if (a[n - 2 + i][m - 2 + j])
          s += '1';
        else
          s += '0';
      }
    for (auto& w : sol[s]) {
      int x = w.first + n - 2, y = w.second + m - 2;
      vv.push_back({x, y});
    }
    auto& e = vv;
    cout << e.size() / 3 << "\n";
    for (int i = (0); i < (e.size()); ++i) {
      auto& w = e[i];
      cout << w.first + 1 << " " << w.second + 1;
      if ((i + 1) % 3)
        cout << " ";
      else
        cout << "\n";
    }
  }
}
