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;
#pragma warning(disable : 4996)
long long gcdx(long long a, long long b) { return b ? gcdx(b, a % b) : a; }
long long GCD(long long a, long long b) { return gcdx(abs(a), abs(b)); }
void SetBit(int &m, int ind, int val) {
  if (val)
    m = (m | (1 << ind));
  else
    m = (m & ~(1 << ind));
}
bool GetBit(int m, int ind) {
  int a = (1 << ind);
  a = (a & m);
  return a;
}
int CntBits(int m) {
  int ret = 0;
  while (m) {
    ret += (m % 2);
    m /= 2;
  }
  return ret;
}
bool IsSmlLtr(char c) {
  if (c >= 'a' && c <= 'z') return 1;
  return 0;
}
bool IsCapLtr(char c) {
  if (c >= 'A' && c <= 'Z') return 1;
  return 0;
}
constexpr auto pi = 3.141592653589793;
const long long mod = 1000000007;
long long pow_mod(long long a, long long b, long long modulo = mod) {
  if (b == 0) return 1ll;
  if (b % 2ll) return (a * pow_mod((a * a) % modulo, b / 2ll)) % modulo;
  return pow_mod((a * a) % modulo, b / 2ll);
}
const long long mod2 = 998244353;
const long long mod3 = 1000000009;
bool print_;
constexpr auto infl = LLONG_MAX;
constexpr auto inf = INT_MAX;
constexpr auto eps = 0.000000000001;
constexpr auto M = 200100;
constexpr auto N = 110;
long long n, m, tmp, Q, t, ans, cnt, sum, mini, maxi, r, p, k;
string a[N];
vector<int> opX;
vector<int> opY;
void printArr() {
  cout << "#####################\n";
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }
  cout << "#####################\n";
}
void clearData() {
  opX.clear();
  opY.clear();
}
void addOp(int x, int y) {
  opX.push_back(x + 1);
  opY.push_back(y + 1);
}
void swapVal(int x, int y) {
  if (a[x][y] == '1')
    a[x][y] = '0';
  else
    a[x][y] = '1';
  addOp(x, y);
}
void clearSpecialSquareX(int y, int cnt) {
  int thisX = n - 2, thisY = y;
  if (cnt == 3) {
    if (a[thisX][thisY] == '1') swapVal(thisX, thisY);
    if (a[thisX][thisY + 1] == '1') swapVal(thisX, thisY + 1);
    if (a[thisX + 1][thisY] == '1') swapVal(thisX + 1, thisY);
    if (a[thisX + 1][thisY + 1] == '1') swapVal(thisX + 1, thisY + 1);
  } else if (cnt == 2) {
    bool swap1 = true;
    if (a[thisX][thisY] == '0')
      swapVal(thisX, thisY);
    else if (swap1) {
      swap1 = false;
      swapVal(thisX, thisY);
    }
    if (a[thisX][thisY + 1] == '0')
      swapVal(thisX, thisY + 1);
    else if (swap1) {
      swap1 = false;
      swapVal(thisX, thisY + 1);
    }
    if (a[thisX + 1][thisY] == '0')
      swapVal(thisX + 1, thisY);
    else if (swap1) {
      swap1 = false;
      swapVal(thisX + 1, thisY);
    }
    if (a[thisX + 1][thisY + 1] == '0')
      swapVal(thisX + 1, thisY + 1);
    else if (swap1) {
      swap1 = false;
      swapVal(thisX + 1, thisY + 1);
    }
    clearSpecialSquareX(y, 3);
  } else if (cnt == 1) {
    int swap0 = 2;
    if (a[thisX][thisY] == '1')
      swapVal(thisX, thisY);
    else if (swap0 > 0) {
      swap0--;
      swapVal(thisX, thisY);
    }
    if (a[thisX][thisY + 1] == '1')
      swapVal(thisX, thisY + 1);
    else if (swap0 > 0) {
      swap0--;
      swapVal(thisX, thisY + 1);
    }
    if (a[thisX + 1][thisY] == '1')
      swapVal(thisX + 1, thisY);
    else if (swap0 > 0) {
      swap0--;
      swapVal(thisX + 1, thisY);
    }
    if (a[thisX + 1][thisY + 1] == '1')
      swapVal(thisX + 1, thisY + 1);
    else if (swap0 > 0) {
      swap0--;
      swapVal(thisX + 1, thisY + 1);
    }
    clearSpecialSquareX(y, 2);
  } else if (cnt == 4) {
    swapVal(thisX, thisY);
    swapVal(thisX, thisY + 1);
    swapVal(thisX + 1, thisY);
    clearSpecialSquareX(y, 1);
  }
}
void clearSpecialSquare(int y) {
  int thisX = n - 2, thisY = y, cnt_1 = 0;
  if (a[thisX][thisY] == '1') cnt_1++;
  if (a[thisX][thisY + 1] == '1') cnt_1++;
  if (a[thisX + 1][thisY] == '1') cnt_1++;
  if (a[thisX + 1][thisY + 1] == '1') cnt_1++;
  if (cnt_1 > 0) clearSpecialSquareX(y, cnt_1);
}
void clearRow(int x, int y) {
  if (y >= m - 1) return;
  if (y % 2 && y != m - 2) return;
  if (x >= n - 2) return;
  if (a[x][y] == '1' || a[x][y + 1] == '1') {
    int temp = 3;
    if (a[x][y] == '1') {
      swapVal(x, y);
      temp--;
    }
    if (a[x][y + 1] == '1') {
      swapVal(x, y + 1);
      temp--;
    }
    swapVal(x + 1, y);
    temp--;
    if (temp > 0) swapVal(x + 1, y + 1);
  }
}
void clearLastRow() {
  for (int i = 0; i <= m - 3; i++) {
    int thisX = n - 2, thisY = i;
    if (a[thisX][thisY] == '1' || a[thisX + 1][thisY] == '1') {
      int swaps = 3;
      if (a[thisX][thisY] == '1') {
        swapVal(thisX, thisY);
        swaps--;
      }
      if (a[thisX + 1][thisY] == '1') {
        swapVal(thisX + 1, thisY);
        swaps--;
      }
      swapVal(thisX, thisY + 1);
      swaps--;
      if (swaps > 0) swapVal(thisX + 1, thisY + 1);
    }
  }
  clearSpecialSquare(m - 2);
}
void clearRows() {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      clearRow(i, j);
    }
  clearLastRow();
}
void printAns() {
  int ansCnt = opX.size();
  cout << ansCnt / 3 << endl;
  int currCnt = 0;
  for (int i = 0; i < ansCnt; i++) {
    cout << opX[i] << " " << opY[i];
    currCnt++;
    if (currCnt == 3) {
      currCnt = 0;
      cout << endl;
    } else
      cout << " ";
  }
}
void printLN() { cout << "..............START..................\n"; }
void printEndl() { cout << ".....................................\n\n\n"; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  print_ = false;
  cin >> t;
  while (t--) {
    clearData();
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> a[i];
    clearRows();
    printAns();
  }
  return 0;
}