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:
 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.StringTokenizer;

/**
 *
 * @author is2ac
 */
public class C_CF {
    static int[][] matrix;
    public static void main(String[] args) {
        FastScanner57 fs = new FastScanner57();
        PrintWriter pw = new PrintWriter(System.out);
        int t = fs.ni();
        //int t = 1;
        for (int tc = 0; tc < t; tc++) {
            int n = fs.ni();
            int m = fs.ni();
            matrix = new int[n][m];
            for (int i = 0; i < n; i++) {
                String s = fs.next();
                for (int j = 0; j < m; j++) {
                    matrix[i][j] += s.charAt(j)-'0';
                }
            }
            int ct = 0;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n-2; i++) {
                for (int j = 0; j < m-1; j++) {
                    if (j==m-2) {
                        if (matrix[i][j]==1 && matrix[i][j+1]==1) {
                            ct++;
                            flip(i,j,4,sb);
                        } else if (matrix[i][j]==1) {
                            ct++;
                            flip(i,j,2,sb);
                        } else if (matrix[i][j+1]==1) {
                            ct++;
                            flip(i,j,1,sb);
                        }
                    } else {
                        if (matrix[i][j]==1) {
                            ct++;
                            flip(i,j,4,sb);
                        }
                    }
                }
            }
            for (int j = 0; j < m-1; j++) {
                if (j==m-2) {
                    ct += fix(n-2,j,matrix,sb);
                } else {
                if (matrix[n-1][j]==1 && matrix[n-2][j]==1) {
                    ct++;
                    flip(n-2,j,4,sb);
                } else if (matrix[n-1][j]==1) {
                    ct++;
                    flip(n-2,j,1,sb);
                } else if (matrix[n-2][j]==1) {
                    ct++;
                    flip(n-2,j,3,sb);
                }
                }
            }
            //pw.println(Arrays.deepToString(matrix));
            pw.println(ct);
            pw.println(sb);
        }
        pw.close();
        
    }
    public static int fix(int i, int j, int[][] matrix,StringBuilder sb) {
        int ct = 0, k = 0;
        ct += matrix[i][j];
        ct += matrix[i+1][j];
        ct += matrix[i][j+1];
        ct += matrix[i+1][j+1];
        if (ct==0) return 0;
        if (ct==1) {
            if (matrix[i][j]==1) {
                flip(i,j,2,sb);
                k += fix(i,j,matrix,sb);
            } else if (matrix[i+1][j]==1) {
                flip(i,j,2,sb);
                k += fix(i,j,matrix,sb);
            } else if (matrix[i+1][j+1]==1) {
                flip(i,j,2,sb);
                k += fix(i,j,matrix,sb);
            } else if (matrix[i][j+1]==1) {
                flip(i,j,4,sb);
                k += fix(i,j,matrix,sb);
            }
        } else if (ct==2) {
            if (matrix[i][j]==1) {
                flip(i,j,1,sb);
                k += fix(i,j,matrix,sb);
            } else if (matrix[i+1][j]==1) {
                flip(i,j,3,sb);
                k += fix(i,j,matrix,sb);
            } else if (matrix[i+1][j+1]==1) {
                flip(i,j,4,sb);
                k += fix(i,j,matrix,sb);
            } else if (matrix[i][j+1]==1) {
                flip(i,j,2,sb);
                k += fix(i,j,matrix,sb);
            }
        } else if (ct==3) {
            if (matrix[i][j]==0) {
                flip(i,j,1,sb);
                k += fix(i,j,matrix,sb);
            } else if (matrix[i+1][j]==0) {
                flip(i,j,3,sb);
                k += fix(i,j,matrix,sb);
            } else if (matrix[i+1][j+1]==0) {
                flip(i,j,4,sb);
                k += fix(i,j,matrix,sb);
            } else if (matrix[i][j+1]==0) {
                flip(i,j,2,sb);
                k += fix(i,j,matrix,sb);
            }
        } else if (ct==4) {
            flip(i,j,4,sb);
            k += fix(i,j,matrix,sb);
        }
        return k+1;
    }
    public static void flip(int i, int j, int d, StringBuilder sb) {
        if (d!=1) matrix[i][j]++;
        if (d!=3)matrix[i+1][j]++;
        if (d!=2)matrix[i][j+1]++;
        if (d!=4)matrix[i+1][j+1]++;
        if (d!=1)matrix[i][j]%=2;
        if (d!=3)matrix[i+1][j]%=2;
        if (d!=2)matrix[i][j+1]%=2;
        if (d!=4)matrix[i+1][j+1]%=2;
        i++;
        j++;
        if (d!=1)sb.append((i) + " " + (j) + " ");
        if (d!=2)sb.append((i) + " " + (j+1) + " ");
        if (d!=3)sb.append((i+1) + " " + (j) + " ");
        if (d!=4)sb.append((i+1) + " " + (j+1) + " ");
        i--;
        j--;
        sb.append(" ");
    }
}
class UnionFind16 {
    int[] id;
    public UnionFind16 (int size) {
        id = new int[size];
        for (int i = 0; i < size; i++) {
            id[i] = i;
        }
    }
    public int find (int p) {
        int root = p;
        while (root != id[root]) {
            root = id[root];
        }
        while (p!=root) {
            int next = id[p];
            id[p] = root;
            p = next;
        }
        return root;
    }
    public void union(int p, int q) {
        int a = find(p),b = find(q);
        if (a==b) return;
        id[b] = a;
    }
}
class FastScanner57 {

    BufferedReader br;
    StringTokenizer st;

    public FastScanner57() {
        br = new BufferedReader(new InputStreamReader(System.in), 32768);
        st = null;
    }

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

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

    int[] intArray(int N) {
        int[] ret = new int[N];
        for (int i = 0; i < N; i++) {
            ret[i] = ni();
        }
        return ret;
    }

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

    long[] longArray(int N) {
        long[] ret = new long[N];
        for (int i = 0; i < N; i++) {
            ret[i] = nl();
        }
        return ret;
    }

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

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