#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 2e18;
struct dat {
  pair<int, int> p, q, r;
  void format() {
    p.first++, p.second++;
    q.first++, q.second++;
    r.first++, r.second++;
  }
};
const int N = 107;
string s[N];
bool a[N][N];
int row, col;
vector<dat> res;
void flip(pair<int, int> p, pair<int, int> q, pair<int, int> r) {
  a[p.first][p.second] ^= 1;
  a[q.first][q.second] ^= 1;
  a[r.first][r.second] ^= 1;
  res.push_back({p, q, r});
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    cin >> row >> col;
    for (int i = 0; i < row; ++i) {
      cin >> s[i];
      for (int j = 0; j < col; ++j) {
        a[i][j] = (s[i][j] == '1');
      }
    }
    res.clear();
    for (int i = 0; i + 1 < row; ++i) {
      for (int j = 0; j + 1 < col; ++j) {
        pair<int, int> p(i, j);
        if (j + 2 == col and i and a[i - 1][j + 1]) {
          flip(p, pair<int, int>(p.first, p.second + 1),
               pair<int, int>(p.first - 1, p.second + 1));
        }
        pair<int, int> q = pair<int, int>(p.first, p.second + 1),
                       r = pair<int, int>(p.first + 1, p.second),
                       s = pair<int, int>(p.first + 1, p.second + 1);
        if (!a[p.first][p.second] and !a[r.first][r.second]) continue;
        if (!a[p.first][p.second]) {
          flip(q, r, s);
        } else {
          if (!a[r.first][r.second]) {
            flip(p, q, s);
          } else if (a[q.first][q.second]) {
            flip(p, q, r);
          } else {
            flip(p, r, s);
          }
        }
      }
    }
    for (int i = 0; i < 4; ++i) {
      pair<int, int> p(row - 2, col - 2);
      vector<pair<int, int> > v = {p, pair<int, int>(p.first, p.second + 1),
                                   pair<int, int>(p.first + 1, p.second),
                                   pair<int, int>(p.first + 1, p.second + 1)};
      int cnt = 0;
      for (pair<int, int> &qq : v) cnt += a[qq.first][qq.second];
      if (!cnt) break;
      sort((v).begin(), (v).end(), [&](pair<int, int> &e, pair<int, int> &f) {
        return a[e.first][e.second] > a[f.first][f.second];
      });
      if (cnt == 2) {
        flip(v[1], v[2], v[3]);
      } else {
        flip(v[0], v[1], v[2]);
      }
    }
    cout << res.size() << "\n";
    for (dat &qq : res) {
      qq.format();
      cout << qq.p.first << " " << qq.p.second << " " << qq.q.first << " "
           << qq.q.second << " " << qq.r.first << " " << qq.r.second << "\n";
    }
  }
  return 0;
}
