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;
unsigned long long min(unsigned long long x, unsigned long long y) {
  if (x < y) return x;
  return y;
}
unsigned long long max(unsigned long long x, unsigned long long y) {
  if (x < y) return y;
  return x;
}
long long min(long long x, long long y) {
  if (x < y) return x;
  return y;
}
long long max(long long x, long long y) {
  if (x < y) return y;
  return x;
}
double min(double x, double y) {
  if (x < y) return x;
  return y;
}
double max(double x, double y) {
  if (x < y) return y;
  return x;
}
unsigned long long gcd(unsigned long long x, unsigned long long y) {
  if (!x) return y;
  if (!y) return x;
  if (x > y) swap(x, y);
  return gcd(x, y % x);
}
unsigned long long inv(unsigned long long a, unsigned long long c) {
  if (a == 1) {
    return 1;
  }
  return ((c - (c / a)) * inv(c % a, c)) % c;
}
int bsl(int a[], int n, int val) {
  int l = 0, r = n - 1;
  while (l < r - 1) {
    int mid = l + (r - l) / 2;
    if (a[mid] < val) {
      l = mid;
    } else {
      r = mid;
    }
  }
  if (a[r] < val) return r;
  if (a[l] < val) return l;
  return l - 1;
}
int bsr(int a[], int n, int val) {
  int l = 0, r = n - 1;
  while (l < r - 1) {
    int mid = l + (r - l) / 2;
    if (a[mid] > val) {
      r = mid;
    } else {
      l = mid;
    }
  }
  if (a[l] > val) return l;
  if (a[r] > val) return r;
  return r + 1;
}
void printc(vector<pair<int, int> > move) {
  int i1 = move[0].first, j1 = move[0].second, i2 = move[1].first,
      j2 = move[1].second, i3 = move[2].first, j3 = move[2].second;
  cout << (i1) << " " << (j1) << " " << (i2) << " " << (j2) << " " << (i3)
       << " " << (j3) << "\n";
}
void flip(string s[], int i, int j) {
  if (s[i][j] == '0')
    s[i][j] = '1';
  else
    s[i][j] = '0';
}
void f(int a[2][2], vector<vector<pair<int, int> > > &moves, int n, int m) {
  for (int i = 0; i < 16; ++i) {
    int b[2][2] = {0};
    for (int j = 0; j < 4; ++j) {
      if (((i >> j) & 1) == 0) continue;
      int row = j / 2, col = j % 2;
      b[row][col] ^= 1;
      if (row < 1) b[row + 1][col] ^= 1;
      if (col < 1) b[row][col + 1] ^= 1;
      if (row > 0) b[row - 1][col] ^= 1;
      if (col > 0) b[row][col - 1] ^= 1;
    }
    bool ok = 1;
    for (int k = 0; k < 2; ++k) {
      for (int l = 0; l < 2; ++l) {
        if (a[k][l] != b[k][l]) ok = 0;
      }
    }
    if (ok) {
      for (int j = 0; j < 4; ++j) {
        if (((i >> j) & 1) == 0) continue;
        int row = j / 2, col = j % 2;
        vector<pair<int, int> > move;
        move.push_back(make_pair(row + n - 1, col + m - 1));
        if (row < 1) move.push_back(make_pair(row + 1 + n - 1, col + m - 1));
        if (col < 1) move.push_back(make_pair(row + n - 1, col + 1 + m - 1));
        if (row > 0) move.push_back(make_pair(row - 1 + n - 1, col + m - 1));
        if (col > 0) move.push_back(make_pair(row + n - 1, col - 1 + m - 1));
        moves.push_back(move);
      }
      return;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, m, cnt = 0;
    cin >> n >> m;
    string s[n];
    for (int i = 0; i < n; ++i) {
      cin >> s[i];
      for (char &c : s[i]) {
        cnt += (c == '1');
      }
    }
    vector<vector<pair<int, int> > > moves;
    for (int i = 0; i < n - 2; ++i) {
      for (int j = 0; j < m; ++j) {
        if (s[i][j] == '0') continue;
        vector<pair<int, int> > move;
        if (j < m - 1) {
          flip(s, i, j);
          move.push_back(make_pair(i + 1, j + 1));
          flip(s, i + 1, j);
          move.push_back(make_pair(i + 2, j + 1));
          flip(s, i + 1, j + 1);
          move.push_back(make_pair(i + 2, j + 2));
          moves.push_back(move);
        } else {
          flip(s, i, j);
          move.push_back(make_pair(i + 1, j + 1));
          flip(s, i + 1, j);
          move.push_back(make_pair(i + 2, j + 1));
          flip(s, i + 1, j - 1);
          move.push_back(make_pair(i + 2, j));
          moves.push_back(move);
        }
      }
    }
    for (int j = 0; j < m - 2; ++j) {
      vector<pair<int, int> > move;
      if (s[n - 2][j] == '1') {
        flip(s, n - 2, j);
        move.push_back(make_pair(n - 1, j + 1));
        flip(s, n - 2, j + 1);
        move.push_back(make_pair(n - 1, j + 2));
        flip(s, n - 1, j + 1);
        move.push_back(make_pair(n, j + 2));
        moves.push_back(move);
      }
      move.clear();
      if (s[n - 1][j] == '1') {
        flip(s, n - 1, j);
        move.push_back(make_pair(n, j + 1));
        flip(s, n - 1, j + 1);
        move.push_back(make_pair(n, j + 2));
        flip(s, n - 2, j + 1);
        move.push_back(make_pair(n - 1, j + 2));
        moves.push_back(move);
      }
    }
    int left[2][2] = {0};
    for (int i = n - 2; i < n; ++i) {
      for (int j = m - 2; j < m; ++j) {
        left[i - n + 2][j - m + 2] = (s[i][j] == '1');
      }
    }
    f(left, moves, n, m);
    cout << moves.size() << "\n";
    for (int i = 0; i < moves.size(); ++i) {
      printc(moves[i]);
    }
  }
  return 0;
}