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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class SolutionC extends Thread {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            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;
        }
    }

    private static final FastReader scanner = new FastReader();
    private static final PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        new Thread(null, new SolutionC(), "Main", 1 << 26).start();
    }

    public void run() {
        int t = scanner.nextInt();
        for (int i = 0; i < t; i++) {
            solve();
        }
        out.close();
    }

    static class Operation {
        int x1;
        int y1;
        int x2;
        int y2;
        int x3;
        int y3;

        public Operation(int x1, int y1, int x2, int y2, int x3, int y3) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            this.x3 = x3;
            this.y3 = y3;
        }

        @Override
        public String toString() {
            return y1 + " " + x1 + " " + y2 + " " + x2 + " " + y3 + " " + x3;
        }
    }


    private static void solve() {
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        boolean[][] grid = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            String s = scanner.next();
            for (int j = 0; j < m; j++) {
                grid[i][j] = s.charAt(j) == '1';
            }
        }

        List<Operation> output = new ArrayList<>();
        int startRow = 0;
        int startCol = 0;

        if (n % 2 == 1) {
            startRow = 1;
            for (int i = 0; i < m; i++) {
                if (grid[0][i] && i < m - 1) {
                    output.add(new Operation(i + 1, 1, i + 1, 2, i + 2, 2));
                    grid[0][i] = false;
                    grid[1][i] = !grid[1][i];
                    grid[1][i+1] = !grid[1][i+1];
                } else if (grid[0][i] && i == m - 1) {
                    output.add(new Operation(i + 1, 1, i + 1, 2, i, 2));
                    grid[0][i] = false;
                    grid[1][i] = !grid[1][i];
                    grid[1][i-1] = !grid[1][i-1];
                }
            }
        }
        if (m % 2 == 1) {
            startCol = 1;
            for (int i = 0; i < n; i++) {
                if (grid[i][0] && i < n - 1) {
                    output.add(new Operation(1, i + 1, 2, i + 1, 2, i + 2));
                    grid[i][0] = false;
                    grid[i][1] = !grid[i][1];
                    grid[i+1][1] = !grid[i+1][1];
                } else if (grid[i][0] && i == n - 1) {
                    output.add(new Operation(1, i + 1, 2, i + 1, 2, i));
                    grid[i][0] = false;
                    grid[i][1] = !grid[i][1];
                    grid[i-1][1] = !grid[i-1][1];
                }
            }
        }

        for (int i = startRow; i < n; i+=2) {
            for (int j = startCol; j < m; j+=2) {
                while (amountOnes(grid, i, j) > 0) {
                    output.add(useOp(grid, i, j));
                }
            }
        }


        out.println(output.size());
        output.forEach(out::println);
    }

    private static Operation useOp(boolean[][] grid, int r, int c) {
        int amountOnes = amountOnes(grid, r, c);

        if (amountOnes == 3) {
            Operation op;
            if (!grid[r][c]) {
                op = new Operation(c+1, r+2, c+2, r+1, c+2, r+2);
            } else if (!grid[r+1][c]) {
                op = new Operation(c+1, r+1, c+2, r+1, c+2, r+2);
            } else if (!grid[r][c+1]) {
                op = new Operation(c+1, r+1, c + 1, r +2, c+2, r+2);
            } else {
                op = new Operation(c+1, r+1, c+1, r+2, c+2, r+1);
            }
            grid[r][c] = false;
            grid[r+1][c] = false;
            grid[r][c+1] = false;
            grid[r+1][c+1] = false;
            return op;
        } else if (amountOnes == 4) {
            grid[r][c] = false;
            grid[r+1][c] = false;
            grid[r][c+1] = false;
            return new Operation(c + 1, r + 1, c + 1, r + 2, c + 2, r + 1);
        } else if (amountOnes == 1) {
            if (grid[r][c]) {
                grid[r][c] = false;
                grid[r+1][c] = true;
                grid[r+1][c+1] = true;
                return new Operation(c+1, r+1, c+1, r+2, c+2, r+2);
            } else if (grid[r+1][c+1]) {
                grid[r+1][c+1] = false;
                grid[r][c] = true;
                grid[r+1][c] = true;
                return new Operation(c+2, r+2, c+1, r+1, c+1, r+2);
            } else if (grid[r+1][c]) {
                grid[r+1][c] = false;
                grid[r][c] = true;
                grid[r+1][c+1] = true;
                return new Operation(c+1, r+2, c+1, r+1, c+2, r+2);
            } else {
                grid[r][c+1] = false;
                grid[r][c] = true;
                grid[r+1][c] = true;
                return new Operation(c+2, r+1, c+1, r+1, c+1, r+2);
            }
        } else {
            int x1 = -1;
            int x2 = -1;
            int x3 = -1;
            int y1 = -1;
            int y2 = -1;
            int y3 = -1;

            if (grid[r][c] && x1 == -1) {
                x1 = c+1;
                y1 = r+1;
            } else if (!grid[r][c]) {
                if (x2 == -1) {
                    x2 = c + 1;
                    y2 = r + 1;
                } else {
                    x3 = c + 1;
                    y3 = r + 1;
                }
            }

            if (grid[r+1][c] && x1 == -1) {
                x1 = c+1;
                y1 = r+2;
            } else if (!grid[r+1][c]) {
                if (x2 == -1) {
                    x2 = c + 1;
                    y2 = r + 2;
                } else {
                    x3 = c + 1;
                    y3 = r + 2;
                }
            }

            if (grid[r+1][c+1] && x1 == -1) {
                x1 = c+2;
                y1 = r+2;
            } else if (!grid[r+1][c+1]) {
                if (x2 == -1) {
                    x2 = c + 2;
                    y2 = r + 2;
                } else {
                    x3 = c + 2;
                    y3 = r + 2;
                }
            }
            if (grid[r][c+1] && x1 == -1) {
                x1 = c+2;
                y1 = r+1;
            } else if (!grid[r][c+1]) {
                if (x2 == -1) {
                    x2 = c + 2;
                    y2 = r + 1;
                } else {
                    x3 = c + 2;
                    y3 = r + 1;
                }
            }

            grid[y1-1][x1-1] = !grid[y1-1][x1-1];
            grid[y2-1][x2-1] = !grid[y2-1][x2-1];
            grid[y3-1][x3-1] = !grid[y3-1][x3-1];

            return new Operation(x1, y1, x2, y2, x3, y3);
        }
    }

    private static int amountOnes(boolean[][] grid, int row, int col) {
        return (grid[row][col] ? 1 : 0) +
               (grid[row][col+1] ? 1 : 0) +
               (grid[row+1][col] ? 1 : 0) +
               (grid[row+1][col+1] ? 1 : 0);
    }

    //REMINDERS:
    //- CHECK FOR INTEGER-OVERFLOW BEFORE SUBMITTING

    //- CAN U BRUTEFORCE OVER SOMETHING, TO MAKE IT EASIER TO CALCULATE THE SOLUTION
}