#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
string s[maxn];
vector<pair<int, int> > A, B, C;
int r, c;
inline void re(int a, int b) {
  if (s[a][b] == '1')
    s[a][b] = '0';
  else
    s[a][b] = '1';
}
inline void re(pair<int, int> u) {
  int a = u.first, b = u.second;
  if (s[a][b] == '1')
    s[a][b] = '0';
  else
    s[a][b] = '1';
}
void solve_R(int x) {
  for (int i = 0; i < c - 1; i++) {
    if (s[x][i] == '1') {
      A.push_back(make_pair(x, i));
      B.push_back(make_pair(x - 1, i));
      C.push_back(make_pair(x - 1, i + 1));
      re(x, i);
      re(x - 1, i);
      re(x - 1, i + 1);
    }
  }
  if (s[x][c - 1] == '1') {
    A.push_back(make_pair(x, c - 1));
    B.push_back(make_pair(x - 1, c - 1));
    C.push_back(make_pair(x - 1, c - 2));
    re(x, c - 1);
    re(x - 1, c - 1);
    re(x - 1, c - 2);
  }
}
void solve_C(int x) {
  for (int i = 0; i < 2; i++) {
    if (s[i][x] == '1') {
      A.push_back(make_pair(i, x));
      B.push_back(make_pair(i, x - 1));
      C.push_back(make_pair(1 - i, x - 1));
      re(i, x);
      re(i, x - 1);
      re(1 - i, x - 1);
    }
  }
}
void pt(pair<int, int> u) { cout << u.first + 1 << " " << u.second + 1 << " "; }
int main() {
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) {
    A.clear();
    B.clear();
    C.clear();
    cin >> r >> c;
    for (int i = 0; i < r; i++) cin >> s[i];
    for (int i = r - 1; i >= 2; i--) solve_R(i);
    for (int i = c - 1; i >= 2; i--) solve_C(i);
    vector<pair<int, int> > point0, point1;
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 2; j++) {
        if (s[i][j] == '1')
          point1.push_back(make_pair(i, j));
        else
          point0.push_back(make_pair(i, j));
      }
    }
    int S = point1.size();
    if (S == 1) {
      A.push_back(point1[0]);
      re(point1[0]);
      B.push_back(point0[0]);
      re(point0[0]);
      C.push_back(point0[1]);
      re(point0[1]);
      A.push_back(point1[0]);
      re(point1[0]);
      B.push_back(point0[2]);
      re(point0[2]);
      C.push_back(point0[1]);
      re(point0[1]);
      A.push_back(point1[0]);
      re(point1[0]);
      B.push_back(point0[0]);
      re(point0[0]);
      C.push_back(point0[2]);
      re(point0[2]);
    } else if (S == 2) {
      A.push_back(point1[0]);
      re(point1[0]);
      B.push_back(point0[0]);
      re(point0[0]);
      C.push_back(point0[1]);
      re(point0[1]);
      A.push_back(point1[1]);
      re(point1[1]);
      B.push_back(point0[0]);
      re(point0[0]);
      C.push_back(point0[1]);
      re(point0[1]);
    } else if (S == 3) {
      A.push_back(point1[0]);
      re(point1[0]);
      B.push_back(point1[1]);
      re(point1[1]);
      C.push_back(point1[2]);
      re(point1[2]);
    } else if (S == 4) {
      A.push_back(point1[0]);
      re(point1[0]);
      B.push_back(point1[1]);
      re(point1[1]);
      C.push_back(point1[2]);
      re(point1[2]);
      A.push_back(point1[0]);
      re(point1[0]);
      B.push_back(point1[1]);
      re(point1[1]);
      C.push_back(point1[3]);
      re(point1[3]);
      A.push_back(point1[0]);
      re(point1[0]);
      B.push_back(point1[2]);
      re(point1[2]);
      C.push_back(point1[3]);
      re(point1[3]);
      A.push_back(point1[3]);
      re(point1[3]);
      B.push_back(point1[1]);
      re(point1[1]);
      C.push_back(point1[2]);
      re(point1[2]);
    }
    cout << A.size() << "\n";
    for (int i = 0; i < A.size(); i++) {
      pt(A[i]);
      pt(B[i]);
      pt(C[i]);
      cout << "\n";
    }
  }
}
