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;
using li = long long;
const char newl = '\n';
template <class T>
vector<vector<T>> vv(const unsigned int n, const unsigned int m, const T x) {
  return vector<vector<T>>(n, vector<T>(m, x));
}
template <class T>
vector<vector<vector<T>>> vvv(const unsigned int n, const unsigned int m,
                              const unsigned int k, const T x) {
  return vector<vector<vector<T>>>(n, vv(m, k, x));
}
template <class T>
bool contains(const set<T>& s, const T& v) {
  return s.find(v) != s.end();
}
template <class K, class V>
bool contains_key(const map<K, V>& m, const K& v) {
  return m.find(v) != m.end();
}
void solve() {
  li n, m;
  cin >> n >> m;
  vector<string> _table(n);
  for (int i = 0; i < n; ++i) {
    cin >> _table[i];
  }
  auto table = vv(n, m, 0LL);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      table[i][j] = _table[i][j] - '0';
    }
  }
  vector<vector<li>> ans;
  auto add_row = [&](vector<li> row) {
    ans.emplace_back(row);
    for (int k = 0; k < 3; ++k) {
      table[row[2 * k]][row[2 * k + 1]] = 1 - table[row[2 * k]][row[2 * k + 1]];
    }
  };
  for (int i = 0; i < n - 2; ++i) {
    for (int j = 0; j < m; j += 2) {
      int left;
      if (j + 1 < m) {
        left = j;
      } else {
        left = j - 1;
      }
      const int right = left + 1;
      vector<li> row;
      if (table[i][left] == 1) {
        row.emplace_back(i);
        row.emplace_back(left);
      }
      if (table[i][right] == 1) {
        row.emplace_back(i);
        row.emplace_back(right);
      }
      if (row.size() < 6) {
        row.emplace_back(i + 1);
        row.emplace_back(left);
      }
      if (row.size() < 6) {
        row.emplace_back(i + 1);
        row.emplace_back(right);
      }
      if (row.size() < 6) {
        continue;
      }
      add_row(row);
    }
  }
  for (int left = 0; left < m - 2; ++left) {
    const int right = left + 1;
    const int up = n - 2;
    const int down = up + 1;
    vector<li> row;
    if (table[up][left] == 1) {
      row.emplace_back(up);
      row.emplace_back(left);
    }
    if (table[down][left] == 1) {
      row.emplace_back(down);
      row.emplace_back(left);
    }
    if (row.size() < 6) {
      row.emplace_back(up);
      row.emplace_back(right);
    }
    if (row.size() < 6) {
      row.emplace_back(down);
      row.emplace_back(right);
    }
    if (row.size() < 6) {
      continue;
    }
    add_row(row);
  }
  while (true) {
    vector<li> row;
    vector<li> negs;
    for (int i = n - 2; i < n; ++i) {
      for (int j = m - 2; j < m; ++j) {
        if (table[i][j] == 1) {
          row.emplace_back(i);
          row.emplace_back(j);
        } else {
          negs.emplace_back(i);
          negs.emplace_back(j);
        }
      }
    }
    if (row.size() == 0) {
      break;
    } else if (row.size() == 2) {
      for (int i = 0; i < 4; ++i) {
        row.emplace_back(negs[i]);
      }
      add_row(row);
    } else if (row.size() == 4) {
      for (int i = 0; i < 2; ++i) {
        negs.emplace_back(row[i]);
      }
      add_row(negs);
    } else if (row.size() == 6) {
      add_row(row);
    } else {
      row.pop_back();
      row.pop_back();
      add_row(row);
    }
  }
  cout << ans.size() << newl;
  for (const auto row : ans) {
    for (int i = 0; i < row.size(); ++i) {
      cout << row[i] + 1;
      if (i + 1 == row.size()) {
        cout << newl;
      } else {
        cout << " ";
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  li t;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    solve();
  }
  return 0;
}