#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int N = 110;
int n, m;
char tmp[N][N];
int g[N][N];
pair<int, int> a[4];
bool st[4];
vector<pair<int, int> > ans;
void work(pair<int, int> a[]) {
  memset(st, 0, sizeof st);
  int cnt = 0;
  for (int i = 0; i < 4; i++) {
    int x = a[i].first, y = a[i].second;
    cnt += g[x][y];
  }
  if (cnt == 1) {
    int now = 0;
    for (int i = 0; i < 4; i++) {
      int x = a[i].first, y = a[i].second;
      if (now < 2 && !g[x][y]) {
        st[i] = 1;
        ans.push_back({x, y});
        g[x][y] = 1;
        now++;
      }
    }
    for (int i = 0; i < 4; i++) {
      int x = a[i].first, y = a[i].second;
      if (!st[i] && g[x][y]) {
        ans.push_back({x, y});
        g[x][y] = 0;
        break;
      }
    }
    work(a);
  } else if (cnt == 2) {
    for (int i = 0; i < 4; i++) {
      int x = a[i].first, y = a[i].second;
      if (!g[x][y]) {
        st[i] = 1;
        ans.push_back({x, y});
        g[x][y] = 1;
      }
    }
    for (int i = 0; i < 4; i++) {
      int x = a[i].first, y = a[i].second;
      if (!st[i] && g[x][y]) {
        ans.push_back({x, y});
        g[x][y] = 0;
        break;
      }
    }
    work(a);
  } else if (cnt == 3) {
    for (int i = 0; i < 4; i++) {
      int x = a[i].first, y = a[i].second;
      if (g[x][y]) {
        ans.push_back({x, y});
        g[x][y] = 0;
      }
    }
  } else if (cnt == 4) {
    for (int i = 0; i < 3; i++) {
      int x = a[i].first, y = a[i].second;
      if (g[x][y]) {
        ans.push_back({x, y});
        g[x][y] = 0;
      }
    }
    work(a);
  } else
    return;
}
void print() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) cout << g[i][j] << ' ';
    cout << endl;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie();
  cout.tie(0);
  int T = 1;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> 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 (n % 2 == 0 && m % 2 == 0) {
      for (int i = 1; i <= n; i += 2)
        for (int j = 1; j <= m; j += 2) {
          a[0] = {i, j}, a[1] = {i + 1, j}, a[2] = {i, j + 1},
          a[3] = {i + 1, j + 1};
          work(a);
        }
    } else if (n % 2 == 0 && m % 2 != 0) {
      for (int i = 1; i <= n; i += 2) {
        if (g[i][1] == 0 && g[i + 1][1] == 0)
          continue;
        else if (g[i][1] == 1 && g[i + 1][1] == 0) {
          ans.push_back({i, 1});
          g[i][1] = 1 - g[i][1];
          ans.push_back({i, 2});
          g[i][2] = 1 - g[i][2];
          ans.push_back({i + 1, 2});
          g[i + 1][2] = 1 - g[i + 1][2];
        } else if (g[i][1] == 0 && g[i + 1][1] == 1) {
          ans.push_back({i + 1, 1});
          g[i + 1][1] = 1 - g[i + 1][1];
          ans.push_back({i, 2});
          g[i][2] = 1 - g[i][2];
          ans.push_back({i + 1, 2});
          g[i + 1][2] = 1 - g[i + 1][2];
        } else {
          ans.push_back({i, 1});
          g[i][1] = 1 - g[i][1];
          ans.push_back({i + 1, 1});
          g[i + 1][1] = 1 - g[i + 1][1];
          ans.push_back({i + 1, 2});
          g[i + 1][2] = 1 - g[i + 1][2];
        }
      }
      for (int i = 1; i <= n; i += 2)
        for (int j = 2; j <= m; j += 2) {
          a[0] = {i, j}, a[1] = {i + 1, j}, a[2] = {i, j + 1},
          a[3] = {i + 1, j + 1};
          work(a);
        }
    } else if (n % 2 != 0 && m % 2 == 0) {
      for (int j = 1; j <= m; j += 2) {
        if (g[1][j] == 0 && g[1][j + 1] == 0)
          continue;
        else if (g[1][j] == 1 && g[1][j + 1] == 0) {
          ans.push_back({1, j});
          g[1][j] = 1 - g[1][j];
          ans.push_back({2, j});
          g[2][j] = 1 - g[2][j];
          ans.push_back({2, j + 1});
          g[2][j + 1] = 1 - g[2][j + 1];
        } else if (g[1][j] == 0 && g[1][j + 1] == 1) {
          ans.push_back({1, j + 1});
          g[1][j + 1] = 1 - g[1][j + 1];
          ans.push_back({2, j});
          g[2][j] = 1 - g[2][j];
          ans.push_back({2, j + 1});
          g[2][j + 1] = 1 - g[2][j + 1];
        } else {
          ans.push_back({1, j});
          g[1][j] = 1 - g[1][j];
          ans.push_back({1, j + 1});
          g[1][j + 1] = 1 - g[1][j + 1];
          ans.push_back({2, j});
          g[2][j] = 1 - g[2][j];
        }
      }
      for (int i = 2; i <= n; i += 2)
        for (int j = 1; j <= m; j += 2) {
          a[0] = {i, j}, a[1] = {i + 1, j}, a[2] = {i, j + 1},
          a[3] = {i + 1, j + 1};
          work(a);
        }
    } else {
      for (int i = 1; i <= n; i += 2) {
        int x = i + 1;
        if (x > n) x -= 2;
        if (g[i][1] == 0 && g[x][1] == 0)
          continue;
        else if (g[i][1] == 1 && g[x][1] == 0) {
          ans.push_back({i, 1});
          g[i][1] = 1 - g[i][1];
          ans.push_back({i, 2});
          g[i][2] = 1 - g[i][2];
          ans.push_back({x, 2});
          g[x][2] = 1 - g[x][2];
        } else if (g[i][1] == 0 && g[x][1] == 1) {
          ans.push_back({x, 1});
          g[x][1] = 1 - g[x][1];
          ans.push_back({i, 2});
          g[i][2] = 1 - g[i][2];
          ans.push_back({x, 2});
          g[x][2] = 1 - g[x][2];
        } else {
          ans.push_back({x, 1});
          g[x][1] = 1 - g[x][1];
          ans.push_back({i, 1});
          g[i][1] = 1 - g[i][1];
          ans.push_back({x, 2});
          g[x][2] = 1 - g[x][2];
        }
      }
      for (int j = 2; j <= m; j += 2) {
        int y = j + 1;
        if (y > m) y -= 2;
        if (g[1][j] == 0 && g[1][y] == 0)
          continue;
        else if (g[1][j] == 1 && g[1][y] == 0) {
          ans.push_back({1, j});
          g[1][j] = 1 - g[1][j];
          ans.push_back({2, j});
          g[2][j] = 1 - g[2][j];
          ans.push_back({2, y});
          g[2][y] = 1 - g[2][y];
        } else if (g[1][j] == 0 && g[1][y] == 1) {
          ans.push_back({1, y});
          g[1][y] = 1 - g[1][y];
          ans.push_back({2, j});
          g[2][j] = 1 - g[2][j];
          ans.push_back({2, y});
          g[2][y] = 1 - g[2][y];
        } else {
          ans.push_back({1, j});
          g[1][j] = 1 - g[1][j];
          ans.push_back({2, j});
          g[2][j] = 1 - g[2][j];
          ans.push_back({1, y});
          g[1][y] = 1 - g[1][y];
        }
      }
      for (int i = 2; i <= n; i += 2)
        for (int j = 2; j <= m; j += 2) {
          a[0] = {i, j}, a[1] = {i + 1, j}, a[2] = {i, j + 1},
          a[3] = {i + 1, j + 1};
          work(a);
        }
    }
    cout << ans.size() / 3 << '\n';
    int cnt = 0;
    for (auto t : ans) {
      cnt++;
      cout << t.first << ' ' << t.second << ' ';
      if (cnt % 3 == 0) cout << '\n';
    }
    ans.clear();
  }
  return 0;
}
