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 java.io.*;
import java.util.*;
 
 
public class Main{
	static int[][] g;
	static List<String> sol;
	static void flip(int i, int j) {
		g[i][j] = 1 - g[i][j];
	}
	static int sum;
	static MoveSet mov;
	static int basei, basej;
	
	static void solveHalf2Top(int i, int j) {
		basei = i;
		basej = j;
		int tsum = g[i][j] + g[i][j+1];
		if (tsum == 0) return;
		if (tsum == 1) {
			if (g[i][j] == 0) {
				mov.add(i, j+1);
			}
			else {
				mov.add(i, j);
			}
			mov.add(i+1, j+1);
			mov.add(i+1, j);
		}
		else {
			mov.add(i, j+1);
			mov.add(i, j);
			mov.add(i+1, j+1);
		}
		sol.add(mov.toString());
	}
	static void solveHalf2Left(int i, int j) {
		basei = i;
		basej = j;
		int tsum = g[i][j] + g[i+1][j];
		if (tsum == 0) return;
		if (tsum == 1) {
			if (g[i][j] == 0) {
				mov.add(i+1, j);
			}
			else {
				mov.add(i, j);
			}
			mov.add(i+1, j+1);
			mov.add(i, j+1);
		}
		else {
			mov.add(i+1, j);
			mov.add(i, j);
			mov.add(i+1, j+1);
		}
		sol.add(mov.toString());
	}
	
	static void solve2(int i, int j) {
		basei = i;
		basej = j;
		sum = g[i][j] + g[i+1][j] + g[i][j+1] + g[i+1][j+1];
		
		if (sum == 4) {
			mov.add(i, j);
			mov.add(i, j+1);
			mov.add(i+1, j);
			// lower right = 1
			sol.add(mov.toString());
		}
		if (sum == 1) {
			if (g[i][j] == 1) {
				mov.add(i, j+1);
				mov.add(i+1, j);
				mov.add(i, j);
			}
			else if (g[i][j+1] == 1) {
				mov.add(i, j);
				mov.add(i, j+1);
				mov.add(i+1, j+1);
			}
			else if (g[i+1][j+1] == 1) {
				mov.add(i, j+1);
				mov.add(i+1, j);
				mov.add(i+1, j+1);
			}
			else if (g[i+1][j] == 1) {
				mov.add(i+1, j);
				mov.add(i, j);
				mov.add(i+1, j+1);
			}
			sol.add(mov.toString());
		}
		if (sum == 2) {
			// if its 00 11 or 0101. 0011 do opposite side, else handle diagonal seperately
			
			
			if (g[i][j] != g[i+1][j+1]) {
				if (g[i][j] == 1 && (g[i+1][j] == 1 || g[i][j+1] == 1)) {
					
					// do 01 
					//    11
				mov.add(i+1, j+1);
				mov.add(i+1, j);
				mov.add(i, j+1);
			}
			else {
				// do 11
				//    10
				mov.add(i, j);
				mov.add(i+1, j);
				mov.add(i, j+1);
			}
			}
			else {
				// diag
				if (g[i][j] == 0) {
					// 1 1
					// 0 1
					mov.add(i, j);
					mov.add(i, j+1);
					mov.add(i+1, j+1);
				}
				else {
					// do 11
					//    10
					mov.add(i, j);
					mov.add(i+1, j);
					mov.add(i, j+1);
				}
			}
			sol.add(mov.toString());
		}
		
		if (sum ==3) {
			if (g[i][j] == 0) {
				mov.add(i, j+1);
				mov.add(i+1, j);
				mov.add(i+1, j+1);
			}
			else if (g[i][j+1] == 0) {
				mov.add(i, j);
				mov.add(i+1, j);
				mov.add(i+1, j+1);
			}
			else if (g[i+1][j+1] == 0) {
				mov.add(i, j+1);
				mov.add(i+1, j);
				mov.add(i, j);
			}
			else if (g[i+1][j] == 0) {
				mov.add(i, j+1);
				mov.add(i, j);
				mov.add(i+1, j+1);
			}
			sol.add(mov.toString());
		}
		
	}
	
	//public static Reader sc = new Reader();
	public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	
	static class MoveSet{
		StringBuilder solve;
		public MoveSet() {
			solve = new StringBuilder();
		}
		public void add(int i, int j) {
			int ti = i+1;
			int tj = j+1;
			solve.append("" + ti + " " + tj + " ");
			flip(i,j);
			ti = basei;
			tj = basej;
			sum = g[ti][tj] + g[ti+1][tj] + g[ti][tj+1] + g[ti+1][tj+1];
		}
		public String toString() {
			String res = solve.toString();
			solve = new StringBuilder();
			return res;
		}
	}
   public static void main(String[] args) throws IOException{
	basei = 0;
	basej= 0;
	int t = Integer.parseInt(br.readLine());
	while (t-->0){
		sum = 0;
		mov = new MoveSet();
		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		g = new int[n][m];
		for (int i = 0; i < n; i++) {
			String s = br.readLine();
			for (int j = 0; j < m; j++) {
				g[i][j] = s.charAt(j) - '0';
				//System.out.print(g[i][j] + " ");
			}
			//System.out.println();
		}
		//System.out.println("FUCK ME " + n + " " + m);
		sol = new ArrayList<String>();
		if (n % 2 == 0 && m % 2 == 0) {
			// get 2x2 cases
			for (int i = 0; i < n; i+=2) {
				for (int j = 0; j < m; j += 2) {
					solve2(i, j);
				}
			}
		}
		else if (n % 2 == 1 && m % 2 == 0) {
			// 2x2 and 2x3, same with other odd-even one
			// cols are even, so do 3x2s for the top.
			// and then, do 2x2s for the rest of the bottom.
			for (int c = 0; c < m; c+=2) {
				solveHalf2Top(0, c);
				solve2(1,c);
			}
			for (int i = 3; i < n; i+=2) {
				for (int j = 0; j < m; j+=2) {
					solve2(i,j);
				}
			}
		}
		else if (n % 2 == 0 && m % 2 == 1) {
			for (int r = 0; r < n; r+=2) {
				solveHalf2Left(r,0);
				solve2(r,1);
				//System.out.println(r + " 0");
			}
			for (int i = 0; i < n; i+=2) {
				for (int j = 3; j < m; j+=2) {
					solve2(i,j);
				}
			}
		}
		else {
			// Do a 3x3 case, then split up into a bunch of 2x2 and 3x2 cases.
			solveHalf2Left(0,0);
			solveHalf2Top(0,1);
			solveHalf2Left(1,0);
			solve2(1,1);
			
			for (int c = 3; c < m; c+=2) {
				solveHalf2Top(0, c);
				solve2(1,c);
			}
			
			for (int r = 3; r < n; r+=2) {
				solveHalf2Left(r,0);
				solve2(r,1);
			}
			
			for (int i = 3; i < n; i+=2) {
				for (int j = 3; j < m; j += 2) {
					solve2(i, j);
				}
			}
			// all odd
		}
		out.println(sol.size());
		for (String str: sol) {
			out.println(str);
		}
	}
	
	out.close();
   }
   static long ceil(long a, long b) {
		return (a+b-1)/b;
	}
  
   static long powMod(long base, long exp, long mod) {
	   if (base == 0 || base == 1) return base;
	   if (exp == 0) return 1;
	   if (exp == 1) return base % mod;
	   long R = powMod(base, exp/2, mod) % mod;
	   R *= R;
	   R %= mod;
	   if ((exp & 1) == 1) {
		   return base * R % mod;
	   }
	   else return R % mod;
   }
   
   static long pow(long base, long exp) {
	   if (base == 0 || base == 1) return base;
	   if (exp == 0) return 1;
	   if (exp == 1) return base;
	   long R = pow(base, exp/2);
	   if ((exp & 1) == 1) {
		   return R * R * base;
	   }
	   else return R * R;
   }
   

 
   static class Reader 
   { 
       final private int BUFFER_SIZE = 1 << 16; 
       private DataInputStream din; 
       private byte[] buffer; 
       private int bufferPointer, bytesRead; 

       public Reader() 
       { 
           din = new DataInputStream(System.in); 
           buffer = new byte[BUFFER_SIZE]; 
           bufferPointer = bytesRead = 0; 
       } 

       public Reader(String file_name) throws IOException 
       { 
           din = new DataInputStream(new FileInputStream(file_name)); 
           buffer = new byte[BUFFER_SIZE]; 
           bufferPointer = bytesRead = 0; 
       } 

       public String readLine() throws IOException 
       { 
           byte[] buf = new byte[64]; // line length 
           int cnt = 0, c;
           while ((c = read()) != -1) 
           { 
               if (c == '\n') 
               {
            	   break;
               }
               buf[cnt++] = (byte) c; 
           } 
           return new String(buf, 0, Integer.max(cnt-1, 0)); 
       } 

       public int nextInt() throws IOException 
       { 
           int ret = 0; 
           byte c = read(); 
           while (c <= ' ') 
               c = read(); 
           boolean neg = (c == '-'); 
           if (neg) 
               c = read(); 
           do
           { 
               ret = ret * 10 + c - '0'; 
           }  while ((c = read()) >= '0' && c <= '9'); 

           if (neg) 
               return -ret; 
           return ret; 
       } 

       public long nextLong() throws IOException 
       { 
           long ret = 0; 
           byte c = read(); 
           while (c <= ' ') 
               c = read(); 
           boolean neg = (c == '-'); 
           if (neg) 
               c = read(); 
           do { 
               ret = ret * 10 + c - '0'; 
           } 
           while ((c = read()) >= '0' && c <= '9'); 
           if (neg) 
               return -ret; 
           return ret; 
       } 

       public double nextDouble() throws IOException 
       { 
           double ret = 0, div = 1; 
           byte c = read(); 
           while (c <= ' ') 
               c = read(); 
           boolean neg = (c == '-'); 
           if (neg) 
               c = read(); 

           do { 
               ret = ret * 10 + c - '0'; 
           } 
           while ((c = read()) >= '0' && c <= '9'); 

           if (c == '.') 
           { 
               while ((c = read()) >= '0' && c <= '9') 
               { 
                   ret += (c - '0') / (div *= 10); 
               } 
           } 

           if (neg) 
               return -ret; 
           return ret; 
       } 

       private void fillBuffer() throws IOException 
       { 
           bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); 
           if (bytesRead == -1) 
               buffer[0] = -1; 
       } 

       private byte read() throws IOException 
       { 
           if (bufferPointer == bytesRead) 
               fillBuffer(); 
           return buffer[bufferPointer++]; 
       } 

       public void close() throws IOException 
       { 
           if (din == null) 
               return; 
           din.close(); 
       } 
   }

	public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
}











