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.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;

public class Coding {

    private static BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
    private static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) {
        try {
            run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class Op {
        public int x1, x2, x3, y1, y2, y3;
    }

    public static void run() throws Exception {
        InputModule inp = new InputModule();
        OutputModule out = new OutputModule();
        int t = inp.cinInt();
        while (t > 0) {
            int[] ints = inp.cinIntArray(2);
            int n = ints[0];
            int m = ints[1];
            String[] mat = new String[n];
            for (int i = 0; i < n; ++i) {
                mat[i] = inp.cinString();
            }
            int[][] biMat = new int[n][m];
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                    if (mat[i].charAt(j) == '1') {
                        biMat[i][j] = 1;
                    } else {
                        biMat[i][j] = 0;
                    }
                }
            }
            int rwStart = (n % 2 == 0) ? 0 : 1;
            int clStart = (m % 2 == 0) ? 0 : 1;
            List<Op> ops = new ArrayList<>();
            if (n % 2 == 1) {
                for (int j = 0; j < m - 1; ++j) {
                    if ((biMat[0][j] == 0) && (biMat[0][j + 1] == 0)) continue;
                    Op op = new Op();
                    if (biMat[0][j] == 1) {
                        op.x1 = 0;
                        op.y1 = j;
                        if (biMat[0][j + 1] == 1) {
                            op.x2 = 0;
                            op.y2 = j + 1;
                            op.x3 = 1;
                            op.y3 = j;
                        } else {
                            op.x2 = 1;
                            op.y2 = j + 1;
                            op.x3 = 1;
                            op.y3 = j;
                        }
                    } else {
                        op.x1 = 0;
                        op.y1 = j + 1;
                        op.x2 = 1;
                        op.y2 = j;
                        op.x3 = 1;
                        op.y3 = j + 1;
                    }
                    biMat[op.x1][op.y1] ^= 1;
                    biMat[op.x2][op.y2] ^= 1;
                    biMat[op.x3][op.y3] ^= 1;
                    ops.add(op);
                }
            }

            if (m % 2 == 1) {
                int start = 0;
                if (n%2==1) start=1;
                for (int i = start; i < n - 1; ++i) {
                    if ((biMat[i][0] == 0) && (biMat[i + 1][0] == 0)) continue;
                    Op op = new Op();
                    if (biMat[i][0] == 1) {
                        op.x1 = i;
                        op.y1 = 0;
                        if (biMat[i + 1][0] == 1) {
                            op.x2 = i + 1;
                            op.y2 = 0;
                            op.x3 = i;
                            op.y3 = 1;
                        } else {
                            op.x2 = i + 1;
                            op.y2 = 1;
                            op.x3 = i;
                            op.y3 = 1;
                        }
                    } else {
                        op.x1 = i + 1;
                        op.y1 = 0;
                        op.x2 = i;
                        op.y2 = 1;
                        op.x3 = i + 1;
                        op.y3 = 1;
                    }
                    biMat[op.x1][op.y1] ^= 1;
                    biMat[op.x2][op.y2] ^= 1;
                    biMat[op.x3][op.y3] ^= 1;
                    ops.add(op);
                }
            }

            /*out.printIntMatrix(biMat, n, m);
            out.printInt(rwStart);
            out.printInt(clStart);
            out.printInt(ops.size());*/

            for (int i = rwStart; i < n - 1; i += 2) {
                for (int j = clStart; j < m - 1; j += 2) {
                    int cn = 0;
                    if (biMat[i][j] == 1) cn++;
                    if (biMat[i][j + 1] == 1) cn++;
                    if (biMat[i + 1][j] == 1) cn++;
                    if (biMat[i + 1][j + 1] == 1) cn++;
                    //System.out.println("Count is " + cn);
                    while (cn != 0) {
                        Op op = new Op();
                        if (cn == 4) {
                            op.x1 = i;
                            op.y1 = j;
                            op.x2 = i + 1;
                            op.y2 = j;
                            op.x3 = i;
                            op.y3 = j + 1;
                            cn = 1;
                        } else if ((cn == 1) || (cn == 2)) {
                            int zero = 0;
                            if (biMat[i][j] == 0) {
                                op.x1 = i;
                                op.y1 = j;
                                zero++;
                            } else {
                                op.x3 = i;
                                op.y3 = j;
                            }
                            if (biMat[i + 1][j] == 0) {
                                if (zero == 0) {
                                    op.x1 = i + 1;
                                    op.y1 = j;
                                } else if (zero == 1) {
                                    op.x2 = i + 1;
                                    op.y2 = j;
                                }
                                zero++;
                            } else {
                                op.x3 = i + 1;
                                op.y3 = j;
                            }

                            if (biMat[i][j + 1] == 0) {
                                if (zero == 0) {
                                    op.x1 = i;
                                    op.y1 = j + 1;
                                } else if (zero == 1) {
                                    op.x2 = i;
                                    op.y2 = j + 1;
                                }
                                zero++;
                            } else {
                                op.x3 = i;
                                op.y3 = j + 1;
                            }

                            if (biMat[i + 1][j + 1] == 0) {
                                if (zero == 0) {
                                    op.x1 = i + 1;
                                    op.y1 = j + 1;
                                } else if (zero == 1) {
                                    op.x2 = i + 1;
                                    op.y2 = j + 1;
                                }
                                zero++;
                            } else {
                                op.x3 = i + 1;
                                op.y3 = j + 1;
                            }
                            if (cn == 1) {
                                cn = 2;
                            } else if (cn == 2) {
                                cn = 3;
                            }
                        } else {
                            int one = 0;
                            if (biMat[i][j] == 1) {
                                op.x1 = i;
                                op.y1 = j;
                                one++;
                            }
                            if (biMat[i + 1][j] == 1) {
                                if (one == 0) {
                                    op.x1 = i + 1;
                                    op.y1 = j;
                                } else if (one == 1) {
                                    op.x2 = i + 1;
                                    op.y2 = j;
                                }
                                one++;
                            }

                            if (biMat[i][j + 1] == 1) {
                                if (one == 0) {
                                    op.x1 = i;
                                    op.y1 = j + 1;
                                } else if (one == 1) {
                                    op.x2 = i;
                                    op.y2 = j + 1;
                                } else if (one == 2) {
                                    op.x3 = i;
                                    op.y3 = j + 1;
                                }
                                one++;
                            }

                            if (biMat[i + 1][j + 1] == 1) {
                                if (one == 0) {
                                    op.x1 = i + 1;
                                    op.y1 = j + 1;
                                } else if (one == 1) {
                                    op.x2 = i + 1;
                                    op.y2 = j + 1;
                                } else if (one == 2) {
                                    op.x3 = i + 1;
                                    op.y3 = j + 1;
                                }
                                one++;
                            }
                            cn = 0;
                        }
                        biMat[op.x1][op.y1] ^= 1;
                        biMat[op.x2][op.y2] ^= 1;
                        biMat[op.x3][op.y3] ^= 1;
                        ops.add(op);
                        //out.printIntMatrix(biMat, n, m);
                    }
                }
            }

            out.printInt(ops.size());
            for (int i = 0; i < ops.size(); ++i) {
                Op op = ops.get(i);
                writer.append((op.x1 + 1) + " " + (op.y1 + 1) + " " + (op.x2 + 1) + " " + (op.y2 + 1) + " " +
                        (op.x3 + 1) + " " + (op.y3 + 1));
                writer.append("\n");
            }
            writer.flush();
            ;
            t--;
        }
    }

    private static class InputModule {
        private int cinInt() throws Exception {
            return Integer.parseInt(bi.readLine().split(" ")[0].trim());
        }

        private long cinLong() throws Exception {
            return Long.parseLong(bi.readLine().split(" ")[0].trim());
        }

        private Double cinDouble() throws Exception {
            return Double.parseDouble(bi.readLine().split(" ")[0].trim());
        }

        private String cinString() throws Exception {
            return bi.readLine();
        }

        private int[] cinIntArray(int n) throws Exception {
            String input = bi.readLine();
            String[] values = input.split(" ");
            int[] ar = new int[n];
            for (int i = 0; i < n; ++i) {
                ar[i] = Integer.parseInt(values[i]);
            }
            return ar;
        }

        private int[] cinIntArray() throws Exception {
            String input = bi.readLine();
            String[] values = input.split(" ");
            int[] ar = new int[values.length];
            for (int i = 0; i < values.length; ++i) {
                ar[i] = Integer.parseInt(values[i]);
            }
            return ar;
        }

        private long[] cinLongArray(int n) throws Exception {
            String input = bi.readLine();
            String[] values = input.split(" ");
            long[] ar = new long[n];
            for (int i = 0; i < n; ++i) {
                ar[i] = Long.parseLong(values[i]);
            }
            return ar;
        }

        private String[] cinStringArray(int n) throws Exception {
            return bi.readLine().split(" ");
        }
    }

    private static class OutputModule {
        private void printInt(int ans) throws Exception {
            writer.append(ans + "\n");
            writer.flush();
        }

        private void printLong(long ans) throws Exception {
            writer.append(ans + "\n");
            writer.flush();
        }

        private void printDouble(Double ans) throws Exception {
            writer.append(String.format("%.10f", ans));
            writer.append("\n");
            writer.flush();
        }

        private void printString(String ans) throws Exception {
            writer.append(ans + "\n");
            writer.flush();
        }

        private void printIntArray(int[] ans) throws Exception {
            for (int i = 0; i < ans.length; ++i) {
                writer.append(ans[i] + " ");
            }
            writer.append("\n");
            writer.flush();
        }

        private void printLongArray(long[] ans) throws Exception {
            for (int i = 0; i < ans.length; ++i) {
                writer.append(ans[i] + " ");
            }
            writer.append("\n");
            writer.flush();
        }

        private void printIntMatrix(int[][] mat, int n, int m) throws Exception {
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                    writer.append(mat[i][j] + " ");
                }
                writer.append("\n");
            }
            writer.flush();
        }

        private void printLongMatrix(long[][] mat, int n, int m) throws Exception {
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                    writer.append(mat[i][j] + " ");
                }
                writer.append("\n");
            }
            writer.flush();
        }

        private void printPoint(Point p) throws Exception {
            writer.append(p.x + " " + p.y + "\n");
            writer.flush();
        }

        private void printPoints(List<Point> p) throws Exception {
            for (Point pp : p) {
                writer.append(pp.x + " " + pp.y + "\n");
            }
            writer.flush();
        }
    }
}