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

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        Scanner in = new Main().new Scanner(inputStream);
        Main solver = new Main();
        solver.runn(in);
    }

    public void runn(Scanner in) {
        int t = in.ni();
        while (t-- > 0) {
            int n = in.ni(), m = in.ni();
            boolean[][] grid = new boolean[n][m];
            for (int i = 0; i < n; i++) {
                String line = in.rl();
                for (int j = 0; j < m; j++)
                    grid[i][j] = (line.charAt(j) == '1');
            }

            // from last row to 2 row (leave 0 and 1)
            for (int i = n - 1; i >= 2; i--) {
                for (int j = m - 1; j >= 1; j--) {
                    boolean rr = grid[i][j];
                    boolean ll = grid[i][j - 1];
                    if (!rr && !ll)
                        continue;
                    else if (rr && !ll)
                        change(grid, i, j, i - 1, j, i - 1, j - 1);
                    else if (!rr && ll)
                        change(grid, i, j - 1, i - 1, j, i - 1, j - 1);
                    else if (rr && ll)
                        change(grid, i, j, i, j - 1, i - 1, j - 1);
                }
            }
            for (int j = m - 1; j >= 2; j--) {
                boolean rr = grid[0][j];
                boolean ll = grid[1][j];
                if (!rr && !ll)
                    continue;
                else if (rr && !ll)
                    change(grid, 0, j, 0, j - 1, 1, j - 1);
                else if (!rr && ll)
                    change(grid, 1, j, 0, j - 1, 1, j - 1);
                else if (rr && ll)
                    change(grid, 0, j, 1, j, 0, j - 1);
            }
            change(grid, 0, 0);
            System.out.println(recs.size());
            for (String pos : recs) {
                System.out.println(pos);
            }
            // for (int i = 0; i < n; i++) {
            // for (int j = 0; j < m; j++) {
            // System.out.print(grid[i][j] ? "1" : "0");
            // }
            // System.out.println();
            // }
            recs.clear();
        }
    }

    public void change(boolean[][] grid, int x, int y) {
        List<Pair> used = new ArrayList<>();
        List<Pair> unused = new ArrayList<>();
        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 2; j++) {
                if (grid[i + x][j + y])
                    used.add(new Pair(i + x, j + y));
                else
                    unused.add(new Pair(i + x, j + y));
            }
        if (used.size() == 4) {
            print(grid, used.get(0), used.get(1), used.get(2));
            change(grid, x, y);
        } else if (used.size() == 1 || used.size() == 2) {
            print(grid, unused.get(0), unused.get(1), used.get(0));
            change(grid, x, y);
        } else if (used.size() == 3) {
            print(grid, used.get(0), used.get(1), used.get(2));
        }
    }

    public void print(boolean[][] grid, Pair p1, Pair p2, Pair p3) {
        change(grid, p1.firsti(), p1.secondi(), p2.firsti(), p2.secondi(), p3.firsti(), p3.secondi());
    }

    List<String> recs = new ArrayList<>();

    public void change(boolean[][] grid, int a, int b, int c, int d, int e, int f) {
        grid[a][b] = !grid[a][b];
        grid[c][d] = !grid[c][d];
        grid[e][f] = !grid[e][f];
        recs.add((a + 1) + " " + (b + 1) + " " + (c + 1) + " " + (d + 1) + " " + (e + 1) + " " + (f + 1));
        // System.out.println(recs.get(recs.size() - 1));
    }

    class Scanner {
        BufferedReader br;
        StringTokenizer in;

        public Scanner(InputStream inputStream) {
            br = new BufferedReader(new InputStreamReader(inputStream));
        }

        public String next() {
            return hasMoreTokens() ? in.nextToken() : null;
        }

        public String rl() {
            try {
                return br.readLine();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        boolean hasMoreTokens() {
            while (in == null || !in.hasMoreTokens()) {
                String s = rl();
                if (s == null)
                    return false;
                in = new StringTokenizer(s);
            }
            return true;
        }

        public String nextString() {
            return next();
        }

        public int ni() {
            return Integer.parseInt(nextString());
        }

        public double nd() {
            return Double.parseDouble(nextString());
        }

        public long nl() {
            return Long.parseLong(nextString());
        }

        public int[] nia(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }

        public long[] nla(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }
    }

    class Pair implements Comparable<Pair> {
        long _first, _second;

        public Pair(int f, int s) {
            _first = f;
            _second = s;
        }

        public Pair(Long f, Long s) {
            _first = f;
            _second = s;
        }

        public int compareTo(Pair o) {
            if (this.first() != o.first())
                return Long.compare(this.first(), o.first());
            else
                return Long.compare(this.second(), o.second());
        }

        long first() {
            return _first;
        }

        long second() {
            return _second;
        }

        int firsti() {
            return (int) _first;
        }

        int secondi() {
            return (int) _second;
        }
    }

    class Triple implements Comparable<Triple> {
        Integer _first, _second, _third;

        public Triple(Integer f, Integer s, Integer t) {
            _first = f;
            _second = s;
            _third = t;
        }

        public int compareTo(Triple o) {
            if (!this.first().equals(o.first()))
                return this.first() - o.first();
            else if (!this.second().equals(o.second()))
                return this.second() - o.second();
            else
                return this.third() - o.third();
        }

        Integer first() {
            return _first;
        }

        Integer second() {
            return _second;
        }

        Integer third() {
            return _third;
        }

        public String toString() {
            return first() + " " + second() + " " + third();
        }
    }
}