#include <bits/stdc++.h>
using namespace std;
typedef struct node {
  int x1, y1, x2, y2, x3, y3;
} node;
int t;
int g[105][105];
node l[10050];
int tmp = 0;
void dfs(int x, int y, int w) {
  if (w == 3) {
    tmp++;
    int o = 0;
    if (g[x][y]) {
      if (o == 0) {
        o++;
        g[x][y] = 0;
        l[tmp].x1 = x;
        l[tmp].y1 = y;
      } else if (o == 1) {
        o++;
        g[x][y] = 0;
        l[tmp].x2 = x;
        l[tmp].y2 = y;
      } else if (o == 2) {
        o++;
        g[x][y] = 0;
        l[tmp].x3 = x;
        l[tmp].y3 = y;
      }
    }
    if (g[x + 1][y]) {
      if (o == 0) {
        o++;
        g[x + 1][y] = 0;
        l[tmp].x1 = x + 1;
        l[tmp].y1 = y;
      } else if (o == 1) {
        o++;
        g[x + 1][y] = 0;
        l[tmp].x2 = x + 1;
        l[tmp].y2 = y;
      } else if (o == 2) {
        o++;
        g[x + 1][y] = 0;
        l[tmp].x3 = x + 1;
        l[tmp].y3 = y;
      }
    }
    if (g[x][y + 1]) {
      if (o == 0) {
        o++;
        g[x][y + 1] = 0;
        l[tmp].x1 = x;
        l[tmp].y1 = y + 1;
      } else if (o == 1) {
        o++;
        g[x][y + 1] = 0;
        l[tmp].x2 = x;
        l[tmp].y2 = y + 1;
      } else if (o == 2) {
        o++;
        g[x][y + 1] = 0;
        l[tmp].x3 = x;
        l[tmp].y3 = y + 1;
      }
    }
    if (g[x + 1][y + 1]) {
      if (o == 0) {
        o++;
        g[x + 1][y + 1] = 0;
        l[tmp].x1 = x + 1;
        l[tmp].y1 = y + 1;
      } else if (o == 1) {
        o++;
        g[x + 1][y + 1] = 0;
        l[tmp].x2 = x + 1;
        l[tmp].y2 = y + 1;
      } else if (o == 2) {
        o++;
        g[x + 1][y + 1] = 0;
        l[tmp].x3 = x + 1;
        l[tmp].y3 = y + 1;
      }
    }
    return;
  } else {
    if (g[x][y] + g[x + 1][y] + g[x][y + 1] == 1) {
      g[x][y] ^= 1;
      g[x + 1][y] ^= 1;
      g[x][y + 1] ^= 1;
      l[++tmp].x1 = x;
      l[tmp].y1 = y;
      l[tmp].x2 = x + 1;
      l[tmp].y2 = y;
      l[tmp].x3 = x;
      l[tmp].y3 = y + 1;
      dfs(x, y, w + 1);
      return;
    } else if (g[x][y] + g[x + 1][y + 1] + g[x][y + 1] == 1) {
      g[x][y] ^= 1;
      g[x + 1][y + 1] ^= 1;
      g[x][y + 1] ^= 1;
      l[++tmp].x1 = x;
      l[tmp].y1 = y;
      l[tmp].x2 = x + 1;
      l[tmp].y2 = y + 1;
      l[tmp].x3 = x;
      l[tmp].y3 = y + 1;
      dfs(x, y, w + 1);
      return;
    } else if (g[x + 1][y] + g[x + 1][y + 1] + g[x][y + 1] == 1) {
      g[x + 1][y] ^= 1;
      g[x + 1][y + 1] ^= 1;
      g[x][y + 1] ^= 1;
      l[++tmp].x1 = x + 1;
      l[tmp].y1 = y;
      l[tmp].x2 = x + 1;
      l[tmp].y2 = y + 1;
      l[tmp].x3 = x;
      l[tmp].y3 = y + 1;
      dfs(x, y, w + 1);
      return;
    } else if (g[x][y] + g[x + 1][y] + g[x + 1][y + 1] == 1) {
      g[x][y] ^= 1;
      g[x + 1][y] ^= 1;
      g[x + 1][y + 1] ^= 1;
      l[++tmp].x1 = x;
      l[tmp].y1 = y;
      l[tmp].x2 = x + 1;
      l[tmp].y2 = y;
      l[tmp].x3 = x + 1;
      l[tmp].y3 = y + 1;
      dfs(x, y, w + 1);
      return;
    }
  }
}
int main() {
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int nn = n, mm = m;
    tmp = 0;
    for (int i = 1; i <= n; i++) {
      string s;
      cin >> s;
      for (int j = 1; j <= m; j++) {
        g[i][j] = s[j - 1] - '0';
      }
    }
    if (n % 2 == 1) {
      for (int i = 1; i <= m; i++) {
        if (g[n][i]) {
          if (i != m) {
            g[n][i] = 0;
            g[n - 1][i] ^= 1;
            g[n - 1][i + 1] ^= 1;
            l[++tmp].x1 = n;
            l[tmp].y1 = i;
            l[tmp].x2 = n - 1;
            l[tmp].y2 = i;
            l[tmp].x3 = n - 1;
            l[tmp].y3 = i + 1;
          } else {
            g[n][i] = 0;
            g[n - 1][i] ^= 1;
            g[n - 1][i - 1] ^= 1;
            l[++tmp].x1 = n;
            l[tmp].y1 = i;
            l[tmp].x2 = n - 1;
            l[tmp].y2 = i;
            l[tmp].x3 = n - 1;
            l[tmp].y3 = i - 1;
          }
        }
      }
      n--;
    }
    if (m % 2 == 1) {
      for (int i = 1; i <= n; i++) {
        if (g[i][m]) {
          if (i != n) {
            g[i][m] = 0;
            g[i][m - 1] ^= 1;
            g[i + 1][m - 1] ^= 1;
            l[++tmp].x1 = i;
            l[tmp].y1 = m;
            l[tmp].x2 = i;
            l[tmp].y2 = m - 1;
            l[tmp].x3 = i + 1;
            l[tmp].y3 = m - 1;
          } else {
            g[i][m] = 0;
            g[i][m - 1] ^= 1;
            g[i - 1][m - 1] ^= 1;
            l[++tmp].x1 = i;
            l[tmp].y1 = m;
            l[tmp].x2 = i;
            l[tmp].y2 = m - 1;
            l[tmp].x3 = i - 1;
            l[tmp].y3 = m - 1;
          }
        }
      }
      m--;
    }
    for (int i = 1; i <= n; i += 2) {
      for (int j = 1; j <= m; j += 2) {
        int xx = i;
        int yy = j;
        int sum = g[xx][yy] + g[xx + 1][yy] + g[xx][yy + 1] + g[xx + 1][yy + 1];
        if (!sum)
          continue;
        else if (sum == 4) {
          g[xx][yy] = 0;
          g[xx + 1][yy] = 0;
          g[xx][yy + 1] = 0;
          l[++tmp].x1 = xx;
          l[tmp].y1 = yy;
          l[tmp].x2 = xx + 1;
          l[tmp].y2 = yy;
          l[tmp].x3 = xx;
          l[tmp].y3 = yy + 1;
          dfs(xx, yy, 1);
        } else {
          dfs(xx, yy, sum);
        }
      }
    }
    cout << tmp << endl;
    for (int i = 1; i <= tmp; i++) {
      cout << l[i].x1 << " ";
      cout << l[i].y1 << " ";
      cout << l[i].x2 << " ";
      cout << l[i].y2 << " ";
      cout << l[i].x3 << " ";
      cout << l[i].y3 << " ";
      cout << endl;
    }
  }
  return 0;
}
