#include <bits/stdc++.h>
using namespace std;
int row, col;
int binary_table[2005][2005];
char one_row[2005];
vector<pair<int, int> > res;
vector<pair<int, int> > pos[2];
void change() {
  pair<int, int> flip = res.back();
  binary_table[flip.first][flip.second] =
      1 - binary_table[flip.first][flip.second];
}
void prepare() {
  if (col % 2) {
    for (int i = 1; i < row; i++) {
      if (binary_table[i][col]) {
        res.push_back(pair<int, int>(i, col));
        change();
        res.push_back(pair<int, int>(i, col - 1));
        change();
        res.push_back(pair<int, int>(i + 1, col - 1));
        change();
      }
    }
    if (binary_table[row][col]) {
      res.push_back(pair<int, int>(row, col));
      change();
      res.push_back(pair<int, int>(row, col - 1));
      change();
      res.push_back(pair<int, int>(row - 1, col - 1));
      change();
    }
    col--;
  }
  if (row % 2) {
    for (int i = 1; i < col; i++) {
      if (binary_table[row][i]) {
        res.push_back(pair<int, int>(row, i));
        change();
        res.push_back(pair<int, int>(row - 1, i));
        change();
        res.push_back(pair<int, int>(row - 1, i + 1));
        change();
      }
    }
    if (binary_table[row][col]) {
      res.push_back(pair<int, int>(row, col));
      change();
      res.push_back(pair<int, int>(row - 1, col));
      change();
      res.push_back(pair<int, int>(row - 1, col - 1));
      change();
    }
    row--;
  }
}
void solve() {
  scanf("%d %d", &row, &col);
  for (int i = 1; i <= row; i++) {
    scanf("%s", one_row + 1);
    for (int j = 1; j <= col; j++) binary_table[i][j] = one_row[j] - '0';
  }
  res.clear();
  prepare();
  for (int i = 1; i <= row; i += 2)
    for (int j = 1; j <= col; j += 2) {
      pos[0].clear(), pos[1].clear();
      for (int m = i; m <= i + 1; m++)
        for (int n = j; n <= j + 1; n++)
          pos[binary_table[m][n]].push_back(pair<int, int>(m, n));
      if (pos[1].size() == 4) {
        for (int m = 1; m <= 3; m++)
          res.push_back(pos[1].back()), pos[0].push_back(pos[1].back()),
              pos[1].pop_back();
      }
      if (pos[1].size() == 1) {
        pair<int, int> black = pos[1].back();
        pos[1].pop_back();
        res.push_back(black);
        for (int m = 1; m <= 2; m++)
          res.push_back(pos[0].back()), pos[1].push_back(pos[0].back()),
              pos[0].pop_back();
        pos[0].push_back(black);
      }
      if (pos[1].size() == 2) {
        pair<int, int> black = pos[1].back();
        pos[1].pop_back();
        res.push_back(black);
        for (int m = 1; m <= 2; m++)
          res.push_back(pos[0].back()), pos[1].push_back(pos[0].back()),
              pos[0].pop_back();
        pos[0].push_back(black);
      }
      if (pos[1].size() == 3) {
        for (int m = 1; m <= 3; m++)
          res.push_back(pos[1].back()), pos[1].pop_back();
      }
    }
  printf("%d\n", res.size() / 3);
  for (int i = 1, sizer = res.size(); i <= sizer; i++) {
    printf("%d %d ", res[i - 1].first, res[i - 1].second);
    if (i % 3 == 0) printf("\n");
  }
}
int main() {
  int tester;
  scanf("%d", &tester);
  while (tester--) solve();
}
