#include <bits/stdc++.h>
using namespace std;
struct point {
  int x, y;
};
struct op {
  point p[3];
} o;
char tmp[105][105];
int g[105][105];
int T, n, m;
vector<op> vec;
void one(int x1, int y1, int x2, int y2) {
  if (g[x1][y1] == 1) {
    o.p[0] = {x1, y1};
    o.p[1] = {x1, y2};
    o.p[2] = {x2, y2};
    vec.push_back(o);
    o.p[1] = {x2, y1};
    vec.push_back(o);
    o.p[2] = {x1, y2};
    vec.push_back(o);
  }
  if (g[x1][y2] == 1) {
    o.p[0] = {x1, y2};
    o.p[1] = {x2, y1};
    o.p[2] = {x2, y2};
    vec.push_back(o);
    o.p[2] = {x1, y1};
    vec.push_back(o);
    o.p[1] = {x2, y2};
    vec.push_back(o);
  }
  if (g[x2][y1] == 1) {
    o.p[0] = {x2, y1};
    o.p[1] = {x2, y2};
    o.p[2] = {x1, y2};
    vec.push_back(o);
    o.p[1] = {x1, y1};
    vec.push_back(o);
    o.p[2] = {x2, y2};
    vec.push_back(o);
  }
  if (g[x2][y2] == 1) {
    o.p[0] = {x1, y2};
    o.p[1] = {x1, y1};
    o.p[2] = {x2, y2};
    vec.push_back(o);
    o.p[0] = {x2, y1};
    vec.push_back(o);
    o.p[1] = {x1, y2};
    vec.push_back(o);
  }
}
void two(int x1, int y1, int x2, int y2) {
  if (g[x1][y1] == 1 && g[x1][y2] == 1) {
    o.p[0] = {x1, y1};
    o.p[1] = {x2, y1};
    o.p[2] = {x2, y2};
    vec.push_back(o);
    o.p[0] = {x1, y2};
    vec.push_back(o);
  }
  if (g[x1][y1] == 1 && g[x2][y1] == 1) {
    o.p[0] = {x1, y1};
    o.p[1] = {x1, y2};
    o.p[2] = {x2, y2};
    vec.push_back(o);
    o.p[0] = {x2, y1};
    vec.push_back(o);
  }
  if (g[x1][y1] == 1 && g[x2][y2] == 1) {
    o.p[0] = {x1, y2};
    o.p[1] = {x2, y2};
    o.p[2] = {x2, y1};
    vec.push_back(o);
    o.p[1] = {x1, y1};
    vec.push_back(o);
  }
  if (g[x1][y2] == 1 && g[x2][y1] == 1) {
    o.p[0] = {x1, y1};
    o.p[1] = {x1, y2};
    o.p[2] = {x2, y2};
    vec.push_back(o);
    o.p[1] = {x2, y1};
    vec.push_back(o);
  }
  if (g[x1][y2] == 1 && g[x2][y2] == 1) {
    o.p[0] = {x1, y1};
    o.p[1] = {x2, y1};
    o.p[2] = {x2, y2};
    vec.push_back(o);
    o.p[2] = {x1, y2};
    vec.push_back(o);
  }
  if (g[x2][y1] == 1 && g[x2][y2] == 1) {
    o.p[0] = {x2, y1};
    o.p[1] = {x1, y1};
    o.p[2] = {x1, y2};
    vec.push_back(o);
    o.p[0] = {x2, y2};
    vec.push_back(o);
  }
}
void three(int x1, int y1, int x2, int y2) {
  if (g[x1][y1] == 0) {
    o.p[0] = {x1, y2};
    o.p[1] = {x2, y1};
    o.p[2] = {x2, y2};
    vec.push_back(o);
  }
  if (g[x1][y2] == 0) {
    o.p[0] = {x1, y1};
    o.p[1] = {x2, y1};
    o.p[2] = {x2, y2};
    vec.push_back(o);
  }
  if (g[x2][y1] == 0) {
    o.p[0] = {x1, y2};
    o.p[1] = {x1, y1};
    o.p[2] = {x2, y2};
    vec.push_back(o);
  }
  if (g[x2][y2] == 0) {
    o.p[0] = {x1, y2};
    o.p[1] = {x2, y1};
    o.p[2] = {x1, y1};
    vec.push_back(o);
  }
}
void four(int x1, int y1, int x2, int y2) {
  o.p[0] = {x1, y1};
  o.p[1] = {x1, y2};
  o.p[2] = {x2, y1};
  vec.push_back(o);
  o.p[0] = {x1, y2};
  o.p[1] = {x1, y1};
  o.p[2] = {x2, y2};
  vec.push_back(o);
  o.p[0] = {x2, y1};
  vec.push_back(o);
  o.p[1] = {x1, y2};
  vec.push_back(o);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    vec.clear();
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) scanf("%s", tmp[i] + 1);
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j) g[i][j] = tmp[i][j] - '0';
    if (m % 2 == 1) {
      for (int i = 1; i < n; ++i)
        if (g[i][m] == 1) {
          o.p[0] = {i, m};
          o.p[1] = {i, m - 1};
          o.p[2] = {i + 1, m - 1};
          vec.push_back(o);
          g[i][m] ^= 1;
          g[i][m - 1] ^= 1;
          g[i + 1][m - 1] ^= 1;
        }
      if (g[n][m] == 1) {
        o.p[0] = {n, m};
        o.p[1] = {n, m - 1};
        o.p[2] = {n - 1, m - 1};
        vec.push_back(o);
        g[n][m] ^= 1;
        g[n][m - 1] ^= 1;
        g[n - 1][m - 1] ^= 1;
      }
      m--;
    }
    if (n % 2 == 1) {
      for (int i = 1; i < m; ++i)
        if (g[n][i] == 1) {
          o.p[0] = {n, i};
          o.p[1] = {n - 1, i};
          o.p[2] = {n - 1, i + 1};
          vec.push_back(o);
          g[n][i] ^= 1;
          g[n - 1][i] ^= 1;
          g[n - 1][i + 1] ^= 1;
        }
      if (g[n][m] == 1) {
        o.p[0] = {n, m};
        o.p[1] = {n - 1, m};
        o.p[2] = {n - 1, m - 1};
        vec.push_back(o);
        g[n][m] ^= 1;
        g[n - 1][m] ^= 1;
        g[n - 1][m - 1] ^= 1;
      }
      n--;
    }
    for (int i = 1; i <= n; i += 2)
      for (int j = 1; j <= m; j += 2) {
        if (g[i][j] + g[i][j + 1] + g[i + 1][j] + g[i + 1][j + 1] == 1)
          one(i, j, i + 1, j + 1);
        if (g[i][j] + g[i][j + 1] + g[i + 1][j] + g[i + 1][j + 1] == 2)
          two(i, j, i + 1, j + 1);
        if (g[i][j] + g[i][j + 1] + g[i + 1][j] + g[i + 1][j + 1] == 3)
          three(i, j, i + 1, j + 1);
        if (g[i][j] + g[i][j + 1] + g[i + 1][j] + g[i + 1][j + 1] == 4)
          four(i, j, i + 1, j + 1);
      }
    printf("%d\n", vec.size());
    for (auto p : vec)
      printf("%d %d %d %d %d %d\n", p.p[0].x, p.p[0].y, p.p[1].x, p.p[1].y,
             p.p[2].x, p.p[2].y);
  }
  return 0;
}
