#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  srand(time(NULL));
  ;
  long long int p;
  cin >> p;
  while (p--) {
    long long int a, b, c, d, e;
    cin >> a >> b;
    char A[a][b];
    vector<long long int> Ans;
    for (c = 0; c < a; c++) {
      for (d = 0; d < b; d++) {
        cin >> A[c][d];
      }
    }
    for (c = 0; c < a - 2; c++) {
      for (d = 0; d < b; d++) {
        if (A[c][d] == '1' && d == 0) {
          A[c][d] = '0';
          if (A[c + 1][d] == '0')
            A[c + 1][d] = '1';
          else
            A[c + 1][d] = '0';
          if (A[c + 1][d + 1] == '0')
            A[c + 1][d + 1] = '1';
          else
            A[c + 1][d + 1] = '0';
          Ans.push_back(c);
          Ans.push_back(d);
          Ans.push_back(c + 1);
          Ans.push_back(d);
          Ans.push_back(c + 1);
          Ans.push_back(d + 1);
        } else if (A[c][d] == '1') {
          A[c][d] = '0';
          if (A[c + 1][d] == '0')
            A[c + 1][d] = '1';
          else
            A[c + 1][d] = '0';
          if (A[c + 1][d - 1] == '0')
            A[c + 1][d - 1] = '1';
          else
            A[c + 1][d - 1] = '0';
          Ans.push_back(c);
          Ans.push_back(d);
          Ans.push_back(c + 1);
          Ans.push_back(d);
          Ans.push_back(c + 1);
          Ans.push_back(d - 1);
        }
      }
    }
    for (d = 0; d < b - 2; d++) {
      if (A[a - 2][d] == '1' && A[a - 1][d] == '1') {
        A[a - 2][d] = '0';
        A[a - 1][d] = '0';
        if (A[a - 2][d + 1] == '1')
          A[a - 2][d + 1] = '0';
        else
          A[a - 2][d + 1] = '1';
        Ans.push_back(a - 2);
        Ans.push_back(d);
        Ans.push_back(a - 1);
        Ans.push_back(d);
        Ans.push_back(a - 2);
        Ans.push_back(d + 1);
      } else if (A[a - 2][d] == '1') {
        A[a - 2][d] = '0';
        if (A[a - 2][d + 1] == '1')
          A[a - 2][d + 1] = '0';
        else
          A[a - 2][d + 1] = '1';
        if (A[a - 1][d + 1] == '1')
          A[a - 1][d + 1] = '0';
        else
          A[a - 1][d + 1] = '1';
        Ans.push_back(a - 2);
        Ans.push_back(d);
        Ans.push_back(a - 2);
        Ans.push_back(d + 1);
        Ans.push_back(a - 1);
        Ans.push_back(d + 1);
      } else if (A[a - 1][d] == '1') {
        A[a - 1][d] = '0';
        if (A[a - 2][d + 1] == '1')
          A[a - 2][d + 1] = '0';
        else
          A[a - 2][d + 1] = '1';
        if (A[a - 1][d + 1] == '1')
          A[a - 1][d + 1] = '0';
        else
          A[a - 1][d + 1] = '1';
        Ans.push_back(a - 1);
        Ans.push_back(d);
        Ans.push_back(a - 2);
        Ans.push_back(d + 1);
        Ans.push_back(a - 1);
        Ans.push_back(d + 1);
      }
    }
    if ((A[a - 2][b - 2] + A[a - 2][b - 1] + A[a - 1][b - 2] + A[a - 1][b - 1] -
             4 * '0' ==
         4)) {
      A[a - 2][b - 2] = '0';
      A[a - 2][b - 1] = '0';
      A[a - 1][b - 2] = '0';
      Ans.push_back(a - 2);
      Ans.push_back(b - 2);
      Ans.push_back(a - 2);
      Ans.push_back(b - 1);
      Ans.push_back(a - 1);
      Ans.push_back(b - 2);
    }
    if ((A[a - 2][b - 2] + A[a - 2][b - 1] + A[a - 1][b - 2] + A[a - 1][b - 1] -
             4 * '0' ==
         1)) {
      if (A[a - 2][b - 2] == '1') {
        A[a - 2][b - 2] = '0';
        A[a - 2][b - 1] = '1';
        A[a - 1][b - 2] = '1';
        Ans.push_back(a - 2);
        Ans.push_back(b - 2);
        Ans.push_back(a - 2);
        Ans.push_back(b - 1);
        Ans.push_back(a - 1);
        Ans.push_back(b - 2);
      } else if (A[a - 2][b - 1] == '1') {
        A[a - 2][b - 1] = '0';
        A[a - 1][b - 2] = '1';
        A[a - 1][b - 1] = '1';
        Ans.push_back(a - 2);
        Ans.push_back(b - 1);
        Ans.push_back(a - 1);
        Ans.push_back(b - 2);
        Ans.push_back(a - 1);
        Ans.push_back(b - 1);
      } else if (A[a - 1][b - 2] == '1') {
        A[a - 1][b - 2] = '0';
        A[a - 1][b - 1] = '1';
        A[a - 2][b - 1] = '1';
        Ans.push_back(a - 1);
        Ans.push_back(b - 2);
        Ans.push_back(a - 1);
        Ans.push_back(b - 1);
        Ans.push_back(a - 2);
        Ans.push_back(b - 1);
      } else if (A[a - 1][b - 1] == '1') {
        A[a - 1][b - 1] = '0';
        A[a - 2][b - 1] = '1';
        A[a - 1][b - 2] = '1';
        Ans.push_back(a - 1);
        Ans.push_back(b - 1);
        Ans.push_back(a - 2);
        Ans.push_back(b - 1);
        Ans.push_back(a - 1);
        Ans.push_back(b - 2);
      }
    }
    if ((A[a - 2][b - 2] + A[a - 2][b - 1] + A[a - 1][b - 2] + A[a - 1][b - 1] -
             4 * '0' ==
         2)) {
      if (A[a - 2][b - 2] == '1') {
        A[a - 2][b - 2] = '0';
        Ans.push_back(a - 2);
        Ans.push_back(b - 2);
        if (A[a - 2][b - 1] == '0') {
          A[a - 2][b - 1] = '1';
          Ans.push_back(a - 2);
          Ans.push_back(b - 1);
        }
        if (A[a - 1][b - 1] == '0') {
          A[a - 1][b - 1] = '1';
          Ans.push_back(a - 1);
          Ans.push_back(b - 1);
        }
        if (A[a - 1][b - 2] == '0') {
          A[a - 1][b - 2] = '1';
          Ans.push_back(a - 1);
          Ans.push_back(b - 2);
        }
      } else if (A[a - 2][b - 1] == '1') {
        A[a - 2][b - 1] = '0';
        Ans.push_back(a - 2);
        Ans.push_back(b - 1);
        if (A[a - 2][b - 2] == '0') {
          A[a - 2][b - 2] = '1';
          Ans.push_back(a - 2);
          Ans.push_back(b - 2);
        }
        if (A[a - 1][b - 1] == '0') {
          A[a - 1][b - 1] = '1';
          Ans.push_back(a - 1);
          Ans.push_back(b - 1);
        }
        if (A[a - 1][b - 2] == '0') {
          A[a - 1][b - 2] = '1';
          Ans.push_back(a - 1);
          Ans.push_back(b - 2);
        }
      } else if (A[a - 1][b - 2] == '1') {
        A[a - 1][b - 2] = '0';
        Ans.push_back(a - 1);
        Ans.push_back(b - 2);
        if (A[a - 2][b - 1] == '0') {
          A[a - 2][b - 1] = '1';
          Ans.push_back(a - 2);
          Ans.push_back(b - 1);
        }
        if (A[a - 1][b - 1] == '0') {
          A[a - 1][b - 1] = '1';
          Ans.push_back(a - 1);
          Ans.push_back(b - 1);
        }
        if (A[a - 2][b - 2] == '0') {
          A[a - 2][b - 2] = '1';
          Ans.push_back(a - 2);
          Ans.push_back(b - 2);
        }
      }
    }
    if (A[a - 1][b - 1] == '1') {
      Ans.push_back(a - 1);
      Ans.push_back(b - 1);
    }
    if (A[a - 2][b - 1] == '1') {
      Ans.push_back(a - 2);
      Ans.push_back(b - 1);
    }
    if (A[a - 1][b - 2] == '1') {
      Ans.push_back(a - 1);
      Ans.push_back(b - 2);
    }
    if (A[a - 2][b - 2] == '1') {
      Ans.push_back(a - 2);
      Ans.push_back(b - 2);
    }
    cout << Ans.size() / 6 << endl;
    for (c = 0; c < Ans.size(); c++) {
      cout << Ans[c] + 1 << " ";
      if ((c + 1) % 6 == 0) cout << endl;
    }
    cout << endl;
  }
}
