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.ArrayList;
import java.util.Scanner;

public class C {
    static StringBuilder out = new StringBuilder();
    static StringBuilder ans = new StringBuilder();
    static Scanner sc = new Scanner(System.in);
    static boolean[][] matrix;
    static int opcounter = 0;
    public static void main(String[] args){
        int t = sc.nextInt();
        while (t-->0){
            int n =sc.nextInt();
            int m =sc.nextInt();
            opcounter = 0;
            out = new StringBuilder();
            matrix = new boolean[200][200];
            for (int y=1;y<=n;y++){
                String line = sc.next();
                for (int x=0;x<m;x++){
                    if (line.charAt(x)=='1'){
                        matrix[x+1][y] = true;
                    }else {
                        matrix[x+1][y] = false;
                    }
                }
            }

            for (int y=1;y<n;y++){
                for (int x=1;x<m;x++){
                    if (y==n-1&&x==m-1){
                        operation(y,x,3);
                    }else if (y==n-1){
                        operation(y,x,2);
                    }else if (x==m-1){
                        operation(y,x,1);
                    }else {
                        operation(y,x,0);
                    }
                }
            }
            ans.append(opcounter+"\n");
            ans.append(out);
            /*// Debug
            System.out.println("DEBUG INFO:");
            for (int i=1;i<=n;i++){
                for (int j=1;j<=m;j++){
                   System.out.print(matrix[i][j]+" ");
                }
                System.out.println();
            }*/


        }
        System.out.println(ans);
    }
    public static void operation(int y,int x,int op) {
        switch (op){
            case 0:
                if (matrix[x][y]){
                    triOp(x,x+1,x+1,y,y,y+1);
                }
                break;
            case 1:
                if (matrix[x][y]&&matrix[x+1][y]){
                    triOp(x,x+1,x+1,y,y,y+1);
                }else if (matrix[x][y]){
                    triOp(x,x,x+1,y,y+1,y+1);
                }else if (matrix[x+1][y]){
                    triOp(x+1,x,x+1,y,y+1,y+1);
                }
                break;
            case 2:
                if (matrix[x][y]&&matrix[x][y+1]){
                    triOp(x,x,x+1,y,y+1,y+1);
                }else if (matrix[x][y]){
                    triOp(x,x+1,x+1,y,y,y+1);
                }else if (matrix[x][y+1]){
                    triOp(x,x+1,x+1,y+1,y,y+1);
                }
                break;
            case 3:
                while (matrix[x][y]||matrix[x+1][y]||matrix[x][y+1]||matrix[x+1][y+1]){
                    ArrayList<Integer> opx = new ArrayList<>();
                    ArrayList<Integer> opy = new ArrayList<>();
                    int counter = 0;
                    for (int i=0;i<2;i++){
                        for (int j=0;j<2;j++){
                            if (matrix[x+i][y+j]){
                                counter++;
                            }
                        }
                    }
                    if (counter >= 3){
                        int op1 = 3;
                        int op0 = 0;
                        for (int i=0;i<2;i++){
                            for (int j=0;j<2;j++){
                                if (matrix[x+i][y+j]&&(op1>0)){
                                    op1--;
                                    opx.add(x+i);
                                    opy.add(y+j);
                                }else if (!matrix[x+i][y+j]&&op0>0){
                                    op0--;
                                    opx.add(x+i);
                                    opy.add(y+j);
                                }
                            }
                        }
                        triOp(opx.get(0),opx.get(1),opx.get(2),opy.get(0),opy.get(1),opy.get(2));
                    }else {
                        int op1 = 1;
                        int op0 = 2;
                        for (int i=0;i<2;i++){
                            for (int j=0;j<2;j++){
                                if (matrix[x+i][y+j]&&(op1>0)){
                                    op1--;
                                    opx.add(x+i);
                                    opy.add(y+j);
                                }else if (!matrix[x+i][y+j]&&op0>0){
                                    op0--;
                                    opx.add(x+i);
                                    opy.add(y+j);
                                }
                            }
                        }
                        triOp(opx.get(0),opx.get(1),opx.get(2),opy.get(0),opy.get(1),opy.get(2));
                    }

                }

        }
    }

    public static void triOp(int x1,int x2,int x3,int y1,int y2,int y3){
        matrix[x1][y1] = !matrix[x1][y1];
        matrix[x2][y2] = !matrix[x2][y2];
        matrix[x3][y3] = !matrix[x3][y3];

        out.append(y1+" "+x1+" "+y2+" "+x2+" "+y3+" "+x3+"\n");
        opcounter++;
    }
}
/**
*  对于一个2x2矩阵 我们只要保证其左侧均为0即可
 *  0 ?
*   0 ？
 *
 *  然后在最后对其进行处理
 *
 *  0 1 | 0 1
 *  0 0 |  0 1
*
 *  第一种 翻转一次-> 2 0 2 1; 反转一次 3 1 1 0; 翻转一次 4 0;
 *  第二种: 翻转一次-> 3 1 1 0; 翻转一次: 4 0;
 *
 *  对于偶数矩阵 可以很好的保持 最多mn次操作这个特性
 *  问题是奇数行:
 *
 *  奇数行很好保证
 *  可是怎么保证奇数列呢
 *  我们可以这样拓展:
 *  对于每一个2x2矩阵 我们只维持
 *
 *   0 ?
 *   ? ?
 *   为 0
 *
 *   对于边界
 *   维持
 *
 *   0 0
 *   ？ ?
 *
 *   这样我们的操作数为: (n-1)*(m-1)
 *   然后最后还有一个2x2矩阵
 *   我们还有m+n次操作每用
 *   因为m,n > 2
 *   所以m+n>=4
 *   所以总次数小于mn
 *
 *
 *  0 0 0 0 0 0
 *  0 0 0 0 0 0
*
*
*
 *
 *    0 0
 *    0 0
 *
 *
 *
 *
 *
 *
 *
 *
 *
* */

/*if (op==0){ //保证左上角为0的操作
            if (matrix[x][y]){
                out.append(x+" "+y+" "+(x+1)+" "+y+" "+(x+1)+" "+(y+1)+"\n");
                matrix[x][y] = !matrix[x][y];
                matrix[x+1][y] = !matrix[x+1][y];
                matrix[x+1][y+1] = !matrix[x+1][y+1];
                opcounter++;
            }else {
                return;
            }
        }else if (op==1){  // 保证 上方一行为0的操作
            int num = 3;
            if (matrix[x][y]){
                num--;
                out.append(x+" "+y+" ");
                matrix[x][y] = !matrix[x][y];
            }
            if (matrix[x+1][y]){
                num--;
                out.append((x+1)+" "+y+" ");
                matrix[x+1][y] = !matrix[x+1][y];
            }
            if (num==3){
                return;
            }else {
                opcounter++;
                for (int z=0;z<num;z++){
                    out.append((x+z)+" "+(y+1)+" ");
                    matrix[x+z][y+1] = !matrix[x+z][y];
                }
                out.append("\n");
            }
        }else if (op==2){ //保证左侧一列为0的操作
            int num = 3;
            if (matrix[x][y]){
                num--;
                out.append(x+" "+y+" ");
                matrix[x][y] = !matrix[x][y];
            }
            if (matrix[x][y+1]){
                num--;
                out.append(x+" "+(y+1)+" ");
                matrix[x][y+1] = !matrix[x][y+1];
            }
            if (num==3){
                return;
            }else {
                opcounter++;
                for (int z=0;z<num;z++){
                    out.append((x+1)+" "+(y+z)+" ");
                    matrix[x+1][y+z] = !matrix[x+1][y+z];
                }
                out.append("\n");
            }
        }else if (op==3){
            // 保证整个2x2矩阵为0的操作
            //首先判断1的数量 分情况讨论:
            //如果1为3 直接翻转
            // 如果1为4 翻转到 1个1
            // 如果 1 为 1 翻转成 2个1
            // 如果1 为2 翻转成3个1 递归操作即可
            int counter = 0;
            for (int i=0;i<2;i++){
                for (int j=0;j<2;j++){
                    if (matrix[y+j][x+i]){
                        counter++;
                    }
                }
            }
            makeGood(y,x,counter);
        }
    }
    public static void makeGood(int x,int y,int numOfOne){
        if (numOfOne == 4){
            out.append(x+" "+y+" "+(x+1)+" "+y+" "+(x+1)+" "+(y+1)+"\n");
            matrix[x][y] = !matrix[x][y];
            matrix[x+1][y] = !matrix[x+1][y];
            matrix[x+1][y+1] = !matrix[x+1][y+1];
            opcounter++;
            makeGood(x,y,1);
        }else if (numOfOne==3){
            int op1 = 3;
            int op0 = 0;
            for (int i=0;i<2;i++){
                for (int j=0;j<2;j++){
                    if (matrix[x+i][y+j]&&(op1>0)){
                        op1--;
                        matrix[x+i][y+j] = !matrix[x+i][y+j];
                        out.append((x+i)+" "+(y+j)+" ");
                    }else if (!matrix[x+i][y+j]&&op0>0){
                        op0--;
                        matrix[x+i][y+j] = !matrix[x+i][y+j];
                        out.append((x+i)+" "+(y+j)+" ");
                    }
                }
            }
            opcounter++;
            out.append("\n");
        }else if (numOfOne==2){
            int op1 = 1;
            int op0 = 2;
            for (int i=0;i<2;i++){
                for (int j=0;j<2;j++){
                    if (matrix[x+i][y+j]&&(op1>0)){
                        op1--;
                        matrix[x+i][y+j] = !matrix[x+i][y+j];
                        out.append((x+i)+" "+(y+j)+" ");
                    }else if (!matrix[x+i][y+j]&&op0>0){
                        op0--;
                        matrix[x+i][y+j] = !matrix[x+i][y+j];
                        out.append((x+i)+" "+(y+j)+" ");
                    }
                }
            }
            out.append("\n");
            opcounter++;
            makeGood(x,y,3);
        }else if (numOfOne==1){
            int op1 = 1;
            int op0 = 2;
            for (int i=0;i<2;i++){
                for (int j=0;j<2;j++){
                    if (matrix[x+i][y+j]&&(op1>0)){
                        op1--;
                        matrix[x+i][y+j] = !matrix[x+i][y+j];
                        out.append((x+i)+" "+(y+j)+" ");
                    }else if (!matrix[x+i][y+j]&&op0>0){
                        op0--;
                        matrix[x+i][y+j] = !matrix[x+i][y+j];
                        out.append((x+i)+" "+(y+j)+" ");
                    }
                }
            }
            out.append("\n");
            opcounter++;
            makeGood(x,y,2);
        }
    }
*/