#include <bits/stdc++.h>
using namespace std;
const int N = 105;
char s[N];
int T, n, m, a[N][N];
vector<pair<int, int>> ans;
inline void flip(int x, int y) {
  ans.push_back(make_pair(x, y));
  a[x][y] = (a[x][y] + 1) % 2;
}
void solve2m2() {
  int _a[3][3];
  vector<pair<int, int>> w;
  w.push_back(make_pair(1, 1));
  w.push_back(make_pair(1, 2));
  w.push_back(make_pair(2, 1));
  w.push_back(make_pair(1, 2));
  w.push_back(make_pair(1, 1));
  w.push_back(make_pair(2, 2));
  w.push_back(make_pair(2, 1));
  w.push_back(make_pair(1, 1));
  w.push_back(make_pair(2, 2));
  w.push_back(make_pair(2, 2));
  w.push_back(make_pair(1, 2));
  w.push_back(make_pair(2, 1));
  for (int i = 1; i <= 2; ++i)
    for (int j = 1; j <= 2; ++j) _a[i][j] = a[n + i - 2][m + j - 2];
  auto fp = [&](int x, int y) { _a[x][y] = (_a[x][y] + 1) % 2; };
  auto ck = [&]() {
    for (int i = 1; i <= 2; ++i) {
      for (int j = 1; j <= 2; ++j) {
        if (_a[i][j]) return false;
      }
    }
    return true;
  };
  for (int i = 0; i < (1 << 4); ++i) {
    for (int j = 0; j < 4; ++j) {
      if (i & (1 << j)) {
        fp(w[j * 3].first, w[j * 3].second);
        fp(w[j * 3 + 1].first, w[j * 3 + 1].second);
        fp(w[j * 3 + 2].first, w[j * 3 + 2].second);
      }
    }
    if (ck()) {
      for (int j = 0; j < 4; ++j) {
        if (i & (1 << j)) {
          flip(w[j * 3].first + n - 2, w[j * 3].second + m - 2);
          flip(w[j * 3 + 1].first + n - 2, w[j * 3 + 1].second + m - 2);
          flip(w[j * 3 + 2].first + n - 2, w[j * 3 + 2].second + m - 2);
        }
      }
      return;
    }
    for (int j = 0; j < 4; ++j) {
      if (i & (1 << j)) {
        fp(w[j * 3].first, w[j * 3].second);
        fp(w[j * 3 + 1].first, w[j * 3 + 1].second);
        fp(w[j * 3 + 2].first, w[j * 3 + 2].second);
      }
    }
  }
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; ++i) {
      scanf("%s", s + 1);
      for (int j = 1; j <= m; ++j) a[i][j] = s[j] - '0';
    }
    vector<pair<int, int>>().swap(ans);
    for (int i = 1; i <= n - 2; ++i) {
      for (int j = 1; j <= m; ++j) {
        if (j == m) {
          if (a[i][j]) flip(i, j), flip(i + 1, j), flip(i + 1, j - 1);
        } else {
          if (a[i][j]) flip(i, j), flip(i + 1, j), flip(i, j + 1);
        }
      }
    }
    for (int j = 1; j <= m - 2; ++j) {
      if (a[n - 1][j]) flip(n - 1, j), flip(n - 1, j + 1), flip(n, j + 1);
      if (a[n][j]) flip(n, j), flip(n, j + 1), flip(n - 1, j + 1);
    }
    solve2m2();
    printf("%d\n", ans.size() / 3);
    for (int i = 0; i < ans.size(); ++i) {
      printf("%d %d\n", ans[i].first, ans[i].second);
    }
  }
  return 0;
}
