#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int MXN = 1e5 + 5;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T, tc = 0;
  cin >> T;
  while (T--) {
    int n, m;
    cin >> n >> m;
    string ss[n];
    for (int i = 0; i < n; ++i) {
      cin >> ss[i];
    }
    int k = 0;
    int bb[n * m][6];
    for (int i = 0; i < n - 2; i++) {
      for (int j = 0; j < m; j += 2) {
        if (j == m - 1) j--;
        if (ss[i][j] == '1' and ss[i][j + 1] == '1') {
          bb[k][0] = i + 1;
          ss[i][j] = (ss[i][j] == '1') ? '0' : '1';
          bb[k][1] = j + 1;
          bb[k][2] = i + 1;
          ss[i][j + 1] = (ss[i][j + 1] == '1') ? '0' : '1';
          bb[k][3] = j + 2;
          bb[k][4] = i + 2;
          ss[i + 1][j] = (ss[i + 1][j] == '1') ? '0' : '1';
          bb[k][5] = j + 1;
          k++;
        } else if (ss[i][j] == '1' and ss[i][j + 1] == '0') {
          bb[k][0] = i + 1;
          bb[k][1] = j + 1;
          ss[i][j] = (ss[i][j] == '1') ? '0' : '1';
          bb[k][2] = i + 2;
          bb[k][3] = j + 2;
          ss[i + 1][j + 1] = (ss[i + 1][j + 1] == '1') ? '0' : '1';
          bb[k][4] = i + 2;
          bb[k][5] = j + 1;
          ss[i + 1][j] = (ss[i + 1][j] == '1') ? '0' : '1';
          k++;
        } else if (ss[i][j] == '0' and ss[i][j + 1] == '1') {
          bb[k][0] = i + 1;
          bb[k][1] = j + 2;
          ss[i][j + 1] = (ss[i][j + 1] == '1') ? '0' : '1';
          bb[k][2] = i + 2;
          bb[k][3] = j + 2;
          ss[i + 1][j + 1] = (ss[i + 1][j + 1] == '1') ? '0' : '1';
          bb[k][4] = i + 2;
          bb[k][5] = j + 1;
          ss[i + 1][j] = (ss[i + 1][j] == '1') ? '0' : '1';
          k++;
        }
      }
    }
    int l = n - 2;
    for (int j = 0; j < m - 1; j++) {
      if (ss[l][j] == '1' and ss[l + 1][j] == '1') {
        bb[k][0] = l + 1;
        bb[k][1] = j + 1;
        ss[l][j] = (ss[l][j] == '1') ? '0' : '1';
        bb[k][2] = l + 1;
        bb[k][3] = j + 2;
        ss[l][j + 1] = (ss[l][j + 1] == '1') ? '0' : '1';
        bb[k][4] = l + 2;
        bb[k][5] = j + 1;
        ss[l + 1][j] = (ss[l + 1][j] == '1') ? '0' : '1';
        k++;
      } else if (ss[l][j] == '1' and ss[l + 1][j] == '0') {
        bb[k][0] = l + 1;
        bb[k][1] = j + 1;
        ss[l][j] = (ss[l][j] == '1') ? '0' : '1';
        bb[k][2] = l + 1;
        bb[k][3] = j + 2;
        ss[l][j + 1] = (ss[l][j + 1] == '1') ? '0' : '1';
        bb[k][4] = l + 2;
        bb[k][5] = j + 2;
        ss[l + 1][j + 1] = (ss[l + 1][j + 1] == '1') ? '0' : '1';
        k++;
      } else if (ss[l][j] == '0' and ss[l + 1][j] == '1') {
        bb[k][0] = l + 1;
        bb[k][1] = j + 2;
        ss[l][j + 1] = (ss[l][j + 1] == '1') ? '0' : '1';
        bb[k][2] = l + 2;
        bb[k][3] = j + 1;
        ss[l + 1][j] = (ss[l + 1][j] == '1') ? '0' : '1';
        bb[k][4] = l + 2;
        bb[k][5] = j + 2;
        ss[l + 1][j + 1] = (ss[l + 1][j + 1] == '1') ? '0' : '1';
        k++;
      }
    }
    int i = n - 2, j = m - 2;
    if (ss[i][j + 1] == '1' and ss[i + 1][j + 1] == '1') {
      bb[k][0] = i + 1;
      bb[k][1] = j + 1;
      ss[i][j] = (ss[i][j] == '1') ? '0' : '1';
      bb[k][2] = i + 2;
      bb[k][3] = j + 1;
      ss[i + 1][j] = (ss[i + 1][j] == '1') ? '0' : '1';
      bb[k][4] = i + 2;
      bb[k][5] = j + 2;
      ss[i + 1][j + 1] = (ss[i + 1][j + 1] == '1') ? '0' : '1';
      k++;
      bb[k][0] = i + 1;
      bb[k][1] = j + 1;
      ss[i][j] = (ss[i][j] == '1') ? '0' : '1';
      bb[k][2] = i + 2;
      bb[k][3] = j + 1;
      ss[i + 1][j] = (ss[i + 1][j] == '1') ? '0' : '1';
      bb[k][4] = i + 1;
      bb[k][5] = j + 2;
      ss[i][j + 1] = (ss[i][j + 1] == '1') ? '0' : '1';
      k++;
    } else if (ss[i][j + 1] == '1' and ss[i + 1][j + 1] == '0') {
      bb[k][0] = i + 1;
      bb[k][1] = j + 1;
      ss[i][j] = (ss[i][j] == '1') ? '0' : '1';
      bb[k][2] = i + 1;
      bb[k][3] = j + 2;
      ss[i][j + 1] = (ss[i][j + 1] == '1') ? '0' : '1';
      bb[k][4] = i + 2;
      bb[k][5] = j + 1;
      ss[i + 1][j] = (ss[i + 1][j] == '1') ? '0' : '1';
      k++;
      bb[k][0] = i + 1;
      bb[k][1] = j + 1;
      ss[i][j] = (ss[i][j] == '1') ? '0' : '1';
      bb[k][2] = i + 1;
      bb[k][3] = j + 2;
      ss[i][j + 1] = (ss[i][j + 1] == '1') ? '0' : '1';
      bb[k][4] = i + 2;
      bb[k][5] = j + 2;
      ss[i + 1][j + 1] = (ss[i + 1][j + 1] == '1') ? '0' : '1';
      k++;
      bb[k][0] = i + 2;
      bb[k][1] = j + 1;
      ss[i + 1][j] = (ss[i + 1][j] == '1') ? '0' : '1';
      bb[k][2] = i + 1;
      bb[k][3] = j + 2;
      ss[i][j + 1] = (ss[i][j + 1] == '1') ? '0' : '1';
      bb[k][4] = i + 2;
      bb[k][5] = j + 2;
      ss[i + 1][j + 1] = (ss[i + 1][j + 1] == '1') ? '0' : '1';
      k++;
    } else if (ss[i][j + 1] == '0' and ss[i + 1][j + 1] == '1') {
      bb[k][0] = i + 1;
      bb[k][1] = j + 1;
      ss[i][j] = (ss[i][j] == '1') ? '0' : '1';
      bb[k][2] = i + 2;
      bb[k][3] = j + 2;
      ss[i + 1][j + 1] = (ss[i + 1][j + 1] == '1') ? '0' : '1';
      bb[k][4] = i + 2;
      bb[k][5] = j + 1;
      ss[i + 1][j] = (ss[i + 1][j] == '1') ? '0' : '1';
      k++;
      bb[k][0] = i + 1;
      bb[k][1] = j + 1;
      ss[i][j] = (ss[i][j] == '1') ? '0' : '1';
      bb[k][2] = i + 1;
      bb[k][3] = j + 2;
      ss[i][j + 1] = (ss[i][j + 1] == '1') ? '0' : '1';
      bb[k][4] = i + 2;
      bb[k][5] = j + 2;
      ss[i + 1][j + 1] = (ss[i + 1][j + 1] == '1') ? '0' : '1';
      k++;
      bb[k][0] = i + 2;
      bb[k][1] = j + 1;
      ss[i + 1][j] = (ss[i + 1][j] == '1') ? '0' : '1';
      bb[k][2] = i + 1;
      bb[k][3] = j + 2;
      ss[i][j + 1] = (ss[i][j + 1] == '1') ? '0' : '1';
      bb[k][4] = i + 2;
      bb[k][5] = j + 2;
      ss[i + 1][j + 1] = (ss[i + 1][j + 1] == '1') ? '0' : '1';
      k++;
    }
    cout << k << "\n";
    for (int i = 0; i < k; ++i) {
      cout << bb[i][0] << " " << bb[i][1] << " " << bb[i][2] << " " << bb[i][3]
           << " " << bb[i][4] << " " << bb[i][5] << "\n";
    }
  }
  return 0;
}
