#include <bits/stdc++.h>
using namespace std;
int read() {
  int w = 0, f = 1;
  char c = ' ';
  while (c < '0' || c > '9') c = getchar(), f = c == '-' ? -1 : f;
  while (c >= '0' && c <= '9') w = w * 10 + c - 48, c = getchar();
  return w * f;
}
int read01() {
  char c = getchar();
  while (c != '0' && c != '1') c = getchar();
  return c == '1';
}
struct node {
  int a[6];
  node(int A, int B, int C, int D, int E, int F) {
    a[0] = A, a[1] = B, a[2] = C, a[3] = D, a[4] = E, a[5] = F;
  }
};
int T, n, m, a[105][105];
bool vis[1 << 4];
vector<node> ans;
signed main() {
  T = read();
  while (T--) {
    n = read(), m = read(), ans.clear();
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) a[i][j] = read01();
    for (int i = 1; i <= n - 2; i++) {
      for (int j = 1; j < m; j += 2)
        if (a[i][j] && a[i][j + 1])
          ans.push_back(node(i, j, i, j + 1, i + 1, j)), a[i][j] ^= 1,
              a[i][j + 1] ^= 1, a[i + 1][j] ^= 1;
        else if (a[i][j] && !a[i][j + 1])
          ans.push_back(node(i, j, i + 1, j, i + 1, j + 1)), a[i][j] ^= 1,
              a[i + 1][j] ^= 1, a[i + 1][j + 1] ^= 1;
        else if (!a[i][j] && a[i][j + 1])
          ans.push_back(node(i, j + 1, i + 1, j, i + 1, j + 1)),
              a[i][j + 1] ^= 1, a[i + 1][j] ^= 1, a[i + 1][j + 1] ^= 1;
      if ((m & 1) && a[i][m])
        ans.push_back(node(i, m, i + 1, m - 1, i + 1, m)), a[i][m] ^= 1,
            a[i + 1][m - 1] ^= 1, a[i + 1][m] ^= 1;
    }
    for (int i = 1; i <= m - 2; i++)
      if (a[n - 1][i] && a[n][i])
        ans.push_back(node(n - 1, i, n, i, n, i + 1)), a[n - 1][i] ^= 1,
            a[n][i] ^= 1, a[n][i + 1] ^= 1;
      else if (a[n - 1][i] && !a[n][i])
        ans.push_back(node(n - 1, i, n - 1, i + 1, n, i + 1)), a[n - 1][i] ^= 1,
            a[n - 1][i + 1] ^= 1, a[n][i + 1] ^= 1;
      else if (!a[n - 1][i] && a[n][i])
        ans.push_back(node(n, i, n - 1, i + 1, n, i + 1)), a[n][i] ^= 1,
            a[n - 1][i + 1] ^= 1, a[n][i + 1] ^= 1;
    queue<vector<int> > q;
    vector<int> now;
    now.push_back(a[n - 1][m - 1] + a[n - 1][m] * 2 + a[n][m - 1] * 4 +
                  a[n][m] * 8);
    q.push(now);
    while (1) {
      vector<int> x = q.front(), y;
      q.pop();
      if (x[0] == 0) {
        for (int i = 1; i < (int)x.size(); i++)
          if (x[i] == 1)
            ans.push_back(node(n - 1, m, n, m - 1, n, m));
          else if (x[i] == 2)
            ans.push_back(node(n - 1, m - 1, n, m - 1, n, m));
          else if (x[i] == 3)
            ans.push_back(node(n - 1, m - 1, n - 1, m, n, m));
          else
            ans.push_back(node(n - 1, m - 1, n - 1, m, n, m - 1));
        break;
      }
      for (int i = 0; i < 4; i++)
        y = x, y.push_back(i + 1), y[0] = y[0] ^ 15 ^ (1 << i), q.push(y);
    }
    printf("%d\n", (int)ans.size());
    for (node i : ans)
      for (int j = 0; j < 6; j++) printf("%d%c", i.a[j], j == 5 ? '\n' : ' ');
  }
  return 0;
}
