#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, m;
    cin >> n >> m;
    string str[n + 7];
    vector<vector<int> > v(n + 7, vector<int>(m + 7, 0));
    vector<vector<pair<int, int> > > ans;
    for (int i = (1); i <= (n); i++) cin >> str[i];
    for (int i = (1); i <= (n); i++)
      for (int j = (0); j <= (m - 1); j++) v[i][j + 1] = str[i][j] - '0';
    for (int i = (1); i <= (n - 2); i++) {
      for (int j = (1); j <= (m - 1); j++) {
        if (v[i][j]) {
          vector<pair<int, int> > temp;
          v[i][j] ^= 1;
          v[i + 1][j] ^= 1;
          v[i + 1][j + 1] ^= 1;
          temp.push_back(make_pair(i, j));
          temp.push_back(make_pair(i + 1, j));
          temp.push_back(make_pair(i + 1, j + 1));
          ans.push_back(temp);
        }
      }
      if (v[i][m]) {
        vector<pair<int, int> > temp;
        v[i][m] ^= 1;
        v[i + 1][m] ^= 1;
        v[i + 1][m - 1] ^= 1;
        temp.push_back(make_pair(i, m));
        temp.push_back(make_pair(i + 1, m));
        temp.push_back(make_pair(i + 1, m - 1));
        ans.push_back(temp);
      }
    }
    int up = n - 1, down = n;
    for (int i = (1); i <= (m - 1); i++) {
      if (v[up][i] && v[down][i]) {
        vector<pair<int, int> > temp;
        v[up][i] ^= 1;
        v[down][i] ^= 1;
        v[down][i + 1] ^= 1;
        temp.push_back(make_pair(up, i));
        temp.push_back(make_pair(down, i));
        temp.push_back(make_pair(down, i + 1));
        ans.push_back(temp);
      } else if (v[up][i]) {
        vector<pair<int, int> > temp;
        v[up][i] ^= 1;
        v[up][i + 1] ^= 1;
        v[down][i + 1] ^= 1;
        temp.push_back(make_pair(up, i));
        temp.push_back(make_pair(up, i + 1));
        temp.push_back(make_pair(down, i + 1));
        ans.push_back(temp);
      } else if (v[down][i]) {
        vector<pair<int, int> > temp;
        v[down][i] ^= 1;
        v[up][i + 1] ^= 1;
        v[down][i + 1] ^= 1;
        temp.push_back(make_pair(down, i));
        temp.push_back(make_pair(up, i + 1));
        temp.push_back(make_pair(down, i + 1));
        ans.push_back(temp);
      }
    }
    if (v[up][m] && v[down][m]) {
      vector<pair<int, int> > temp;
      v[up][m - 1] ^= 1;
      v[down][m - 1] ^= 1;
      v[up][m] ^= 1;
      temp.push_back(make_pair(up, m - 1));
      temp.push_back(make_pair(down, m - 1));
      temp.push_back(make_pair(up, m));
      ans.push_back(temp);
      temp.clear();
      v[up][m - 1] ^= 1;
      v[down][m - 1] ^= 1;
      v[down][m] ^= 1;
      temp.push_back(make_pair(up, m - 1));
      temp.push_back(make_pair(down, m - 1));
      temp.push_back(make_pair(down, m));
      ans.push_back(temp);
    } else if (v[up][m]) {
      vector<pair<int, int> > temp;
      v[up][m] ^= 1;
      v[up][m - 1] ^= 1;
      v[down][m - 1] ^= 1;
      temp.push_back(make_pair(up, m));
      temp.push_back(make_pair(up, m - 1));
      temp.push_back(make_pair(down, m - 1));
      ans.push_back(temp);
      temp.clear();
      v[up][m] ^= 1;
      v[down][m] ^= 1;
      v[up][m - 1] ^= 1;
      temp.push_back(make_pair(up, m));
      temp.push_back(make_pair(down, m));
      temp.push_back(make_pair(up, m - 1));
      ans.push_back(temp);
      temp.clear();
      v[up][m] ^= 1;
      v[down][m] ^= 1;
      v[down][m - 1] ^= 1;
      temp.push_back(make_pair(up, m));
      temp.push_back(make_pair(down, m));
      temp.push_back(make_pair(down, m - 1));
      ans.push_back(temp);
    } else if (v[down][m]) {
      vector<pair<int, int> > temp;
      v[down][m] ^= 1;
      v[up][m - 1] ^= 1;
      v[down][m - 1] ^= 1;
      temp.push_back(make_pair(down, m));
      temp.push_back(make_pair(up, m - 1));
      temp.push_back(make_pair(down, m - 1));
      ans.push_back(temp);
      temp.clear();
      v[up][m] ^= 1;
      v[down][m] ^= 1;
      v[up][m - 1] ^= 1;
      temp.push_back(make_pair(up, m));
      temp.push_back(make_pair(down, m));
      temp.push_back(make_pair(up, m - 1));
      ans.push_back(temp);
      temp.clear();
      v[up][m] ^= 1;
      v[down][m] ^= 1;
      v[down][m - 1] ^= 1;
      temp.push_back(make_pair(up, m));
      temp.push_back(make_pair(down, m));
      temp.push_back(make_pair(down, m - 1));
      ans.push_back(temp);
    }
    cout << ans.size() << endl;
    for (int i = (1); i <= (ans.size()); i++) {
      for (int j = (0); j <= (2); j++)
        cout << ans[i - 1][j].first << " " << ans[i - 1][j].second << " ";
      cout << endl;
    }
  }
}
