#include <bits/stdc++.h>
using namespace std;
void init() {}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<string> vs(n);
  do {
    for (auto& x139874 : vs) {
      cin >> x139874;
    }
  } while (0);
  vector<vector<int> > a(n, vector<int>(m, 0));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      a[i][j] = vs[i][j] - '0';
    }
  }
  vector<vector<int> > ans;
  for (int i = n - 1; i >= 2; --i) {
    for (int j = 0; j < m - 1; ++j) {
      if (a[i][j]) {
        a[i][j] ^= 1;
        a[i - 1][j] ^= 1;
        a[i - 1][j + 1] ^= 1;
        ans.push_back({i, j, i - 1, j, i - 1, j + 1});
      }
    }
    if (a[i][m - 1]) {
      a[i][m - 1] ^= 1;
      a[i - 1][m - 2] ^= 1;
      a[i - 1][m - 1] ^= 1;
      ans.push_back({i, m - 1, i - 1, m - 2, i - 1, m - 1});
    }
  }
  for (int j = m - 1; j >= 2; --j) {
    if (a[0][j]) {
      a[0][j] ^= 1;
      a[0][j - 1] ^= 1;
      a[1][j - 1] ^= 1;
      ans.push_back({0, j, 0, j - 1, 1, j - 1});
    }
    if (a[1][j]) {
      a[1][j] ^= 1;
      a[0][j - 1] ^= 1;
      a[1][j - 1] ^= 1;
      ans.push_back({1, j, 0, j - 1, 1, j - 1});
    }
  }
  int cnt = 0;
  for (int i : {0, 1}) {
    for (int j : {0, 1}) {
      cnt += a[i][j];
    }
  }
  if (cnt == 4) {
    a[0][1] ^= 1;
    a[1][0] ^= 1;
    a[1][1] ^= 1;
    ans.push_back({0, 1, 1, 0, 1, 1});
    cnt = 1;
  }
  if (cnt == 1) {
    if (a[0][0]) {
      a[0][0] ^= 1;
      a[1][0] ^= 1;
      a[0][1] ^= 1;
      ans.push_back({0, 0, 1, 0, 0, 1});
    } else if (a[1][0]) {
      a[0][0] ^= 1;
      a[1][0] ^= 1;
      a[1][1] ^= 1;
      ans.push_back({0, 0, 1, 0, 1, 1});
    } else if (a[0][1]) {
      a[0][0] ^= 1;
      a[0][1] ^= 1;
      a[1][1] ^= 1;
      ans.push_back({0, 0, 1, 1, 0, 1});
    } else if (a[1][1]) {
      a[1][0] ^= 1;
      a[0][1] ^= 1;
      a[1][1] ^= 1;
      ans.push_back({1, 0, 1, 1, 0, 1});
    }
    cnt = 2;
  }
  if (cnt == 2) {
    if (a[0][0] == a[1][1]) {
      if (a[0][0]) {
        a[0][0] ^= 1;
        a[0][1] ^= 1;
        a[1][0] ^= 1;
        ans.push_back({0, 0, 0, 1, 1, 0});
      } else {
        a[0][1] ^= 1;
        a[0][0] ^= 1;
        a[1][1] ^= 1;
        ans.push_back({0, 0, 0, 1, 1, 1});
      }
    } else {
      if (a[0][0] == 1) {
        a[0][1] ^= 1;
        a[1][0] ^= 1;
        a[1][1] ^= 1;
        ans.push_back({0, 1, 1, 1, 1, 0});
      } else {
        a[0][1] ^= 1;
        a[1][0] ^= 1;
        a[0][0] ^= 1;
        ans.push_back({0, 1, 0, 0, 1, 0});
      }
    }
    cnt = 3;
  }
  if (cnt == 3) {
    vector<int> tmp;
    for (int i : {0, 1}) {
      for (int j : {0, 1}) {
        if (a[i][j]) {
          a[i][j] ^= 1;
          tmp.emplace_back(i);
          tmp.emplace_back(j);
        }
      }
    }
    ans.emplace_back(tmp);
  }
  cout << ans.size() << "\n";
  for (auto& v : ans) {
    do {
      for (auto& x139s74 : v) {
        x139s74 += (1);
      }
    } while (0);
    do {
      for (auto& x139374 : v) {
        cout << x139374 << " ";
      }
      cout << "\n";
    } while (0);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  init();
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
