#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    string arr[n];
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    int ans = 0;
    vector<int> v;
    for (int i = 0; i < n - 2; i++) {
      for (int j = 0; j < m - 2; j++) {
        if (arr[i][j] == '1') {
          ans += 1;
          v.push_back(i + 1);
          v.push_back(j + 1);
          v.push_back(i + 2);
          v.push_back(j + 2);
          v.push_back(i + 2);
          v.push_back(j + 1);
          arr[i][j] = '0';
          if (arr[i + 1][j + 1] == '0') {
            arr[i + 1][j + 1] = '1';
          } else {
            arr[i + 1][j + 1] = '0';
          }
          if (arr[i + 1][j] == '0') {
            arr[i + 1][j] = '1';
          } else {
            arr[i + 1][j] = '0';
          }
        }
      }
    }
    for (int i = 0; i < n - 2; i++) {
      if (arr[i][m - 2] == '1') {
        ans += 1;
        v.push_back(i + 1);
        v.push_back(m - 1);
        v.push_back(i + 2);
        v.push_back(m);
        v.push_back(i + 2);
        v.push_back(m - 1);
        if (arr[i + 1][m - 1] == '0') {
          arr[i + 1][m - 1] = '1';
        } else {
          arr[i + 1][m - 1] = '0';
        }
        if (arr[i + 1][m - 2] == '0') {
          arr[i + 1][m - 2] = '1';
        } else {
          arr[i + 1][m - 2] = '0';
        }
        arr[i][m - 2] = '0';
      }
      if (arr[i][m - 1] == '1') {
        ans += 1;
        v.push_back(i + 1);
        v.push_back(m);
        v.push_back(i + 2);
        v.push_back(m);
        v.push_back(i + 2);
        v.push_back(m - 1);
        if (arr[i + 1][m - 1] == '0') {
          arr[i + 1][m - 1] = '1';
        } else {
          arr[i + 1][m - 1] = '0';
        }
        if (arr[i + 1][m - 2] == '0') {
          arr[i + 1][m - 2] = '1';
        } else {
          arr[i + 1][m - 2] = '0';
        }
        arr[i][m - 1] = '0';
      }
    }
    for (int j = 0; j < m - 2; j++) {
      if (arr[n - 2][j] == '1') {
        ans += 1;
        v.push_back(n - 1);
        v.push_back(j + 1);
        v.push_back(n - 1);
        v.push_back(j + 2);
        v.push_back(n);
        v.push_back(j + 2);
        if (arr[n - 2][j + 1] == '0') {
          arr[n - 2][j + 1] = '1';
        } else {
          arr[n - 2][j + 1] = '0';
        }
        if (arr[n - 1][j + 1] == '0') {
          arr[n - 1][j + 1] = '1';
        } else {
          arr[n - 1][j + 1] = '0';
        }
        arr[n - 2][j] = '0';
      }
      if (arr[n - 1][j] == '1') {
        ans += 1;
        v.push_back(n);
        v.push_back(j + 1);
        v.push_back(n - 1);
        v.push_back(j + 2);
        v.push_back(n);
        v.push_back(j + 2);
        if (arr[n - 2][j + 1] == '0') {
          arr[n - 2][j + 1] = '1';
        } else {
          arr[n - 2][j + 1] = '0';
        }
        if (arr[n - 1][j + 1] == '0') {
          arr[n - 1][j + 1] = '1';
        } else {
          arr[n - 1][j + 1] = '0';
        }
        arr[n - 1][j] = '0';
      }
    }
    int i = n - 2;
    int j = m - 2;
    int count = 0;
    vector<int> u;
    vector<int> op;
    if (arr[i][j] == '1') {
      count++;
      u.push_back(i + 1);
      u.push_back(j + 1);
    } else {
      op.push_back(i + 1);
      op.push_back(j + 1);
    }
    if (arr[i][j + 1] == '1') {
      count++;
      u.push_back(i + 1);
      u.push_back(j + 2);
    } else {
      op.push_back(i + 1);
      op.push_back(j + 2);
    }
    if (arr[i + 1][j + 1] == '1') {
      count++;
      u.push_back(i + 2);
      u.push_back(j + 2);
    } else {
      op.push_back(i + 2);
      op.push_back(j + 2);
    }
    if (arr[i + 1][j] == '1') {
      count++;
      u.push_back(i + 2);
      u.push_back(j + 1);
    } else {
      op.push_back(i + 2);
      op.push_back(j + 1);
    }
    arr[i][j + 1] = '0';
    arr[i + 1][j + 1] = '0';
    arr[i][j] = '0';
    arr[i + 1][j] = '0';
    if (count == 0) {
    } else {
      if (count == 1) {
        ans += 3;
        v.push_back(u[0]);
        v.push_back(u[1]);
        v.push_back(op[0]);
        v.push_back(op[1]);
        v.push_back(op[2]);
        v.push_back(op[3]);
        v.push_back(u[0]);
        v.push_back(u[1]);
        v.push_back(op[2]);
        v.push_back(op[3]);
        v.push_back(op[4]);
        v.push_back(op[5]);
        v.push_back(u[0]);
        v.push_back(u[1]);
        v.push_back(op[0]);
        v.push_back(op[1]);
        v.push_back(op[4]);
        v.push_back(op[5]);
      } else {
        if (count == 2) {
          ans += 2;
          v.push_back(u[0]);
          v.push_back(u[1]);
          v.push_back(op[0]);
          v.push_back(op[1]);
          v.push_back(op[2]);
          v.push_back(op[3]);
          v.push_back(u[2]);
          v.push_back(u[3]);
          v.push_back(op[2]);
          v.push_back(op[3]);
          v.push_back(op[0]);
          v.push_back(op[1]);
        } else {
          if (count == 3) {
            ans += 1;
            v.push_back(u[0]);
            v.push_back(u[1]);
            v.push_back(u[2]);
            v.push_back(u[3]);
            v.push_back(u[4]);
            v.push_back(u[5]);
          } else {
            if (count == 4) {
              ans += 4;
              v.push_back(u[0]);
              v.push_back(u[1]);
              v.push_back(u[2]);
              v.push_back(u[3]);
              v.push_back(u[4]);
              v.push_back(u[5]);
              v.push_back(u[6]);
              v.push_back(u[7]);
              v.push_back(u[0]);
              v.push_back(u[1]);
              v.push_back(u[2]);
              v.push_back(u[3]);
              v.push_back(u[6]);
              v.push_back(u[7]);
              v.push_back(u[2]);
              v.push_back(u[3]);
              v.push_back(u[4]);
              v.push_back(u[5]);
              v.push_back(u[6]);
              v.push_back(u[7]);
              v.push_back(u[0]);
              v.push_back(u[1]);
              v.push_back(u[4]);
              v.push_back(u[5]);
            }
          }
        }
      }
    }
    int ab = v.size();
    cout << ans << "\n";
    for (int i = 0; i < ans; i++) {
      for (int j = 0; j < 6; j++) {
        cout << v[i * 6 + j] << " ";
      }
      cout << "\n";
    }
  }
  return 0;
}
