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:
//package Codeforces.Round684Div2;

import java.io.*;
import java.util.*;

public class C1 {
    public static void fun4(StringBuilder sb, char ch00, char ch01, char ch10, char ch11,
                            int i, int j)
    {
        sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
        sb.append(i + 1).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
        sb.append(i).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
        sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j).append("\n");
    }
    public static void fun3(StringBuilder sb, char ch00, char ch01, char ch10, char ch11,
                            int i, int j)
    {
        if(ch00=='0')
        {
            sb.append(i + 1).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
        }
        else if(ch01=='0')
        {
            sb.append(i).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
        }
        else if(ch10=='0')
        {
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
        }
        else
        {
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j).append("\n");
        }
    }
    public static void fun2(StringBuilder sb, char ch00, char ch01, char ch10, char ch11,
                            int i, int j)
    {
        if(ch00=='0' && ch01=='0')
        {
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j).append("\n");
        }
        else if(ch01=='0' && ch11=='0')
        {
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
            sb.append(i + 1).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
        }
        else if(ch11=='0' && ch10=='0')
        {
            sb.append(i + 1).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
            sb.append(i).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
        }
        else if(ch00=='0' && ch10=='0')
        {
            sb.append(i).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j).append("\n");
        }
        else if(ch00=='0' && ch11=='0')
        {
            sb.append(i).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
        }
        else {
            sb.append(i + 1).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j).append("\n");
        }
    }
    public static void fun1(StringBuilder sb, char ch00, char ch01, char ch10, char ch11,
                            int i, int j)
    {
        if(ch00=='1')
        {
            sb.append(i).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j).append("\n");
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
        }
        else if(ch01=='1')
        {
            sb.append(i + 1).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j).append("\n");
        }
        else if(ch10=='1')
        {
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j).append("\n");
            sb.append(i + 1).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
            sb.append(i).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
        }
        else
        {
            sb.append(i).append(" ").append(j).append(" ").append(i + 1).append(" ").append(j).append(" ").append(i+1).append(" ").append(j + 1).append("\n");
            sb.append(i).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
            sb.append(i + 1).append(" ").append(j).append(" ").append(i).append(" ").append(j + 1).append(" ").append(i + 1).append(" ").append(j + 1).append("\n");
        }
    }
    public static void main(String[] args) throws IOException {
        Soumit sc = new Soumit();

        int t = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (t-->0)
        {
            int n = sc.nextInt();
            int m = sc.nextInt();

            char[][] mat = new char[n][m];
            for(int i=0;i<n;i++)
                mat[i] = sc.next().toCharArray();

            char[][] matc = new char[n][m];
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                    matc[i][j] = mat[i][j];
            }

            StringBuilder ops = new StringBuilder();
            int countops = 0;

            for(int i=0;i<2*(n/2);i+=2)
            {
                for(int j=0;j<m-2;j++)
                {
                    int ones = 0;
                    StringBuilder temp = new StringBuilder();
                    if(mat[i][j] == '1')
                    {
                        temp.append(i+1).append(" ").append(j+1).append(" ");
                        mat[i][j] = '0';
                        ones++;
                    }
                    if(mat[i+1][j] == '1')
                    {
                        temp.append(i + 1+1).append(" ").append(j+1).append(" ");
                        mat[i+1][j] = '0';
                        ones++;
                    }

                    if(ones==0)
                        continue;

                    if(ones==1)
                    {
                        temp.append(i+1).append(" ").append(j +1+1).append(" ");
                        if(mat[i][j+1]=='0')
                            mat[i][j+1]='1';
                        else mat[i][j+1]='0';
                    }
                    temp.append(i + 1+1).append(" ").append(j + 1+1).append("\n");
                    if(mat[i+1][j+1]=='0')
                        mat[i+1][j+1]='1';
                    else mat[i+1][j+1]='0';

                    countops++;
                    ops.append(temp);
                }
            }

            for(int i=0;i<n-2;i++)
            {
                int ones = 0;
                StringBuilder temp = new StringBuilder();
                if(mat[i][m-2] == '1')
                {
                    temp.append(i+1).append(" ").append(m-2+1).append(" ");
                    mat[i][m-2] = '0';
                    ones++;
                }
                if(mat[i][m-2+1] == '1')
                {
                    temp.append(i + 1).append(" ").append(m-2+1+1).append(" ");
                    mat[i][m-2+1] = '0';
                    ones++;
                }

                if(ones==0)
                    continue;

                if(ones==1)
                {
                    temp.append(i+1+1).append(" ").append(m-2 + 1).append(" ");
                    if(mat[i+1][m-2]=='0')
                        mat[i+1][m-2]='1';
                    else mat[i+1][m-2]='0';
                }
                temp.append(i + 1+1).append(" ").append(m-2 + 1+1).append("\n");
                if(mat[i+1][m-2+1]=='0')
                    mat[i+1][m-2+1]='1';
                else mat[i+1][m-2+1]='0';

                countops++;
                ops.append(temp);
            }

            for(int j=0;j<m-2;j++)
            {
                int ones = 0;
                StringBuilder temp = new StringBuilder();
                if(mat[n-2][j] == '1')
                {
                    temp.append(n-2+1).append(" ").append(j+1).append(" ");
                    mat[n-2][j] = '0';
                    ones++;
                }
                if(mat[n-2+1][j] == '1')
                {
                    temp.append(n-2 + 1+1).append(" ").append(j+1).append(" ");
                    mat[n-2+1][j] = '0';
                    ones++;
                }

                if(ones==0)
                    continue;

                if(ones==1)
                {
                    temp.append(n-2+1).append(" ").append(j +1+1).append(" ");
                    if(mat[n-2][j+1]=='0')
                        mat[n-2][j+1]='1';
                    else mat[n-2][j+1]='0';
                }
                temp.append(n-2 + 1+1).append(" ").append(j + 1+1).append("\n");
                if(mat[n-2+1][j+1]=='0')
                    mat[n-2+1][j+1]='1';
                else mat[n-2+1][j+1]='0';

                countops++;
                ops.append(temp);
            }

            int iend = n-2;
            char ch00 = mat[iend][m-2];
            char ch01 = mat[iend][m-2+1];
            char ch10 = mat[iend+1][m-2];
            char ch11 = mat[iend+1][m-2+1];

            int ones = 0;
            if(ch00=='1')
                ones++;
            if(ch01=='1')
                ones++;
            if(ch10=='1')
                ones++;
            if(ch11=='1')
                ones++;

            if(ones==4) {
                fun4(ops, ch00, ch01, ch10, ch11, iend+1, m-2+1);
                countops += 4;
            }
            else if(ones==3) {
                fun3(ops, ch00, ch01, ch10, ch11, iend+1, m-2+1);
                countops += 1;
            }
            else if(ones==2) {
                fun2(ops, ch00, ch01, ch10, ch11, iend+1, m-2+1);
                countops += 2;
            }
            else if(ones==1) {
                fun1(ops, ch00, ch01, ch10, ch11, iend+1, m-2+1);
                countops += 3;
            }

            mat[iend][m-2] = '0';
            mat[iend][m-2+1] = '0';
            mat[iend+1][m-2] = '0';
            mat[iend+1][m-2+1] = '0';

            sb.append(countops).append("\n");
            sb.append(ops);


            /*if(countops>(n*m))
            {
                System.out.println("Operations limit exceeded");
                for(int i=0;i<n;i++)
                {
                    for(int j=0;j<m;j++)
                    {
                        System.out.print(matc[i][j]);
                    }
                    System.out.println();
                }
                System.exit(0);
            }

            Scanner sc1 = new Scanner(ops.toString());
            for(int i=0;i<countops;i++)
            {
                int x1 = sc1.nextInt();
                int y1 = sc1.nextInt();
                int x2 = sc1.nextInt();
                int y2 = sc1.nextInt();
                int x3 = sc1.nextInt();
                int y3 = sc1.nextInt();

                //System.out.println(x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+y3);

                if(matc[x1-1][y1-1]=='0')
                    matc[x1-1][y1-1] = '1';
                else matc[x1-1][y1-1] = '0';

                if(matc[x2-1][y2-1]=='0')
                    matc[x2-1][y2-1] = '1';
                else matc[x2-1][y2-1] = '0';

                if(matc[x3-1][y3-1]=='0')
                    matc[x3-1][y3-1] = '1';
                else matc[x3-1][y3-1] = '0';
                for(int i1=0;i1<n;i1++)
                {
                    for(int j=0;j<m;j++)
                    {
                        System.out.print(matc[i1][j]+" ");
                    }
                    System.out.println();
                }

            }

            boolean flag= true;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(matc[i][j] == '1') {
                        flag = false;
                        break;
                    }
                }
            }

            if(!flag)
            {
                System.out.println("Wrong answer: "+n+" "+m+" "+t);
                for(int i=0;i<n;i++)
                {
                    for(int j=0;j<m;j++)
                    {
                        System.out.print(matc[i][j]+" ");
                    }
                    System.out.println();
                }
                System.exit(0);
            }*/
        }

        System.out.println(sb);

        sc.close();
    }

    static class Soumit {
        final private int BUFFER_SIZE = 1 << 18;
        final private DataInputStream din;
        final private byte[] buffer;
        private PrintWriter pw;
        private int bufferPointer, bytesRead;
        StringTokenizer st;

        public Soumit() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Soumit(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public void streamOutput(String file) throws IOException {
            FileWriter fw = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(fw);
            pw = new PrintWriter(bw);
        }

        public void println(String a) {
            pw.println(a);
        }

        public void print(String a) {
            pw.print(a);
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[100064]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

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

        public int[] nextIntArray(int n) throws IOException {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }

            return arr;
        }

        public long[] nextLongArray(int n) throws IOException {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }

            return arr;
        }

        public double[] nextDoubleArray(int n) throws IOException {
            double[] arr = new double[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextDouble();
            }

            return arr;
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            /*if (din == null)
                return;*/
            if (din != null) din.close();
            if (pw != null) pw.close();
        }
    }
}