#include <bits/stdc++.h>
using namespace std;
struct P {
  int i, j;
  P() = default;
  P(int i, int j) : i(i), j(j){};
  void print() const { cout << i + 1 << ' ' << j + 1 << ' '; }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n, vector<int>(m));
    for (int i = 0; i < n; ++i) {
      string s;
      cin >> s;
      for (int j = 0; j < m; ++j) {
        a[i][j] = s[j] - '0';
      }
    }
    vector<P> ret;
    auto print = [&]() {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
          cout << a[i][j];
        }
        cout << endl;
      }
      cout << endl;
    };
    auto flip = [&](const P& p) {
      ret.emplace_back(p);
      a[p.i][p.j] ^= 1;
    };
    auto solve4 = [&](const vector<P>& points) {
      vector<P> ones, zeros;
      for (const P p : points) {
        if (a[p.i][p.j] == 1) {
          ones.push_back(p);
        } else {
          zeros.push_back(p);
        }
      }
      if (ones.size() == 1) {
        flip(zeros[0]);
        flip(zeros[1]);
        flip(ones[0]);
        flip(zeros[0]);
        flip(zeros[2]);
        flip(ones[0]);
        flip(zeros[1]);
        flip(zeros[2]);
        flip(ones[0]);
      } else if (ones.size() == 2) {
        flip(zeros[0]);
        flip(zeros[1]);
        flip(ones[0]);
        flip(zeros[0]);
        flip(zeros[1]);
        flip(ones[1]);
      } else if (ones.size() == 3) {
        for (const P p : ones) {
          flip(p);
        }
      } else if (ones.size() == 4) {
        flip(ones[0]);
        flip(ones[2]);
        flip(ones[3]);
        flip(ones[1]);
        flip(ones[2]);
        flip(ones[3]);
        flip(ones[0]);
        flip(ones[1]);
        flip(ones[3]);
        flip(ones[0]);
        flip(ones[1]);
        flip(ones[2]);
      }
    };
    for (int i = 0; i < n - 2; ++i) {
      for (int j = 0; j < m; ++j) {
        if (a[i][j] == 1) {
          if (j + 1 < m) {
            flip(P(i, j));
            flip(P(i, j + 1));
            flip(P(i + 1, j + 1));
          } else {
            flip(P(i, j));
            flip(P(i + 1, j - 1));
            flip(P(i + 1, j));
          }
        }
      }
    }
    for (int j = 0; j < m; j += 2) {
      if (j + 1 < m) {
        solve4({P(n - 2, j), P(n - 2, j + 1), P(n - 1, j), P(n - 1, j + 1)});
      } else {
        solve4({P(n - 2, j - 1), P(n - 2, j), P(n - 1, j - 1), P(n - 1, j)});
      }
    }
    int sz = (int)ret.size() / 3;
    assert(sz <= n * m * 3);
    cout << sz << '\n';
    for (int i = 0; i < ret.size(); i += 3) {
      for (int j = 0; j < 3; ++j) {
        ret[i + j].print();
      }
      cout << '\n';
    }
  }
  return 0;
}
