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 static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;

   public class C2
   {
      public static void main(String hi[]) throws Exception
      {
         FastScanner infile = new FastScanner();
         int T = infile.nextInt();
         StringBuilder sb = new StringBuilder();
         while(T-->0)
         {
            int N = infile.nextInt();
            int M = infile.nextInt();
            int[][] grid = new int[N+1][M+1];
            for(int r=0; r < N; r++)
            {
               String row = infile.next();
               for(int c=0; c < M; c++)
                  grid[r+1][c+1] = row.charAt(c)-'0';
            }
            moves = new ArrayList<Integer>();
            //last column
            if(M%2 == 1)
            {
               int boundN = N-(N%2);
               for(int r=1; r <= boundN; r+=2)
               {
                  int c1 = grid[r][M];
                  int c2 = grid[r+1][M];
                  if(c1 == 1 && c2 == 1)
                  {
                     add(r, M);
                     add(r+1, M);
                     add(r, M-1);
                     grid[r][M] = grid[r+1][M] = 0;
                     grid[r][M-1] ^= 1;
                  }
                  else if(c1 == 1 && c2 == 0)
                  {
                     add(r, M);
                     add(r, M-1);
                     add(r+1, M-1);
                     grid[r][M] = 0;
                     grid[r][M-1] ^= 1;
                     grid[r+1][M-1] ^= 1;
                  }
                  else if(c1 == 0 && c2 == 1)
                  {
                     add(r+1, M);
                     add(r, M-1);
                     add(r+1, M-1);
                     grid[r+1][M] = 0;
                     grid[r][M-1] ^= 1;
                     grid[r+1][M-1] ^= 1;
                  }
               }
            }
            //last row
            if(N%2 == 1)
            {
               int boundM = M-(M%2);
               for(int c=1; c <= boundM; c+=2)
               {
                  int c1 = grid[N][c];
                  int c2 = grid[N][c+1];
                  if(c1 == 1 && c2 == 1)
                  {
                     add(N-1, c);
                     add(N, c);
                     add(N, c+1);
                     grid[N][c] = grid[N][c+1] = 0;
                     grid[N-1][c] ^= 1;
                  }
                  else if(c1 == 1 && c2 == 0)
                  {
                     add(N, c);
                     add(N-1, c);
                     add(N-1, c+1);
                     grid[N][c] = 0;
                     grid[N-1][c] ^= 1;
                     grid[N-1][c+1] ^= 1;
                  }
                  else if(c1 == 0 && c2 == 1)
                  {
                     add(N, c+1);
                     add(N-1, c);
                     add(N-1, c+1);
                     grid[N][c+1] = 0;
                     grid[N-1][c] ^= 1;
                     grid[N-1][c+1] ^= 1;
                  }
               }
            }
            //edge case
            if(N%2 == 1 && M%2 == 1 && grid[N][M] == 1)
            {
               add(N-1, M);
               add(N, M-1);
               add(N, M);
               //next
               add(N-1, M-1);
               add(N, M-1);
               add(N, M);
               //last
               add(N-1, M-1);
               add(N-1, M);
               add(N, M);
               grid[N][M] = 0;
            }
            //normal
            int boundN = N-(N%2);
            int boundM = M-(M%2);
            for(int r=1; r <= boundN; r+=2)
               for(int col=1; col <= boundM; col+=2)
               {
                  //ab
                  //cd
                  int a = grid[r][col];
                  int b = grid[r][col+1];
                  int c = grid[r+1][col];
                  int d = grid[r+1][col+1];
                  int lol = a+b+c+d;
                  if(lol == 1)
                  {
                     if(a == 1)
                        single1(r, col);
                     else if(b == 1)
                        single2(r, col);
                     else if(c == 1)
                        single3(r, col);
                     else
                        single4(r, col);
                  }
                  else if(a == 1 && b == 0 && c == 0 && d == 1)
                     diag1(r, col);
                  else if(a == 0 && b == 1 && c == 1 && d == 0)
                     diag2(r, col);
                  else if(a == 1 && b == 1 && c == 1 && d == 0)
                     easy1(r, col);
                  else if(a == 1 && b == 1 && c == 0 && d == 1)
                     easy2(r, col);
                  else if(a == 1 && b == 0 && c == 1 && d == 1)
                     easy3(r, col);
                  else if(a == 0 && b == 1 && c == 1 && d == 1)
                     easy4(r, col);
                  else if(lol == 4)
                  {
                     easy1(r, col);
                     single4(r, col);
                  }
                  else if(a == 1 && b == 1 && c == 0 && d == 0)
                  {
                     easy4(r, col);
                     easy3(r, col);
                  }
                  else if(a == 1 && b == 0 && c == 1 && d == 0)
                  {
                     easy2(r, col);
                     easy4(r, col);
                  }
                  else if(a == 0 && b == 1 && c == 0 && d == 1)
                  {
                     easy1(r, col);
                     easy3(r, col);
                  }
                  else if(a == 0 && b == 0 && c == 1 && d == 1)
                  {
                     easy1(r, col);
                     easy2(r, col);
                  }
               }
            sb.append(moves.size()/6+"\n");
            for(int i=0; i < moves.size(); i+=6)
            {
               for(int a=i; a < i+6; a++)
                  sb.append(moves.get(a)+" ");
               sb.append("\n");
            }
         }
         System.out.print(sb);
      }
      static ArrayList<Integer> moves;
      public static void single1(int r, int c)
      {
         /*
         10
         00
         */
         add(r, c);
         add(r+1, c);
         add(r, c+1);
         diag2(r, c);
      }
      public static void single2(int r, int c)
      {
         /*
         01
         00
         */
         add(r, c);
         add(r, c+1);
         add(r+1, c+1);
         diag1(r, c);
      }
      public static void single3(int r, int c)
      {
         /*
         00
         10
         */
         add(r, c);
         add(r+1, c);
         add(r+1, c+1);
         diag1(r, c);
      }
      public static void single4(int r, int c)
      {
         /*
         00
         01
         */
         add(r+1, c);
         add(r+1, c+1);
         add(r, c+1);
         diag2(r, c);
      }
      public static void diag1(int r, int c)
      {
         /*
         10
         01
         */
         add(r, c+1);
         add(r+1, c);
         add(r+1, c+1);
         easy1(r, c);
      }
      public static void diag2(int r, int c)
      {
         /*
         01
         10
         */
         add(r, c);
         add(r+1, c);
         add(r+1, c+1);
         easy2(r, c);
      }
      public static void easy1(int r, int c)
      {
         /*
         11
         10
         */
         add(r, c);
         add(r+1, c);
         add(r, c+1);
      }
      public static void easy2(int r, int c)
      {
         /*
         11
         01
         */
         add(r, c);
         add(r, c+1);
         add(r+1, c+1);
      }
      public static void easy3(int r, int c)
      {
         /*
         10
         11
         */
         add(r, c);
         add(r+1, c);
         add(r+1, c+1);
      }
      public static void easy4(int r, int c)
      {
         /*
         01
         11
         */
         add(r, c+1);
         add(r+1, c);
         add(r+1, c+1);
      }
      public static void add(int r, int c)
      {
         moves.add(r);
         moves.add(c);
      }
   }
  class FastScanner {
        private int BS = 1 << 16;
        private char NC = (char) 0;
        private byte[] buf = new byte[BS];
        private int bId = 0, size = 0;
        private char c = NC;
        private double cnt = 1;
        private BufferedInputStream in;
 
        public FastScanner() {
            in = new BufferedInputStream(System.in, BS);
        }
 
        public FastScanner(String s) {
            try {
                in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
            } catch (Exception e) {
                in = new BufferedInputStream(System.in, BS);
            }
        }
 
        private char getChar() {
            while (bId == size) {
                try {
                    size = in.read(buf);
                } catch (Exception e) {
                    return NC;
                }
                if (size == -1) return NC;
                bId = 0;
            }
            return (char) buf[bId++];
        }
 
        public int nextInt() {
            return (int) nextLong();
        }
 
        public int[] nextInts(int N) {
            int[] res = new int[N];
            for (int i = 0; i < N; i++) {
                res[i] = (int) nextLong();
            }
            return res;
        }
 
        public long[] nextLongs(int N) {
            long[] res = new long[N];
            for (int i = 0; i < N; i++) {
                res[i] = nextLong();
            }
            return res;
        }
 
        public long nextLong() {
            cnt = 1;
            boolean neg = false;
            if (c == NC) c = getChar();
            for (; (c < '0' || c > '9'); c = getChar()) {
                if (c == '-') neg = true;
            }
            long res = 0;
            for (; c >= '0' && c <= '9'; c = getChar()) {
                res = (res << 3) + (res << 1) + c - '0';
                cnt *= 10;
            }
            return neg ? -res : res;
        }
 
        public double nextDouble() {
            double cur = nextLong();
            return c != '.' ? cur : cur + nextLong() / cnt;
        }
 
        public double[] nextDoubles(int N) {
            double[] res = new double[N];
            for (int i = 0; i < N; i++) {
                res[i] = nextDouble();
            }
            return res;
        }
 
        public String next() {
            StringBuilder res = new StringBuilder();
            while (c <= 32) c = getChar();
            while (c > 32) {
                res.append(c);
                c = getChar();
            }
            return res.toString();
        }
 
        public String nextLine() {
            StringBuilder res = new StringBuilder();
            while (c <= 32) c = getChar();
            while (c != '\n') {
                res.append(c);
                c = getChar();
            }
            return res.toString();
        }
 
        public boolean hasNext() {
            if (c > 32) return true;
            while (true) {
                c = getChar();
                if (c == NC) return false;
                else if (c > 32) return true;
            }
        }
    }