#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<string> vec(n);
    for (int i = 0; i < n; i++) cin >> vec[i];
    vector<pair<pair<int, int>, pair<pair<int, int>, pair<int, int>>>> ans;
    for (int i = 0; i < n - 2; i++) {
      for (int j = 0; j < m; j++) {
        pair<pair<int, int>, pair<pair<int, int>, pair<int, int>>> x;
        if (j == 0) {
          if (vec[i][j] == '1') {
            vec[i][j] = '0';
            x = make_pair(
                make_pair(i + 1, j + 1),
                make_pair(make_pair(i + 2, j + 1), make_pair(i + 2, j + 2)));
            if (vec[i + 1][j] == '1')
              vec[i + 1][j] = '0';
            else
              vec[i + 1][j] = '1';
            if (vec[i + 1][j + 1] == '1')
              vec[i + 1][j + 1] = '0';
            else
              vec[i + 1][j + 1] = '1';
            ans.push_back(x);
          }
        } else {
          if (vec[i][j] == '1') {
            vec[i][j] = '0';
            x = make_pair(
                make_pair(i + 1, j + 1),
                make_pair(make_pair(i + 2, j + 1), make_pair(i + 2, j)));
            if (vec[i + 1][j] == '1')
              vec[i + 1][j] = '0';
            else
              vec[i + 1][j] = '1';
            if (vec[i + 1][j - 1] == '1')
              vec[i + 1][j - 1] = '0';
            else
              vec[i + 1][j - 1] = '1';
            ans.push_back(x);
          }
        }
      }
    }
    for (int i = 0; i < m - 2; i++) {
      if (vec[n - 2][i] == '1') {
        vec[n - 2][i] = '0';
        ans.push_back(
            make_pair(make_pair(n - 1, i + 1),
                      make_pair(make_pair(n - 1, i + 2), make_pair(n, i + 2))));
        if (vec[n - 2][i + 1] == '1')
          vec[n - 2][i + 1] = '0';
        else
          vec[n - 2][i + 1] = '1';
        if (vec[n - 1][i + 1] == '1')
          vec[n - 1][i + 1] = '0';
        else
          vec[n - 1][i + 1] = '1';
      }
      if (vec[n - 1][i] == '1') {
        vec[n - 1][i] = '0';
        ans.push_back(
            make_pair(make_pair(n, i + 1),
                      make_pair(make_pair(n - 1, i + 2), make_pair(n, i + 2))));
        if (vec[n - 2][i + 1] == '1')
          vec[n - 2][i + 1] = '0';
        else
          vec[n - 2][i + 1] = '1';
        if (vec[n - 1][i + 1] == '1')
          vec[n - 1][i + 1] = '0';
        else
          vec[n - 1][i + 1] = '1';
      }
    }
    int cnt = 0;
    cnt = (vec[n - 2][m - 2] == '1') + (vec[n - 1][m - 2] == '1') +
          (vec[n - 2][m - 1] == '1') + (vec[n - 1][m - 1] == '1');
    if (cnt == 4) {
      int d = 0;
      vector<pair<int, int>> dd;
      for (int i = n - 2; i < n; i++) {
        for (int j = m - 2; j < m; j++) {
          if (vec[i][j] == '1' && d < 3) {
            vec[i][j] = '0';
            dd.push_back(make_pair(i + 1, j + 1));
            d++;
          }
        }
      }
      ans.push_back(make_pair(dd[0], make_pair(dd[1], dd[2])));
    }
    if (cnt == 1 || cnt == 4) {
      int d = 0;
      vector<pair<int, int>> dd;
      for (int i = n - 2; i < n; i++) {
        for (int j = m - 2; j < m; j++) {
          if (vec[i][j] == '0' && d < 2) {
            vec[i][j] = '1';
            dd.push_back(make_pair(i + 1, j + 1));
            d++;
            continue;
          }
          if (vec[i][j] == '1') {
            dd.push_back(make_pair(i + 1, j + 1));
            vec[i][j] = '0';
          }
        }
      }
      ans.push_back(make_pair(dd[0], make_pair(dd[1], dd[2])));
    }
    if (cnt == 2 || cnt == 1 || cnt == 4) {
      vector<pair<int, int>> dd;
      int d = 0;
      for (int i = n - 2; i < n; i++) {
        for (int j = m - 2; j < m; j++) {
          if (vec[i][j] == '0') {
            vec[i][j] = '1';
            dd.push_back(make_pair(i + 1, j + 1));
            continue;
          }
          if (vec[i][j] == '1' && d < 1) {
            dd.push_back(make_pair(i + 1, j + 1));
            vec[i][j] = '0';
            d++;
          }
        }
      }
      ans.push_back(make_pair(dd[0], make_pair(dd[1], dd[2])));
    }
    if (cnt == 3 || cnt == 2 || cnt == 1 || cnt == 4) {
      int d = 0;
      vector<pair<int, int>> dd;
      for (int i = n - 2; i < n; i++) {
        for (int j = m - 2; j < m; j++) {
          if (vec[i][j] == '1' && d < 3) {
            vec[i][j] = '0';
            dd.push_back(make_pair(i + 1, j + 1));
            d++;
          }
        }
      }
      ans.push_back(make_pair(dd[0], make_pair(dd[1], dd[2])));
    }
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i].first.first << " " << ans[i].first.second << " "
           << ans[i].second.first.first << " " << ans[i].second.first.second
           << " " << ans[i].second.second.first << " "
           << ans[i].second.second.second << endl;
    }
  }
}
