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  
    
import sys
import bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range;R=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def NLI(n):return [[int(i) for i in input().split()] for i in range(n)]
def NLI_(n):return [[int(i)-1 for i in input().split()] for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RA():return map(int,open(0).read().split())
def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)]
def RI(a=1,b=10):return random.randint(a,b)
def INP():
    N=9
    n=random.randint(1,N)
    m=random.randint(1,n*n)
    A=[random.randint(1,n) for i in range(m)]
    B=[random.randint(1,n) for i in range(m)]
    G=[[]for i in range(n)];RG=[[]for i in range(n)]
    for i in range(m):
        a,b=A[i]-1,B[i]-1
        if a==b:continue
        G[a]+=(b,1),;RG[b]+=(a,1),
    return n,m,G,RG
def Rtest(T):
    case,err=0,0
    for i in range(T):
        inp=INP()
        a1=naive(*inp)
        a2=solve(*inp)
        if a1==a2:
            print(inp)
            print('naive',a1)
            print('solve',a2)
            err+=1
        case+=1
    print('Tested',case,'case with',err,'errors')
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
    #h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in R(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
    rt=[0]
    for i in ls:rt+=[rt[-1]+i]
    return rt
def bit_combination(n,base=2):
    rt=[]
    for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

mo=10**9+7
#mo=998244353
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
#l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**9)
read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip()

class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1);self.inv=[1]*(n+1);self.fac[0]=1;self.fact(n)
        for i in range(1,n+1):self.fac[i]=i*self.fac[i-1]%mo;self.inv[n]*=i;self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    def fact(self,n):return self.fac[n]
    def invf(self,n):return self.inv[n]
    def comb(self,x,y):
        if y<0 or y>x:return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo


show_flg=False
show_flg=True


ans=0

def solve(n,m,mp):    
    cnt=0
    rt=[]
    #sm(mp)
    for i in range(n-2):
        for j in range(m):
            if mp[i][j]==1:
                ans=[i,j,i+1,j,i+1,j+(-1 if j==m-1 else 1)]
                rt+=ans
                cnt+=1
                while ans:
                    w=ans.pop()
                    h=ans.pop()
                    mp[h][w]=1-mp[h][w]
                
    for j in range(m-2):
        if mp[n-2][j]==1 and mp[n-1][j]==1:
            ans=[n-2,j,n-1,j,n-1,j+1]
            rt+=ans
            while ans:
                w=ans.pop()
                h=ans.pop()
                mp[h][w]=1-mp[h][w]
            cnt+=1
        elif mp[n-2][j]==1 and mp[n-1][j]==0:
            ans=[n-2,j,n-1,j+1,n-2,j+1]
            rt+=ans
            while ans:
                w=ans.pop()
                h=ans.pop()
                mp[h][w]=1-mp[h][w]
            cnt+=1
        elif mp[n-2][j]==0 and mp[n-1][j]==1:
            ans=[n-2,j+1,n-1,j,n-1,j+1]
            rt+=ans
            while ans:
                w=ans.pop()
                h=ans.pop()
                mp[h][w]=1-mp[h][w]
            cnt+=1
    
    
    #show('last')
    
    lst=[[mp[i][j] for j in range(m-2,m)]for i in range(n-2,n)]
    lst2=lst[0]+lst[1]
    ans=[]
    if sum(lst2)==4:
        ans=[n-1,m-1,n-1,m-2,n-2,m-1]
        rt+=ans
        while ans:
            w=ans.pop()
            h=ans.pop()
            mp[h][w]=1-mp[h][w]
        lst=[[mp[i][j] for j in range(m-2,m)]for i in range(n-2,n)]
        lst2=lst[0]+lst[1]
    
        
    if sum(lst2)==0:
        pass
    elif sum(lst2)==3:
        for i in range(n-2,n):
            for j in range(m-2,m):
                if mp[i][j]==1:
                    ans+=[i,j]
        rt+=ans
        while ans:
            w=ans.pop()
            h=ans.pop()
            mp[h][w]=1-mp[h][w]
        
        cnt+=1
    elif sum(lst2)<=2:
        zero=[]
        one=[]
        for i in range(n-2,n):
            for j in range(m-2,m):
                if mp[i][j]==0:
                    zero+=[i,j]
                else:
                    one+=[i,j]
        ans=zero[:4]+one[:2]
        rt+=ans
        cnt+=1
        while ans:
            w=ans.pop()
            h=ans.pop()
            mp[h][w]=1-mp[h][w]
        
        if len(one)==4:
            #end
            ans=zero[:4]+one[2:]
            rt+=ans
            cnt+=1
            while ans:
                w=ans.pop()
                h=ans.pop()
                mp[h][w]=1-mp[h][w]
            
        else:
            zero=[]
            one=[]
            for i in range(n-2,n):
                for j in range(m-2,m):
                    if mp[i][j]==0:
                        zero+=[i,j]
                    else:
                        one+=[i,j]
            ans=zero[:4]+one[:2]
            rt+=ans
            cnt+=1
            while ans:
                w=ans.pop()
                h=ans.pop()
                mp[h][w]=1-mp[h][w]
            
            ans=zero[:4]+one[2:]
            rt+=ans
            cnt+=1
            while ans:
                w=ans.pop()
                h=ans.pop()
                mp[h][w]=1-mp[h][w]
    
    return [rr+1 for rr in rt]
    #sm(mp)
    #print('cnt',cnt,n*m,lst,lst2,rt)

def check(n,m,mp,rt):
    nn=len(rt)//6
    ch=[i[:] for i in mp]
    
    for i in range(nn):
        x,y,s,t,u,v=[w-1 for w in rt[i*6:(i+1)*6]]
        if 0<=x<n and 0<=s<n and 0<=u<n and 0<=y<m and 0<=t<m and 0<=v<m:
            pass
        else:
            return False,"not in area"
        if max(x,s,u)-min(x,s,u)>1:
            return False,"not in square"
        if max(y,t,v)-min(y,t,v)>1:
            return False,"not in square"
        if len(set([(x,y),(s,t),(u,v)]))<3:
            return False,"same points"
        ch[x][y]=1-ch[x][y]
        ch[s][t]=1-ch[s][t]
        ch[u][v]=1-ch[u][v]
    tmp=0
    for i in ch:
        tmp+=sum(i)
    #show(ch)
    if tmp!=0:
        return False,"not 0",ch
    
    return True,"ok",ch

def sm(mp):
    show('-----------')
    for i in mp:
        show(*i)
    
T=I()

for _ in range(T):
    #show('(',_,'-----------')
    n,m=LI()
    mp=[]
    
    #n=0
    for i in range(n):
        mp+=[int(i) for i in input()],
        
    # test
    #n=RI(2,5)
    #m=RI(2,5)
    #mp=[RLI(m,0,1)for i in range(n)]
    #org=[i[:] for i in mp]
    # test
        
    
    rt=solve(n,m,mp)
    nn=len(rt)//6
    print(nn)
    for i in range(nn):
        print(*rt[i*6:(i+1)*6])
    
    