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>
long long fpow(long long x, long long y, long long p = 1000000007) {
  x = x % p;
  long long sum = 1;
  while (y) {
    if (y & 1) sum = sum * x;
    sum %= p;
    y = y >> 1;
    x = x * x;
    x %= p;
  }
  return sum;
}
long long fact[3000007] = {0};
void facto() {
  fact[0] = 1;
  fact[1] = 1;
  for (long long i = 2; i < 3000007; i++)
    fact[i] = (fact[i - 1] * i) % 1000000007;
}
long long ncr(long long n, long long r) {
  if (r > n) return 0;
  long long res = 1;
  res = fact[n];
  res = (res * (fpow(fact[r], 1000000007 - 2))) % 1000000007;
  res = (res * (fpow(fact[n - r], 1000000007 - 2))) % 1000000007;
  return res;
}
long long npr(long long n, long long r) {
  if (r > n) return 0;
  long long res = 1;
  res = fact[n];
  res = (res * (fpow(fact[n - r], 1000000007 - 2))) % 1000000007;
  return res;
}
long long modmul(long long x, long long y) { return (x * y) % 1000000007; }
long long modadd(long long x, long long y) { return (x + y) % 1000000007; }
long long modsub(long long x, long long y) {
  return (x - y + 1000000007) % 1000000007;
}
long long xx[4] = {1, 0, -1, 0};
long long yy[4] = {0, 1, 0, -1};
using namespace std;
void __print(long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  long long f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
long long n, m;
vector<vector<pair<long long, long long> > > v;
long long a[200][200];
long long fun(long long i, long long j, long long c) {
  vector<pair<long long, long long> > temp;
  if (c == 1) {
    long long f = 0;
    for (long long ii = i; ii < i + 2; ii += 1) {
      for (long long jj = j; jj < j + 2; jj += 1) {
        if (a[ii][jj]) {
          temp.push_back({ii + 1, jj + 1});
          a[ii][jj] ^= 1;
        } else {
          if (f == 0) {
            f = 1;
          } else {
            temp.push_back({ii + 1, jj + 1});
            a[ii][jj] ^= 1;
          }
        }
      }
    }
  } else if (c == 2) {
    long long f = 0;
    for (long long ii = i; ii < i + 2; ii += 1) {
      for (long long jj = j; jj < j + 2; jj += 1) {
        if (a[ii][jj] == 0) {
          temp.push_back({ii + 1, jj + 1});
          a[ii][jj] ^= 1;
        } else {
          if (f == 0) {
            f = 1;
          } else {
            temp.push_back({ii + 1, jj + 1});
            a[ii][jj] ^= 1;
          }
        }
      }
    }
  } else if (c == 3) {
    long long f = 0;
    for (long long ii = i; ii < i + 2; ii += 1) {
      for (long long jj = j; jj < j + 2; jj += 1) {
        if (a[ii][jj]) {
          temp.push_back({ii + 1, jj + 1});
          a[ii][jj] ^= 1;
        } else {
        }
      }
    }
  } else if (c == 4) {
    temp.push_back({i + 1, j + 1});
    temp.push_back({i + 2, j + 1});
    temp.push_back({i + 1, j + 2});
    a[i][j] ^= 1;
    a[i + 1][j] ^= 1;
    a[i][j + 1] ^= 1;
  }
  v.push_back(temp);
  if (c == 4) return 1;
  return c + 1;
}
void solve() {
  cin >> n >> m;
  char ch;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      cin >> ch;
      a[i][j] = ch - '0';
    }
  }
  v.clear();
  for (long long i = 0; i < n - 1; i += 2) {
    for (long long j = 0; j < m - 2; j++) {
      long long c = 0;
      vector<pair<long long, long long> > temp;
      if (a[i][j]) {
        c++;
        temp.push_back({i + 1, j + 1});
        a[i][j] ^= 1;
      }
      if (a[i + 1][j]) {
        c++;
        temp.push_back({i + 2, j + 1});
        a[i + 1][j] ^= 1;
      }
      if (c == 0) {
      } else if (c == 1) {
        temp.push_back({i + 1, j + 2});
        temp.push_back({i + 2, j + 2});
        a[i][j + 1] ^= 1;
        a[i + 1][j + 1] ^= 1;
      } else {
        if (a[i][j + 1] == 1) {
          temp.push_back({i + 1, j + 2});
          a[i][j + 1] ^= 1;
        } else if (a[i + 1][j + 1] == 1) {
          temp.push_back({i + 2, j + 2});
          a[i + 1][j + 1] ^= 1;
        } else {
          temp.push_back({i + 1, j + 2});
          a[i][j + 1] ^= 1;
        }
      }
      if (!temp.empty()) v.push_back(temp);
    }
  }
  if (n % 2 == 1) {
    long long i = n - 2;
    for (long long j = 0; j < m - 2; j++) {
      long long c = 0;
      vector<pair<long long, long long> > temp;
      if (a[i][j]) {
        c++;
        temp.push_back({i + 1, j + 1});
        a[i][j] ^= 1;
      }
      if (a[i + 1][j]) {
        c++;
        temp.push_back({i + 2, j + 1});
        a[i + 1][j] ^= 1;
      }
      if (c == 0) {
      } else if (c == 1) {
        temp.push_back({i + 1, j + 2});
        temp.push_back({i + 2, j + 2});
        a[i][j + 1] ^= 1;
        a[i + 1][j + 1] ^= 1;
      } else {
        temp.push_back({i + 1, j + 2});
        a[i][j + 1] ^= 1;
      }
      if (!temp.empty()) v.push_back(temp);
    }
  }
  long long j = m - 2;
  if (a[0][j] + a[1][j] + a[0][j + 1] + a[1][j + 1] == 4) {
    for (long long i = n - 2; i >= 0; i -= 2) {
      long long c = 0;
      c = a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1];
      if (c == 4) {
        c = fun(i, j, 4);
      }
      if (c == 1) {
        c = fun(i, j, 1);
      }
      if (c == 2) {
        c = fun(i, j, 2);
      }
      if (c == 3) {
        c = fun(i, j, 3);
      }
    }
    if (n % 2 == 1) {
      long long i = 0, j = m - 2;
      long long c = 0;
      c = a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1];
      if (c == 1) {
        c = fun(i, j, 1);
      }
      if (c == 2) {
        c = fun(i, j, 2);
      }
      if (c == 3) {
        c = fun(i, j, 3);
      }
    }
  } else {
    for (long long i = 0; i < n - 1; i += 2) {
      long long c = 0;
      c = a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1];
      if (c == 4) {
        c = fun(i, j, 4);
      }
      if (c == 1) {
        c = fun(i, j, 1);
      }
      if (c == 2) {
        c = fun(i, j, 2);
      }
      if (c == 3) {
        c = fun(i, j, 3);
      }
    }
    if (n % 2 == 1) {
      long long i = n - 2, j = m - 2;
      long long c = 0;
      c = a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1];
      if (c == 1) {
        c = fun(i, j, 1);
      }
      if (c == 2) {
        c = fun(i, j, 2);
      }
      if (c == 3) {
        c = fun(i, j, 3);
      }
    }
  }
  cout << v.size() << endl;
  for (auto it : v) {
    for (auto x : it) {
      cout << x.first << " " << x.second << " ";
    }
    cout << endl;
  }
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) solve();
}