#include <bits/stdc++.h>
using namespace std;
int a[105][105], n, m;
struct Node {
  int x1, y1, x2, y2, x3, y3;
};
vector<Node> op;
void print() {
  for (int k = 1; k <= n; ++k) {
    for (int q = 1; q <= m; ++q) cout << a[k][q];
    cout << endl;
  }
  cout << endl;
}
void op1(int x, int y, int mode) {
  if (mode == 1) {
    op.push_back({x, y, x + 1, y, x, y + 1});
    a[x][y] ^= 1, a[x + 1][y] ^= 1, a[x][y + 1] ^= 1;
  }
  if (mode == 2) {
    op.push_back({x, y, x + 1, y, x + 1, y + 1});
    a[x][y] ^= 1, a[x + 1][y] ^= 1, a[x + 1][y + 1] ^= 1;
  }
  if (mode == 3) {
    op.push_back({x, y, x, y + 1, x + 1, y + 1});
    a[x][y] ^= 1, a[x][y + 1] ^= 1, a[x + 1][y + 1] ^= 1;
  }
  if (mode == 4) {
    op.push_back({x + 1, y + 1, x + 1, y, x, y + 1});
    a[x + 1][y + 1] ^= 1, a[x + 1][y] ^= 1, a[x][y + 1] ^= 1;
  }
}
void operate(int i, int j) {
  int sum = a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1];
  if (sum == 0) return;
  if (sum == 1) {
    if (a[i][j] == 1)
      op1(i, j, 1);
    else if (a[i + 1][j] == 1)
      op1(i, j, 2);
    else if (a[i][j + 1] == 1)
      op1(i, j, 3);
    else if (a[i + 1][j + 1] == 1)
      op1(i, j, 4);
    operate(i, j);
  }
  if (sum == 2) {
    if (a[i][j] && a[i + 1][j])
      op1(i, j, 3);
    else if (a[i][j] && a[i][j + 1])
      op1(i, j, 2);
    else if (a[i + 1][j + 1] && a[i + 1][j])
      op1(i, j, 3);
    else if (a[i][j + 1] && a[i + 1][j + 1])
      op1(i, j, 1);
    else if (a[i][j] && a[i + 1][j + 1])
      op1(i, j, 1);
    else if (a[i + 1][j] && a[i][j + 1])
      op1(i, j, 2);
    operate(i, j);
  }
  if (sum == 3) {
    if (a[i][j] == 0)
      op1(i, j, 4);
    else if (a[i + 1][j] == 0)
      op1(i, j, 3);
    else if (a[i][j + 1] == 0)
      op1(i, j, 2);
    else if (a[i + 1][j + 1] == 0)
      op1(i, j, 1);
    operate(i, j);
  }
  if (sum == 4) {
    op1(i, j, 1);
    operate(i, j);
  }
}
string s;
signed main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    op.clear();
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
      cin >> s;
      for (int j = 1; j <= m; ++j) a[i][j] = s[j - 1] - '0';
    }
    if (n % 2 == 1 && m % 2 == 1 && a[n][m] == 1) op1(n - 1, m - 1, 3);
    if (n % 2 == 1) {
      for (int j = 1; j <= m; j += 2) {
        if (a[n][j] == 1 && a[n][j + 1] == 1)
          op1(n - 1, j, 2);
        else if (a[n][j] == 1)
          op1(n - 1, j, 1);
        else if (a[n][j + 1] == 1)
          op1(n - 1, j, 3);
      }
      --n;
    }
    if (m % 2 == 1) {
      for (int j = 1; j <= n; j += 2) {
        if (a[j][m] == 1 && a[j + 1][m] == 1)
          op1(j, m - 1, 3);
        else if (a[j][m] == 1)
          op1(j, m - 1, 1);
        else if (a[j + 1][m] == 1)
          op1(j, m - 1, 2);
      }
      --m;
    }
    for (int i = 1; i <= n; i += 2)
      for (int j = 1; j <= m; j += 2) {
        if (i != n && j != m) operate(i, j);
      }
    cout << op.size() << endl;
    for (auto q : op)
      cout << q.x1 << " " << q.y1 << " " << q.x2 << " " << q.y2 << " " << q.x3
           << " " << q.y3 << endl;
  }
  return 0;
}
