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 JAVA):

Solution:
import java.io.*;
import java.util.*;



import java.math.*;
import java.math.BigInteger;


public final class B
{    
	static PrintWriter out = new PrintWriter(System.out); 
	static StringBuilder ans=new StringBuilder();
	static FastReader in=new FastReader();
	//	static ArrayList<Pair> g[];
	static long mod=(long)998244353,INF=Long.MAX_VALUE;
	static boolean set[],col[]; 
	static int par[],tot[],partial[];
	static int D[],P[][];
	static int dp[][],sum=0,size[];
	static long seg[],fact[];
	//	static node1 seg[];
	//static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)};
	public static void main(String args[])throws IOException
	{				
		int T=i();
		while(T-->0)
		{
			int N=i(),M=i();
			char X[][]=new char[N+1][M+1];
			for(int i=1; i<=N; i++)X[i]=(" "+in.next()).toCharArray();
			int c=0;
			StringBuilder sb=new StringBuilder();
			if(N==2 && M%2!=0)
			{
				if(X[1][1]=='1')
				{
					c++;
					sb.append(1+" "+1+" ");change(1,1,X);
					sb.append(1+" "+2+" ");change(1,2,X);                	
					sb.append(2+" "+2+" ");change(2,2,X);
					sb.append("\n");
				}
				if(X[2][1]=='1')
				{
					c++;
					sb.append(2+" "+1+" ");change(1,1,X);
					sb.append(1+" "+2+" ");change(1,2,X);                	
					sb.append(2+" "+2+" ");change(2,2,X);
					sb.append("\n");
				}
				for(int i=2; i<M; i+=2)
				{
					//now we have our square
					int cnt=cnt(1,i,X);
					c+=f(1,i,cnt,X,sb);
				}
				ans.append(c+"\n"+sb+"\n");
				continue;
			}
			if(N%2!=0 && M==2)
			{
				if(X[1][1]=='1')
				{
					sb.append(1+" "+1+" ");change(1,1,X);
					sb.append(2+" "+1+" ");change(2,1,X);
					sb.append(2+" "+2+" ");change(2,2,X);
					c++;
					sb.append("\n");
				}
				if(X[1][2]=='1')
				{
					sb.append(1+" "+2+" ");change(1,2,X);
					sb.append(2+" "+1+" ");change(2,1,X);
					sb.append(2+" "+2+" ");change(2,2,X);
					c++;
					sb.append("\n");
				}
				for(int i=2; i<N; i+=2)
				{
					int cnt=cnt(i,M-1,X);
					c+=f(i,M-1,cnt,X,sb);
				}
				ans.append(c+"\n"+sb+"\n");
				continue;
			}
			for(int i=1; i<=N-2; i++)
			{
				for(int j=1; j<=M-2; j++)
				{
					if(X[i][j]=='1')
					{
						change(i,j,X);
						change(i+1,j,X);
						change(i+1,j+1,X);
						c++;
						sb.append(i+" "+j+" "+(i+1)+" "+(j)+" "+(i+1)+" "+(j+1)+"\n");
					}
				}
			}
			int a=1,b=1;
			if(N%2!=0)
			{
				a=2;
				if(X[1][M-1]=='1')
				{
					change(1,M-1,X);
					change(2,M-1,X);
					change(2,M,X);
					c++;
					sb.append(1+" "+(M-1)+" "+(2)+" "+(M-1)+" "+(2)+" "+(M)+"\n");

				}
				if(X[1][M]=='1')
				{

					change(1,M,X);
					change(2,M-1,X);
					change(2,M,X);
					c++;
					sb.append(1+" "+(M)+" "+(2)+" "+(M-1)+" "+(2)+" "+(M)+"\n");				
				}		 
			}
			if(M%2!=0)
			{
				b=2;
				if(X[N-1][1]=='1')
				{
					change(N-1,1,X);
					change(N-1,2,X);
					change(N,2,X);
					c++;
					sb.append((N-1)+" "+(1)+" "+(N-1)+" "+(2)+" "+(N)+" "+(2)+"\n");				

				}
				if(X[N][1]=='1')
				{
					change(N,1,X);
					change(N-1,2,X);
					change(N,2,X);
					c++;
					sb.append((N)+" "+(1)+" "+(N-1)+" "+(2)+" "+(N)+" "+(2)+"\n");						
				}
			}
			//			System.out.println(sb);
			//print(X);
			c+=f(a,b,X,N,M,sb);
			ans.append(c+"\n"+sb);
			//System.out.println(sb);
		}
		out.println(ans);
		out.close();
	}
	static void print (char X[][])
	{
		for (char x[]:X)print(x);

	}
	static int  f(int a,int b,char X[][],int N,int M,StringBuilder sb)
	{
		int c=0;

		for(int i=a; i<N; i+=2)
		{
			int cnt=cnt(i,M-1,X);
			//System.out.println("cnt== "+cnt);
			c+=f(i,M-1,cnt,X,sb);
		}
//		print(X);
		for(int j=b; j<M-2; j+=2)
		{
			//System.out.println("RUn"+" "+(N-1)+" "+j);
			int cnt=cnt(N-1,j,X);
//			System.out.println(cnt);
			//System.out.println("calling for--> "+(N-1)+" "+j+" cnt--> "+cnt);
			c+=f(N-1,j,cnt,X,sb);
		}
		return c;
	}

	static int f(int i,int j,int cnt,char X[][],StringBuilder sb)
	{
		if(cnt==0)
		{
			return 0;
		}
		else if(cnt==1)
		{
			if(X[i+1][j+1]=='1')
			{
				sb.append((i+1)+" "+(j+1)+" ");
				change(i+1,j+1,X);
			}
			else 
			{
				change(i,j,X);
				sb.append(i+" "+j+" ");
			}
			sb.append((i)+" "+(j+1)+" ");change(i,j+1,X);
			sb.append((i+1)+" "+(j)+" "+"\n");change(i+1,j,X);
//			System.out.println("for--> 1");
//			print(X);
			f(i,j,2,X,sb);
			return 3;
		}
		else if(cnt==2)
		{
			boolean f=true;
			for(int x=i; x<=i+1; x++)
			{
				for(int y=j; y<=j+1; y++)
				{
					//System.out.println(x+" "+y);
					if(X[x][y]=='0')
					{
						sb.append(x+" "+y+" ");	
						change(x,y,X);
					}
					else if(f)
					{
						sb.append(x+" "+y+" ");	
						change(x,y,X);
						f=false;
					}

				}
			}

			sb.append("\n");
//			System.out.println("for--> 2");
//			print(X);
			//System.out.println("DONE FOR--> 2"+sb);
			f(i,j,3,X,sb);
			return 2;
		}
		else if(cnt==3)
		{
			for(int x=i; x<=i+1; x++)
			{
				for(int y=j; y<=j+1; y++)
				{
					if(X[x][y]=='1')
					{
						sb.append(x+" "+y+" ");
						change(x,y,X);						
					}
				}
			}
//			System.out.println("for--> 3");
//			print(X);
			sb.append("\n");
			return 1;
		}
		else
		{
			sb.append(i+" "+j+" ");change(i,j,X);
			sb.append(i+" "+(j+1)+" ");change(i,j+1,X);
			sb.append((i+1)+" "+j+"\n");change(i+1,j,X);
//			System.out.println("for--> 4");
//			print(X);
			f(i,j,1,X,sb);
			return 4;
		}
	}
	static int cnt(int i,int j,char X[][])
	{
		int c=0;
		if(X[i][j]=='1')c++;
		if(X[i+1][j]=='1')c++;
		if(X[i][j+1]=='1')c++;
		if(X[i+1][j+1]=='1')c++;		
		return c;
	}
	static void change(int i,int j,char X[][])
	{
		if(X[i][j]=='1')X[i][j]='0';
		else X[i][j]='1';
	}
	static long nCr(long n,long c)
	{
		long s=0;
		long a=fact(n)%mod;
		long b=fact(c)%mod;
		long d=b;
		b=pow(b,mod-2)%mod;
		d=pow(d,mod-2)%mod;
		long x=(b*d)%mod;
		s=(a*x)%mod;
		return s;
	}
	static int f(int i,int l,int r,int N)
	{
		while(r-l>1)
		{
			int m=(l+r)/2;
			long g=ask(1,0,N-2,i,m);
			if(g>1)l=m;
			else r=m;
		}
		return l;
	}
	static void build(int v,int tl,int tr,long A[])
	{
		if(tl==tr)
		{
			seg[v]=A[tl];
			return;
		}
		int tm=(tl+tr)/2;
		build(v*2,tl,tm,A);
		build(v*2+1,tm+1,tr,A);
		seg[v]=GCD(seg[v*2],seg[v*2+1]);

	}
	static long ask(int v,int tl,int tr,int l,int r)
	{
		if(l>r)return 0;
		if(tl==l && tr==r)
		{
			return seg[v];
		}
		int tm=(tl+tr)/2;		
		long a=ask(v*2,tl,tm,l,Math.min(tm, r));
		long b=ask(v*2+1,tm+1,tr,Math.max(l,tm+1),r);
		//		System.out.print(tl+" "+tm+" "+tr+" ");
		//		System.out.println(a+" "+b);
		return GCD(a,b);
	}
	static int cost(int s,int A[],int B[],int K)
	{
		int c=0;
		int N=A.length;
		for(int i=0; i<N/2; i++)
		{
			if(B[i]==s)continue;
			if(s>B[i])
			{
				//we need to add
				int max=Math.max(K-A[i], K-A[N-i-1]);
				if(s-B[i]<=max)c++;
				else c+=2;
			}
			else
			{
				int max=Math.max(A[i]-1, (K-A[N-i-1])-1);
				if(s-B[i]<=max)c++;
				else c+=2;
			}
		}
		return c;
	}


	static int [] sub(int A[],int B[])
	{
		int N=A.length;
		int f[]=new int[N];
		for(int i=N-1; i>=0; i--)
		{
			if(B[i]<A[i])
			{
				B[i]+=26;
				B[i-1]-=1;
			}
			f[i]=B[i]-A[i];
		}
		for(int i=0; i<N; i++)
		{
			if(f[i]%2!=0)f[i+1]+=26;
			f[i]/=2;
		}
		return f;
	}
	static int[] f(int  N)
	{
		char X[]=in.next().toCharArray();
		int A[]=new int[N];
		for(int i=0; i<N; i++)A[i]=X[i]-'a';
		return A;
	}




	static int max(int a ,int b,int c,int d)
	{
		a=Math.max(a, b);
		c=Math.max(c,d);
		return Math.max(a, c);
	}
	static int min(int a ,int b,int c,int d)
	{
		a=Math.min(a, b);
		c=Math.min(c,d);
		return Math.min(a, c);
	}



	static HashMap<Integer,Integer> Hash(int A[])
	{
		HashMap<Integer,Integer> mp=new HashMap<>();
		for(int a:A)
		{
			int f=mp.getOrDefault(a,0)+1;
			mp.put(a, f);
		}
		return mp;
	}
	static long mul(long a, long b)
	{
		return ( a %mod * 1L * b%mod )%mod;
	}
	static void swap(int A[],int a,int b)
	{
		int t=A[a];
		A[a]=A[b];
		A[b]=t;
	}


	static int find(int a)
	{
		if(par[a]<0)return a;
		return par[a]=find(par[a]);
	}
	static void union(int a,int b)
	{
		a=find(a);
		b=find(b);
		if(a!=b)
		{
			par[a]+=par[b];
			par[b]=a;
		}
	}
	static boolean isSorted(int A[])
	{
		for(int i=1; i<A.length; i++)
		{
			if(A[i]<A[i-1])return false;
		}
		return true;
	}

	static boolean isDivisible(StringBuilder X,int i,long num)
	{
		long r=0;
		for(; i<X.length(); i++)
		{
			r=r*10+(X.charAt(i)-'0');
			r=r%num;
		}
		return r==0;
	}    



	static int lower_Bound(int A[],int low,int high, int x) 
	{ 
		if (low > high) 
			if (x >= A[high]) 
				return A[high]; 

		int mid = (low + high) / 2; 

		if (A[mid] == x) 
			return A[mid]; 

		if (mid > 0 && A[mid - 1] <= x && x < A[mid]) 
			return A[mid - 1]; 

		if (x < A[mid]) 
			return lower_Bound( A, low, mid - 1, x); 

		return lower_Bound(A, mid + 1, high, x); 
	} 

	static String f(String A)
	{
		String X="";
		for(int i=A.length()-1; i>=0; i--)
		{
			int c=A.charAt(i)-'0';
			X+=(c+1)%2;
		}
		return X;
	}

	static void sort(long[] a) //check for long
	{
		ArrayList<Long> l=new ArrayList<>();
		for (long i:a) l.add(i);
		Collections.sort(l);
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}

	static String swap(String X,int i,int j)
	{
		char ch[]=X.toCharArray();
		char a=ch[i];
		ch[i]=ch[j];
		ch[j]=a;
		return new String(ch);
	}

	static int sD(long n)  
	{  

		if (n % 2 == 0 )  
			return 2;  

		for (int i = 3; i * i <= n; i += 2) {  
			if (n % i == 0 )  
				return i;  
		}  

		return (int)n;  
	}  

	static void setGraph(int N)
	{
		tot=new int[N+1];
		partial=new int[N+1];
		D=new int[N+1];
		P=new int[N+1][(int)(Math.log(N)+10)];
		set=new boolean[N+1];
		//		g=new ArrayList[N+1];
		for(int i=0; i<=N; i++)
		{

			//			g[i]=new ArrayList<>();		
			D[i]=Integer.MAX_VALUE;
			//D2[i]=INF;
		}
	}



	static  long pow(long a,long b)
	{
		long pow=1;
		long x=a;
		while(b!=0)
		{
			if((b&1)!=0)pow=(pow*x)%mod;
			x=(x*x)%mod;
			b/=2;
		}
		return pow;
	}

	static long toggleBits(long x)//one's complement || Toggle bits
	{
		int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;

		return ((1<<n)-1)^x;
	}

	static int countBits(long a)
	{
		return (int)(Math.log(a)/Math.log(2)+1);
	}

	static long fact(long N)
	{ 
		long n=2; 
		if(N<=1)return 1;
		else
		{
			for(long i=3; i<=N; i++)n=(n*i)%mod;
		}
		return n;
	}

	static int kadane(int A[])
	{
		int lsum=A[0],gsum=A[0];
		for(int i=1; i<A.length; i++)
		{
			lsum=Math.max(lsum+A[i],A[i]);
			gsum=Math.max(gsum,lsum);
		}
		return gsum;
	}

	static void sort(int[] a) {
		ArrayList<Integer> l=new ArrayList<>();
		for (int i:a) l.add(i);
		Collections.sort(l);
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}

	static boolean isPrime(long N)
	{
		if (N<=1)  return false; 
		if (N<=3)  return true; 
		if (N%2 == 0 || N%3 == 0) return false; 
		for (int i=5; i*i<=N; i=i+6) 
			if (N%i == 0 || N%(i+2) == 0) 
				return false; 
		return true; 
	}
	static void print(char A[])
	{
		for(char c:A)System.out.print(c+" ");
		System.out.println();
	}
	static void print(boolean A[])
	{
		for(boolean c:A)System.out.print(c+" ");
		System.out.println();
	}
	static void print(int A[])
	{
		for(int a:A)System.out.print(a+" ");
		System.out.println();	
	}
	static void print(long A[])
	{
		for(long i:A)System.out.print(i+ " ");
		System.out.println();

	}
	static void print(boolean A[][])
	{
		for(boolean a[]:A)print(a);
	}
	static void print(long A[][])
	{
		for(long a[]:A)print(a);
	}
	static void print(int A[][])
	{
		for(int a[]:A)print(a);
	}
	static void print(ArrayList<Integer> A)
	{
		for(int a:A)System.out.print(a+" ");
		System.out.println();
	}

	static int i()
	{
		return in.nextInt();
	}

	static long l()
	{
		return in.nextLong();
	}

	static int[] input(int N){
		int A[]=new int[N];
		for(int i=0; i<N; i++)
		{
			A[i]=in.nextInt();
		}
		return A;
	}

	static long[] inputLong(int N)     {
		long A[]=new long[N];
		for(int i=0; i<A.length; i++)A[i]=in.nextLong();
		return A;
	}

	static long GCD(long a,long b) 
	{
		if(b==0)
		{
			return a;
		}
		else return GCD(b,a%b );
	}

}


//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
class FastReader
{
	BufferedReader br;
	StringTokenizer st;
	public FastReader()
	{
		br=new BufferedReader(new InputStreamReader(System.in));
	}

	String next()
	{
		while(st==null || !st.hasMoreElements())
		{
			try
			{
				st=new StringTokenizer(br.readLine());
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

	int nextInt()
	{
		return Integer.parseInt(next());
	}

	long nextLong()
	{
		return Long.parseLong(next());
	}

	double nextDouble()
	{
		return Double.parseDouble(next());
	}

	String nextLine()
	{
		String str="";
		try
		{
			str=br.readLine();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return str;
	}

}