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 javax.naming.NoPermissionException;
import java.io.*;
import java.util.*;
import java.util.logging.LoggingPermission;

public class Main implements Runnable {
    static int stack_size = 1 << 27;
    int n, m;
    int[][] mt;

    static boolean use_n_tests = true;

    void solve(FastScanner in, PrintWriter out, int testNumber) {
        n = in.nextInt();
        m = in.nextInt();
        char[][] mt = new char[n][];
        for (int i = 0; i < n; i++) {
            mt[i] = in.next().toCharArray();
            for (int j = 0; j < mt[i].length; j++) {
                mt[i][j] -= '0';
            }
        }
        List<Integer> ops = new ArrayList<>();
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < m - 1; j++) {
                int c = mt[i][j] + mt[i][j + 1];
                if (c >= 1) {
                    if (mt[i][j] == 1) {
                        ops.add(i);
                        ops.add(j);
                        mt[i][j] = (char) (1 - mt[i][j]);
                    }
                    if (mt[i][j + 1] == 1) {
                        ops.add(i);
                        ops.add(j + 1);
                        mt[i][j + 1] = (char) (1 - mt[i][j + 1]);
                    }
                    if (c < 3) {
                        ops.add(i + 1);
                        ops.add(j);
                        mt[i + 1][j] = (char) (1 - mt[i + 1][j]);
                        c++;
                    }
                    if (c < 3) {
                        ops.add(i + 1);
                        ops.add(j + 1);
                        mt[i + 1][j + 1] = (char) (1 - mt[i + 1][j + 1]);
                    }
                }
            }
        }
        for (int j = 0; j < m - 2; j++) {
            int c = mt[n - 1][j] + mt[n - 2][j];
            if (c >= 1) {
                if (mt[n - 1][j] == 1) {
                    ops.add(n - 1);
                    ops.add(j);
                    mt[n - 1][j] = (char) (1 - mt[n - 1][j]);
                }
                if (mt[n - 2][j] == 1) {
                    ops.add(n - 2);
                    ops.add(j);
                    mt[n - 2][j] = (char) (1 - mt[n - 2][j]);
                }
                if (c < 3) {
                    ops.add(n - 1);
                    ops.add(j + 1);
                    mt[n - 1][j + 1] = (char) (1 - mt[n - 1][j + 1]);
                    c++;
                }
                if (c < 3) {
                    ops.add(n - 2);
                    ops.add(j + 1);
                    mt[n - 2][j + 1] = (char) (1 - mt[n - 2][j + 1]);
                }
            }
        }
        int c = mt[n - 1][m - 1] + mt[n - 2][m - 1] +  mt[n - 2][m - 2] +  mt[n - 1][m - 2];
        // copy past trash
        if (c == 3) {
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    if (mt[n - 2 + i][m - 2 + j] == 1) {
                        ops.add(n - 2 + i);
                        ops.add(m - 2 + j);
                    }
                }
            }
        }
        else if (c == 2) {
            int nl = 2;
            int on = 1;
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    if (mt[n - 2 + i][m - 2 + j] == 0 && nl > 0) {
                        ops.add(n - 2 + i);
                        ops.add(m - 2 + j);
                        mt[n - 2 + i][m - 2 + j] = 1;
                        nl--;
                    } else if (mt[n - 2 + i][m - 2 + j] == 1 && on > 0) {
                        ops.add(n - 2 + i);
                        ops.add(m - 2 + j);
                        mt[n - 2 + i][m - 2 + j] = 0;
                        on--;
                    }
                }
            }

            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    if (mt[n - 2 + i][m - 2 + j] == 1) {
                        ops.add(n - 2 + i);
                        ops.add(m - 2 + j);
                    }
                }
            }
        } else if (c == 1) {
            int nl = 2;
            int on = 1;
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    if (mt[n - 2 + i][m - 2 + j] == 0 && nl > 0) {
                        ops.add(n - 2 + i);
                        ops.add(m - 2 + j);
                        mt[n - 2 + i][m - 2 + j] = 1;
                        nl--;
                    } else if (mt[n - 2 + i][m - 2 + j] == 1 && on > 0) {
                        ops.add(n - 2 + i);
                        ops.add(m - 2 + j);
                        mt[n - 2 + i][m - 2 + j] = 0;
                        on--;
                    }
                }
            }
            nl = 2;
            on = 1;
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    if (mt[n - 2 + i][m - 2 + j] == 0 && nl > 0) {
                        ops.add(n - 2 + i);
                        ops.add(m - 2 + j);
                        mt[n - 2 + i][m - 2 + j] = 1;
                        nl--;
                    } else if (mt[n - 2 + i][m - 2 + j] == 1 && on > 0) {
                        ops.add(n - 2 + i);
                        ops.add(m - 2 + j);
                        mt[n - 2 + i][m - 2 + j] = 0;
                        on--;
                    }
                }
            }
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    if (mt[n - 2 + i][m - 2 + j] == 1) {
                        ops.add(n - 2 + i);
                        ops.add(m - 2 + j);
                    }
                }
            }
        }
        out.println(ops.size() / 6);
        for (int i = 0; i < ops.size(); i += 6) {
            out.printf("%d %d %d %d %d %d\n", ops.get(i) + 1, ops.get(i + 1) + 1, ops.get(i + 2) + 1, ops.get(i + 3) + 1, ops.get(i + 4) + 1, ops.get(i + 5) + 1);
        }

    }

    boolean in(long p, long l, long r) {
        return p >= l && p <= r;
    }

// ****************************** template code ***********

    class Coeff {
        long mod;
        long[][] C;
        long[] fact;
        boolean cycleWay = false;

        Coeff(int n, long mod) {
            this.mod = mod;
            fact = new long[n + 1];
            fact[0] = 1;
            for (int i = 1; i <= n; i++) {
                fact[i] = i;
                fact[i] %= mod;
                fact[i] *= fact[i - 1];
                fact[i] %= mod;
            }
        }

        Coeff(int n, int m, long mod) {
            // n > m
            cycleWay = true;
            this.mod = mod;
            C = new long[n + 1][m + 1];
            for (int i = 0; i <= n; i++) {
                for (int j = 0; j <= Math.min(i, m); j++) {
                    if (j == 0 || j == i) {
                        C[i][j] = 1;
                    } else {
                        C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
                        C[i][j] %= mod;
                    }
                }
            }
        }

        public long C(int n, int m) {
            if (cycleWay) {
                return C[n][m];
            }
            return fC(n, m);
        }

        private long fC(int n, int m) {
            return (fact[n] * inv(fact[n - m] * fact[m] % mod)) % mod;
        }

        private long inv(long r) {
            if (r == 1)
                return 1;
            return ((mod - mod / r) * inv(mod % r)) % mod;
        }
    }

    class Pair {
        int first;
        int second;

        public int getFirst() {
            return first;
        }

        public int getSecond() {
            return second;
        }
    }

    class MultisetTree<T> {
        int size = 0;

        TreeMap<T, Integer> mp = new TreeMap<>();

        void add(T x) {
            mp.merge(x, 1, Integer::sum);
            size++;
        }

        void remove(T x) {
            if (mp.containsKey(x)) {
                mp.merge(x, -1, Integer::sum);
                if (mp.get(x) == 0) {
                    mp.remove(x);
                }
                size--;
            }
        }

        T greatest() {
            return mp.lastKey();
        }

        T smallest() {
            return mp.firstKey();
        }

        int size() {
            return size;
        }

        int diffSize() {
            return mp.size();
        }
    }

    class Multiset<T> {
        int size = 0;

        Map<T, Integer> mp = new HashMap<>();

        void add(T x) {
            mp.merge(x, 1, Integer::sum);
            size++;
        }

        void remove(T x) {
            if (mp.containsKey(x)) {
                mp.merge(x, -1, Integer::sum);
                if (mp.get(x) == 0) {
                    mp.remove(x);
                }
                size--;
            }
        }

        int size() {
            return size;
        }

        int diffSize() {
            return mp.size();
        }
    }

    static class Range {
        int l, r;
        int id;

        public int getL() {
            return l;
        }

        public int getR() {
            return r;
        }

        public Range(int l, int r, int id) {
            this.l = l;
            this.r = r;
            this.id = id;
        }
    }

    static class Array {
        static Range[] readRanges(int n, FastScanner in) {
            Range[] result = new Range[n];
            for (int i = 0; i < n; i++) {
                result[i] = new Range(in.nextInt(), in.nextInt(), i);
            }
            return result;
        }

        static boolean isSorted(Integer[] a) {
            for (int i = 0; i < a.length - 1; i++) {
                if (a[i] > a[i + 1]) {
                    return false;
                }
            }
            return true;
        }

        static public Integer[] read(int n, FastScanner in) {
            Integer[] out = new Integer[n];
            for (int i = 0; i < out.length; i++) {
                out[i] = in.nextInt();
            }
            return out;
        }

        static public int min(Integer[] a) {
            int mn = Integer.MAX_VALUE;
            for (int x : a) {
                mn = Math.min(mn, x);
            }
            return mn;
        }

        static public int max(Integer[] a) {
            int mx = Integer.MIN_VALUE;
            for (int x : a) {
                mx = Math.max(mx, x);
            }
            return mx;
        }

        static public int[] readint(int n, FastScanner in) {
            int[] out = new int[n];
            for (int i = 0; i < out.length; i++) {
                out[i] = in.nextInt();
            }
            return out;
        }
    }

    class Graph {
        List<List<Integer>> create(int n) {
            List<List<Integer>> graph = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                graph.add(new ArrayList<>());
            }
            return graph;
        }
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream io) {
            br = new BufferedReader(new InputStreamReader(io));
        }

        public String line() {
            String result = "";
            try {
                result = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }

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

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

        public long nextLong() {
            return Long.parseLong(next());
        }

    }

    void run_t_tests() {
        int t = in.nextInt();
        int i = 0;
        while (t-- > 0) {
            solve(in, out, i++);
        }
    }

    void run_one() {
        solve(in, out, -1);
    }

    @Override
    public void run() {
        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);
        if (use_n_tests) {
            run_t_tests();
        } else {
            run_one();
        }
        out.close();
    }

    static FastScanner in;
    static PrintWriter out;

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(null, new Main(), "", stack_size);
        thread.start();
        thread.join();
    }
}