#include <bits/stdc++.h>
using namespace std;
const long long maxn = 230 + 200 + 10;
const long long maxk = 1e4 + 10;
const long long mod = 1e9 + 7;
vector<string> s;
vector<pair<long long, long long>> points;
void invert(long long x, long long y) {
  s[x][y] = 1 - s[x][y];
  points.emplace_back(x, y);
}
void solve(long long i, long long j) {
  long long cnt = 0;
  vector<pair<long long, long long>> ones;
  vector<pair<long long, long long>> zeros;
  for (long long x = i; x < i + 2; ++x) {
    for (long long y = j; y < j + 2; ++y) {
      if (s[x][y] == 1) {
        ones.push_back({x, y});
        ++cnt;
      } else {
        zeros.push_back({x, y});
      }
    }
  }
  if (cnt == 0) {
    return;
  }
  if (cnt == 3) {
    for (auto [x, y] : ones) {
      invert(x, y);
    }
    return;
  }
  if (cnt == 2) {
    invert(ones[0].first, ones[0].second);
    for (auto [x, y] : zeros) {
      invert(x, y);
    }
    solve(i, j);
  }
  if (cnt == 1) {
    invert(ones[0].first, ones[0].second);
    zeros.resize(2);
    for (auto [x, y] : zeros) {
      invert(x, y);
    }
    solve(i, j);
  }
  if (cnt == 4) {
    ones.resize(3);
    for (auto [x, y] : ones) {
      invert(x, y);
    }
    solve(i, j);
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t-- > 0) {
    long long n, m;
    cin >> n >> m;
    s.assign(n, "");
    for (long long i = 0; i < n; ++i) {
      cin >> s[i];
      for (long long j = 0; j < m; ++j) {
        s[i][j] -= '0';
      }
    }
    points.clear();
    if (n % 2 == 1 && m % 2 == 1) {
      if (s[n - 1][m - 1] != 0) {
        invert(n - 1, m - 1);
        invert(n - 1, m - 2);
        invert(n - 2, m - 1);
      }
    }
    if (n % 2 == 1) {
      for (long long i = 0; i < m / 2 * 2; i += 2) {
        if (s[n - 1][i] == 1 && s[n - 1][i + 1] == 0) {
          invert(n - 1, i);
          invert(n - 2, i);
          invert(n - 2, i + 1);
        }
        if (s[n - 1][i] == 0 && s[n - 1][i + 1] == 1) {
          invert(n - 1, i + 1);
          invert(n - 2, i);
          invert(n - 2, i + 1);
        }
        if (s[n - 1][i] == 1 && s[n - 1][i + 1] == 1) {
          invert(n - 1, i + 1);
          invert(n - 1, i);
          invert(n - 2, i + 1);
        }
      }
    }
    if (m % 2 == 1) {
      for (long long i = 0; i < n / 2 * 2; i += 2) {
        if (s[i][m - 1] == 1 && s[i + 1][m - 1] == 0) {
          invert(i, m - 1);
          invert(i, m - 2);
          invert(i + 1, m - 2);
        }
        if (s[i][m - 1] == 0 && s[i + 1][m - 1] == 1) {
          invert(i + 1, m - 1);
          invert(i, m - 2);
          invert(i + 1, m - 2);
        }
        if (s[i][m - 1] == 1 && s[i + 1][m - 1] == 1) {
          invert(i + 1, m - 1);
          invert(i, m - 1);
          invert(i + 1, m - 2);
        }
      }
    }
    for (long long i = 0; i < n / 2 * 2; i += 2) {
      for (long long j = 0; j < m / 2 * 2; j += 2) {
        solve(i, j);
      }
    }
    cout << points.size() / 3 << "\n";
    for (auto [x, y] : points) {
      cout << x + 1 << " " << y + 1 << "\n";
    }
  }
}
