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  
    
import java.io.*;
import java.security.SecureRandom;
import java.util.*;
import java.util.stream.*;


public class C {

    // 420 remainder: use long everywhere

    private final static boolean MULTIPLE_TESTS = true;

    public static class Pos {
        int x, y;

        Pos(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    List<Pos> getValPositions(int[][] a, int val, int fromX, int fromY, int toX, int toY) {
        ArrayList<Pos> res = new ArrayList<>();
        for (int i = fromX; i <= toX; i++) {
            for (int j = fromY; j <= toY; j++) {
                if (a[i][j] == val)
                    res.add(new Pos(i, j));
            }
        }
        return res;
    }

    void change(int[][] a, int x1, int y1, int x2, int y2, int x3, int y3) {
        a[x1][y1] = 1 - a[x1][y1];
        a[x2][y2] = 1 - a[x2][y2];
        a[x3][y3] = 1 - a[x3][y3];
    }

    void change(int[][] a, Pos p1, Pos p2, Pos p3) {
        change(a, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
    }

    private void solve() {
        int n = in.readInt();
        int m = in.readInt();
        int[][] a = new int[n][m];
        for (int i = 0; i < n; i++) {
            String s = in.readToken();
            for (int j = 0; j < m; j++)
                a[i][j] = s.charAt(j) - '0';
        }

        ArrayList<Integer[]> ans = new ArrayList<>();
        for (int i = 0; i < n - 2; i++) {
            for (int j = 0; j < m; j++) {
                if (a[i][j] == 1) {
                    if (j == m - 1) {
                        a[i][j] = 1 - a[i][j];
                        a[i + 1][j] = 1 - a[i + 1][j];
                        a[i + 1][j - 1] = 1 - a[i + 1][j - 1];
                        ans.add(new Integer[] {i, j, i + 1, j, i + 1, j - 1});
                    } else {
                        a[i][j] = 1 - a[i][j];
                        a[i + 1][j] = 1 - a[i + 1][j];
                        a[i + 1][j + 1] = 1 - a[i + 1][j + 1];
                        ans.add(new Integer[] {i, j, i + 1, j, i + 1, j + 1});
                    }
                }
            }
        }

        int last = n - 1;
        int pred = n - 2;
        for (int i = 0; i < m - 2; i++) {
            if (a[last][i] == 1 && a[pred][i] == 1) {
                a[last][i] = 1 - a[last][i];
                a[pred][i] = 1 - a[pred][i];
                a[pred][i + 1] = 1 - a[pred][i + 1];
                ans.add(new Integer[] {last, i, pred, i, pred, i + 1});
            } else if (a[last][i] == 1) {
                a[last][i] = 1 - a[last][i];
                a[last][i + 1] = 1 - a[last][i + 1];
                a[pred][i + 1] = 1 - a[pred][i + 1];
                ans.add(new Integer[] {last, i, last, i + 1, pred, i + 1});
            } else if (a[pred][i] == 1) {
                a[pred][i] = 1 - a[pred][i];
                a[last][i + 1] = 1 - a[last][i + 1];
                a[pred][i + 1] = 1 - a[pred][i + 1];
                ans.add(new Integer[] {pred, i, last, i + 1, pred, i + 1});
            }
        }

        while (true) {
            List<Pos> ones = getValPositions(a, 1, n - 2, m - 2, n - 1, m - 1);
            List<Pos> zeros = getValPositions(a, 0, n - 2, m - 2, n - 1, m - 1);
            if (ones.isEmpty()) {
                break;
            }
            if (ones.size() >= 3) {
                change(a, ones.get(0), ones.get(1), ones.get(2));
                ans.add(new Integer[] {ones.get(0).x, ones.get(0).y, ones.get(1).x, ones.get(1).y, ones.get(2).x, ones.get(2).y});
            } else {
                change(a, ones.get(0), zeros.get(0), zeros.get(1));
                ans.add(new Integer[] {ones.get(0).x, ones.get(0).y, zeros.get(1).x, zeros.get(1).y, zeros.get(0).x, zeros.get(0).y});
            }
        }

        out.println(ans.size());
        for (Integer[] ar : ans) {
            for (int i = 0; i < ar.length; i++) {
                out.print(ar[i] + 1);
                out.print(' ');
            }
            out.println();
        }
//        out.println();
//        for (int i = 0; i < n; i++) {
//            out.println(a[i]);
//        }
//        out.println();
//        out.println();
    }

    private FastReader in;
    private FastWriter out;

    private void run() throws Exception {
        in = new FastReader();
        out = new FastWriter();

        int t = MULTIPLE_TESTS ? in.readInt() : 1;

        for (int testNum = 0; testNum < t; testNum++) {
            solve();
        }

        out.close();
        in.close();
    }

    public static void main(String[] args) throws Exception {
        new C().run();
    }

    private final static boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    private static class FastWriter implements AutoCloseable {
        @SuppressWarnings(value="all")
        private final static Optional<String> OUTPUT_FILENAME = Optional.empty();

        private final PrintWriter out;

        public FastWriter() throws FileNotFoundException {
            this.out = OUTPUT_FILENAME.isEmpty() ? new PrintWriter(System.out) : new PrintWriter(new File(OUTPUT_FILENAME.get()));
        }

        public void println() {
            out.println();
        }

        public <T> void print(T value) {
            out.print(value);
        }

        public <T> void println(T value) {
            out.println(value);
        }

        public <T> void print(List<? extends T> list) {
            for (var it = list.listIterator(); it.hasNext(); ) {
                out.print(it.next());
                if (it.hasNext()) {
                    out.print(' ');
                }
            }
        }

        public <T> void println(List<? extends T> list) {
            print(list);
            out.println();
        }

        public <T> void print(T[] array) {
            for (int i = 0; i < array.length; i++) {
                out.print(array[i]);
                if (i + 1 < array.length) {
                    out.print(' ');
                }
            }
        }

        public <T> void println(T[] array) {
            print(array);
            out.println();
        }

        public void print(int[] array) {
            for (int i = 0; i < array.length; i++) {
                out.print(array[i]);
                if (i + 1 < array.length) {
                    out.print(' ');
                }
            }
        }

        public void println(int[] array) {
            print(array);
            out.println();
        }

        public void print(long[] array) {
            for (int i = 0; i < array.length; i++) {
                out.print(array[i]);
                if (i + 1 < array.length) {
                    out.print(' ');
                }
            }
        }

        public void println(long[] array) {
            print(array);
            out.println();
        }

        public void print(double[] array) {
            for (int i = 0; i < array.length; i++) {
                out.print(array[i]);
                if (i + 1 < array.length) {
                    out.print(' ');
                }
            }
        }

        public void println(double[] array) {
            print(array);
            out.println();
        }

        @Override
        public void close() {
            out.close();
        }
    }

    private static class FastReader implements AutoCloseable {
        private final static String INPUT_FILENAME = "input.txt";

        private final InputStream inputStream;
        private final byte[] buffer = new byte[1024];

        private int bufferLen = 0;
        private int bufferPtr = 0;

        public FastReader() throws FileNotFoundException {
            this.inputStream = ONLINE_JUDGE ? System.in : new FileInputStream(INPUT_FILENAME);
        }

        public int[] readIntArray(int size) {
            int[] array = new int[size];
            for (int i = 0; i < size; i++) {
                array[i] = readInt();
            }
            return array;
        }

        public long[] readLongArray(int size) {
            long[] array = new long[size];
            for (int i = 0; i < size; i++) {
                array[i] = readLong();
            }
            return array;
        }

        public double[] readDoubleArray(int size) {
            double[] array = new double[size];
            for (int i = 0; i < size; i++) {
                array[i] = readLong();
            }
            return array;
        }

        public double readDouble() {
            return Double.parseDouble(readToken());
        }

        public String readToken() {
            int b = skip();
            StringBuilder sb = new StringBuilder();
            while (!(isSpaceChar(b))) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public int readInt() {
            int num = 0, b;
            boolean minus = false;

            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));

            if (b == '-') {
                minus = true;
                b = readByte();
            }

            while (true) {
                if (b >= '0' && b <= '9') {
                    num = num * 10 + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }

        public long readLong() {
            long num = 0;
            int b;
            boolean minus = false;

            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));

            if (b == '-') {
                minus = true;
                b = readByte();
            }

            while (true) {
                if (b >= '0' && b <= '9') {
                    num = num * 10 + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }

        public int readByte() {
            if (bufferLen == -1) {
                throw new InputMismatchException();
            }
            if (bufferPtr >= bufferLen) {
                bufferPtr = 0;
                try {
                    bufferLen = inputStream.read(buffer);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (bufferLen <= 0) {
                    return -1;
                }
            }
            return buffer[bufferPtr++];
        }

        public char readChar() {
            return (char) skip();
        }

        private int skip() {
            int b;
            while ((b = readByte()) != -1 && isSpaceChar(b));
            return b;
        }

        private static boolean isSpaceChar(int c) {
            return !(c >= 33 && c <= 126);
        }

        @Override
        public void close() throws Exception {
            inputStream.close();
            bufferPtr = bufferLen;
        }
    }

    private static class Tuple {
        public static <A, B> Tuple2<A, B> of(A first, B second) {
            return new Tuple2<>(first, second);
        }

        public static <A, B> List<Tuple2<A, B>> array(A[] first, B[] second) {
            if (first.length != second.length) {
                throw new RuntimeException(
                        String.format("Can't create tuples array from arrays with different length (%d != %d)",
                                first.length, second.length));
            }
            return IntStream.range(0, first.length)
                    .mapToObj(i -> Tuple.of(first[i], second[i]))
                    .collect(Collectors.toList());
        }

        public static <A, B, C> Tuple3<A, B, C> of(A first, B second, C third) {
            return new Tuple3<>(first, second, third);
        }

        public static <A, B, C> List<Tuple3<A, B, C>> array(A[] first, B[] second, C[] third) {
            if (first.length != second.length || second.length != third.length) {
                throw new RuntimeException(
                        String.format("Can't create tuples array from arrays with different length (%d, %d, %d)",
                                first.length, second.length, third.length));
            }
            return IntStream.range(0, first.length)
                    .mapToObj(i -> Tuple.of(first[i], second[i], third[i]))
                    .collect(Collectors.toList());
        }

        public static <A extends Comparable<A>, B extends Comparable<B>> Comparator<Tuple2<A, B>> tuple2Comparator() {
            return Comparator.comparing((Tuple2<A, B> t) -> t.first).thenComparing(t -> t.second);
        }

        public static <A extends Comparable<A>, B extends Comparable<B>, C extends Comparable<C>> Comparator<Tuple3<A, B, C>> tuple3Comparator() {
            return Comparator.comparing((Tuple3<A, B, C> t) -> t.first).thenComparing(t -> t.second).thenComparing(t -> t.third);
        }

        private static class Tuple2<A, B> extends Tuple {
            public final A first;
            public final B second;

            private Tuple2(A first, B second) {
                this.first = first;
                this.second = second;
            }

            @Override
            public String toString() {
                return "(" + first + ", " + second + ")";
            }
        }

        private static class Tuple3<A, B, C> extends Tuple2<A, B> {
            public final C third;

            private Tuple3(A first, B second, C third) {
                super(first, second);
                this.third = third;
            }

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

    private static class Utils {
        private static final Random RANDOM = new SecureRandom();

        public static <T extends Comparable<T>> void sort(T[] array) {
            shuffle(array);
            Arrays.sort(array);
        }

        public static void sort(int[] array) {
            shuffle(array);
            Arrays.sort(array);
        }

        public static void sort(long[] array) {
            shuffle(array);
            Arrays.sort(array);
        }

        public static void sort(double[] array) {
            shuffle(array);
            Arrays.sort(array);
        }

        public static <T> void sort(T[] array, Comparator<T> comparator) {
            shuffle(array);
            Arrays.sort(array, comparator);
        }

        public static <T> void shuffle(T[] array) {
            for (int i = array.length - 1; i > 0; i--) {
                int index = RANDOM.nextInt(i + 1);
                T a = array[index];
                array[index] = array[i];
                array[i] = a;
            }
        }

        public static void shuffle(int[] array) {
            for (int i = array.length - 1; i > 0; i--) {
                int index = RANDOM.nextInt(i + 1);
                int a = array[index];
                array[index] = array[i];
                array[i] = a;
            }
        }

        public static void shuffle(long[] array) {
            for (int i = array.length - 1; i > 0; i--) {
                int index = RANDOM.nextInt(i + 1);
                long a = array[index];
                array[index] = array[i];
                array[i] = a;
            }
        }

        public static void shuffle(double[] array) {
            for (int i = array.length - 1; i > 0; i--) {
                int index = RANDOM.nextInt(i + 1);
                double a = array[index];
                array[index] = array[i];
                array[i] = a;
            }
        }

        public static <T extends Comparable<T>> void sort(List<T> list) {
            Collections.shuffle(list);
            Collections.sort(list);
        }

        public static <T> void sort(List<T> list, Comparator<T> comparator) {
            Collections.shuffle(list);
            list.sort(comparator);
        }

        public static int randomInt(int fromIncluded, int toExcluded) {
            return fromIncluded + RANDOM.nextInt(toExcluded - fromIncluded);
        }

        public static Integer[] generify(int[] a) {
            return IntStream.of(a).boxed().toArray(Integer[]::new);
        }

        public static Long[] generify(long[] a) {
            return LongStream.of(a).boxed().toArray(Long[]::new);
        }

        public static Double[] generify(double[] a) {
            return DoubleStream.of(a).boxed().toArray(Double[]::new);
        }
    }
}
