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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class CodeForces extends Functions {
    public static void main(String[] args) {
        Scanner sc = new Scanner();
        int tescCase = sc.nextInt();
        while (tescCase-->0){
            int r = sc.nextInt();
            int c = sc.nextInt();
            boolean[][] arr = new boolean[r][c];
            for(int i=0;i<r;i++){
                String str = sc.next();
                for(int j=0;j<c;j++){
                    arr[i][j] = str.charAt(j) != '0';
                }
            }
            ArrayList<Point> list = new ArrayList<>();
            for(int i=0;i<c-2;i++){
                int j;
                for( j=0;j<r-1;j++){
                    if(arr[j][i]){
                        arr[j][i]=false;
                        arr[j+1][i] = !arr[j+1][i];
                        arr[j+1][i+1] = !arr[j+1][i+1];
                        list.add(new Point(j,i,j+1,i,j+1,i+1));
                    }
                }
                if(j==r-1 && arr[j][i]){
                    arr[j][i]=false;
                    arr[j][i+1] = !arr[j][i+1];
                    arr[j-1][i+1] = !arr[j-1][i+1];
                    list.add(new Point(j,i,j,i+1,j-1,i+1));
                }
            }
//            for(boolean[] ele : arr) System.out.println(Arrays.toString(ele));
            if(r%2==1){
                if(arr[0][c-2] && arr[0][c-1]){
                    arr[0][c-2]=false;
                    arr[0][c-1]=false;
//                    System.out.println(!arr[1][c-1]);
                    arr[1][c-1]=!arr[1][c-1];
                    list.add(new Point(0,c-2,0,c-1,1,c-1));
                }else if(arr[0][c-2]){
                    arr[0][c-2]=false;arr[1][c-2]=!arr[1][c-2];arr[1][c-1]=!arr[1][c-1];
                    list.add(new Point(0,c-2,1,c-2,1,c-1));
                }
                else if(arr[0][c-1]){
                    arr[0][c-1]=false;arr[1][c-2]=!arr[1][c-2];arr[1][c-1]=!arr[1][c-1];
                    list.add(new Point(0,c-1,1,c-2,1,c-1));
                }
            }
//            for(boolean[] ele : arr) System.out.println(Arrays.toString(ele));
            int k=0;
            if(r%2==1)k++;
            for(int i=k;i<r-1;i+=2){
                handle2x2(arr,i,c,list);
            }

//            for(boolean[] ele : arr) System.out.println(Arrays.toString(ele));

            System.out.println(list.size());
            for(Point i : list)
                System.out.println(i);

        }

    }

    private static void handle2x2(boolean[][] arr, int i,int c, ArrayList<Point> list) {
        int ones = 0;
        if(arr[i][c-2])ones++;
        if(arr[i][c-1])ones++;
        if(arr[i+1][c-2])ones++;
        if(arr[i+1][c-1])ones++;
        if(ones==4){
            list.add(new Point(i,c-2,i,c-1,i+1,c-2));
            arr[i][c-2]=false;arr[i][c-1]=false;arr[i+1][c-2]=false;
            ones=1;
        }
        if(ones==1){
            if(arr[i][c-2] || arr[i][c-1]){
                int col = arr[i][c - 2] ? c-2 : c-1;
                arr[i][col]=false;arr[i+1][c-2]=true;arr[i+1][c-1]=true;
                list.add(new Point(i+1,c-2,i+1,c-1,i,col));
            }
            else{
                int col = arr[i+1][c-2]?c-2:c-1;
                arr[i+1][col]=false;arr[i][c-1]=true;arr[i][c-2]=true;
                list.add(new Point(i+1,col,i,c-1,i,c-2));
            }
            ones++;
        }
        if(ones==2){
            if(arr[i][c-2]){
                arr[i][c-1]=!arr[i][c-1];arr[i+1][c-1]=!arr[i+1][c-1];arr[i+1][c-2]=!arr[i+1][c-2];
                list.add(new Point(i,c-1,i+1,c-2,i+1,c-1));
            }
            else if(arr[i][c-1]){
                arr[i][c-2]=!arr[i][c-2];arr[i+1][c-1]=!arr[i+1][c-1];arr[i+1][c-2]=!arr[i+1][c-2];
                list.add(new Point(i,c-2,i+1,c-2,i+1,c-1));
            }
            else{
                arr[i][c-2]=!arr[i][c-2];arr[i][c-1]=!arr[i][c-1];arr[i+1][c-1]=!arr[i+1][c-1];
                list.add(new Point(i,c-2,i,c-1,i+1,c-1));
            }
            ones++;
        }
        if(ones==3){
            if(!arr[i][c-2]) list.add(new Point(i,c-1,i+1,c-2,i+1,c-1));
            else if(!arr[i][c-1]) list.add(new Point(i,c-2,i+1,c-2,i+1,c-1));
            else if(!arr[i+1][c-2]) list.add(new Point(i,c-2,i,c-1,i+1,c-1));
            else list.add(new Point(i,c-2,i,c-1,i+1,c-2));
        }
    }


}
class Point{
    int x1,y1,x2,y2,x3,y3;

    public Point(int x1, int y1, int x2, int y2, int x3, int y3) {
        this.x1 = x1+1;
        this.y1 = y1+1;
        this.x2 = x2+1;
        this.y2 = y2+1;
        this.x3 = x3+1;
        this.y3 = y3+1;
    }

    @Override
    public String toString() {
        return x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+y3;
    }
}
class Scanner {
    private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private StringTokenizer st = new StringTokenizer("");
    public String next(){
        while (!st.hasMoreTokens())
            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());
    }
    public double nextDouble(){
        return Double.parseDouble(next());
    }
    public int[] setIntegerArray(int n){
        int[] arr =new int[n];
        for(int i=0;i<n;i++)arr[i] = nextInt();
        return arr;
    }
    public long[] setLongArray(int n){
        long[] arr =new long[n];
        for(int i=0;i<n;i++)arr[i] = nextLong();
        return arr;
    }
}
class Functions {
    public static int mod = (int)1e9+7;
    public static void sort(int[] a){
        ArrayList<Integer> temp = new ArrayList<>();
        for (int j : a) temp.add(j);
        Collections.sort(temp);;
        for(int i=0;i<a.length;i++)a[i] = temp.get(i);
    }
    public static long factorial(int n){
        long fact = 1L;
        for(int i=2;i<=n;i++)fact=  (fact*i)%mod;
        return fact;
    }
    public static void sortRev(int[] a){
        sort(a);
        int left = 0;
        int right = a.length-1;
        while (left<right){
            int temp =a[left];
            a[left] = a[right];
            a[right] = temp;
            left++;
            right--;
        }
    }
    public static boolean isPrime(int n){
        for(int i=2 ;i*i<=n;i++)
            if(n%i==0)
                return true;
        return false;
    }
}
/*
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e6+5;
    long long a[maxn];
    long long b[maxn];
    long long c[maxn];
    long long t,u,v,n;
    int main()
    {
        ios_base::sync_with_stdio(false);cin.tie();cout.tie(0);
        cin>>t;
        while(t--)
        {
           cin>>n;long long sum=0,maxa=0;
           for(int i=1;i<=n;i++)
           {
               cin>>a[i];sum+=a[i];
               maxa=max(maxa,a[i]);
           }
           while(1)
           {
               long long ans=(n-1)*maxa;
               if(ans>=sum)break;
               maxa++;
           }
           long long ans=(n-1)*maxa;
           cout<<ans-sum<<endl;
        }
    }
 */