Binary Table (Hard Version)
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 \times m$$$. This table consists of symbols $$$0$$$ and $$$1$$$.You can make such operation: select $$$3$$$ different cells that belong to one $$$2 \times 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.
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: 011001000  operation with cells $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$. After it the table will be: 000000000 In the fifth test case:  operation with cells $$$(1, 3)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$. After it the table will be: 010110  operation with cells $$$(1, 2)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$. After it the table will be: 000000 
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 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 \leq n, m \leq 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$$$.
For each test case print the integer $$$k$$$ ($$$0 \leq k \leq 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 \leq x_1, x_2, x_3 \leq n, 1 \leq y_1, y_2, y_3 \leq 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 \times 2$$$ square.
import math
import random
import time
from collections import *
start_time = time.time()
alfabet = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
alfabet_2 = {'1': 'a', '2': 'b', '3': 'c', '4': 'd', '5': 'e', '6': 'f', '7': 'g', '8': 'h', '9': 'i', '10': 'j', '11': 'k', '12': 'l', '13': 'm', '14': 'n', '15': 'o', '16': 'p', '17': 'q', '18': 'r', '19': 's', '20': 't', '21': 'u', '22': 'v', '23': 'w', '24': 'x', '25': 'y', '26': 'z'}
global adev

def prime_generator(nr_elemente_prime):
    vector_prime = [-1] * nr_elemente_prime
    vector_rasp = [0] * nr_elemente_prime
    vector_prime[1] = 1
    vector_rasp[1] = 1
    contor = 2
    for i in range(2, nr_elemente_prime):
        if vector_prime[i] == -1:
            vector_prime[i] = 1
            vector_rasp[contor] = i
            contor = contor + 1
        for j in range(i + i, nr_elemente_prime, i):
            if vector_prime[j] == -1:
                vector_prime[j] = i
    set_prime = set(vector_rasp)
    set_prime.remove(0)
    set_prime.remove(1)
    answ_prime = list(set_prime)
    answ_prime.sort()
    return answ_prime

def parcurgere(nod, graphurile, vizitate, potentialul, parinte, peste):
    global adev
    if vizitate[nod] == 0 and adev == 0:
        vizitate[nod] = 1
        potentialul.append(nod)
        for element in graphurile[nod]:
            if vizitate[element] == 1 and element in peste and (parinte != element) and (adev == 0):
                adev = 1
                potentialul.append(element)
                return adev
                break
            elif vizitate[element] == 0 and adev == 0:
                parcurgere(element, graphurile, vizitate, potentialul, nod, peste)
        return adev
from bisect import bisect_left as lower_bound
from bisect import bisect_right as upper_bound

class FenwickTree:

    def __init__(self, x):
        bit = self.bit = list(x)
        size = self.size = len(bit)
        for i in range(size):
            j = i | i + 1
            if j < size:
                bit[j] += bit[i]

    def update(self, idx, x):
        """"""
        while idx < self.size:
            self.bit[idx] += x
            idx |= idx + 1

    def __call__(self, end):
        """"""
        x = 0
        while end:
            x += self.bit[end - 1]
            end &= end - 1
        return x

    def find_kth(self, k):
        """"""
        idx = -1
        for d in reversed(range(self.size.bit_length())):
            right_idx = idx + (1 << d)
            if right_idx < self.size and self.bit[right_idx] <= k:
                idx = right_idx
                k -= self.bit[idx]
        return (idx + 1, k)

class SortedList:
    block_size = 700

    def __init__(self, iterable=()):
        self.macro = []
        self.micros = [[]]
        self.micro_size = [0]
        self.fenwick = FenwickTree([0])
        self.size = 0
        for item in iterable:
            self.insert(item)

    def insert(self, x):
        i = lower_bound(self.macro, x)
        j = upper_bound(self.micros[i], x)
        self.micros[i].insert(j, x)
        self.size += 1
        self.micro_size[i] += 1
        self.fenwick.update(i, 1)
        if len(self.micros[i]) >= self.block_size:
            self.micros[i:i + 1] = (self.micros[i][:self.block_size >> 1], self.micros[i][self.block_size >> 1:])
            self.micro_size[i:i + 1] = (self.block_size >> 1, self.block_size >> 1)
            self.fenwick = FenwickTree(self.micro_size)
            self.macro.insert(i, self.micros[i + 1][0])

    def pop(self, k=-1):
        (i, j) = self._find_kth(k)
        self.size -= 1
        self.micro_size[i] -= 1
        self.fenwick.update(i, -1)
        return self.micros[i].pop(j)

    def __getitem__(self, k):
        (i, j) = self._find_kth(k)
        return self.micros[i][j]

    def count(self, x):
        return self.upper_bound(x) - self.lower_bound(x)

    def __contains__(self, x):
        return self.count(x) > 0

    def lower_bound(self, x):
        i = lower_bound(self.macro, x)
        return self.fenwick(i) + lower_bound(self.micros[i], x)

    def upper_bound(self, x):
        i = upper_bound(self.macro, x)
        return self.fenwick(i) + upper_bound(self.micros[i], x)

    def _find_kth(self, k):
        return self.fenwick.find_kth(k + self.size if k < 0 else k)

    def __len__(self):
        return self.size

    def __iter__(self):
        return (x for micro in self.micros for x in micro)

    def __repr__(self):
        return str(list(self))

def binary_search_egal(vectorul, valoarea):
    left = 0
    right = len(vectorul) - 1
    while left <= right:
        centru = (left + right) // 2
        if vectorul[centru] <= valoarea:
            left = centru + 1
        else:
            right = centru - 1
    return left

def binary_search_superior(vectorul, valoarea):
    left = 0
    right = len(vectorul) - 1
    while left <= right:
        centru = (left + right) // 2
        if vectorul[centru] <= valoarea:
            left = centru + 1
        else:
            right = centru - 1
    return len(vectorul) - left

def binary_search_inferior(vectorul, valoarea):
    left = 0
    right = len(vectorul) - 1
    while left <= right:
        centru = (left + right) // 2
        if vectorul[centru] < valoarea:
            left = centru + 1
        else:
            right = centru - 1
    return left

def binary_search_egal(vectorul, valoarea):
    left = 0
    right = len(vectorul) - 1
    while left <= right:
        centru = (left + right) // 2
        if vectorul[centru] <= valoarea:
            left = centru + 1
        else:
            right = centru - 1
    return left

def binary_search_vectorul(vectorul, valoarea):
    left = 0
    right = len(vectorul) - 1
    while left <= right:
        centru = (left + right) // 2
        if vectorul[centru] < valoarea:
            left = centru + 1
        else:
            right = centru - 1
    return left

def suma_cifre(numar):
    ss = 0
    for t in str(numar):
        ss += int(t)
    return ss

def functie(nod, graph, vizitat, setul):
    vizitat[nod] = 1
    el = graph[nod]
    if vizitat[el] == 0:
        setul.add(el)
        functie(el, graph, vizitat, setul)
    return setul

def afisare(matricea):
    randuri = len(matricea)
    coloane = len(matricea[0])
    for i in range(randuri):
        for j in range(coloane):
            print(matricea[i][j], end=' ')
        print()

def functie_matrice(x1, y1, x2, y2, x3, y3, matricea, answ):
    matricea[x1][y1] = 1 - matricea[x1][y1]
    matricea[x2][y2] = 1 - matricea[x2][y2]
    matricea[x3][y3] = 1 - matricea[x3][y3]
    answ.append((x1 + 1, y1 + 1, x2 + 1, y2 + 1, x3 + 1, y3 + 1))
teste = int(input())

def main():
    pp = 10 ** 18
    modulul = 10 ** 9 + 7
    answ = -1
    for gg in range(teste):
        (n, m) = list(map(int, input().split()))
        matrice = []
        initiala = []
        contor = 0
        for i in range(n):
            stringul = input()
            vector = []
            for t in stringul:
                vector.append(int(t))
            matrice.append(vector)
            initiala.append(vector)
        if gg == 593:
            s = ''
            randuri = len(matrice)
            coloane = len(matrice[0])
            for i in range(randuri):
                for j in range(coloane):
                    s += str(matrice[i][j]) + '|'
                s += '||'
        answ = []
        for i in range(0, n - 2, 2):
            for j in range(m - 2):
                if matrice[i + 1][j] == 1:
                    if matrice[i][j] == 1:
                        matrice[i][j] = 1 - matrice[i][j]
                        matrice[i + 1][j] = 1 - matrice[i + 1][j]
                        matrice[i][j + 1] = 1 - matrice[i][j + 1]
                        answ.append((i + 1, j + 1, i + 2, j + 1, i + 1, j + 2))
                    else:
                        matrice[i + 1][j] = 1 - matrice[i + 1][j]
                        matrice[i][j + 1] = 1 - matrice[i][j + 1]
                        matrice[i + 1][j + 1] = 1 - matrice[i + 1][j + 1]
                        answ.append((i + 2, j + 1, i + 1, j + 2, i + 2, j + 2))
                elif matrice[i][j] == 1:
                    if matrice[i + 1][j] == 1:
                        matrice[i][j] = 1 - matrice[i][j]
                        matrice[i + 1][j] = 1 - matrice[i + 1][j]
                        matrice[i][j + 1] = 1 - matrice[i][j + 1]
                        answ.append((j + 1, i + 1, j + 1, i + 2, j + 1, i + 1))
                    else:
                        matrice[i][j] = 1 - matrice[i][j]
                        matrice[i][j + 1] = 1 - matrice[i][j + 1]
                        matrice[i + 1][j + 1] = 1 - matrice[i + 1][j + 1]
                        answ.append((i + 1, j + 1, i + 1, j + 2, i + 2, j + 2))
            j = m - 2
            if j > 0 and matrice[i][j] == 1 and (matrice[i][j + 1] == 0) and (matrice[i + 1][j] == 0) and (matrice[i + 1][j + 1] == 1):
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i, j, i + 1, j, matrice, answ)
            if j > 0 and matrice[i][j] == 0 and (matrice[i][j + 1] == 1) and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 0):
                functie_matrice(i, j, i, j + 1, i + 1, j + 1, matrice, answ)
            if j > 0 and matrice[i][j] == 0 and (matrice[i][j + 1] == 0) and (matrice[i + 1][j] == 0) and (matrice[i + 1][j + 1] == 1):
                functie_matrice(i, j, i + 1, j + 1, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, answ)
            if j > 0 and matrice[i][j] == 1 and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 0) and (matrice[i][j + 1] == 0):
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, answ)
            if j > 0 and matrice[i][j] == 1 and (matrice[i + 1][j] == 0) and (matrice[i + 1][j + 1] == 1) and (matrice[i][j + 1] == 1):
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, answ)
            if j > 0 and matrice[i][j] == 1 and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 1) and (matrice[i][j + 1] == 1):
                functie_matrice(i, j + 1, i, j, i + 1, j, matrice, answ)
                functie_matrice(i, j, i + 1, j + 1, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, answ)
            if j > 0 and matrice[i][j] == 0 and (matrice[i + 1][j] == 0) and (matrice[i + 1][j + 1] == 0) and (matrice[i][j + 1] == 1):
                functie_matrice(i, j + 1, i, j, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, answ)
            if j > 0 and matrice[i][j] == 1 and (matrice[i + 1][j] == 0) and (matrice[i + 1][j + 1] == 0) and (matrice[i][j + 1] == 0):
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, answ)
                functie_matrice(i + 1, j, i + 1, j + 1, i, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j, i, j, matrice, answ)
            if j > 0 and matrice[i][j] == 0 and (matrice[i + 1][j] == 0) and (matrice[i + 1][j + 1] == 1) and (matrice[i][j + 1] == 1):
                functie_matrice(i + 1, j, i + 1, j + 1, i, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j, i, j, matrice, answ)
            if j > 0 and matrice[i][j] == 1 and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 0) and (matrice[i][j + 1] == 1):
                functie_matrice(i, j, i + 1, j, i, j + 1, matrice, answ)
            if j > 0 and matrice[i][j] == 0 and (matrice[i][j + 1] == 0) and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 0):
                functie_matrice(i + 1, j + 1, i + 1, j, i, j + 1, matrice, answ)
                functie_matrice(i + 1, j, i + 1, j + 1, i, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j, i, j, matrice, answ)
            if j > 0 and matrice[i][j] == 1 and (matrice[i][j + 1] == 1) and (matrice[i + 1][j] == 0) and (matrice[i + 1][j + 1] == 0):
                functie_matrice(i + 1, j, i + 1, j + 1, i, j + 1, matrice, answ)
                functie_matrice(i + 1, j, i + 1, j + 1, i, j, matrice, answ)
            if j > 0 and matrice[i][j] == 0 and (matrice[i][j + 1] == 0) and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 1):
                functie_matrice(i, j, i, j + 1, i + 1, j + 1, matrice, answ)
                functie_matrice(i, j, i, j + 1, i + 1, j, matrice, answ)
            if j > 0 and matrice[i][j] == 1 and (matrice[i][j + 1] == 1) and (matrice[i + 1][j] == 0) and (matrice[i + 1][j + 1] == 1):
                functie_matrice(i, j, i, j + 1, i + 1, j + 1, matrice, answ)
            if j > 0 and matrice[i][j] == 0 and (matrice[i][j + 1] == 1) and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 1):
                functie_matrice(i + 1, j, i, j + 1, i + 1, j + 1, matrice, answ)
            if j > 0 and matrice[i][j] == 1 and (matrice[i][j + 1] == 0) and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 1):
                functie_matrice(i, j, i + 1, j + 1, i + 1, j, matrice, answ)
        new_answ = []
        if m == 2:
            for i in range(n - 2):
                if matrice[i][0] == 1 and matrice[i][1] == 1:
                    matrice[i][0] = 1 - matrice[i][0]
                    matrice[i][1] = 1 - matrice[i][1]
                    matrice[i + 1][0] = 1 - matrice[i + 1][0]
                    new_answ.append((i + 1, 1, i + 1, 2, i + 2, 1))
                elif matrice[i][0] == 1 and matrice[i][1] == 0:
                    matrice[i][0] = 1 - matrice[i][0]
                    matrice[i + 1][1] = 1 - matrice[i + 1][1]
                    matrice[i + 1][0] = 1 - matrice[i + 1][0]
                    new_answ.append((i + 1, 1, i + 2, 2, i + 2, 1))
                elif matrice[i][0] == 0 and matrice[i][1] == 1:
                    matrice[i][1] = 1 - matrice[i][1]
                    matrice[i + 1][1] = 1 - matrice[i + 1][1]
                    matrice[i + 1][0] = 1 - matrice[i + 1][0]
                    new_answ.append((i + 1, 2, i + 2, 2, i + 2, 1))
        i = n - 2
        for j in range(m - 2):
            if matrice[i + 1][j] == 1:
                if matrice[i][j] == 1:
                    matrice[i][j] = 1 - matrice[i][j]
                    matrice[i + 1][j] = 1 - matrice[i + 1][j]
                    matrice[i][j + 1] = 1 - matrice[i][j + 1]
                    answ.append((i + 1, j + 1, i + 2, j + 1, i + 1, j + 2))
                else:
                    matrice[i + 1][j] = 1 - matrice[i + 1][j]
                    matrice[i][j + 1] = 1 - matrice[i][j + 1]
                    matrice[i + 1][j + 1] = 1 - matrice[i + 1][j + 1]
                    answ.append((i + 2, j + 1, i + 1, j + 2, i + 2, j + 2))
            elif matrice[i][j] == 1:
                if matrice[i + 1][j] == 1:
                    matrice[i][j] = 1 - matrice[i][j]
                    matrice[i + 1][j] = 1 - matrice[i + 1][j]
                    matrice[i][j + 1] = 1 - matrice[i][j + 1]
                    answ.append((j + 1, i + 1, j + 1, i + 2, j + 1, i + 1))
                else:
                    matrice[i][j] = 1 - matrice[i][j]
                    matrice[i][j + 1] = 1 - matrice[i][j + 1]
                    matrice[i + 1][j + 1] = 1 - matrice[i + 1][j + 1]
                    answ.append((i + 1, j + 1, i + 1, j + 2, i + 2, j + 2))
        j = m - 2
        if matrice[i][j] == 1 and matrice[i][j + 1] == 0 and (matrice[i + 1][j] == 0) and (matrice[i + 1][j + 1] == 1):
            if m > 2:
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i, j, i + 1, j, matrice, answ)
            else:
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, new_answ)
                functie_matrice(i, j + 1, i, j, i + 1, j, matrice, new_answ)
        if matrice[i][j] == 0 and matrice[i][j + 1] == 1 and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 0):
            if m > 2:
                functie_matrice(i, j, i, j + 1, i + 1, j + 1, matrice, answ)
                functie_matrice(i, j, i + 1, j, i + 1, j + 1, matrice, answ)
            else:
                functie_matrice(i, j, i, j + 1, i + 1, j + 1, matrice, new_answ)
                functie_matrice(i, j, i + 1, j, i + 1, j + 1, matrice, new_answ)
        if matrice[i][j] == 0 and matrice[i][j + 1] == 0 and (matrice[i + 1][j] == 0) and (matrice[i + 1][j + 1] == 1):
            if m > 2:
                functie_matrice(i, j, i + 1, j + 1, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, answ)
            else:
                functie_matrice(i, j, i + 1, j + 1, i + 1, j, matrice, new_answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, new_answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, new_answ)
        if matrice[i][j] == 1 and matrice[i + 1][j] == 1 and (matrice[i + 1][j + 1] == 0) and (matrice[i][j + 1] == 0):
            if m > 2:
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, answ)
            else:
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, new_answ)
                if m > 2:
                    functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, answ)
                else:
                    functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, new_answ)
        if matrice[i][j] == 1 and matrice[i + 1][j] == 0 and (matrice[i + 1][j + 1] == 1) and (matrice[i][j + 1] == 1):
            if m > 2:
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, answ)
            else:
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, new_answ)
        if matrice[i][j] == 1 and matrice[i + 1][j] == 1 and (matrice[i + 1][j + 1] == 1) and (matrice[i][j + 1] == 1):
            if m > 2:
                functie_matrice(i, j + 1, i, j, i + 1, j, matrice, answ)
                functie_matrice(i, j, i + 1, j + 1, i + 1, j, matrice, answ)
            else:
                functie_matrice(i, j, i + 1, j + 1, i + 1, j, matrice, new_answ)
                functie_matrice(i, j + 1, i, j, i + 1, j, matrice, new_answ)
            if m > 2:
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, answ)
            else:
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, new_answ)
            if m > 2:
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, answ)
            else:
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, new_answ)
        if matrice[i][j] == 0 and matrice[i + 1][j] == 0 and (matrice[i + 1][j + 1] == 0) and (matrice[i][j + 1] == 1):
            if m > 2:
                functie_matrice(i, j + 1, i, j, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, answ)
            else:
                functie_matrice(i, j + 1, i, j, i + 1, j, matrice, new_answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i + 1, j, matrice, new_answ)
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, new_answ)
        if matrice[i][j] == 1 and matrice[i + 1][j] == 0 and (matrice[i + 1][j + 1] == 0) and (matrice[i][j + 1] == 0):
            if m > 2:
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, answ)
                functie_matrice(i + 1, j, i + 1, j + 1, i, j, matrice, answ)
            else:
                functie_matrice(i, j + 1, i + 1, j + 1, i, j, matrice, new_answ)
                functie_matrice(i + 1, j, i + 1, j + 1, i, j, matrice, new_answ)
                if m > 2:
                    functie_matrice(i, j + 1, i + 1, j, i, j, matrice, answ)
                else:
                    functie_matrice(i, j + 1, i + 1, j, i, j, matrice, new_answ)
        if matrice[i][j] == 0 and matrice[i + 1][j] == 0 and (matrice[i + 1][j + 1] == 1) and (matrice[i][j + 1] == 1):
            if m > 2:
                functie_matrice(i + 1, j, i + 1, j + 1, i, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j, i, j, matrice, answ)
            else:
                functie_matrice(i + 1, j, i + 1, j + 1, i, j, matrice, new_answ)
                functie_matrice(i, j + 1, i + 1, j, i, j, matrice, new_answ)
        if matrice[i][j] == 1 and matrice[i + 1][j] == 1 and (matrice[i + 1][j + 1] == 0) and (matrice[i][j + 1] == 1):
            if m > 2:
                functie_matrice(i, j, i + 1, j, i, j + 1, matrice, answ)
            else:
                functie_matrice(i, j, i + 1, j, i, j + 1, matrice, new_answ)
        if matrice[i][j] == 0 and matrice[i][j + 1] == 0 and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 0):
            if m > 2:
                functie_matrice(i + 1, j + 1, i + 1, j, i, j + 1, matrice, answ)
                functie_matrice(i + 1, j, i + 1, j + 1, i, j, matrice, answ)
                functie_matrice(i, j + 1, i + 1, j, i, j, matrice, answ)
            else:
                functie_matrice(i + 1, j + 1, i + 1, j, i, j + 1, matrice, new_answ)
                functie_matrice(i + 1, j, i + 1, j + 1, i, j, matrice, new_answ)
                functie_matrice(i, j + 1, i + 1, j, i, j, matrice, new_answ)
        if matrice[i][j] == 1 and matrice[i][j + 1] == 1 and (matrice[i + 1][j] == 0) and (matrice[i + 1][j + 1] == 0):
            if m > 2:
                functie_matrice(i + 1, j, i + 1, j + 1, i, j + 1, matrice, answ)
                functie_matrice(i + 1, j, i + 1, j + 1, i, j, matrice, answ)
            else:
                functie_matrice(i + 1, j, i + 1, j + 1, i, j + 1, matrice, new_answ)
                functie_matrice(i + 1, j, i + 1, j + 1, i, j, matrice, new_answ)
        if matrice[i][j] == 0 and matrice[i][j + 1] == 0 and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 1):
            if m > 2:
                functie_matrice(i, j, i, j + 1, i + 1, j + 1, matrice, answ)
                functie_matrice(i, j, i, j + 1, i + 1, j, matrice, answ)
            else:
                functie_matrice(i, j, i, j + 1, i + 1, j + 1, matrice, new_answ)
                functie_matrice(i, j, i, j + 1, i + 1, j, matrice, new_answ)
        if matrice[i][j] == 1 and matrice[i][j + 1] == 1 and (matrice[i + 1][j] == 0) and (matrice[i + 1][j + 1] == 1):
            if m > 2:
                functie_matrice(i, j, i, j + 1, i + 1, j + 1, matrice, answ)
            else:
                functie_matrice(i, j, i, j + 1, i + 1, j + 1, matrice, new_answ)
        if matrice[i][j] == 0 and matrice[i][j + 1] == 1 and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 1):
            if m > 2:
                functie_matrice(i + 1, j, i, j + 1, i + 1, j + 1, matrice, answ)
            else:
                functie_matrice(i + 1, j, i, j + 1, i + 1, j + 1, matrice, new_answ)
        if matrice[i][j] == 1 and matrice[i][j + 1] == 0 and (matrice[i + 1][j] == 1) and (matrice[i + 1][j + 1] == 1):
            if m > 2:
                functie_matrice(i, j, i + 1, j + 1, i + 1, j, matrice, answ)
            else:
                functie_matrice(i, j, i + 1, j + 1, i + 1, j, matrice, new_answ)
        if m != 2:
            print(len(answ))
            for zz in answ:
                print(*zz)
        else:
            print(len(new_answ))
            for zz in new_answ:
                print(*zz)
main()
t = int(input())
for _ in range(t):
    (n, m) = map(int, input().split())
    a = [list(map(int, input())) for i in range(n)]

    def add(p):
        a[p[0]][p[1]] ^= 1
        a[p[2]][p[3]] ^= 1
        a[p[4]][p[5]] ^= 1
        ans.append(' '.join(map(lambda x: str(x + 1), p)))
    ans = []
    for i in range(n - 1, 1, -1):
        for j in range(m):
            if a[i][j]:
                add((i, j, i - 1, j, i - 1, j + (1 if j < m - 1 else -1)))
    for j in range(m - 1, 1, -1):
        for i in (0, 1):
            if a[i][j]:
                add((i, j, 0, j - 1, 1, j - 1))
    for i in (0, 1):
        for j in (0, 1):
            if (a[0][0] + a[0][1] + a[1][0] + a[1][1] + a[i][j]) % 2:
                add((i ^ 1, j, i, j ^ 1, i ^ 1, j ^ 1))
    print(len(ans))
    print('\n'.join(ans))
import sys

def minp():
    return sys.stdin.readline().strip()

def mint():
    return int(minp())

def mints():
    return map(int, minp().split())

def solve():
    (n, m) = mints()
    a = [list(map(int, minp())) for i in range(n)]
    ans = []
    for i in range(n - 2):
        l = a[i]
        ll = a[i + 1]
        for j in range(m - 1):
            if l[j] == 1:
                if l[j + 1] == 1:
                    ans.append((i, j, i, j + 1, i + 1, j))
                    l[j + 1] ^= 1
                    ll[j] ^= 1
                else:
                    ans.append((i, j, i + 1, j, i + 1, j + 1))
                    ll[j] ^= 1
                    ll[j + 1] ^= 1
            elif l[j + 1] == 1:
                ans.append((i, j + 1, i + 1, j, i + 1, j + 1))
                l[j + 1] ^= 1
                ll[j] ^= 1
                ll[j + 1] ^= 1
    aa = a[n - 2]
    bb = a[n - 1]
    for j in range(m - 2):
        if aa[j] == 1:
            if bb[j] == 1:
                ans.append((n - 2, j, n - 1, j, n - 2, j + 1))
                aa[j + 1] ^= 1
            else:
                ans.append((n - 2, j, n - 2, j + 1, n - 1, j + 1))
                aa[j + 1] ^= 1
                bb[j + 1] ^= 1
        elif bb[j] == 1:
            ans.append((n - 1, j, n - 2, j + 1, n - 1, j + 1))
            aa[j + 1] ^= 1
            bb[j + 1] ^= 1
    c = [(n - 2, m - 2), (n - 2, m - 1), (n - 1, m - 2), (n - 1, m - 1)]
    while True:
        ss = 0
        for (x, y) in c:
            ss += a[x][y]
        if ss == 0:
            break
        if ss == 1 or ss == 3:
            for i in range(4):
                (x, y) = c[i]
                if a[x][y] == 0:
                    break
        if ss == 2:
            for i in range(4):
                (x, y) = c[i]
                if a[x][y] == 1:
                    break
        if ss == 4:
            i = 0
        d = c.copy()
        d.pop(i)
        tmp = []
        for (x, y) in d:
            a[x][y] ^= 1
            tmp.append(x)
            tmp.append(y)
        ans.append(tmp)
    print(len(ans))
    for l in ans:
        print(' '.join([str(i + 1) for i in l]))
for i in range(mint()):
    solve()