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

public class Main {
    static Scanner sc;
    static BufferedReader br;
    static PrintWriter out;

    public static void main(String[] args) throws Exception{
        sc = new Scanner(System.in);
         //  br=new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);

        int t = 1;
        // t = Integer.valueOf(br.readLine().split(" ")[0]);
        //seiveOfEri();
        t=sc.nextInt();
        while (t-- > 0) {
            forEachTestCase();
        }


       //    br.close();
        sc.close();
        out.close();
    }
//    1,1-> 2
//
//    1,2-> 3
//    2,1-> 3
//
//    1,3-> 4
//    2,2-> 4
//    3,1-> 4


    static void forEachTestCase() throws  Exception{
        /*

        *     1->|
        *        __
        *
        *     2-> |
        *        __
        *
        *      3->__
        *         |
        *
        *      4->__
        *          |
        *
                1   3
             01  11  00
             01  10  00

                1   2   4
             00  10  11  00
             01  10  01  00

                3
             01  10
             00  10
        * */

        int n=sc.nextInt();
        int m=sc.nextInt();
        int a[][]=new int[n][m];
        for (int i = 0; i <n ; i++) {
            String s=sc.next();
            for (int j = 0; j <m ; j++) {
                a[i][j]=s.charAt(j)-'0';
            }
        }
        ArrayList<Node> l=new ArrayList<>();
        for (int i = 0; i <n-2 ; i++) {
            for (int j = 0; j <m-1 ; j++) {
                if(a[i][j]==1){
                    l.add(new Node(i,j,1));
                    f(i,j,1,a);
                }
            }
            if(a[i][m-1]==1){
                l.add(new Node(i,m-2,2));
                f(i,m-2,2,a);
            }
        }
        for (int i = 0; i <m-1; i++) {
            if(a[n-1][i]==a[n-2][i]){
                if(a[n-1][i]==1){
                    l.add(new Node(n-2,i,1));
                    f(n-2,i,1,a);
                }
            }
            else{
                if(a[n-2][i]==1){
                    l.add(new Node(n-2,i,4));
                    f(n-2,i,4,a);
                }
                else{
                    l.add(new Node(n-2,i,2));
                    f(n-2,i,2,a);
                }
            }
        }
        if(a[n-1][m-1]==a[n-2][m-1]){
            if(a[n-1][m-1]==1){
                l.add(new Node(n-2,m-2,1));
                f(n-2,m-2,1,a);
                l.add(new Node(n-2,m-2,3));
                f(n-2,m-2,3,a);
            }
        }
        else{
            if(a[n-1][m-1]==1){
                l.add(new Node(n-2,m-2,1));
                f(n-2,m-2,1,a);
                l.add(new Node(n-2,m-2,2));
                f(n-2,m-2,2,a);
                l.add(new Node(n-2,m-2,4));
                f(n-2,m-2,4,a);
            }
            else{
                l.add(new Node(n-2,m-2,3));
                f(n-2,m-2,3,a);
                l.add(new Node(n-2,m-2,2));
                f(n-2,m-2,2,a);
                l.add(new Node(n-2,m-2,4));
                f(n-2,m-2,4,a);
            }
        }

        ArrayList<Integer> ans=new ArrayList<>();
        for(Node u:l){
            if(u.type==1){
                ans.add(u.x);
                ans.add(u.y);
                ans.add(u.x+1);
                ans.add(u.y);
                ans.add(u.x+1);
                ans.add(u.y+1);
            }
            else if(u.type==2){
                ans.add(u.x);
                ans.add(u.y+1);
                ans.add(u.x+1);
                ans.add(u.y);
                ans.add(u.x+1);
                ans.add(u.y+1);
            }
            else if(u.type==3){
                ans.add(u.x);
                ans.add(u.y);
                ans.add(u.x);
                ans.add(u.y+1);
                ans.add(u.x+1);
                ans.add(u.y);
            }
            else{
                ans.add(u.x);
                ans.add(u.y);
                ans.add(u.x);
                ans.add(u.y+1);
                ans.add(u.x+1);
                ans.add(u.y+1);
            }
        }
        out.println(ans.size()/6);
        int c=0;
        for(int x:ans){
            out.print((x+1)+" ");
            c++;
            if(c%6==0)out.println();
        }
    }

    static void f(int x,int y,int type,int a[][]){
        if(type==1){
            a[x][y]^=1;
            a[x+1][y]^=1;
            a[x+1][y+1]^=1;
        }
        else if(type==2){
            a[x][y+1]^=1;
            a[x+1][y]^=1;
            a[x+1][y+1]^=1;
        }
        else if(type==3){
            a[x][y]^=1;
            a[x][y+1]^=1;
            a[x+1][y]^=1;
        }
        else{
            a[x][y]^=1;
            a[x][y+1]^=1;
            a[x+1][y+1]^=1;
        }
    }

    static class Node{
        int x,y,type;
        Node(int x,int y,int type){
            this.x=x;
            this.y=y;
            this.type=type;
        }

    }



    static void remove(TreeMap<Integer,Integer> tm,int key){
        int fre=tm.get(key);
        if(fre==1){
            tm.remove(key);
            return;
        }
        tm.put(key,fre-1);
    }
    static long mod = 1000000007;


    static long[][] mul(long a[][],long b[][]){
        long ans[][]=new long[a.length][b[0].length];
        for (int i = 0; i <a.length ; i++) {
            for (int j = 0; j <b[0].length ; j++) {
                for (int k = 0; k <a[0].length ; k++) {
                    ans[i][j]=add(ans[i][j],mul(a[i][k],b[k][j]));
                }
            }
        }
        return ans;
    }
    static long add(long a, long b) {
        a %= mod;
        b %= mod;
        return (a + b) % mod;
    }

    static long mul(long a, long b) {
        a %= mod;
        b %= mod;
        return (a * b) % mod;
    }

    static long sub(long a, long b) {
        a %= mod;
        b %= mod;
        return (((a - b) % mod) + mod) % mod;
    }

    static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }
    static int gcd(int a,int b){
        if(b==0)return a;
        return gcd(b,a%b);
    }

    static long modPow(long a, long b) {
        long ans = 1;
        while (b > 0) {
            if (b % 2 == 1) {
                ans = mul(ans, a);
            }
            a = mul(a, a);
            b >>= 1;
        }
        return ans;
    }

    static long modInv(long a) {
        return modPow(a, mod - 2);
    }

    static long fact[],ifact[];
    static int maxFactSize=200001;
    static void  initFact(){
        fact=new long[maxFactSize];
        ifact=new long[maxFactSize];
        fact[0]=ifact[0]=1;
        for (int i = 1; i <maxFactSize ; i++) {
            fact[i]=mul(fact[i-1],i);
            ifact[i]=mul(ifact[i-1],modInv(i));
        }
    }

    static long nCr(int n,int r){
        if(n>r || n<0 || r<0)return 0;
        if(fact==null)initFact();
        return mul(fact[n],mul(ifact[n],ifact[r]));
    }

    static int seive[];
    static ArrayList<Integer> primes;
    static int seiveLength=2000001;
    static void seiveOfEri(){
        seive=new int[seiveLength];
        primes=new ArrayList<>();
        for (int i = 2; i <seiveLength ; i++) {
            if(seive[i]==0) {
                primes.add(i);
                for (int j = i; j < seiveLength; j += i) {
                    seive[j]=i;
                }
            }
        }
    }



    static class DSU{
        int n,par[],size[],connectedComponent;
        DSU(int n){
            this.n=n;
            par=new int[n];
            size=new int[n];
            connectedComponent=n;
            for(int i=0;i<n;i++){
                par[i]=i;
                size[i]=1;
            }
        }
        int root(int u){
            if(u==par[u]){
                return u;
            }
            return par[u]=root(par[u]);
        }
        boolean isConnected(int u,int v){
            u=root(u);
            v=root(v);
            if(u==v)return true;
            return false;
        }
        void union(int u,int v){
            u=root(u);
            v=root(v);
            if(u!=v){
                if(size[u]<size[v]){
                    int temp=u;
                    u=v;
                    v=temp;
                }
                size[u]+=size[v];
                par[v]=u;
                connectedComponent--;
            }
        }
        boolean oneComponent(){
            if(connectedComponent==1)return true;
            return false;
        }
    }
}