#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, m, i, j, count, len, x;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    vector<int> v;
    char a[n][m];
    for (i = 0; i < n; i++)
      for (j = 0; j < m; j++) cin >> a[i][j];
    count = 0;
    for (i = 0; i < n - 2; i++) {
      for (j = 1; j < m; j++) {
        if (a[i][j - 1] == '1' || a[i][j] == '1') {
          count++;
          if (a[i][j - 1] == '1') {
            if (a[i][j] == '1') {
              a[i][j - 1] = a[i][j] = '0';
              if (a[i + 1][j - 1] == '1')
                a[i + 1][j - 1] = '0';
              else
                a[i + 1][j - 1] = '1';
              v.push_back(i + 1);
              v.push_back(j);
              v.push_back(i + 1);
              v.push_back(j + 1);
              v.push_back(i + 2);
              v.push_back(j);
            } else {
              a[i][j - 1] = '0';
              if (a[i + 1][j - 1] == '1')
                a[i + 1][j - 1] = '0';
              else
                a[i + 1][j - 1] = '1';
              if (a[i + 1][j] == '1')
                a[i + 1][j] = '0';
              else
                a[i + 1][j] = '1';
              v.push_back(i + 1);
              v.push_back(j);
              v.push_back(i + 2);
              v.push_back(j);
              v.push_back(i + 2);
              v.push_back(j + 1);
            }
          } else if (a[i][j] == '1') {
            a[i][j] = '0';
            if (a[i + 1][j - 1] == '1')
              a[i + 1][j - 1] = '0';
            else
              a[i + 1][j - 1] = '1';
            if (a[i + 1][j] == '1')
              a[i + 1][j] = '0';
            else
              a[i + 1][j] = '1';
            v.push_back(i + 1);
            v.push_back(j + 1);
            v.push_back(i + 2);
            v.push_back(j);
            v.push_back(i + 2);
            v.push_back(j + 1);
          }
        }
      }
    }
    i = n - 2;
    for (j = 0; j < m - 2; j++) {
      if (a[i][j] == '1' || a[i + 1][j] == '1') {
        count++;
        if (a[i][j] == '1') {
          if (a[i + 1][j] == '1') {
            a[i][j] = a[i + 1][j] = '0';
            if (a[i][j + 1] == '1')
              a[i][j + 1] = '0';
            else
              a[i][j + 1] = '1';
            v.push_back(i + 1);
            v.push_back(j + 1);
            v.push_back(i + 1);
            v.push_back(j + 2);
            v.push_back(i + 2);
            v.push_back(j + 1);
          } else {
            a[i][j] = '0';
            if (a[i][j + 1] == '1')
              a[i][j + 1] = '0';
            else
              a[i][j + 1] = '1';
            if (a[i + 1][j + 1] == '1')
              a[i + 1][j + 1] = '0';
            else
              a[i + 1][j + 1] = '1';
            v.push_back(i + 1);
            v.push_back(j + 1);
            v.push_back(i + 1);
            v.push_back(j + 2);
            v.push_back(i + 2);
            v.push_back(j + 2);
          }
        } else if (a[i + 1][j] == '1') {
          a[i + 1][j] = '0';
          if (a[i][j + 1] == '1')
            a[i][j + 1] = '0';
          else
            a[i][j + 1] = '1';
          if (a[i + 1][j + 1] == '1')
            a[i + 1][j + 1] = '0';
          else
            a[i + 1][j + 1] = '1';
          v.push_back(i + 2);
          v.push_back(j + 1);
          v.push_back(i + 1);
          v.push_back(j + 2);
          v.push_back(i + 2);
          v.push_back(j + 2);
        }
      }
    }
    if (a[i][j] == '1' && a[i][j + 1] == '1' && a[i + 1][j] == '1' &&
        a[i + 1][j + 1] == '1') {
      count += 4;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 2);
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
    } else if (a[i][j] == '1' && a[i][j + 1] == '1' && a[i + 1][j] == '1' &&
               a[i + 1][j + 1] == '0') {
      count++;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
    } else if (a[i][j] == '1' && a[i][j + 1] == '1' && a[i + 1][j] == '0' &&
               a[i + 1][j + 1] == '1') {
      count++;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 2);
    } else if (a[i][j] == '1' && a[i][j + 1] == '0' && a[i + 1][j] == '1' &&
               a[i + 1][j + 1] == '1') {
      count++;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
    } else if (a[i][j] == '0' && a[i][j + 1] == '1' && a[i + 1][j] == '1' &&
               a[i + 1][j + 1] == '1') {
      count++;
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
    } else if (a[i][j] == '1' && a[i][j + 1] == '1' && a[i + 1][j] == '0' &&
               a[i + 1][j + 1] == '0') {
      count += 2;
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
    } else if (a[i][j] == '1' && a[i][j + 1] == '0' && a[i + 1][j] == '1' &&
               a[i + 1][j + 1] == '0') {
      count += 2;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 2);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
    } else if (a[i][j] == '0' && a[i][j + 1] == '1' && a[i + 1][j] == '1' &&
               a[i + 1][j + 1] == '0') {
      count += 2;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 2);
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
    } else if (a[i][j] == '0' && a[i][j + 1] == '1' && a[i + 1][j] == '0' &&
               a[i + 1][j + 1] == '1') {
      count += 2;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
    } else if (a[i][j] == '0' && a[i][j + 1] == '0' && a[i + 1][j] == '1' &&
               a[i + 1][j + 1] == '1') {
      count += 2;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 2);
    } else if (a[i][j] == '1' && a[i][j + 1] == '0' && a[i + 1][j] == '0' &&
               a[i + 1][j + 1] == '1') {
      count += 2;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
    } else if (a[i][j] == '0' && a[i][j + 1] == '0' && a[i + 1][j] == '0' &&
               a[i + 1][j + 1] == '1') {
      count += 3;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 2);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
    } else if (a[i][j] == '0' && a[i][j + 1] == '0' && a[i + 1][j] == '1' &&
               a[i + 1][j + 1] == '0') {
      count += 3;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
    } else if (a[i][j] == '0' && a[i][j + 1] == '1' && a[i + 1][j] == '0' &&
               a[i + 1][j + 1] == '0') {
      count += 3;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 2);
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
    } else if (a[i][j] == '1' && a[i][j + 1] == '0' && a[i + 1][j] == '0' &&
               a[i + 1][j + 1] == '0') {
      count += 3;
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 2);
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 2);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 1);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 1);
      v.push_back(i + 2);
      v.push_back(j + 2);
    }
    cout << count << endl;
    i = 0, x = 5;
    len = v.size();
    for (i = 0; i < len; i++) {
      cout << v[i];
      if (i == x) {
        cout << endl;
        x += 6;
      } else
        cout << " ";
    }
  }
  return 0;
}
