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;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  ~debug() { cerr << endl; }
  template <class c>
  typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class c>
  typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) {
    return *this << range(begin(i), end(i));
  }
  template <class c, class b>
  debug &operator<<(pair<b, c> d) {
    return *this << "(" << d.first << ", " << d.second << ")";
  }
  template <class c>
  debug &operator<<(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
};
const long double eps = 1e-9;
const int T = 1e5;
const int M = 1e9 + 7;
const long long inf = 1e18;
auto rnd = bind(uniform_int_distribution<int>(1, 5), mt19937(time(0)));
const int N = 2e5 + 1;
map<multiset<int>, int> mp;
map<multiset<int>, multiset<int>> p;
void brute(multiset<int> st) {
  if (*st.rbegin() == 0) {
    mp[st] = 0;
    return;
  }
  if (mp.find(st) != mp.end()) return;
  vector<int> a;
  for (auto &z : st) a.push_back(z);
  mp[st] = 2e9;
  for (int i = 0; i < 4; i++) {
    for (int j = i + 1; j < 4; j++) {
      for (int k = j + 1; k < 4; k++) {
        a[i] ^= 1;
        a[j] ^= 1;
        a[k] ^= 1;
        multiset<int> tet;
        for (auto &z : a) tet.insert(z);
        brute(tet);
        if (mp[tet] != 2e9) {
          if (mp[tet] < mp[st] - 1) {
            mp[st] = min(mp[st], mp[tet] + 1);
            p[st] = tet;
          }
        }
        a[i] ^= 1;
        a[j] ^= 1;
        a[k] ^= 1;
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ;
  int t;
  cin >> t;
  int cs = 0;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<char>> a(n, vector<char>(m));
    vector<vector<int>> b(n, vector<int>(m));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> a[i][j];
        b[i][j] = a[i][j] - '0';
      }
    }
    vector<vector<pair<int, int>>> ans;
    if (n & 1) {
      if (b[n - 1][0] == 1) {
        vector<pair<int, int>> second;
        second.push_back({n - 1, 0});
        second.push_back({n - 2, 1});
        second.push_back({n - 2, 0});
        b[n - 2][0] ^= 1;
        b[n - 2][1] ^= 1;
        b[n - 1][0] ^= 1;
        ans.push_back(second);
      }
      for (int j = 1; j < m; j++) {
        if (b[n - 1][j] == 0) continue;
        vector<pair<int, int>> second;
        second.push_back({n - 1, j});
        second.push_back({n - 2, j - 1});
        second.push_back({n - 2, j});
        b[n - 2][j - 1] ^= 1;
        b[n - 2][j] ^= 1;
        b[n - 1][j] ^= 1;
        ans.push_back(second);
      }
    }
    if (m & 1) {
      if (b[0][m - 1] == 1) {
        vector<pair<int, int>> second;
        second.push_back({0, m - 1});
        second.push_back({1, m - 2});
        second.push_back({0, m - 2});
        b[0][m - 1] ^= 1;
        b[1][m - 2] ^= 1;
        b[0][m - 2] ^= 1;
        ans.push_back(second);
      }
      for (int i = 1; i < n - (n % 2); i++) {
        if (b[i][m - 1] == 0) continue;
        vector<pair<int, int>> second;
        second.push_back({i, m - 1});
        second.push_back({i, m - 2});
        second.push_back({i - 1, m - 2});
        b[i - 1][m - 2] ^= 1;
        b[i][m - 2] ^= 1;
        b[i][m - 1] ^= 1;
        ans.push_back(second);
      }
    }
    map<pair<int, int>, int> us;
    for (int i = 0; i < n - 1; i += 2) {
      for (int j = 0; j < m - 1; j += 2) {
        vector<pair<int, pair<int, int>>> w;
        w.push_back({b[i][j], {i, j}});
        w.push_back({b[i][j + 1], {i, j + 1}});
        w.push_back({b[i + 1][j], {i + 1, j}});
        w.push_back({b[i + 1][j + 1], {i + 1, j + 1}});
        sort(w.begin(), w.end());
        if (w[0].first == 1 && w[1].first == 1 && w[2].first == 1 &&
            w[3].first == 1) {
          vector<pair<int, int>> second;
          second.push_back(w[0].second);
          second.push_back(w[1].second);
          second.push_back(w[2].second);
          ans.push_back(second);
          second.clear();
          second.push_back(w[3].second);
          second.push_back(w[1].second);
          second.push_back(w[2].second);
          ans.push_back(second);
          second.clear();
          second.push_back(w[0].second);
          second.push_back(w[1].second);
          second.push_back(w[3].second);
          ans.push_back(second);
          second.clear();
          second.push_back(w[0].second);
          second.push_back(w[2].second);
          second.push_back(w[3].second);
          ans.push_back(second);
        }
        if (w[0].first == 0 && w[1].first == 1 && w[2].first == 1 &&
            w[3].first == 1) {
          vector<pair<int, int>> second;
          second.push_back(w[1].second);
          second.push_back(w[2].second);
          second.push_back(w[3].second);
          ans.push_back(second);
        }
        if (w[0].first == 0 && w[1].first == 0 && w[2].first == 1 &&
            w[3].first == 1) {
          vector<pair<int, int>> second;
          second.push_back(w[0].second);
          second.push_back(w[1].second);
          second.push_back(w[3].second);
          ans.push_back(second);
          second.clear();
          second.push_back(w[0].second);
          second.push_back(w[1].second);
          second.push_back(w[2].second);
          ans.push_back(second);
        }
        if (w[0].first == 0 && w[1].first == 0 && w[2].first == 0 &&
            w[3].first == 1) {
          vector<pair<int, int>> second;
          second.push_back(w[0].second);
          second.push_back(w[1].second);
          second.push_back(w[3].second);
          ans.push_back(second);
          second.clear();
          second.push_back(w[3].second);
          second.push_back(w[1].second);
          second.push_back(w[2].second);
          ans.push_back(second);
          second.clear();
          second.push_back(w[0].second);
          second.push_back(w[2].second);
          second.push_back(w[3].second);
          ans.push_back(second);
          second.clear();
        }
        for (auto &z : w)
          us[z.second] = 1, a[z.second.first][z.second.second] = '0',
          b[z.second.first][z.second.second] = 0;
      }
    }
    cout << (int)ans.size() << '\n';
    for (auto &z : ans) {
      cout << z[0].first + 1 << ' ' << z[0].second + 1 << ' ' << z[1].first + 1
           << ' ' << z[1].second + 1 << ' ' << z[2].first + 1 << ' '
           << z[2].second + 1 << '\n';
    }
  }
  return 0;
}