#include <bits/stdc++.h>
using namespace std;
struct node {
  int a, b, c, d, e, f;
  node(int a, int b, int c, int d, int e, int f)
      : a(a), b(b), c(c), d(d), e(e), f(f) {}
};
vector<node> res;
vector<vector<int> > a;
void flip(const node &x) {
  a[x.a][x.b] ^= 1;
  a[x.c][x.d] ^= 1;
  a[x.e][x.f] ^= 1;
  res.push_back(x);
}
int main() {
  int q;
  cin >> q;
  while (q-- > 0) {
    int n, m;
    cin >> n >> m;
    res.clear();
    a.assign(n + 1, vector<int>(m + 1, 0));
    for (int i = 1; i <= n; ++i) {
      string s;
      cin >> s;
      for (int j = 1; j <= m; ++j) a[i][j] = s[j - 1] - '0';
    }
    if (m & 1) {
      for (int i0 = 1; i0 <= n; ++i0) {
        if (a[i0][m] == 0) continue;
        int i1 = (i0 == n) ? (i0 - 1) : (i0 + 1);
        if (a[i1][m])
          flip(node(i0, m - 0, i1, m - 0, i0, m - 1));
        else
          flip(node(i0, m - 0, i0, m - 1, i1, m - 1));
      }
      --m;
    }
    while (n > 2) {
      for (int j0 = 1; j0 <= m; ++j0) {
        if (a[n][j0] == 0) continue;
        int j1 = (j0 == m) ? (j0 - 1) : (j0 + 1);
        if (a[n][j1]) {
          int p = a[n - 1][j1] ? j1 : j0;
          flip(node(n - 0, j0, n - 0, j1, n - 1, p));
        } else
          flip(node(n - 0, j0, n - 1, j0, n - 1, j1));
      }
      --n;
    }
    while (m > 2) {
      for (int i0 = 1; i0 <= n; ++i0) {
        if (a[i0][m] == 0) continue;
        int i1 = (i0 == n) ? (i0 - 1) : (i0 + 1);
        if (a[i1][m]) {
          int p = a[i1][m - 1] ? i1 : i0;
          flip(node(i0, m - 0, i1, m - 0, p, m - 1));
        } else
          flip(node(i0, m - 0, i0, m - 1, i1, m - 1));
      }
      --m;
    }
    int x = 0, y = 0, z = 0, t = 0;
    if (a[1][1]) x ^= 0, y ^= 1, z ^= 1, t ^= 1;
    if (a[2][1]) x ^= 1, y ^= 0, z ^= 1, t ^= 1;
    if (a[1][2]) x ^= 1, y ^= 1, z ^= 0, t ^= 1;
    if (a[2][2]) x ^= 1, y ^= 1, z ^= 1, t ^= 0;
    if (x) flip(node(2, 1, 1, 2, 2, 2));
    if (y) flip(node(1, 1, 1, 2, 2, 2));
    if (z) flip(node(1, 1, 2, 1, 2, 2));
    if (t) flip(node(1, 1, 2, 1, 1, 2));
    cout << res.size() << '\n';
    for (const node &x : res) {
      cout << x.a << ' ' << x.b << ' ';
      cout << x.c << ' ' << x.d << ' ';
      cout << x.e << ' ' << x.f << '\n';
    }
  }
  return 0;
}
