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.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.io.*;

public class Equal {


    static class pair implements Comparable<pair> {
        int x;
        int y;

        public pair(int u, int  v) {
            this.x = u;
            this.y = v;
        }

        @Override
        public int compareTo(pair o) {
            if(y==o.y)
                return 0;
            if(y<o.y)
                return 1;
            return -1;
        }
    }

    static double dis(pair a, pair b) {
        return Math.sqrt(((a.x - b.x) * (a.x - b.x)) + ((a.y - b.y) * (a.y - b.y)));
    }

    static int c;
    static int d;
    static int e;

    static void extendedEuclid(int a, int b) {
        if (b == 0) {
            c = 1;
            d = 0;
            e = a;
            return;
        }
        extendedEuclid(b, a % b);
        int x1 = d;
        int y1 = c - (a / b) * d;
        c = x1;
        d = y1;
    }
    static int[]rank;
    static int[]p;
    static int sets;
    static int findSet(int i){
        return (p[i]==i)?i:(p[i]=findSet(p[i]));
    }
    static boolean isSameSet(int i,int j){
        return findSet(i)==findSet(j);
    }
    static void unionSet(int i,int j){
        if(!isSameSet(i,j)){
            sets--;
            int x=findSet(i);
            int y=findSet(j);
            if(rank[x]>rank[y])
                p[y]=x;
            else {
                p[x]=y;
                if(rank[x]==rank[y])
                    rank[y]++;
            }
        }
    }
    static class SegmentTree { // 1-based DS, OOP

        int N; // the number of elements in the array as a power of 2 (i.e. after padding)
        long[] array, sTree, lazy;

        SegmentTree(long[] in) {
            array = in;
            N = in.length - 1;
            sTree = new long[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero
            lazy = new long[N << 1];
            build(1, 1, N);
        }

        void build(int node, int b, int e) // O(n)
        {
            if (b == e)
                sTree[node] = array[b];
            else {
                int mid = b + e >> 1;
                build(node << 1, b, mid);
                build(node << 1 | 1, mid + 1, e);
                sTree[node] = Math.min(sTree[node << 1] , sTree[node << 1 | 1]);
            }
        }


        long query(int i, int j) {
            return query(1, 1, N, i, j);
        }

        long query(int node, int b, int e, int i, int j) // O(log n)
        {
            if (i > e || j < b)
                return Long.MAX_VALUE;
            if (b >= i && e <= j)
                return sTree[node];
            int mid = b + e >> 1;
            long q1 = query(node << 1, b, mid, i, j);
            long q2 = query(node << 1 | 1, mid + 1, e, i, j);
            return Math.min(q1 , q2);

        }
    }

    static int minIdx(long val,int idx,SegmentTree sg) {

        int lo=idx+1;
        int hi=sg.N;
        int ans=-1;
        while(lo<=hi) {
            int mid=(lo+hi)>>1;
            long x=sg.query(idx+1, mid);
            if(x<=val) {
                ans=mid-1;
                hi=mid-1;
            }
            else {
                lo=mid+1;
            }
        }
        return ans;
    }
    static class tri implements Comparable<tri>{
        int a;pair p;
        public tri(int  aa,pair pp){
            a=aa;p=pp;
        }

        @Override
        public int compareTo(tri o) {
           return o.a-a;
        }
    }
    static long[]mem;
    static pair[]in;
    static int [][]vis;
    static long max;
    static int[]next;
    static long[]a;
    static long  dp(int i){
       if(i<0||i>=mem.length)
           return -1;
       if(mem[i]!=-1)
           return mem[i];
       long x=dp(next[i]);

       if(x==-1)
          return mem[i]=a[i];

       return mem[i]=gcd(a[i],x);

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

static ArrayList<pair>res;
    static tri[] change2(tri[]cur){
        //Arrays.sort(cur);
        if(cur[0].a=='0')
           return cur;
        if(cur[2].a=='1'){
           // System.err.println("hey");
            res.add(cur[0].p);
            res.add(cur[1].p);
            res.add(cur[2].p);
            cur[0].a='0';
            cur[1].a='0';
            cur[2].a='0';
        }
        else {
            if(cur[1].a=='1'){
                res.add(cur[1].p);
                res.add(cur[2].p);
                res.add(cur[3].p);
                cur[1].a='0';
                cur[2].a='1';
                cur[3].a='1';
            }
            else {
                res.add(cur[0].p);
                res.add(cur[1].p);
                res.add(cur[2].p);
                cur[0].a='0';
                cur[1].a='1';
                cur[2].a='1';
            }
        }
        return cur;
    }
    static void change(int ibeg,int iend,int jbeg,int jend,char[][]mat){
        for (int i = ibeg; i <=iend ; i+=2) {
            for (int j = jbeg; j <=jend ; j+=2) {
                tri[]cur=new tri[4];
                cur[0]=new tri(mat[i][j],new pair(i,j));
                cur[1]=new tri(mat[i][j+1],new pair(i,j+1));
                cur[2]=new tri(mat[i+1][j],new pair(i+1,j));
                cur[3]=new tri(mat[i+1][j+1],new pair(i+1,j+1));
                 Arrays.sort(cur);
              while(cur[0].a=='1'){
                  cur=change2(cur);
                  Arrays.sort(cur);
              }


            }
        }
    }

    public static void main(String[] args) throws IOException {
        //BufferedReader br = new BufferedReader(new FileReader("name.in"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        PrintWriter out = new PrintWriter(System.out);

       int T=Integer.parseInt(br.readLine());
       while(T-->0){
           res=new ArrayList<>();
           st=new StringTokenizer(br.readLine());
           int n=Integer.parseInt(st.nextToken());
           int m=Integer.parseInt(st.nextToken());
           char[][]mat=new char[n][m];
           for (int i = 0; i <n ; i++) {
               mat[i]=br.readLine().toCharArray();
           }
           int ibeg=n%2;
           int iend=n-1;
           int jbeg=m%2;
           int jend=m-1;

           if(ibeg==1){
               for (int j = jbeg; j <m ; j+=2) {
                   if(mat[0][j]=='0'&&mat[0][j+1]=='0')
                       continue;
                   if(mat[0][j]=='1'&&mat[0][j+1]=='1'){
                       res.add(new pair(0,j));
                       res.add(new pair(0,j+1));
                       res.add(new pair(1,j));
                       mat[1][j]=(mat[1][j]=='0')?'1':'0';
                   }
                   else {
                       if(mat[0][j]=='1')
                       res.add(new pair(0,j));
                       else
                           res.add(new pair(0,j+1));
                       res.add(new pair(1,j+1));
                       res.add(new pair(1,j));
                           mat[1][j]=(mat[1][j]=='0')?'1':'0';
                           mat[1][j+1]=(mat[1][j+1]=='0')?'1':'0';

                   }
               }
           }
           if(jbeg==1){
               if(ibeg==1){
                   if (mat[0][0] == '1') {
                       res.add(new pair(0,0));
                       res.add(new pair(1,0));
                       res.add(new pair(1,1));
                       mat[1][0]=(mat[1][0]=='0')?'1':'0';
                       mat[1][1]=(mat[1][1]=='0')?'1':'0';
                   }
               }
               for (int i = ibeg; i < n; i+=2) {
                   if(mat[i][0]=='0'&&mat[i+1][0]=='0')
                       continue;
                   if(mat[i][0]=='1'&&mat[i+1][0]=='1'){
                       mat[i][1]=(mat[i][1]=='0')?'1':'0';
                       res.add(new pair(i,0));
                       res.add(new pair(i+1,0));
                       res.add(new pair(i,1));
                   }
                   else{
                       res.add(new pair(i,1));
                       res.add(new pair(i+1,1));
                       mat[i][1]=(mat[i][1]=='0')?'1':'0';
                       mat[i+1][1]=(mat[i+1][1]=='0')?'1':'0';
                       if(mat[i][0]=='1'){
                           res.add(new pair(i,0));
                       }
                       else res.add(new pair(i+1,0));
                   }
               }

           }
//           for (int i = 0; i < n;     i++) {
//               for (int j = 0; j <m ; j++) {
//                   System.err.print(mat[i][j]+" ");
//               }
//               System.err.println();
//           }
           change(ibeg,iend,jbeg,jend,mat);
           out.println(res.size()/3);
           for (int i = 0; i <res.size() ; i+=3) {
               out.print(res.get(i).x+1+" "+(res.get(i).y+1)+" ");
               out.print(res.get(i+1).x+1+" "+(res.get(i+1).y+1)+" ");
               out.print(res.get(i+2).x+1+" "+(res.get(i+2).y+1));
               out.println();
           }
       }




        out.flush();
        out.close();

    }

}