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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class Solution{
	
	static ArrayList<ArrayList<Integer>> ans;
	static int[][] a;
	
	
    public static void main(String[] args) throws IOException {
    	
		
    	FastScanner fs = new FastScanner();
    	PrintWriter out = new PrintWriter(System.out);
    	
    		
    	int tt = fs.nextInt();
    	outer:
    	while(tt-->0) {
    		
    		ans = new ArrayList<ArrayList<Integer>>();
    		
    		int n = fs.nextInt(), m = fs.nextInt();
    		
    		a = new int[n+1][m+1];
    		
    		for(int i=1;i<=n;i++) {
    			char[] s = fs.next().toCharArray();
    			for(int j=1;j<=m;j++) {
    				a[i][j] = s[j-1]-'0';
    			}
    		}
    		
    		if(n%2==1) {
    			for(int i=1;i<m;i++) {
    				if(a[n][i]==1) {
    					ArrayList<Integer> list = new ArrayList<>();
    					list.add(n); list.add(i);
    					list.add(n-1); list.add(i); 
    					list.add(n-1); list.add(i+1);
    					ans.add(list);
    					a[n][i] = 0;
    					a[n-1][i] ^= 1;
    					a[n-1][i+1] ^= 1;
    				}
    			}
    			if(a[n][m]==1) {
    				ArrayList<Integer> list = new ArrayList<>();
    				list.add(n); list.add(m);
    				list.add(n-1); list.add(m);
    				list.add(n-1); list.add(m-1);
    				ans.add(list);
    				a[n][m] = 0;
    				a[n-1][m] ^= 1;
    				a[n-1][m-1] ^= 1;
    			}
    			n -= 1;
    		}
    		
    		if(m%2==1) {
    			for(int i=1;i<n;i++) {
    				if(a[i][m]==1) {
    					ArrayList<Integer> list = new ArrayList<>();
    					list.add(i); list.add(m);
    					list.add(i); list.add(m-1);
    					list.add(i+1); list.add(m-1);
    					ans.add(list);
    					a[i][m] = 0;
    					a[i][m-1] ^= 1;
    					a[i+1][m-1] ^= 1;
    				}
    			}
    			if(a[n][m]==1) {
    				ArrayList<Integer> list = new ArrayList<>();
    				list.add(n); list.add(m);
    				list.add(n); list.add(m-1);
    				list.add(n-1); list.add(m-1);
    				ans.add(list);
    				a[n][m] = 0;
    				a[n][m-1] ^= 1;
    				a[n-1][m-1] ^= 1;
    			}
    			m -= 1;
    		}
    		
    		
    		for(int i=0;i<n;i+=2) {
    			for(int j=0;j<m;j+=2) {
    				solve(i, j);
    			}
    		}
    		
    		out.println(ans.size());
    		for(ArrayList<Integer> l: ans) {
    			for(int i: l) {
    				out.print(i+" ");
    			}
    			out.println();
    		}
    		
    		
    		
    		
    		
    		
    	}
    	
    	out.close();
    	
    }
    
    
    static void solve(int dx, int dy) {
    	int cnt = 0;
    	for(int i= 1 +dx; i<=dx+2;i++) {
    		for(int j=1+dy;j<=dy+2;j++) {
    			cnt += a[i][j];
    		}
    	}
    	
    	if(cnt==0) return;
    	
    	if(cnt==3) {
    		ArrayList<Integer> list = new ArrayList<>();
    		for(int i=1+dx;i<=2+dx;i++) {
    			for(int j=1+dy;j<=2+dy;j++) {
    				if(a[i][j]==1) {
    					list.add(i); list.add(j);
    					a[i][j] = 0;
    				}
    			}
    		}
    		ans.add(list);
    		return;
    	}
    	
    	if(cnt==2) {
    		boolean bool = true;
    		ArrayList<Integer> list = new ArrayList<>();
    		
    		for(int i=1+dx;i<=2+dx;i++) {
    			for(int j=1+dy;j<=2+dy;j++) {
    				if(a[i][j]==0) {
    					list.add(i); list.add(j);
    					a[i][j] = 1;
    				}
    				else if(bool) {
    					list.add(i); list.add(j);
    					bool = false;
    					a[i][j] = 0;
    				}
    			}
    		}
    		
    		ans.add(list);
    		solve(dx, dy);
    	}
    	
    	if(cnt==1) {
    		int f = 0;
    		ArrayList<Integer> list = new ArrayList<>();
    		
    		for(int i=1+dx;i<=2+dx;i++) {
    			for(int j=1+dy;j<=2+dy;j++) {
    				if(a[i][j]==1) {
    					list.add(i); list.add(j);
    					a[i][j] = 0;
    				}
    				else if(f<2) {
    					list.add(i); list.add(j);
    					f++;
    					a[i][j] = 1;
    				}
    			}
    		}
    		
    		ans.add(list);
    		solve(dx, dy);
    	}
    	
    	if(cnt==4) {
    		int f = 0;
    		ArrayList<Integer> list = new ArrayList<>();
    		
    		for(int i=1+dx;i<=2+dx;i++) {
    			for(int j=1+dy;j<=2+dy;j++) {
    				if(a[i][j]==1 && f<3) {
    					list.add(i); list.add(j);
    					f++;
    					a[i][j] = 0;
    				}
    			}
    		}
    		
    		ans.add(list);
    		solve(dx, dy);
    	}
    	
    }
    
    
    
    
    
    
    
    static final Random random=new Random();
    
    static <T> void shuffle(T[] arr) {
    	int n = arr.length;
    	for(int i=0;i<n;i++	) {
    		int k = random.nextInt(n);
    		T temp = arr[k]; arr[k] = arr[i]; arr[i] = temp;
    	}
    }
    
    	
    static void ruffleSort(int[] a) {
    	int n=a.length;//shuffle, then sort 
    	for (int i=0; i<n; i++) {
    		int oi=random.nextInt(n); int temp=a[oi];
    		a[oi]=a[i]; a[i]=temp;
    	}
    	Arrays.sort(a);
    }
    
    static void ruffleSort(long[] a) {
    	int n=a.length;//shuffle, then sort 
    	for (int i=0; i<n; i++) {
    		int oi=random.nextInt(n); long temp=a[oi];
    		a[oi]=a[i]; a[i]=temp;
    	}
    	Arrays.sort(a);
    }
   
  
    
    static void reverse(int[] arr, int l, int r) {
    	for(int i=l;i<l+(r-l)/2;i++){
    		int temp = arr[i]; arr[i] = arr[r-i+l-1]; arr[r-i+l-1] = temp;
    	}
    }
    
    static void reverse(long[] arr, int l, int r) {
    	for(int i=l;i<l+(r-l)/2;i++){
    		long temp = arr[i]; arr[i] = arr[r-i+l-1]; arr[r-i+l-1] = temp;
    	}
    }
    
    
    static <T> void reverse(T[] arr, int l, int r) {
    	for(int i=l;i<l+(r-l)/2;i++) {
    		T temp = arr[i]; arr[i] = arr[r-i+l-1]; arr[r-i+l-1] = temp;
    	}
    }
    	
    	
    static class FastScanner{
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer st = new StringTokenizer("");
     
    	public String next(){
    		while(!st.hasMoreElements()){
    			try{
    				st = new StringTokenizer(br.readLine());
    			} catch(IOException e){
    				e.printStackTrace();
    			}
    		}
    		return st.nextToken();
    	}
    		
    	public String nextLine() throws IOException {
    		return br.readLine();
    	}
    		
    	public int nextInt(){
    		return Integer.parseInt(next());
    	}
     
    	public int[] readArray(int n){
    		int[] a = new int[n];
    		for(int i=0;i<n;i++)
    			a[i] = nextInt();
    		return a;
    	}
    		
    	public long nextLong() {
    		return Long.parseLong(next());
    	}
    		
    	public char nextChar() {
    		return next().toCharArray()[0];
    	}
    }
   	
}