#include <bits/stdc++.h>
using namespace std;
const int MAXN = 102;
char grid[MAXN][MAXN];
vector<vector<pair<int, int>>> rta;
void clearSq(int j, int k) {
  vector<pair<int, int>> onBits;
  for (int l = 0; l < int(4); l++) {
    onBits.clear();
    if (grid[j][k] == '1') onBits.push_back({j, k});
    if (grid[j + 1][k] == '1') onBits.push_back({j + 1, k});
    if (grid[j][k + 1] == '1') onBits.push_back({j, k + 1});
    if (grid[j + 1][k + 1] == '1') onBits.push_back({j + 1, k + 1});
    if (onBits.empty()) break;
    if ((int)onBits.size() == 4 || (int)onBits.size() == 3) {
      rta.push_back({onBits[0], onBits[1], onBits[2]});
      for (int o = 0; o < int(3); o++)
        grid[onBits[o].first][onBits[o].second] = '0';
    } else {
      int cnt = 0;
      vector<pair<int, int>> tmp;
      for (int g = 0; g < int(2); g++)
        for (int h = 0; h < int(2); h++)
          if (cnt < 2 && grid[j + g][k + h] == '0') {
            grid[j + g][k + h] = '1';
            cnt++;
            tmp.push_back({j + g, k + h});
          }
      tmp.push_back(onBits[0]);
      grid[onBits[0].first][onBits[0].second] = '0';
      rta.push_back(tmp);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ;
  int t;
  cin >> t;
  for (int i = 0; i < int(t); i++) {
    int n, m;
    cin >> n >> m;
    for (int j = 0; j < int(n); j++)
      for (int k = 0; k < int(m); k++) cin >> grid[j][k];
    rta.clear();
    for (int j = 0; j < int(n - 2); j++)
      for (int k = 0; k < int(m - 2); k++) {
        if (grid[j][k] == '1') {
          grid[j][k] = '0';
          grid[j + 1][k] = (grid[j + 1][k] == '0' ? '1' : '0');
          grid[j][k + 1] = (grid[j][k + 1] == '0' ? '1' : '0');
          rta.push_back({{j, k}, {j + 1, k}, {j, k + 1}});
        }
      }
    for (int j = 0; j < int(n - 2); j++) {
      if (grid[j][m - 2] == '1' || grid[j][m - 1] == '1') {
        vector<pair<int, int>> tmp;
        for (int g = 0; g < int(2); g++)
          for (int h = 0; h < int(2); h++)
            if ((int)tmp.size() < 3 && grid[j + g][m - 2 + h] == '1')
              tmp.push_back({j + g, m - 2 + h});
        for (int h = 0; h < int(2); h++)
          if ((int)tmp.size() < 3 && grid[j + 1][m - 2 + h] == '0')
            tmp.push_back({j + 1, m - 2 + h});
        rta.push_back(tmp);
        for (auto &k : tmp)
          grid[k.first][k.second] =
              (grid[k.first][k.second] == '0' ? '1' : '0');
      }
    }
    for (int j = 0; j < int(m - 2); j++) {
      if (grid[n - 1][j] == '1' || grid[n - 2][j] == '1') {
        vector<pair<int, int>> tmp;
        for (int g = 0; g < int(2); g++)
          for (int h = 0; h < int(2); h++)
            if ((int)tmp.size() < 3 && grid[n - 2 + h][j + g] == '1')
              tmp.push_back({n - 2 + h, j + g});
        for (int h = 0; h < int(2); h++)
          if ((int)tmp.size() < 3 && grid[n - 2 + h][j + 1] == '0')
            tmp.push_back({n - 2 + h, j + 1});
        rta.push_back(tmp);
        for (auto &k : tmp)
          grid[k.first][k.second] =
              (grid[k.first][k.second] == '0' ? '1' : '0');
      }
    }
    clearSq(n - 2, m - 2);
    cout << rta.size() << '\n';
    for (auto &j : rta) {
      for (auto &k : j) cout << k.first + 1 << ' ' << k.second + 1 << ' ';
      cout << '\n';
    }
  }
  return 0;
}
