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.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

public class Solution1440C2 {
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    PrintWriter out = new PrintWriter(System.out);
    Solver1440C2 solver = new Solver1440C2();
    int n = in.nextInt();
    for (int i = 0; i < n; i++) {
      solver.solve(i, in, out);
    }
    out.close();
  }

  static class Solver1440C2 {
    public static final int[][][][] oneseq = new int[2][2][3][6];

    public static final int[][][][][][] twoseq = new int[2][2][2][2][2][6];

    public static final int[][] fourseq = new int[][]{
        {0, 1, 1, 0, 1, 1}, {0, 0, 0, 1, 1, 0}, {0, 0, 1, 0, 1, 1}, {0, 0, 0, 1, 1, 1}
    };

    private void genOneSeq(int[][][][] oneseq) {
      Set<Integer> set = new HashSet<>();

      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          if (i == 0 && j == 0) {
            oneseq[i][j] = new int[][]{
                {0, 0, 0, 1, 1, 0}, {0, 1, 0, 0, 1, 1}, {0, 0, 1, 0, 1, 1}
            };
          } else if (i == 0 && j == 1) {
            oneseq[i][j] = new int[][]{
                {0, 1, 0, 0, 1, 0}, {0, 0, 0, 1, 1, 1}, {0, 1, 1, 0, 1, 1}
            };
          } else if (i == 1 && j == 0) {
            oneseq[i][j] = new int[][]{
                {0, 0, 0, 1, 1, 0}, {0, 1, 1, 0, 1, 1}, {0, 0, 1, 0, 1, 1}
            };
          } else if (i == 1 && j == 1) {
            oneseq[i][j] = new int[][]{
                {1, 1, 0, 1, 1, 0}, {0, 1, 0, 0, 1, 1}, {0, 0, 1, 0, 1, 1}
            };
          }
        /*
          set.add(i * 2 + j);
          for (int k = 0; k < 3; k++) {
            if (k < 2) {
              int tmp = 0;
              for (Integer item : set) {
                tmp = item;
                break;
              }
              oneseq[i][j][k][0] = tmp / 2;
              oneseq[i][j][k][1] = tmp % 2;
              int ind = 2;
              for (int p = 0; p < 2; p++) {
                for (int q = 0; q < 2; q++) {
                  int thistmp = p * 2 + q;
                  if (ind < 6 && !set.contains(thistmp)) {
                    oneseq[i][j][k][ind] = p;
                    oneseq[i][j][k][ind+1] = q;
                    set.add(thistmp);
                    ind += 2;
                  }
                }
              }
              set.remove(tmp);
            } else {
              int ind = 0;
              for (Integer item : set) {
                oneseq[i][j][k][ind] = item / 2;
                oneseq[i][j][k][ind+1] = item % 2;
                ind += 2;
              }
            }
          }

           */
        }
      }
    }

    private void genTwoSeq(int[][][][][][] twoseq) {
      Set<Integer> set = new HashSet<>();

      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          set.add(i * 2 + j);
          for (int x = 0; x < 2; x++) {
            for (int y = 0; y < 2; y++) {
              if (i == x && j == y) {
                continue;
              }

              if (i == 0 && j == 0 && x == 0 && y == 1) {
                twoseq[i][j][x][y] = new int[][]{
                    {0, 0, 1, 0, 1, 1}, {0, 1, 1, 0, 1, 1}
                };
              } else if (i == 0 && j == 0 && x == 1 && y == 0) {
                twoseq[i][j][x][y] = new int[][]{
                    {0, 0, 0, 1, 1, 1}, {1, 0, 0, 1, 1, 1}
                };
              } else if (i == 0 && j == 0 && x == 1 && y == 1) {
                twoseq[i][j][x][y] = new int[][]{
                    {0, 0, 1, 0, 0, 1}, {1, 1, 1, 0, 0, 1}
                };
              } else if (i == 0 && j == 1 && x == 1 && y == 0) {
                twoseq[i][j][x][y] = new int[][]{
                    {0, 1, 0, 0, 1, 1}, {1, 0, 0, 0, 1, 1}
                };
              } else if (i == 0 && j == 1 && x == 1 && y == 1) {
                twoseq[i][j][x][y] = new int[][]{
                    {0, 1, 1, 0, 0, 0}, {1, 1, 1, 0, 0, 0}
                };
              } else if (i == 1 && j == 0 && x == 1 && y == 1) {
                twoseq[i][j][x][y] = new int[][]{
                    {1, 0, 0, 0, 0, 1}, {1, 1, 0, 0, 0, 1}
                };
              }

              /*
              set.add(i * 2 + j);
              set.add(x * 2 + y);
              for (int k = 0; k < 2; k++) {
                if (k < 1) {
                  int tmp = 0;
                  for (Integer item : set) {
                    tmp = item;
                    break;
                  }
                  twoseq[i][j][x][y][k][0] = tmp / 2;
                  twoseq[i][j][x][y][k][1] = tmp % 2;
                  int ind = 2;
                  for (int p = 0; p < 2; p++) {
                    for (int q = 0; q < 2; q++) {
                      int thistmp = p * 2 + q;
                      if (ind < 6 && !set.contains(thistmp)) {
                        twoseq[i][j][x][y][k][ind] = p;
                        twoseq[i][j][x][y][k][ind+1] = q;
                        set.add(thistmp);
                        ind += 2;
                      }
                    }
                  }
                  set.remove(tmp);
                } else {
                  int ind = 0;
                  for (Integer item : set) {
                    twoseq[i][j][x][y][k][ind] = item / 2;
                    twoseq[i][j][x][y][k][ind+1] = item % 2;
                    ind += 2;
                  }
                }
              }

               */
            }
          }
        }
      }
    }

    public void solve(int testNumber, InputReader in, PrintWriter out) {
      int n = in.nextInt(), m = in.nextInt();
      char[][] b = new char[n][m];
      for (int i = 0; i < n; i++) {
        String s = in.next();
        b[i] = s.toCharArray();
      }

      genOneSeq(oneseq);
      genTwoSeq(twoseq);

      /*
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          out.println("loc: " + i + " " + j);
          for (int k = 0; k < 3; k++) {
            for (int l = 0; l < 6; l++) {
              out.print(" " + oneseq[i][j][k][l]);
            }
            out.println();
          }
        }
      }

      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          for (int x = 0; x < 2; x++) {
            for (int y = 0; y < 2; y++) {
              if (i == x && j == y) {
                continue;
              }
              out.println("loc: " + i + " " + j + " " + x + " " + y);
              for (int k = 0; k < 2; k++) {
                for (int l = 0; l < 6; l++) {
                  out.print(" " + twoseq[i][j][x][y][k][l]);
                }
                out.println();
              }
            }
          }
        }
      }
      */
      List<List<Integer>> res = new ArrayList<>();

      for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < m - 1; j++) {
          /*
          for (int x = 0; x < n; x++) {
            for (int y = 0; y < m; y++) {
              out.print(" " + b[x][y]);
            }
            out.println();
          }

           */
          List<Integer> tmp = new ArrayList<>();
          if (i != n - 2) {
            //out.println(i + " " + j);
            if (b[i][j] == '0' && b[i][j + 1] == '0') {
              continue;
            }
            //out.println(i + " " + j + "com");
            if (b[i][j] == '1') {
              tmp.add(i+1);
              tmp.add(j+1);
              b[i][j] = '0';
            }
            if (b[i][j+1] == '1') {
              tmp.add(i+1);
              tmp.add(j+2);
              b[i][j+1] = '0';
            }
            if (tmp.size() == 2) {
              tmp.add(i + 2);
              tmp.add(j + 1);
              tmp.add(i + 2);
              tmp.add(j + 2);
              b[i+1][j] = (b[i+1][j] == '1') ? '0' : '1';
              b[i+1][j+1] = (b[i+1][j+1] == '1') ? '0' : '1';
            } else {
              if (b[i + 1][j] == '1') {
                tmp.add(i + 2);
                tmp.add(j + 1);
                b[i+1][j] = '0';
              }
              if (tmp.size() == 4 && b[i + 1][j+1] == '1') {
                tmp.add(i + 2);
                tmp.add(j + 2);
                b[i+1][j+1] = '0';
              }
              if (tmp.size() == 4) {
                tmp.add(i + 2);
                tmp.add(j + 2);
                b[i+1][j+1] = (b[i+1][j+1] == '1') ? '0' : '1';
              }
            }
            res.add(tmp);
          } else if (j != m - 2) {
            //out.println(i + " " + j);
            if (b[i][j] == '0' && b[i+1][j] == '0') {
              continue;
            }
            //out.println(i + " " + j + " xx");
            if (b[i][j] == '1') {
              tmp.add(i+1);
              tmp.add(j+1);
              b[i][j] = '0';
            }
            if (b[i+1][j] == '1') {
              tmp.add(i+2);
              tmp.add(j+1);
              b[i+1][j] = '0';
            }
            if (tmp.size() == 2) {
              tmp.add(i + 1);
              tmp.add(j + 2);
              tmp.add(i + 2);
              tmp.add(j + 2);
              b[i][j+1] = (b[i][j+1] == '1') ? '0' : '1';
              b[i+1][j+1] = (b[i+1][j+1] == '1') ? '0' : '1';
            } else {
              if (b[i][j+1] == '1') {
                tmp.add(i + 1);
                tmp.add(j + 2);
                b[i][j+1] = '0';
              }
              if (tmp.size() == 4 && b[i + 1][j+1] == '1') {
                tmp.add(i + 2);
                tmp.add(j + 2);
                b[i+1][j+1] = '0';
              }
              if (tmp.size() == 4) {
                tmp.add(i + 2);
                tmp.add(j + 2);
                b[i+1][j+1] = (b[i+1][j+1] == '1') ? '0' : '1';
              }
            }
            res.add(tmp);
          } else {
            //out.println(i + " " + j + "last");

            tmp.clear();
            for (int k = 0; k < 2; k++) {
              for (int l = 0; l < 2; l++) {
                if (b[i+k][j+l] == '1') {
                  tmp.add(i+k+1);
                  tmp.add(j+l+1);
                }
              }
            }
            if (tmp.size() == 6) {
              res.add(tmp);
              for (int k = 0; k < 2; k++) {
                for (int l = 0; l < 2; l++) {
                  b[i+k][j+l] = '0';
                }
              }
            } else {
              res.addAll(genSeq(i, j, b));
            }
          }

          /*
          for (int k = 0; k < 2; k++) {
            for (int l = 0; l < 2; l++) {
              if (b[i+k][j+l] == '1') {
                tmp.add(i+k+1);
                tmp.add(j+l+1);
              }
            }
          }
          if (tmp.size() == 6) {
            res.add(tmp);
            for (int k = 0; k < 2; k++) {
              for (int l = 0; l < 2; l++) {
                b[i+k][j+l] = '0';
              }
            }
          }
          */
          /*
          for (int x = 0; x < n; x++) {
            for (int y = 0; y < m; y++) {
              out.print(" " + b[x][y]);
            }
            out.println();
          }*/
        }
      }

      out.println(res.size());
      for (List<Integer> item : res) {
        for (int i = 0; i < 6; i++) {
          if (i > 0) {
            out.print(" ");
          }
          out.print(item.get(i));
        }
        out.println();
      }
    }

    private List<List<Integer>> genSeq(int x, int y, char[][] b) {
      List<List<Integer>> res = new ArrayList<>();
      int cnt = 0;
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          if (b[x+i][y+j] == '1') {
            cnt++;
          }
        }
      }

      if (cnt == 1) {
        int x1 = -1, y1 = -1;
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 2; j++) {
            if (b[x+i][y+j] == '1') {
              x1 = i;
              y1 = j;
            }
          }
        }

        for (int[] item : oneseq[x1][y1]) {
          List<Integer> list = new ArrayList<>();
          for (int i = 0; i < 6; i++) {
            if (i % 2 == 0) {
              list.add(x + item[i] + 1);
            } else {
              list.add(y + item[i] + 1);
            }
          }
          res.add(list);
        }
      } else if (cnt == 2) {
        int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 2; j++) {
            if (b[x+i][y+j] == '1') {
              if (x1 < 0) {
                x1 = i;
                y1 = j;
              } else {
                x2 = i;
                y2 = j;
              }
            }
          }
        }
        for (int[] item : twoseq[x1][y1][x2][y2]) {
          List<Integer> list = new ArrayList<>();
          for (int i = 0; i < 6; i++) {
            if (i % 2 == 0) {
              list.add(x + item[i] + 1);
            } else {
              list.add(y + item[i] + 1);
            }
          }
          res.add(list);
        }
      } else if (cnt == 4) {
        for (int[] item : fourseq) {
          List<Integer> list = new ArrayList<>();
          for (int i = 0; i < 6; i++) {
            if (i % 2 == 0) {
              list.add(x + item[i] + 1);
            } else {
              list.add(y + item[i] + 1);
            }
          }
          res.add(list);
        }
      }

      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          b[x+i][y+j] = '0';
        }
      }
      return res;
    }
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream), 32768);
      tokenizer = null;
    }

    public String next() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }
  }
}