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.util.*;
import java.io.*;
public class EdA {
	static long[] mods = {1000000007, 998244353, 1000000009};
	static long mod = mods[0];
	public static MyScanner sc;
    public static PrintWriter out;
    static int[][] grid;
    static int[][] newgrid;
    static ArrayList<Operation> array;
    static int n;
    static int m;
    public static void run(){
    	for(int j = 0;j+1<n;j+=2){
 			for(int k = 0;k+1<m;k+=2){
 					
 					if (j+1 < n && k+1 < m){
 						fix(j, k);
 						grid[j][k] = 0;
 						grid[j+1][k] = 0;
 						grid[j][k+1] = 0;
 						grid[j+1][k+1] = 0;
 					}
 					else if (j+1 < n && k-1 >=0){
 						fix(j, k-1);
 						grid[j][k-1] = 0;
 						grid[j+1][k-1] = 0;
 						grid[j][k] = 0;
 						grid[j+1][k] = 0;
 					}
 					else if (j-1 >=0 && k+1 < m){
 						fix(j-1, k);
 						grid[j-1][k] = 0;
 						grid[j-1][k+1] = 0;
 						grid[j][k] = 0;
 						grid[j][k+1] = 0;
 					}
 					else if (j-1 >=0 && k-1>=0){
 						fix(j-1, k-1);
 						grid[j-1][k-1] = 0;
 						grid[j-1][k] = 0;
 						grid[j][k-1] = 0;
 						grid[j][k] = 0;
 					}
 					
 					
 					
 					
 				
 			}
 		}
    }
   
	public static void main(String[] omkar) throws Exception{
		// TODO Auto-generated method stub
 		sc = new MyScanner();
 		out = new PrintWriter(System.out);
 		int t = sc.nextInt();
 		for(int i = 0;i<t;i++){
//	 		String input1 = bf.readLine().trim();
//	 		String input2 = bf.readLine().trim();
//			COMPARING INTEGER OBJECTS U DO DOT EQUALS NOT ==
	 		n = sc.nextInt();
	 		m = sc.nextInt();
	 		grid = new int[n][m];
	 		array = new ArrayList<Operation>();
	 		newgrid = new int[n][m];
	 		for(int j =0 ;j<n;j++){
	 			String s = sc.next();
	 			for(int k = 0;k<m;k++){
	 				grid[j][k] = Integer.parseInt(s.charAt(k)+ "");
	 				newgrid[j][k] = Integer.parseInt(s.charAt(k)+ "");
	 			}
	 		}
	 		if (n%2 == 1 || m%2 == 1){
	 			if (grid[n-1][m-1] == 1){
	 				z11(n-2, m-2);
	 			}
	 		}
	 		if (m%2 == 1){
	 			for(int j = n-2;j>0;j--){
	 				if (grid[j][m-1] == 1){
	 					z10(j-1, m-2);
	 				}
	 			}
	 			if (grid[0][m-1] == 1){
	 				z00(0, m-2);
	 			}
	 		}
	 		if (n%2 == 1){
	 			for(int j = m-2;j>0;j--){
	 				if (grid[n-1][j] == 1){
	 					z01(n-2, j-1);
	 				}
	 			}
	 			if (grid[n-1][0] == 1){
	 				z00(n-2, 0);
	 			}
	 		}
	 		run();
	 		out.println(array.size());
	 		for(Operation j : array){
	 			out.println((j.x1+1) + " " + (j.y1+1) + " " + (j.x2+1) + " " + (j.y2+1) + " " + (j.x3+1) + " " + (j.y3+1));
	 		}
	 		
	 		
	 		 
//	 		for(int j = 0;j<array.length;j++){
//	 			out.print(array[j] + " ");
//	 		}
//	 		out.println();
 		}
 		out.println();
	 		
 		out.close();	
 	}
	public static void fix(int r, int c){
		int count = 0;
		if (grid[r][c] == 1)
			count++;
		if (grid[r+1][c] == 1)
			count++;
		if (grid[r][c+1] == 1)
			count++;
		if (grid[r+1][c+1] == 1)
			count++;
		
		if (count == 1){
			if (grid[r][c] == 1){
				z00(r, c);
				z10(r, c);
				z01(r, c);
			}
			else if (grid[r+1][c] == 1){
				z00(r, c);
				z10(r, c);
				z11(r, c);
			}
			else if (grid[r][c+1] == 1){
				z00(r, c);
				z01(r, c);
				z11(r, c);
			}
			else if (grid[r+1][c+1] == 1){
				z10(r, c);
				z01(r, c);
				z11(r, c);
			}
		}
		else if (count == 2){
			if (grid[r][c] == 1 && grid[r+1][c] == 1){
				z01(r, c);
				z11(r, c);
			}
			else if (grid[r+1][c] == 1 && grid[r+1][c+1] == 1){
				z00(r, c);
				z01(r, c);
			}
			else if (grid[r+1][c+1] == 1 && grid[r][c+1] == 1){
				z00(r, c);
				z10(r, c);
			}
			else if (grid[r][c+1] == 1 && grid[r][c] == 1){
				z10(r, c);
				z11(r, c);
			}
			else if (grid[r][c] == 1 && grid[r+1][c+1] == 1){
				z00(r, c);
				z11(r, c);
			}
			else if (grid[r+1][c] == 1 && grid[r][c+1] == 1){
				z10(r, c);
				z01(r, c);
			}
			
		}
		else if (count == 3){
			if (grid[r][c] == 0){
				z11(r, c);
			}
			else if (grid[r+1][c] == 0){
				z01(r, c);
			}
			else if (grid[r][c+1] == 0){
				z10(r, c);
			}
			else if (grid[r+1][c+1] == 0){
				z00(r, c);
			}
		}
		else if (count == 4){
			z00(r, c);
			z11(r, c);
			z01(r, c);
			z10(r, c);
		}
	}
	
	public static void z00(int r, int c){
		array.add(new Operation(r, c, r+1, c, r, c+1));
		grid[r][c] = 1-grid[r][c];
		grid[r+1][c] = 1-grid[r+1][c];
		grid[r][c+1] = 1-grid[r][c+1];
	}
	public static void z01(int r, int c){
		array.add(new Operation(r, c+1, r, c, r+1, c+1));
		grid[r][c] = 1-grid[r][c];
		grid[r][c+1] = 1-grid[r][c+1];
		grid[r+1][c+1] = 1-grid[r+1][c+1];
	}
	public static void z10(int r, int c){
		array.add(new Operation(r+1, c, r, c, r+1, c+1));
		grid[r][c] = 1-grid[r][c];
		grid[r+1][c] = 1-grid[r+1][c];
		grid[r+1][c+1] = 1-grid[r+1][c+1];
	}
	public static void z11(int r, int c){
		array.add(new Operation(r+1, c+1, r+1, c, r, c+1));
		grid[r+1][c] = 1-grid[r+1][c];
		grid[r][c+1] = 1-grid[r][c+1];
		grid[r+1][c+1] = 1-grid[r+1][c+1];
	}
	static class Operation{
		private int x1;
		private int y1;
		private int x2;
		private int y2;
		private int x3;
		private int y3;
		public Operation(int x1, int y1, int x2, int y2, int x3, int y3){
			this.x1 = x1;
			this.x2 = x2;
			this.x3 = x3;
			this.y1 = y1;
			this.y2 = y2;
			this.y3 = y3;
		}
	}
	public static void sort(int[] array){
		ArrayList<Integer> copy = new ArrayList<Integer>();
		for (int i : array)
			copy.add(i);
		Collections.sort(copy);
		for(int i = 0;i<array.length;i++)
			array[i] = copy.get(i);
	}
	static String[] readArrayString(int n){
		String[] array = new String[n];
		for(int j =0 ;j<n;j++)
			array[j] = sc.next();
		return array;
	}
	static int[] readArrayInt(int n){
    	int[] array = new int[n];
    	for(int j = 0;j<n;j++)
    		array[j] = sc.nextInt();
    	return array;
    }
	static int[] readArrayInt1(int n){
		int[] array = new int[n+1];
		for(int j = 1;j<=n;j++){
			array[j] = sc.nextInt();
		}
		return array;
	}
	static long[] readArrayLong(int n){
		long[] array = new long[n];
		for(int j =0 ;j<n;j++)
			array[j] = sc.nextLong();
		return array;
	}
	static double[] readArrayDouble(int n){
		double[] array = new double[n];
		for(int j =0 ;j<n;j++)
			array[j] = sc.nextDouble();
		return array;
	}
	static int minIndex(int[] array){
		int minValue = Integer.MAX_VALUE;
		int minIndex = -1;
		for(int j = 0;j<array.length;j++){
			if (array[j] < minValue){
				minValue = array[j];
				minIndex = j;
			}
		}
		return minIndex;
	}
	static int minIndex(long[] array){
		long minValue = Long.MAX_VALUE;
		int minIndex = -1;
		for(int j = 0;j<array.length;j++){
			if (array[j] < minValue){
				minValue = array[j];
				minIndex = j;
			}
		}
		return minIndex;
	}
	static int minIndex(double[] array){
		double minValue = Double.MAX_VALUE;
		int minIndex = -1;
		for(int j = 0;j<array.length;j++){
			if (array[j] < minValue){
				minValue = array[j];
				minIndex = j;
			}
		}
		return minIndex;
	}
	static long power(long x, long y){
		if (y == 0)
			return 1;
		if (y%2 == 1)
			return (x*power(x, y-1))%mod;
		return power((x*x)%mod, y/2)%mod;
	}
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            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;
        }
        
    }	
}

//StringJoiner sj = new StringJoiner(" "); 
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is