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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA2 solver = new TaskA2();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskA2 {
        int height;
        int width;
        int[][] a;
        List<String> ans = new ArrayList<>();

        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int numTests = in.nextInt();
            for (int test = 0; test < numTests; test++) {
                height = in.nextInt();
                width = in.nextInt();
                a = new int[height][width];
                for (int r = 0; r < height; r++) {
                    char[] s = in.next().toCharArray();
                    for (int c = 0; c < width; c++) {
                        a[r][c] = s[c] - '0';
                    }
                }
                ans.clear();
                for (int r = height - 1; r >= 2; r--) {
                    for (int c = width - 1; c >= 0; c--) {
                        if (a[r][c] == 1) {
                            flip(r * width + c, (r - 1) * width + c, (r - 1) * width + (c == 0 ? c + 1 : c - 1));
                        }
                    }
                }
                for (int c = width - 1; c >= 2; c--) {
                    if (a[0][c] == 1 && a[1][c] == 1) {
                        flip(0 * width + c, 1 * width + c, 0 * width + (c - 1));
                    } else if (a[0][c] == 1) {
                        flip(0 * width + c, 0 * width + (c - 1), 1 * width + (c - 1));
                    } else if (a[1][c] == 1) {
                        flip(1 * width + c, 0 * width + (c - 1), 1 * width + (c - 1));
                    }
                }
                List<Integer> ones = new ArrayList<>();
                List<Integer> zeros = new ArrayList<>();
                for (int r = 0; r < 2; r++) {
                    for (int c = 0; c < 2; c++) {
                        int x = r * width + c;
                        if (a[r][c] == 1) {
                            ones.add(x);
                        } else {
                            zeros.add(x);
                        }
                    }
                }
                switch (ones.size()) {
                    case 0:
                        break;
                    case 1:
                        solveOne(ones.get(0), zeros.get(0), zeros.get(1), zeros.get(2));
                        break;
                    case 2:
                        flip(ones.get(0), ones.get(1), zeros.get(0));
                        solveOne(zeros.get(0), ones.get(0), ones.get(1), zeros.get(1));
                        break;
                    case 3:
                        flip(ones.get(0), ones.get(1), ones.get(2));
                        break;
                    case 4:
                        flip(ones.get(0), ones.get(1), ones.get(2));
                        solveOne(ones.get(3), ones.get(0), ones.get(1), ones.get(2));
                        break;
                }
                out.println(ans.size());
                for (String s : ans) {
                    out.println(s);
                }
            }
        }

        private void flip(int x, int y, int z) {
            int rx = x / width;
            int cx = x % width;
            int ry = y / width;
            int cy = y % width;
            int rz = z / width;
            int cz = z % width;
            a[rx][cx] ^= 1;
            a[ry][cy] ^= 1;
            a[rz][cz] ^= 1;
            ans.add(String.format("%d %d %d %d %d %d", rx + 1, cx + 1, ry + 1, cy + 1, rz + 1, cz + 1));
        }

        private void solveOne(int one, int z0, int z1, int z2) {
            flip(one, z0, z1);
            flip(one, z1, z2);
            flip(one, z2, z0);
        }

    }

    static class FastScanner {
        private BufferedReader in;
        private StringTokenizer st;

        public FastScanner(InputStream stream) {
            in = new BufferedReader(new InputStreamReader(stream));
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}