Problem: This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.

You are given a binary table of size n × m. This table consists of symbols 0 and 1.

You can make such operation: select 3 different cells that belong to one 2 × 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0).

Your task is to make all symbols in the table equal to 0. You are allowed to make at most nm operations. You don't need to minimize the number of operations.

It can be proved, that it is always possible.

Input

The first line contains a single integer t (1 ≤ t ≤ 5000) — the number of test cases. The next lines contain descriptions of test cases.

The first line of the description of each test case contains two integers n, m (2 ≤ n, m ≤ 100).

Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table.

It is guaranteed, that the sum of nm for all test cases does not exceed 20000.

Output

For each test case print the integer k (0 ≤ k ≤ nm) — the number of operations.

In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≤ x_1, x_2, x_3 ≤ n, 1 ≤ y_1, y_2, y_3 ≤ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong to some 2 × 2 square.

Example

Input


5
2 2
10
11
3 3
011
101
110
4 4
1111
0110
0110
1111
5 5
01011
11001
00010
11011
10000
2 3
011
101


Output


1
1 1 2 1 2 2
2 
2 1 3 1 3 2
1 2 1 3 2 3
4
1 1 1 2 2 2 
1 3 1 4 2 3
3 2 4 1 4 2
3 3 4 3 4 4
4
1 2 2 1 2 2 
1 4 1 5 2 5 
4 1 4 2 5 1
4 4 4 5 3 4
2
1 3 2 2 2 3
1 2 2 1 2 2

Note

In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0.

In the second test case:

  * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 
    
          
    011  
    001  
    000  
    

  * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 
    
          
    000  
    000  
    000  
    




In the fifth test case:

  * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 
    
          
    010  
    110  
    

  * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 
    
          
    000  
    000  
    

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int inf;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T, class T2>
int chkmin(T &a, T2 b) {
  return a > b ? a = b, 1 : 0;
}
template <class T, class T2>
int chkmax(T &a, T2 b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T, class T2>
T mmin(T a, T2 b) {
  return a < b ? a : b;
}
template <class T, class T2>
T mmax(T a, T2 b) {
  return a > b ? a : b;
}
template <class T>
T aabs(T a) {
  return a < 0 ? -a : a;
}
template <class T>
int dcmp(T a, T b) {
  return a > b;
}
template <int *a>
int cmp_a(int first, int second) {
  return a[first] < a[second];
}
template <class T>
bool sort2(T &a, T &b) {
  return a > b ? swap(a, b), 1 : 0;
}
struct __INIT__ {
  __INIT__() {
    fill((unsigned char *)&inf, (unsigned char *)&inf + sizeof(inf), 0x3f);
  }
} __INIT___;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
int f, qr;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char first) {
  *oS++ = first;
  if (oS == oT) flush();
}
template <typename A>
inline bool read(A &first) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (first = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    first = first * 10 + (c & 15);
  first *= f;
  return 1;
}
inline bool read(char &first) {
  while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                               (iS == iT ? EOF : *iS++))
                            : *iS++)) == ' ' ||
         first == '\n' || first == '\r')
    ;
  return first != EOF;
}
inline bool read(char *first) {
  while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                                (iS == iT ? EOF : *iS++))
                             : *iS++)) == '\n' ||
         *first == ' ' || *first == '\r')
    ;
  if (*first == EOF) return 0;
  while (!(*first == '\n' || *first == ' ' || *first == '\r' || *first == EOF))
    *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                           : *iS++);
  *first = 0;
  return 1;
}
template <typename A, typename... B>
inline bool read(A &first, B &...second) {
  return read(first) && read(second...);
}
template <typename A>
inline bool write(A first) {
  if (!first) putc('0');
  if (first < 0) putc('-'), first = -first;
  while (first) qu[++qr] = first % 10 + '0', first /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(char first) {
  putc(first);
  return 0;
}
inline bool write(const char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
inline bool write(char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
template <typename A, typename... B>
inline bool write(A first, B... second) {
  return write(first) || write(second...);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
}  // namespace io
using io ::putc;
using io ::read;
using io ::write;
const int w[16][3] = {{0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3},
                      {2, 3, 4}, {2, 3, 5}, {2, 4, 5}, {3, 4, 5},
                      {6, 1, 7}, {6, 1, 3}, {6, 7, 3}, {1, 7, 3},
                      {7, 3, 8}, {7, 3, 5}, {7, 8, 5}, {3, 8, 5}};
int mn[1 << 6 | 5];
int ck[1 << 6 | 5];
int mn2[1 << 4 | 5];
int ck2[1 << 4 | 5];
int mn3[1 << 9 | 5];
int ck3[1 << 9 | 5];
int a[105][105];
vector<vector<pair<int, int> > > ans;
void mk2(vector<pair<int, int> > vt) {
  int v = 0;
  for (int i = 0; i < 4; ++i) v |= a[vt[i].first][vt[i].second] << i;
  for (int i = 0; i < 4; ++i)
    if (ck2[v] & (1 << i)) {
      ans.emplace_back(
          vector<pair<int, int> >{vt[w[i][0]], vt[w[i][1]], vt[w[i][2]]});
    }
  for (auto i : vt) a[i.first][i.second] = 0;
}
void mk3(vector<pair<int, int> > vt) {
  int v = 0;
  for (int i = 0; i < 6; ++i) v |= a[vt[i].first][vt[i].second] << i;
  for (int i = 0; i < 8; ++i)
    if (ck[v] & (1 << i)) {
      ans.emplace_back(
          vector<pair<int, int> >{vt[w[i][0]], vt[w[i][1]], vt[w[i][2]]});
    }
  for (auto i : vt) a[i.first][i.second] = 0;
}
void mk33(vector<pair<int, int> > vt) {
  int v = 0;
  for (int i = 0; i < 9; ++i) v |= a[vt[i].first][vt[i].second] << i;
  for (int i = 0; i < 16; ++i)
    if (ck3[v] & (1 << i)) {
      ans.emplace_back(
          vector<pair<int, int> >{vt[w[i][0]], vt[w[i][1]], vt[w[i][2]]});
    }
  for (auto i : vt) a[i.first][i.second] = 0;
}
char s[105];
signed main() {
  for (int i = 0; i < (1 << 6); ++i) mn[i] = inf;
  for (int i = 0; i < (1 << 9); ++i) mn3[i] = inf;
  for (int i = 0; i < (1 << 4); ++i) mn2[i] = inf;
  for (int i = 0; i < (1 << 16); ++i) {
    int s = 0;
    for (int j = 0; j < 16; ++j)
      if (i & (1 << j)) {
        for (int k = 0; k < 3; ++k) s ^= 1 << w[j][k];
      }
    if (chkmin(mn3[s], __builtin_popcount(i))) ck3[s] = i;
  }
  for (int i = 0; i < (1 << 8); ++i) {
    int s = 0;
    for (int j = 0; j < 8; ++j)
      if (i & (1 << j)) {
        for (int k = 0; k < 3; ++k) s ^= 1 << w[j][k];
      }
    if (chkmin(mn[s], __builtin_popcount(i))) ck[s] = i;
  }
  for (int i = 0; i < (1 << 4); ++i) {
    int s = 0;
    for (int j = 0; j < 4; ++j)
      if (i & (1 << j)) {
        for (int k = 0; k < 3; ++k) s ^= 1 << w[j][k];
      }
    if (chkmin(mn2[s], __builtin_popcount(i))) ck2[s] = i;
  }
  int t;
  read(t);
  for (; t; --t) {
    int n, m;
    read(n, m);
    for (int i = 1; i <= n; ++i) {
      read(s + 1);
      for (int j = 1; j <= m; ++j) a[i][j] = s[j] - '0';
    }
    ans.clear();
    for (int i = n; i > 1; i -= 2) {
      if (i != 3)
        for (int j = m; j > 1; j -= 2) {
          if (j == 3) {
            mk3({{i, j},
                 {i - 1, j},
                 {i, j - 1},
                 {i - 1, j - 1},
                 {i, j - 2},
                 {i - 1, j - 2}});
          } else {
            mk2({{i, j}, {i - 1, j}, {i, j - 1}, {i - 1, j - 1}});
          }
        }
      else
        for (int j = m; j > 1; j -= 2) {
          if (j == 3) {
            mk33({{i, j},
                  {i - 1, j},
                  {i, j - 1},
                  {i - 1, j - 1},
                  {i, j - 2},
                  {i - 1, j - 2},
                  {i - 2, j},
                  {i - 2, j - 1},
                  {i - 2, j - 2}});
          } else {
            mk3({{i, j},
                 {i, j - 1},
                 {i - 1, j},
                 {i - 1, j - 1},
                 {i - 2, j},
                 {i - 2, j - 1}});
          }
        }
    }
    write(ans.size(), '\n');
    for (auto i : ans) {
      write(i[0].first, ' ', i[0].second, ' ', i[1].first, ' ', i[1].second,
            ' ', i[2].first, ' ', i[2].second, '\n');
    }
  }
  return 0;
}