#include <bits/stdc++.h>
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
template <class T>
using vec = std::vector<T>;
template <class T>
using vec2d = std::vector<std::vector<T>>;
template <class T>
vec2d<T> makeVec2d(const int n1, const int n2) {
  return vec2d<T>(n1, vec<T>(n2));
}
bool __hack = std::ios::sync_with_stdio(false);
auto __hack1 = cin.tie(nullptr);
template <class T>
void addOne(vec<T> &v) {
  for_each(v.begin(), v.end(), [](T &el) { ++el; });
}
struct Input {
  Input(istream &in) : in(&in) {}
  template <class T>
  T next() const {
    T x;
    *in >> x;
    return x;
  }
  int ni() const { return next<int>(); }
  string ns() const { return next<string>(); }
  istream *in;
};
Input in(cin);
class Output {
 private:
  ostream *out;
  template <typename T>
  void printSingle(const T &value) {
    *out << value;
  }
  template <typename T>
  void printSingle(const vec<T> &v) {
    for (std::size_t i = 0; i < v.size(); ++i) {
      if (i == 0) {
        printSingle(v[i]);
      } else {
        *out << ' ';
        printSingle(v[i]);
      }
    }
  }

 public:
  Output(ostream &out) : out(&out) {}
  inline void print() {}
  template <typename T, typename... Ts>
  inline void print(const T &f, const Ts &...args) {
    printSingle(f);
    if (sizeof...(args) != 0) {
      *out << ' ';
      print(args...);
    }
  }
  template <typename... Ts>
  inline void println(const Ts &...args) {
    print(args...);
    (*out) << '\n';
  }
  template <typename... Ts>
  inline void operator()(const Ts &...args) {
    println(args...);
  }
};
Output out(cout);
namespace template_util {
constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; }
template <int N>
struct bytetype {};
template <uint64_t N>
struct minimal_uint : bytetype<bytecount(N)> {};
}  // namespace template_util
void solveOne() {
  auto n = in.ni();
  auto m = in.ni();
  vec2d<int> a = makeVec2d<int>(n, m);
  for (int i = 0; i < (n); ++i) {
    string s = in.ns();
    for (int j = 0; j < (m); ++j) {
      a[i][j] = s[j] - '0';
    }
  }
  vec2d<int> res;
  auto doOper1 = [&](int x, int y) {
    a[x][y] ^= 1;
    a[x + 1][y] ^= 1;
    a[x][y + 1] ^= 1;
    res.push_back({x, y, x + 1, y, x, y + 1});
  };
  auto doOper2 = [&](int x, int y) {
    a[x][y] ^= 1;
    a[x + 1][y] ^= 1;
    a[x + 1][y - 1] ^= 1;
    res.push_back({x, y, x + 1, y, x + 1, y - 1});
  };
  for (int i = 0; i < (n - 2); ++i) {
    for (int j = 0; j < (m - 1); ++j) {
      if (a[i][j] == 1) {
        doOper1(i, j);
      }
    }
    if (a[i][m - 1] == 1) {
      doOper2(i, m - 1);
    }
  }
  int r1 = n - 2, r2 = n - 1;
  for (int j = 0; j < (m - 2); ++j) {
    if (a[r1][j] == a[r2][j]) {
      if (a[r1][j] == 1) {
        a[r1][j] ^= 1;
        a[r2][j] ^= 1;
        a[r1][j + 1] ^= 1;
        res.push_back({r1, j, r2, j, r1, j + 1});
      }
    } else if (a[r1][j] == 1 && a[r2][j] == 0) {
      a[r1][j] ^= 1;
      a[r2][j + 1] ^= 1;
      a[r1][j + 1] ^= 1;
      res.push_back({r1, j, r2, j + 1, r1, j + 1});
    } else if (a[r1][j] == 0 && a[r2][j] == 1) {
      a[r2][j] ^= 1;
      a[r2][j + 1] ^= 1;
      a[r1][j + 1] ^= 1;
      res.push_back({r2, j, r2, j + 1, r1, j + 1});
    }
  }
  int c1 = m - 2, c2 = m - 1;
  int cnt = 0;
  for (int i = (r1); i <= (r2); ++i) {
    for (int j = (c1); j <= (c2); ++j) {
      cnt += a[i][j];
    }
  }
  while (true) {
    cnt = 0;
    for (int i = (r1); i <= (r2); ++i) {
      for (int j = (c1); j <= (c2); ++j) {
        cnt += a[i][j];
      }
    }
    if (cnt == 0) {
      out(res.size());
      for (auto v : res) {
        addOne(v);
        out(v);
      }
      return;
    }
    if (cnt == 1) {
      vec<int> add;
      int cnt0 = 0;
      for (int i = (r1); i <= (r2); ++i) {
        for (int j = (c1); j <= (c2); ++j) {
          if (cnt0 < 2 && a[i][j] == 0) {
            ++cnt0;
            add.push_back(i);
            add.push_back(j);
            a[i][j] ^= 1;
            continue;
          }
          if (a[i][j] == 1) {
            add.push_back(i);
            add.push_back(j);
            a[i][j] ^= 1;
            continue;
          }
        }
      }
      res.push_back(add);
      continue;
    }
    if (cnt == 2) {
      vec<int> add;
      int cnt1 = 0;
      for (int i = (r1); i <= (r2); ++i) {
        for (int j = (c1); j <= (c2); ++j) {
          if (a[i][j] == 0) {
            add.push_back(i);
            add.push_back(j);
            a[i][j] ^= 1;
            continue;
          }
          if (cnt1 == 0 && a[i][j] == 1) {
            add.push_back(i);
            add.push_back(j);
            a[i][j] ^= 1;
            ++cnt1;
            continue;
          }
        }
      }
      res.push_back(add);
      continue;
    }
    if (cnt == 3) {
      vec<int> add;
      for (int i = (r1); i <= (r2); ++i) {
        for (int j = (c1); j <= (c2); ++j) {
          if (a[i][j] == 1) {
            add.push_back(i);
            add.push_back(j);
            a[i][j] ^= 1;
          }
        }
      }
      res.push_back(add);
      continue;
    }
    if (cnt == 4) {
      vec<int> add;
      int cnt1 = 0;
      for (int i = (r1); i <= (r2); ++i) {
        for (int j = (c1); j <= (c2); ++j) {
          if (cnt1 < 3 && a[i][j] == 1) {
            add.push_back(i);
            add.push_back(j);
            a[i][j] ^= 1;
            ++cnt1;
          }
        }
      }
      res.push_back(add);
      continue;
    }
  }
}
void solve(istream &inStream, ostream &outStream) {
  in = Input(inStream);
  out = Output(outStream);
  auto tests = in.ni();
  while (tests--) {
    solveOne();
  }
}
int main() {
  solve(cin, cout);
  return 0;
}
