#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int N = 105;
pii ans[N * N][3];
int pt;
int a[N][N];
char s[N][N];
void push(pii p1, pii p2, pii p3) {
  pt++;
  ans[pt][0] = p1;
  ans[pt][1] = p2;
  ans[pt][2] = p3;
  a[p1.first][p1.second] = 1 - a[p1.first][p1.second];
  a[p2.first][p2.second] = 1 - a[p2.first][p2.second];
  a[p3.first][p3.second] = 1 - a[p3.first][p3.second];
  return;
}
void solve_2(int r, int c) {
  vector<pii> v[2];
  for (int i = r; i <= r + 1; i++) {
    for (int j = c; j <= c + 1; j++) {
      v[a[i][j]].push_back({i, j});
    }
  }
  if ((int)v[1].size() == 4) {
    push(v[1][1], v[1][2], v[1][3]);
    for (int i = 1; i <= 3; i++) {
      v[0].push_back(v[1].back());
      v[1].pop_back();
    }
  }
  if ((int)v[1].size() == 1) {
    push(v[1][0], v[0][1], v[0][2]);
    pii p = v[1][0];
    v[1].pop_back();
    v[1].push_back(v[0][1]), v[1].push_back(v[0][2]);
    v[0].pop_back(), v[0].pop_back();
    v[0].push_back(p);
  }
  if ((int)v[1].size() == 2) {
    push(v[0][0], v[0][1], v[1][1]);
    pii p = v[1][1];
    v[1].pop_back();
    for (int i = 0; i < 2; i++) {
      v[1].push_back(v[0].back());
      v[0].pop_back();
    }
    v[0].push_back(p);
  }
  if ((int)v[1].size() == 3) {
    push(v[1][0], v[1][1], v[1][2]);
  }
  return;
}
void solve_3(int r, int c) {
  int cnt[3] = {};
  for (int i = r; i <= r + 1; i++) {
    for (int j = c; j <= c + 2; j++) {
      cnt[j - c] += a[i][j];
    }
  }
  if (cnt[0] == 1) {
    pii p = {a[r][c] == 0 ? r + 1 : r, c};
    pii q = {a[r][c] == 0 ? r : r + 1, c};
    if (cnt[1] == 0 && cnt[2] == 2) {
      push(q, {r, c + 1}, {r + 1, c + 1});
      cnt[0] = cnt[1] = 2;
    } else {
      push(p, {r, c + 1}, {r + 1, c + 1});
    }
  }
  if (cnt[0] == 2) {
    if (a[r][c + 1] == 1)
      push({r, c}, {r + 1, c}, {r, c + 1});
    else
      push({r, c}, {r + 1, c}, {r + 1, c + 1});
  }
  solve_2(r, c + 1);
}
void clear_2(pii p1, pii p2, pii q1, pii q2) {
  pii p[2] = {p1, p2}, q[2] = {q1, q2};
  if (a[p1.first][p1.second] == 0 && a[p2.first][p2.second] == 0) return;
  if (a[p1.first][p1.second] == 1 && a[p2.first][p2.second] == 1) {
    push(p1, p2, q1);
    return;
  }
  if (a[p1.first][p1.second] == 1) {
    push(p1, q1, q2);
    return;
  } else
    push(p2, q1, q2);
  return;
}
void printAns() {
  printf("%d\n", pt);
  for (int i = 1; i <= pt; i++) {
    for (int j = 0; j < 3; j++) {
      printf("%d %d ", ans[i][j].first, ans[i][j].second);
    }
    printf("\n");
  }
  return;
}
void solve() {
  int n, m;
  scanf("%d %d", &n, &m);
  pt = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= m; j++) a[i][j] = s[i][j] - '0';
  }
  if (m == 2 && n == 2) {
    solve_2(1, 1);
    printAns();
    return;
  }
  if (n & 1) {
    for (int i = 1; i <= m; i += 2) {
      clear_2({1, i}, {1, i + (i == m ? -1 : 1)}, {2, i},
              {2, i + (i == m ? -1 : 1)});
    }
  }
  for (int i = 1 + (n & 1); i <= n; i += 2) {
    for (int j = 1; j < m; j++) {
      if (j + 1 == m) {
        solve_2(i, j);
        break;
      }
      if (j + 2 == m) {
        solve_3(i, j);
        break;
      }
      clear_2({i, j}, {i + 1, j}, {i + 1, j + 1}, {i, j + 1});
    }
  }
  printAns();
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    solve();
  }
}
