#include <bits/stdc++.h>
using namespace std;
const int maxSum = 1e6;
pair<int, int> ans[maxSum];
pair<int, int> dir[4] = {{1, 1}, {0, 0}, {0, 1}, {1, 0}};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  unsigned short t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    int n, m;
    cin >> n >> m;
    int grid[n][m];
    for (int x = 0; x < n; x++) {
      for (int y = 0; y < m; y++) {
        char tmp;
        cin >> tmp;
        grid[x][y] = (tmp - '0');
      }
    }
    fill(ans, ans + (n * m + 10), make_pair(-1, -1));
    int idx = 0;
    bool change = true;
    while (change) {
      change = false;
      for (int x = 0; x < n - 1; x++) {
        for (int y = 0; y < m - 1; y++) {
          vector<pair<int, int>> zero, one;
          for (int i = 0; i < 4; i++) {
            if (grid[x + dir[i].first][y + dir[i].second])
              one.push_back({x + dir[i].first, y + dir[i].second});
            else
              zero.push_back({x + dir[i].first, y + dir[i].second});
          }
          if (one.size() >= 3) {
            int cnt = 0;
            while (cnt < 3) {
              ans[idx++] = one.back();
              grid[one.back().first][one.back().second] = 0;
              one.pop_back();
              cnt++;
            }
          }
        }
      }
      for (int x = 0; x < n - 1; x++) {
        for (int y = 0; y < m - 1; y++) {
          vector<pair<int, int>> zero, one;
          for (int i = 0; i < 4; i++) {
            if (grid[x + dir[i].first][y + dir[i].second])
              one.push_back({x + dir[i].first, y + dir[i].second});
            else
              zero.push_back({x + dir[i].first, y + dir[i].second});
          }
          if (one.size() == 2) {
            ans[idx++] = one.back();
            grid[one.back().first][one.back().second] = 0;
            one.pop_back();
            while (zero.size() > 0) {
              ans[idx++] = zero.back();
              grid[zero.back().first][zero.back().second] = 1;
              one.push_back(zero.back());
              zero.pop_back();
            }
            while (one.size() > 0) {
              ans[idx++] = one.back();
              grid[one.back().first][one.back().second] = 0;
              one.pop_back();
            }
          }
        }
      }
      for (int x = 0; x < n - 1; x++) {
        for (int y = 0; y < m - 1; y++) {
          vector<pair<int, int>> zero, one;
          for (int i = 0; i < 4; i++) {
            if (grid[x + dir[i].first][y + dir[i].second])
              one.push_back({x + dir[i].first, y + dir[i].second});
            else
              zero.push_back({x + dir[i].first, y + dir[i].second});
          }
          if (one.size() == 1) {
            if (grid[x][y] == 1) {
              grid[x][y] = 0;
              ans[idx++] = {x, y};
              grid[x][y + 1] = 1;
              ans[idx++] = {x, y + 1};
              grid[x + 1][y + 1] = 1;
              ans[idx++] = {x + 1, y + 1};
              change = true;
            } else if (x == n - 2 && grid[x + 1][y] == 1) {
              grid[x + 1][y] = 0;
              ans[idx++] = {x + 1, y};
              grid[x + 1][y + 1] = 1;
              ans[idx++] = {x + 1, y + 1};
              grid[x][y + 1] = 1;
              ans[idx++] = {x, y + 1};
              change = true;
            } else if (y == m - 2 && grid[x][y + 1] == 1) {
              grid[x][y + 1] = 0;
              ans[idx++] = {x, y + 1};
              grid[x + 1][y] = 1;
              ans[idx++] = {x + 1, y};
              grid[x + 1][y + 1] = 1;
              ans[idx++] = {x + 1, y + 1};
              change = true;
            }
          }
        }
      }
    }
    vector<pair<int, int>> zero, one;
    for (int i = 0; i < 4; i++) {
      if (grid[n - 2 + dir[i].first][m - 2 + dir[i].second])
        one.push_back({n - 2 + dir[i].first, m - 2 + dir[i].second});
      else
        zero.push_back({n - 2 + dir[i].first, m - 2 + dir[i].second});
    }
    if (one.size() == 1) {
      pair<int, int> tmp = one.back();
      one.pop_back();
      grid[tmp.first][tmp.second] = 0;
      ans[idx++] = tmp;
      while (zero.size() > 1) {
        ans[idx++] = zero.back();
        grid[zero.back().first][zero.back().second] = 1;
        one.push_back(zero.back());
        zero.pop_back();
      }
      zero.push_back(tmp);
      tmp = one.back();
      one.pop_back();
      grid[tmp.first][tmp.second] = 0;
      ans[idx++] = tmp;
      while (zero.size() > 0) {
        ans[idx++] = zero.back();
        grid[zero.back().first][zero.back().second] = 1;
        one.push_back(zero.back());
        zero.pop_back();
      }
      zero.push_back(tmp);
      while (one.size() > 0) {
        ans[idx++] = one.back();
        grid[one.back().first][one.back().second] = 0;
        zero.push_back(one.back());
        one.pop_back();
      }
    }
    cout << idx / 3 << "\n";
    for (int x = 0; x < idx; x += 3) {
      cout << ans[x].first + 1 << " " << ans[x].second + 1 << " "
           << ans[x + 1].first + 1 << " " << ans[x + 1].second + 1 << " "
           << ans[x + 2].first + 1 << " " << ans[x + 2].second + 1 << "\n";
    }
  }
  return 0;
}
