This is the easy 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 3nm 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 ≤ 3nm) — 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 into 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

Source: codeforces | Difficulty: MEDIUM
Tags: Implementation

Constructive algorithms


Solutions:
import sys
from itertools import permutations
from itertools import combinations
from itertools import combinations_with_replacement

def makeThree(mat, i, j, liAns):
	liToAdd = []
	if mat[i][j] == 1:
		liToAdd.append(i)
		liToAdd.append(j)
		mat[i][j] = 0
	if mat[i + 1][j] == 1:
		liToAdd.append(i + 1)
		liToAdd.append(j)
		mat[i + 1][j] = 0
	if mat[i][j + 1] == 1:
		liToAdd.append(i)
		liToAdd.append(j + 1)
		mat[i][j + 1] = 0
	if mat[i + 1][j + 1] == 1:
		liToAdd.append(i + 1)
		liToAdd.append(j + 1)
		mat[i + 1][j + 1] = 0
	liAns.append(liToAdd)

def makeTwo(mat, i, j, liAns):
	liToAdd = []
	liAllCoord = [(i, j), (i + 1, j), (i, j + 1), (i + 1, j + 1)]
	flag = 0
	for coord in liAllCoord:
		if mat[coord[0]][coord[1]] == 1 and flag == 0:
			flag = 1
			liToAdd.append(coord[0])
			liToAdd.append(coord[1])
		if mat[coord[0]][coord[1]] == 0:
			liToAdd.append(coord[0])
			liToAdd.append(coord[1])
	a = 0
	while a < 6:
		initial = mat[liToAdd[a]][liToAdd[a + 1]]
		if initial == 1:
			mat[liToAdd[a]][liToAdd[a + 1]] = 0
		else:
			mat[liToAdd[a]][liToAdd[a + 1]] = 1
		a += 2
	liAns.append(liToAdd)
	makeThree(mat, i, j, liAns)

def makeOne(mat, i, j, liAns):
	liToAdd = []
	liAllCoord = [(i, j), (i + 1, j), (i, j + 1), (i + 1, j + 1)]
	ctr = 0
	for coord in liAllCoord:
		if mat[coord[0]][coord[1]] == 1:
			liToAdd.append(coord[0])
			liToAdd.append(coord[1])
		if mat[coord[0]][coord[1]] == 0 and ctr < 2:
			ctr += 1
			liToAdd.append(coord[0])
			liToAdd.append(coord[1])
	a = 0
	while a < 6:
		initial = mat[liToAdd[a]][liToAdd[a + 1]]
		if initial == 1:
			mat[liToAdd[a]][liToAdd[a + 1]] = 0
		else:
			mat[liToAdd[a]][liToAdd[a + 1]] = 1
		a += 2
	liAns.append(liToAdd)
	makeTwo(mat, i, j, liAns)

def makeFour(mat, i, j, liAns):
	liAllCoord = [(i, j), (i + 1, j), (i, j + 1), (i + 1, j + 1)]
	ctr = 0
	liToAdd = []
	for coord in liAllCoord:
		if ctr >= 3:
			break
		ctr += 1
		liToAdd.append(coord[0])
		liToAdd.append(coord[1])
		mat[coord[0]][coord[1]] = 0
	liAns.append(liToAdd)
	makeOne(mat, i, j, liAns)
for _ in range(int(input())):
	liStr = input().split()
	p = int(liStr[0])
	q = int(liStr[1])
	mat = []
	for _ in range(0, p):
		liStr = list(input())
		li = []
		for i in liStr:
			li.append(int(i))
		mat.append(li)
	liAns = []
	i = 0
	while i < p:
		j = 0
		while j < q:
			if i == p - 1:
				i = i - 1
			if j == q - 1:
				j = j - 1
			ctr = 0
			if mat[i][j] == 1:
				ctr += 1
			if mat[i + 1][j] == 1:
				ctr += 1
			if mat[i][j + 1] == 1:
				ctr += 1
			if mat[i + 1][j + 1] == 1:
				ctr += 1
			if ctr == 1:
				makeOne(mat, i, j, liAns)
			if ctr == 2:
				makeTwo(mat, i, j, liAns)
			if ctr == 3:
				makeThree(mat, i, j, liAns)
			if ctr == 4:
				makeFour(mat, i, j, liAns)
			j += 2
		i += 2
	print(len(liAns))
	for sixNum in liAns:
		for a in sixNum:
			print(a + 1, end=' ')
		print()


import os
import sys
from io import BytesIO, IOBase
import threading
from bisect import bisect_right
from math import gcd, log
from collections import Counter, defaultdict
from pprint import pprint
import copy

def main(case_no):
	(n, m) = map(int, input().split())
	grid = []
	for i in range(n):
		grid.append([int(c) for c in input()])
	ans = []
	llt = copy.deepcopy(grid)
	for i in range(n - 2):
		for j in range(m):
			if grid[i][j] == 1:
				grid[i][j] = (grid[i][j] + 1) % 2
				grid[i + 1][j] = (grid[i + 1][j] + 1) % 2
				if j + 1 < m:
					grid[i][j + 1] = (grid[i][j + 1] + 1) % 2
					ans.append((i, j, i + 1, j, i, j + 1))
				else:
					grid[i + 1][j - 1] = (grid[i + 1][j - 1] + 1) % 2
					ans.append((i, j, i + 1, j, i + 1, j - 1))
	for i in range(m - 2):
		if grid[-2][i] == 1:
			grid[n - 2][i + 1] = (grid[n - 2][i + 1] + 1) % 2
			grid[n - 1][i + 1] = (grid[n - 1][i + 1] + 1) % 2
			grid[n - 2][i] = (grid[n - 2][i] + 1) % 2
			ans.append((n - 2, i + 1, n - 1, i + 1, n - 2, i))
		if grid[-1][i] == 1:
			grid[n - 2][i + 1] = (grid[n - 2][i + 1] + 1) % 2
			grid[n - 1][i + 1] = (grid[n - 1][i + 1] + 1) % 2
			grid[n - 1][i] = (grid[n - 1][i] + 1) % 2
			ans.append((n - 2, i + 1, n - 1, i + 1, n - 1, i))
	cc = [(n - 1, m - 1), (n - 1, m - 2), (n - 2, m - 1), (n - 2, m - 2)]
	ct = 0
	for (x, y) in cc:
		if grid[x][y] == 1:
			ct += 1
	if ct == 4:
		a = [n - 1, m - 1, n - 1, m - 2, n - 2, m - 1]
		ans.append(tuple(a))
		grid[a[0]][a[1]] = (grid[a[0]][a[1]] + 1) % 2
		grid[a[2]][a[3]] = (grid[a[2]][a[3]] + 1) % 2
		grid[a[4]][a[5]] = (grid[a[4]][a[5]] + 1) % 2
		ct = 1
	if ct == 1:
		a = []
		for (x, y) in cc:
			if grid[x][y] == 0:
				a.append(x)
				a.append(y)
				if len(a) == 4:
					break
		for (x, y) in cc:
			if grid[x][y] == 1:
				a.append(x)
				a.append(y)
				break
		ans.append(tuple(a))
		grid[a[0]][a[1]] = (grid[a[0]][a[1]] + 1) % 2
		grid[a[2]][a[3]] = (grid[a[2]][a[3]] + 1) % 2
		grid[a[4]][a[5]] = (grid[a[4]][a[5]] + 1) % 2
	if ct == 1 or ct == 2:
		a = []
		for (x, y) in cc:
			if grid[x][y] == 0:
				a.append(x)
				a.append(y)
		for (x, y) in cc:
			if grid[x][y] == 1:
				a.append(x)
				a.append(y)
				break
		grid[a[0]][a[1]] = (grid[a[0]][a[1]] + 1) % 2
		grid[a[2]][a[3]] = (grid[a[2]][a[3]] + 1) % 2
		grid[a[4]][a[5]] = (grid[a[4]][a[5]] + 1) % 2
		ans.append(a)
	if ct > 0:
		a = []
		for (x, y) in cc:
			if grid[x][y] == 1:
				a.append(x)
				a.append(y)
		ans.append(a)
		grid[a[0]][a[1]] = (grid[a[0]][a[1]] + 1) % 2
		grid[a[2]][a[3]] = (grid[a[2]][a[3]] + 1) % 2
		grid[a[4]][a[5]] = (grid[a[4]][a[5]] + 1) % 2
	for a in ans:
		llt[a[0]][a[1]] = (llt[a[0]][a[1]] + 1) % 2
		llt[a[2]][a[3]] = (llt[a[2]][a[3]] + 1) % 2
		llt[a[4]][a[5]] = (llt[a[4]][a[5]] + 1) % 2
	for i in range(n):
		for j in range(m):
			assert grid[i][j] == llt[i][j] == 0
	print(len(ans))
	assert len(ans) <= m * n
	for li in ans:
		print(*[i + 1 for i in li])
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	main(_ + 1)


from math import *
from bisect import *
from collections import *
from random import *
from decimal import *
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	(n, m) = ma()
	r = []
	for i in range(n):
		a = st()
		a = list(a)
		r.append(a)
	res = []
	for i in range(n - 1):
		for j in range(m - 1):
			ll = 0
			one = []
			zer = []
			if r[i][j] == '1':
				ll += 1
				one.append([i + 1, j + 1])
				r[i][j] = '0'
			else:
				zer.append([i + 1, j + 1])
			if r[i + 1][j] == '1':
				ll += 1
				one.append([i + 2, j + 1])
				r[i + 1][j] = '0'
			else:
				zer.append([i + 2, j + 1])
			if r[i][j + 1] == '1':
				ll += 1
				one.append([i + 1, j + 2])
				r[i][j + 1] = '0'
			else:
				zer.append([i + 1, j + 2])
			if r[i + 1][j + 1] == '1':
				ll += 1
				one.append([i + 2, j + 2])
				r[i + 1][j + 1] = '0'
			else:
				zer.append([i + 2, j + 2])
			if ll == 0:
				continue
			if ll == 3:
				res.append(one[0])
				res.append(one[1])
				res.append(one[2])
				continue
			if ll == 2:
				res.append(zer[0])
				res.append(zer[1])
				res.append(one[0])
				res.append(zer[0])
				res.append(zer[1])
				res.append(one[1])
				continue
			if ll == 1:
				res.append(zer[0])
				res.append(zer[1])
				res.append(one[0])
				res.append(zer[2])
				res.append(zer[0])
				res.append(one[0])
				res.append(zer[2])
				res.append(zer[1])
				res.append(one[0])
				continue
			if ll == 4:
				res.append(one[0])
				res.append(one[1])
				res.append(one[2])
				res.append(one[0])
				res.append(one[1])
				res.append(one[3])
				res.append(one[2])
				res.append(one[3])
				res.append(one[1])
				res.append(one[0])
				res.append(one[2])
				res.append(one[3])
				continue
	print(len(res) // 3)
	i = 0
	while i < len(res):
		print(*res[i], *res[i + 1], *res[i + 2])
		i += 3


from copy import deepcopy

def op(x1, y1, x2, y2, x3, y3, ans, a):
	ans.append((x1 + 1, y1 + 1, x2 + 1, y2 + 1, x3 + 1, y3 + 1))
	a[x1][y1] = 1 - a[x1][y1]
	a[x2][y2] = 1 - a[x2][y2]
	a[x3][y3] = 1 - a[x3][y3]

def perf(i, j, a, ans):
	mat = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
	if mat == [1, 1, 1, 1]:
		op(i, j, i + 1, j, i, j + 1, ans, a)
	mat = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
	if mat == [0, 0, 0, 1]:
		op(i, j + 1, i + 1, j, i + 1, j + 1, ans, a)
	elif mat == [0, 0, 1, 0]:
		op(i, j, i + 1, j, i + 1, j + 1, ans, a)
	elif mat == [0, 1, 0, 0]:
		op(i, j, i, j + 1, i + 1, j + 1, ans, a)
	elif mat == [1, 0, 0, 0]:
		op(i, j, i + 1, j, i, j + 1, ans, a)
	mat = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
	if mat == [1, 0, 0, 1]:
		op(i, j, i + 1, j, i, j + 1, ans, a)
	elif mat == [0, 1, 1, 0]:
		op(i, j, i + 1, j, i + 1, j + 1, ans, a)
	mat = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
	if mat == [1, 1, 0, 0]:
		op(i + 1, j, i + 1, j + 1, i, j + 1, ans, a)
	elif mat == [1, 0, 1, 0]:
		op(i + 1, j, i + 1, j + 1, i, j + 1, ans, a)
	elif mat == [0, 1, 0, 1]:
		op(i, j, i + 1, j, i + 1, j + 1, ans, a)
	elif mat == [0, 0, 1, 1]:
		op(i, j, i, j + 1, i + 1, j + 1, ans, a)
	mat = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
	if mat == [1, 1, 1, 0]:
		op(i, j, i, j + 1, i + 1, j, ans, a)
	elif mat == [1, 1, 0, 1]:
		op(i, j, i, j + 1, i + 1, j + 1, ans, a)
	elif mat == [1, 0, 1, 1]:
		op(i, j, i + 1, j, i + 1, j + 1, ans, a)
	elif mat == [0, 1, 1, 1]:
		op(i, j + 1, i + 1, j, i + 1, j + 1, ans, a)

def side1(a):
	ans = []
	for i in range(0, n - 1, 2):
		for j in range(0, m - 1, 2):
			perf(i, j, a, ans)
	for i in range(n - 2, -1, -2):
		for j in range(m - 2, -1, -2):
			perf(i, j, a, ans)
	for i in range(0, n - 1, 2):
		for j in range(m - 2, -1, -2):
			perf(i, j, a, ans)
	for i in range(n - 2, -1, -2):
		for j in range(0, m - 1, 2):
			perf(i, j, a, ans)
	return ans
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = [list(map(int, input())) for _ in range(n)]
	ans = side1(deepcopy(a))
	print(len(ans))
	for i in ans:
		print(*i)


def flip(x, y, n, m):
	a = [x + 1, y + 1]
	b = []
	c = []
	d = []
	if y < m - 1:
		b = [a[0], a[1] + 1]
	else:
		b = [a[0], a[1] - 1]
	if x < n - 1:
		c = [a[0] + 1, a[1]]
		d = [b[0] + 1, b[1]]
	else:
		c = [a[0] - 1, a[1]]
		d = [b[0] - 1, b[1]]
	print(*a, *b, *c)
	print(*a, *b, *d)
	print(*a, *c, *d)

def main():
	for case in range(int(input())):
		(n, m) = [int(x) for x in input().split()]
		grid = []
		ct = 0
		for i in range(n):
			s = input()
			arr = [int(x) for x in s]
			ct += arr.count(1)
			grid.append(arr)
		print(ct * 3)
		for i in range(n):
			for j in range(m):
				if grid[i][j] == 0:
					continue
				flip(i, j, n, m)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def main():
	from sys import stdin
	from random import randint, choice, shuffle
	from functools import lru_cache
	input = stdin.readline
	dx = [0, 0, 1, 1]
	dy = [0, 1, 0, 1]

	def g(i, j, a):
		return (i + dx[a] + 1, j + dy[a] + 1)

	def f(i, j, a, b, c):
		return (g(i, j, a), g(i, j, b), g(i, j, c))
	for _ in range(int(input())):
		ans = []
		(n, m) = map(int, input().split())
		for i in range(n):
			s = input()
			for j in range(m):
				if s[j] == '0':
					continue
				if i != n - 1 and j != m - 1:
					ans.append(f(i, j, 0, 1, 3))
					ans.append(f(i, j, 0, 2, 3))
					ans.append(f(i, j, 0, 1, 2))
				elif i != n - 1:
					ans.append(f(i, j - 1, 0, 1, 2))
					ans.append(f(i, j - 1, 1, 2, 3))
					ans.append(f(i, j - 1, 0, 1, 3))
				elif j != m - 1:
					ans.append(f(i - 1, j, 0, 1, 2))
					ans.append(f(i - 1, j, 1, 2, 3))
					ans.append(f(i - 1, j, 0, 2, 3))
				else:
					ans.append(f(i - 1, j - 1, 0, 1, 3))
					ans.append(f(i - 1, j - 1, 0, 2, 3))
					ans.append(f(i - 1, j - 1, 1, 2, 3))
		ans.sort()
		p = []
		c = 0
		ans1 = []
		for i in ans:
			if i == p:
				c += 1
			else:
				if c & 1:
					ans1.append(p)
				c = 1
				p = i
		if c & 1:
			ans1.append(p)
		print(len(ans1))
		for i in ans1:
			for j in i:
				print(*j, end=' ')
			print()
main()


t = int(input())
for l in range(t):
	(n, m) = [int(x) for x in input().split(' ')]
	A = []
	B = []
	for j in range(n):
		A.append(input())
	ans = 0
	for i in range(n):
		for j in range(m):
			if A[i][j] == '1':
				ans = ans + 3
				if i + 1 < n and j + 1 < m:
					B.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					B.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					B.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
				elif i + 1 < n:
					B.append([i + 1, j + 1, i + 1, j, i + 2, j + 1])
					B.append([i + 2, j + 1, i + 1, j + 1, i + 2, j])
					B.append([i + 1, j, i + 1, j + 1, i + 2, j])
				elif j + 1 < m:
					B.append([i, j + 1, i + 1, j + 1, i + 1, j + 2])
					B.append([i, j + 1, i, j + 2, i + 1, j + 1])
					B.append([i + 1, j + 2, i + 1, j + 1, i, j + 2])
				else:
					B.append([i + 1, j + 1, i, j + 1, i, j])
					B.append([i + 1, j + 1, i + 1, j, i, j])
					B.append([i + 1, j + 1, i + 1, j, i, j + 1])
	print(ans)
	for w in range(ans):
		print(*B[w])


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[1]

def sort2(l):
	return sorted(l, key=getKey, reverse=True)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def ceil(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def main():
	I = lambda : list(map(int, input().split()))
	(A, B, C, D) = ([0, 0], [0, 1], [1, 0], [1, 1])

	def ch(a, b, c, d):
		(a, b, c, d) = (int(a), int(b), int(c), int(d))
		x = a + b + c + d
		zz = A * (not a) + B * (not b) + C * (not c) + D * (not d)
		oo = A * a + B * b + C * c + D * d
		ar = []
		if x == 1:
			ar.append(oo + zz[2:])
			ar.append(oo + zz[:2] + zz[4:])
			ar.append(oo + zz[:4])
		elif x == 2:
			ar.append(zz + oo[:2])
			ar.append(zz + oo[2:])
		elif x == 3:
			ar.append(A * a + B * b + C * c + D * d)
		elif x == 4:
			ar.append(oo[2:])
			zz = oo[2:]
			oo = oo[:2]
			ar.append(oo + zz[2:])
			ar.append(oo + zz[:2] + zz[4:])
			ar.append(oo + zz[:4])
		return ar
	for tc in range(int(input())):
		(n, m) = I()
		l = []
		for i in range(n):
			l.append(list(input().strip()))
		an = []
		for i in range(0, n - 1):
			for j in range(0, m - 1):
				x = ch(l[i][j], l[i][j + 1], l[i + 1][j], l[i + 1][j + 1])
				for pp in x:
					for k in range(6):
						pp[k] += 1 + i * (k % 2 == 0) + j * (k % 2)
				an += x
				(l[i][j], l[i][j + 1], l[i + 1][j], l[i + 1][j + 1]) = list('0000')
		print(len(an))
		for i in an:
			print(*i)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = []
	for i in range(n):
		b = input()
		c = []
		for j in b:
			c.append(int(j))
		a.append(c)
	ans = []
	for i in range(0, n, 2):
		if i == n - 1:
			i -= 1
		for j in range(0, m, 2):
			if j == m - 1:
				j -= 1
			arr = [[a[i][j], a[i][j + 1]], [a[i + 1][j], a[i + 1][j + 1]]]
			cnt = a[i][j] + a[i][j + 1] + a[i + 1][j] + a[i + 1][j + 1]
			if cnt == 4:
				ans.append([i, j, i + 1, j, i, j + 1])
				arr[0][0] = 0
				arr[0][1] = 0
				arr[1][0] = 0
				cnt -= 3
			if cnt == 1:
				cnt += 1
				check = False
				if arr[0][0] == 1:
					ans.append([i, j, i + 1, j, i, j + 1])
				elif arr[0][1] == 1:
					ans.append([i, j, i, j + 1, i + 1, j + 1])
				elif arr[1][0] == 1:
					ans.append([i, j, i + 1, j, i + 1, j + 1])
				else:
					ans.append([i + 1, j, i, j + 1, i + 1, j + 1])
				for k in range(2):
					for l in range(2):
						if arr[k][l] == 1:
							arr[(k + 1) % 2][l] = 1
							arr[k][(l + 1) % 2] = 1
							arr[k][l] = 0
							check = True
							break
					if check:
						break
			if cnt == 2:
				check = False
				ans1 = []
				for k in range(2):
					for l in range(2):
						if arr[k][l] == 0:
							arr[k][l] = 1
							ans1.append(i + k)
							ans1.append(j + l)
						elif not check:
							arr[k][l] = 0
							check = True
							ans1.append(i + k)
							ans1.append(j + l)
				ans.append(ans1)
				cnt += 1
			if cnt == 3:
				ans1 = []
				for k in range(2):
					for l in range(2):
						if arr[k][l] == 1:
							ans1.append(i + k)
							ans1.append(j + l)
				ans.append(ans1)
			a[i][j] = 0
			a[i][j + 1] = 0
			a[i + 1][j] = 0
			a[i + 1][j + 1] = 0
	print(len(ans))
	for i in ans:
		for j in i:
			print(j + 1, end=' ')
		print()


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
import math

def gift():
	for _ in range(t):
		(n, m) = list(map(int, input().split()))
		arys = []
		for i in range(n):
			tem = [int(x) for x in input()]
			arys.append(tem)
		ans = []
		for i in range(n - 1):
			for j in range(m - 1):
				x1 = arys[i][j]
				x2 = arys[i][j + 1]
				x3 = arys[i + 1][j]
				x4 = arys[i + 1][j + 1]
				if j != m - 2:
					if sum([x1, x3]) == 0:
						continue
					else:
						diu = []
						if x1 == 1:
							diu.append(i)
							diu.append(j)
						if x3 == 1:
							diu.append(i + 1)
							diu.append(j)
						if len(diu) == 2:
							diu.append(i)
							diu.append(j + 1)
							diu.append(i + 1)
							diu.append(j + 1)
						else:
							if x2 == 1:
								diu.append(i)
								diu.append(j + 1)
							if len(diu) == 4:
								diu.append(i + 1)
								diu.append(j + 1)
						ans.append(diu)
						for z in range(3):
							x = diu[z * 2]
							y = diu[z * 2 + 1]
							arys[x][y] = 1 - arys[x][y]
				elif sum([x1, x2, x3, x4]) == 4:
					ans.append([i, j + 1, i + 1, j, i + 1, j + 1])
					ans.append([i, j, i, j + 1, i + 1, j])
					ans.append([i, j, i, j + 1, i + 1, j + 1])
					ans.append([i, j, i + 1, j, i + 1, j + 1])
					arys[i][j] = 0
					arys[i][j + 1] = 0
					arys[i + 1][j] = 0
					arys[i + 1][j + 1] = 0
				elif sum([x1, x2, x3, x4]) == 0:
					continue
				else:
					while sum([x1, x2, x3, x4]) != 0:
						x1 = arys[i][j]
						x2 = arys[i][j + 1]
						x3 = arys[i + 1][j]
						x4 = arys[i + 1][j + 1]
						diu = []
						if sum([x1, x2, x3, x4]) == 1:
							if x1 == 1:
								diu.extend([i, j])
							elif x2 == 1:
								diu.extend([i, j + 1])
							elif x3 == 1:
								diu.extend([i + 1, j])
							else:
								diu.extend([i + 1, j + 1])
							if x1 == 0:
								diu.extend([i, j])
							if x2 == 0:
								diu.extend([i, j + 1])
							if len(diu) == 4:
								if x3 == 0:
									diu.extend([i + 1, j])
								else:
									diu.extend([i + 1, j + 1])
							elif len(diu) == 2:
								diu.extend([i + 1, j])
								diu.extend([i + 1, j + 1])
						elif sum([x1, x2, x3, x4]) == 2:
							if x1 == 0:
								diu.extend([i, j])
							if x2 == 0:
								diu.extend([i, j + 1])
							if x3 == 0:
								diu.extend([i + 1, j])
							if x4 == 0:
								diu.extend([i + 1, j + 1])
							if x1 == 1:
								diu.extend([i, j])
							elif x2 == 1:
								diu.extend([i, j + 1])
							elif x3 == 1:
								diu.extend([i + 1, j])
							else:
								diu.extend([i + 1, j + 1])
						elif sum([x1, x2, x3, x4]) == 3:
							if x1 == 1:
								diu.extend([i, j])
							if x2 == 1:
								diu.extend([i, j + 1])
							if x3 == 1:
								diu.extend([i + 1, j])
							if x4 == 1:
								diu.extend([i + 1, j + 1])
						if len(diu) > 6:
							print(sum([x1, x2, x3, x4]))
							print(diu, 'lat')
						if len(diu) != 0:
							ans.append(diu)
							for z in range(3):
								x = diu[z * 2]
								y = diu[z * 2 + 1]
								arys[x][y] = 1 - arys[x][y]
		yield len(ans)
		for ele in ans:
			yield ' '.join([str(x + 1) for x in ele])
t = int(input())
ans = gift()
print(*ans, sep='\n')


inp = lambda : map(int, input().split(' '))
(t,) = inp()
for _ in range(t):
	(n, m) = inp()
	count = 0
	res = []
	for i in range(n - 1):
		s = input()
		if s[0] == '1':
			count += 3
			res.append('{} {} {} {} {} {}'.format(i + 1, 1, i + 2, 1, i + 2, 2))
			res.append('{} {} {} {} {} {}'.format(i + 1, 2, i + 1, 1, i + 2, 1))
			res.append('{} {} {} {} {} {}'.format(i + 1, 2, i + 1, 1, i + 2, 2))
		for j in range(1, m):
			if s[j] == '1':
				count += 3
				res.append('{} {} {} {} {} {}'.format(i + 1, j + 1, i + 2, j + 1, i + 2, j))
				res.append('{} {} {} {} {} {}'.format(i + 1, j, i + 1, j + 1, i + 2, j + 1))
				res.append('{} {} {} {} {} {}'.format(i + 1, j + 1, i + 1, j, i + 2, j))
	s = input()
	if s[0] == '1':
		count += 3
		res.append('{} {} {} {} {} {}'.format(n, 1, n - 1, 1, n - 1, 2))
		res.append('{} {} {} {} {} {}'.format(n, 1, n - 1, 1, n, 2))
		res.append('{} {} {} {} {} {}'.format(n, 1, n - 1, 2, n, 2))
	for j in range(1, m):
		if s[j] == '1':
			count += 3
			res.append('{} {} {} {} {} {}'.format(n, j + 1, n - 1, j + 1, n - 1, j))
			res.append('{} {} {} {} {} {}'.format(n, j + 1, n, j, n - 1, j + 1))
			res.append('{} {} {} {} {} {}'.format(n, j + 1, n, j, n - 1, j))
	print(count)
	for i in res:
		print(i)


import sys
from collections import deque
from copy import *
from bisect import *
from math import gcd, ceil, sqrt
from itertools import permutations as prm, product

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]

def string(s):
	return ''.join(s)

def fori(n):
	return [fi() for i in range(n)]

def inc(d, c, x=1):
	d[c] = d[c] + x if c in d else x

def bo(i):
	return ord(i) - ord('A')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def gtc(tc, ans):
	print('Case #' + str(tc) + ':', ans)

def cil(n, m):
	return n // m + int(n % m > 0)

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j, n, m):
	return 0 <= i < n and 0 <= j < m

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()

def foh(b, p):
	v = []
	u = []
	for (i, j) in b:
		u.append(a[i][j])
	if p == 1:
		for j in range(4):
			if u[j] == '1':
				a[b[j][0]][b[j][1]] = '0'
				v.append(b[j][0] + 1)
				v.append(b[j][1] + 1)
		ans.append(v)
	elif p == 2:
		f = 0
		for j in range(4):
			if u[j] == '0':
				a[b[j][0]][b[j][1]] = '1'
				v.append(b[j][0] + 1)
				v.append(b[j][1] + 1)
			elif f == 0:
				a[b[j][0]][b[j][1]] = '0'
				v.append(b[j][0] + 1)
				v.append(b[j][1] + 1)
				f += 1
		ans.append(v)
		foh(b, 1)
	elif p == 3:
		f = 0
		for j in range(4):
			if u[j] == '0':
				if f < 2:
					a[b[j][0]][b[j][1]] = '1'
					v.append(b[j][0] + 1)
					v.append(b[j][1] + 1)
					f += 1
			else:
				a[b[j][0]][b[j][1]] = '0'
				v.append(b[j][0] + 1)
				v.append(b[j][1] + 1)
		ans.append(v)
		foh(b, 2)
	elif p == 0:
		for j in range(3):
			if u[j] == '1':
				a[b[j][0]][b[j][1]] = '0'
				v.append(b[j][0] + 1)
				v.append(b[j][1] + 1)
		ans.append(v)
		foh(b, 3)
while t > 0:
	t -= 1
	(n, m) = mi()
	a = []
	ans = []
	for i in range(n):
		a.append(si())
	for i in range(n - 1):
		for j in range(m - 1):
			(p, q, r, s) = (a[i][j], a[i + 1][j], a[i + 1][j + 1], a[i][j + 1])
			u = [p, q, r, s]
			b = [(i, j), (i + 1, j), (i + 1, j + 1), (i, j + 1)]
			y = u.count('0')
			foh(b, y)
	print(len(ans))
	for i in ans:
		print(*i)


from sys import stdin, stdout
from math import gcd, sqrt, factorial
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 0 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7

def op(x, flg):
	cnt = 3 - flg
	ans = []
	for (i, j) in x:
		if flg and a[i][j]:
			a[i][j] = 0
			ans.extend([i + 1, j + 1])
			flg -= 1
		elif cnt and (not a[i][j]):
			a[i][j] = 1
			ans.extend([i + 1, j + 1])
			cnt -= 1
	return ans
for i in range(I()):
	(n, m) = R()
	a = [list(map(int, S())) for i in range(n)]
	ans = []
	for i in range(n - 1):
		for j in range(m - 1):
			x = [(i, j), (i + 1, j), (i, j + 1), (i + 1, j + 1)]
			cnt = sum([a[p][q] for (p, q) in x])
			if cnt == 0:
				continue
			if cnt == 4:
				ans += (op(x, 4),)
				cnt = 1
			if cnt == 1:
				ans += (op(x, 1),)
				cnt = 2
			if cnt == 2:
				ans += (op(x, 1),)
				cnt = 3
			if cnt == 3:
				ans += (op(x, 3),)
	print(len(ans))
	for i in ans:
		print(*i)


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, Counter, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 998244353
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('0')
file = 1

def solve():
	for _ in range(ii()):
		(n, m) = mi()
		a = []
		for i in range(n):
			s = si()
			p = []
			for j in s:
				p.append(int(j))
			a.append(p)

		def change(p):
			for i in range(0, 5, 2):
				a[p[i]][p[i + 1]] ^= 1
		ans = []
		for i in range(n - 1):
			for j in range(m):
				if a[i][j] == 0:
					if j == 0 or a[i][j - 1] == 0:
						continue
					else:
						ans.append([i, j - 1, i + 1, j - 1, i + 1, j])
						change(ans[-1])
				else:
					if j > 0 and a[i][j - 1] == 1:
						if a[i + 1][j - 1] == 1:
							ans.append([i, j, i + 1, j - 1, i, j - 1])
						else:
							ans.append([i, j, i, j - 1, i + 1, j])
					elif j < m - 1 and (a[i][j + 1] or a[i + 1][j + 1]):
						if a[i][j + 1] == 1:
							if a[i + 1][j] == 1:
								ans.append([i, j, i + 1, j, i, j + 1])
							else:
								ans.append([i, j, i + 1, j + 1, i, j + 1])
						else:
							ans.append([i, j, i + 1, j, i + 1, j + 1])
					elif j > 0:
						ans.append([i, j, i + 1, j - 1, i + 1, j])
					else:
						ans.append([i, j, i + 1, j + 1, i + 1, j])
					change(ans[-1])
		if a[n - 1][0] == 1:
			ans.append([n - 1, 0, n - 2, 0, n - 2, 1])
			change(ans[-1])
		for j in range(1, m):
			if a[n - 2][j - 1] == 1:
				ans.append([n - 2, j - 1, n - 2, j, n - 1, j])
				change(ans[-1])
				if a[n - 1][j] == 0:
					continue
				elif j < m - 1:
					ans.append([n - 2, j, n - 1, j, n - 2, j + 1])
				else:
					ans.append([n - 2, j, n - 1, j, n - 2, j - 1])
				change(ans[-1])
			elif a[n - 1][j]:
				if j < m - 1:
					ans.append([n - 2, j, n - 1, j, n - 2, j + 1])
					change(ans[-1])
		if a[n - 2][m - 1] == 1:
			ans.append([n - 2, m - 1, n - 2, m - 2, n - 1, m - 1])
			a[n - 1][m - 1] = 1
		if a[n - 1][m - 1] == 1:
			ans.append([n - 2, m - 1, n - 1, m - 1, n - 1, m - 2])
			ans.append([n - 2, m - 2, n - 2, m - 1, n - 1, m - 1])
			ans.append([n - 1, m - 1, n - 1, m - 2, n - 2, m - 2])
		print(len(ans))
		for i in ans:
			for j in i:
				print(j + 1, end=' ')
			print()
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


t = int(input())
for a in range(t):
	operations = []
	(n, m) = map(int, input().split())
	grid = [list(map(int, list(input()))) for _ in range(n)]
	for y in range(n):
		for x in range(m):
			if grid[y][x]:
				if y == n - 1 and x == 0:
					operations.append((x, y, x + 1, y, x, y - 1))
					operations.append((x, y, x + 1, y, x + 1, y - 1))
					operations.append((x, y, x + 1, y - 1, x, y - 1))
				elif y == 0 and x == m - 1:
					operations.append((x, y, x - 1, y, x, y + 1))
					operations.append((x, y, x - 1, y, x - 1, y + 1))
					operations.append((x, y, x - 1, y + 1, x, y + 1))
				elif y < n - 1 and x < m - 1:
					operations.append((x, y, x + 1, y, x, y + 1))
					operations.append((x, y, x + 1, y, x + 1, y + 1))
					operations.append((x, y, x + 1, y + 1, x, y + 1))
				else:
					operations.append((x, y, x - 1, y, x, y - 1))
					operations.append((x, y, x - 1, y, x - 1, y - 1))
					operations.append((x, y, x - 1, y - 1, x, y - 1))
	print(len(operations))
	for t in operations:
		print(*[u + 1 for u in t[::-1]])


res = []

def move(table, tup):
	(a, b, c, d, e, f) = tup
	res.append((a, b, c, d, e, f))
	table[a][b] = 1 - table[a][b]
	table[c][d] = 1 - table[c][d]
	table[e][f] = 1 - table[e][f]

def solve22(n1, m1, n, m, table):
	cur = [table[n1][m1], table[n1][m1 + 1], table[n1 + 1][m1], table[n1 + 1][m1 + 1]]
	if cur == [0, 0, 0, 0]:
		return
	cnt1 = 0
	for c in cur:
		if c == 1:
			cnt1 += 1
	l = []
	if cnt1 == 3:
		for n2 in range(n1, n):
			for m2 in range(m1, m):
				if table[n2][m2] == 1:
					l += [n2, m2]
		move(table, tuple(l))
	else:
		for n2 in range(n1, n):
			for m2 in range(m1, m):
				if table[n2][m2] == 1:
					l += [n2, m2]
					if n2 - 1 >= n1:
						l += [n2 - 1, m2]
					if n2 + 1 < n:
						l += [n2 + 1, m2]
					if m2 - 1 >= m1:
						l += [n2, m2 - 1]
					if m2 + 1 < m:
						l += [n2, m2 + 1]
					move(table, tuple(l))
					solve22(n1, m1, n, m, table)
					return

def solve2m(n1, m1, n, m, table):
	if m - m1 == 2:
		solve22(n1, m1, n, m, table)
		return
	if table[n1][m1] == 1 and table[n1 + 1][m1] == 1:
		t = (n1, m1, n1 + 1, m1, n1, m1 + 1)
		move(table, t)
	elif table[n1][m1] == 1 and table[n1 + 1][m1] == 0:
		t = (n1, m1, n1 + 1, m1 + 1, n1, m1 + 1)
		move(table, t)
	elif table[n1][m1] == 0 and table[n1 + 1][m1] == 1:
		t = (n1 + 1, m1, n1 + 1, m1 + 1, n1, m1 + 1)
		move(table, t)
	solve2m(n1, m1 + 1, n, m, table)

def solve(n1, m1, n, m, table):
	if n - n1 == 2:
		solve2m(n1, m1, n, m, table)
		return
	for j in range(0, m - 1):
		if table[n1][j] == 1:
			t = (n1, j, n1 + 1, j, n1 + 1, j + 1)
			move(table, t)
	if table[n1][m - 1] == 1:
		t = (n1, m - 1, n1 + 1, m - 1, n1 + 1, m - 2)
		move(table, t)
	solve(n1 + 1, m1, n, m, table)
t = int(input())
for tt in range(t):
	(n, m) = map(int, input().split())
	table = []
	res = []
	for i in range(n):
		row = input()
		table.append(row)
	table = [[ord(c) - ord('0') for c in row] for row in table]
	solve(0, 0, n, m, table)
	print(len(res))
	for (a, b, c, d, e, f) in res:
		print(a + 1, b + 1, c + 1, d + 1, e + 1, f + 1)


import os
import sys
from io import BytesIO, IOBase

def main():
	for t in range(int(input())):
		(n, m) = map(int, input().split())
		a = []
		for i in range(n):
			a.append(list(input()))
		ops = []

		def f(b, c, e, f2, g, h):
			ops.append((b + 1, c + 1, e + 1, f2 + 1, g + 1, h + 1))
			a[b][c] = '1' if a[b][c] == '0' else '0'
			a[e][f2] = '1' if a[e][f2] == '0' else '0'
			a[g][h] = '1' if a[g][h] == '0' else '0'
		if m % 2 == 1:
			for i in range(n - 1):
				if a[i][0] == '1':
					f(i, 0, i, 1, i + 1, 1)
			if a[n - 1][0] == '1':
				f(n - 1, 0, n - 1, 1, n - 2, 1)
		for j in range(m % 2, m, 2):
			for i in range(n - 2):
				if a[i][j] == '1':
					f(i, j, i + 1, j, i + 1, j + 1)
				if a[i][j + 1] == '1':
					f(i, j + 1, i + 1, j, i + 1, j + 1)
			q = ''.join([a[n - 2][j], a[n - 2][j + 1], a[n - 1][j], a[n - 1][j + 1]])
			if q == '1111':
				f(n - 2, j, n - 2, j + 1, n - 1, j)
				f(n - 2, j, n - 2, j + 1, n - 1, j + 1)
				f(n - 2, j, n - 1, j, n - 1, j + 1)
				f(n - 2, j + 1, n - 1, j, n - 1, j + 1)
			elif q == '1110':
				f(n - 2, j, n - 2, j + 1, n - 1, j)
			elif q == '1011':
				f(n - 2, j, n - 1, j, n - 1, j + 1)
			elif q == '1101':
				f(n - 2, j, n - 2, j + 1, n - 1, j + 1)
			elif q == '0111':
				f(n - 2, j + 1, n - 1, j, n - 1, j + 1)
			elif q == '1010':
				f(n - 2, j, n - 2, j + 1, n - 1, j + 1)
				f(n - 2, j + 1, n - 1, j, n - 1, j + 1)
			elif q == '1001':
				f(n - 2, j, n - 2, j + 1, n - 1, j)
				f(n - 2, j + 1, n - 1, j, n - 1, j + 1)
			elif q == '1100':
				f(n - 2, j, n - 1, j, n - 1, j + 1)
				f(n - 2, j + 1, n - 1, j, n - 1, j + 1)
			elif q == '0101':
				f(n - 2, j, n - 2, j + 1, n - 1, j)
				f(n - 2, j, n - 1, j, n - 1, j + 1)
			elif q == '0110':
				f(n - 2, j, n - 2, j + 1, n - 1, j + 1)
				f(n - 2, j, n - 1, j, n - 1, j + 1)
			elif q == '0011':
				f(n - 2, j, n - 2, j + 1, n - 1, j)
				f(n - 2, j, n - 2, j + 1, n - 1, j + 1)
			elif q == '1000':
				f(n - 2, j, n - 2, j + 1, n - 1, j)
				f(n - 2, j, n - 2, j + 1, n - 1, j + 1)
				f(n - 2, j, n - 1, j, n - 1, j + 1)
			elif q == '0100':
				f(n - 2, j, n - 2, j + 1, n - 1, j)
				f(n - 2, j, n - 2, j + 1, n - 1, j + 1)
				f(n - 2, j + 1, n - 1, j, n - 1, j + 1)
			elif q == '0010':
				f(n - 2, j, n - 2, j + 1, n - 1, j)
				f(n - 2, j, n - 1, j, n - 1, j + 1)
				f(n - 2, j + 1, n - 1, j, n - 1, j + 1)
			elif q == '0001':
				f(n - 2, j, n - 2, j + 1, n - 1, j + 1)
				f(n - 2, j, n - 1, j, n - 1, j + 1)
				f(n - 2, j + 1, n - 1, j, n - 1, j + 1)
		print(len(ops))
		for op in ops:
			print(op[0], op[1], op[4], op[5], op[2], op[3])
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys

def input():
	return sys.stdin.readline().rstrip()

def getAnsfromPositions(a, b, c):
	local_ans = [a[0] + 1, a[1] + 1, b[0] + 1, b[1] + 1, c[0] + 1, c[1] + 1]
	return local_ans

def one1handler(zero_pos, one_pos, ans):
	unit_ans = getAnsfromPositions(one_pos[0], zero_pos[0], zero_pos[1])
	ans.append(unit_ans)
	zero_pos.append(one_pos.pop(0))
	one_pos.append(zero_pos.pop(0))
	one_pos.append(zero_pos.pop(0))
	two1handler(zero_pos, one_pos, ans)

def two1handler(zero_pos, one_pos, ans):
	unit_ans = getAnsfromPositions(one_pos[0], zero_pos[0], zero_pos[1])
	ans.append(unit_ans)
	zero_pos.append(one_pos.pop(0))
	one_pos.append(zero_pos.pop(0))
	one_pos.append(zero_pos.pop(0))
	three1handler(zero_pos, one_pos, ans)

def three1handler(zero_pos, one_pos, ans):
	unit_ans = getAnsfromPositions(one_pos[0], one_pos[1], one_pos[2])
	ans.append(unit_ans)

def four1handler(zero_pos, one_pos, ans):
	unit_ans = getAnsfromPositions(one_pos[0], one_pos[1], one_pos[2])
	ans.append(unit_ans)
	zero_pos.append(one_pos.pop(0))
	zero_pos.append(one_pos.pop(0))
	zero_pos.append(one_pos.pop(0))
	one1handler(zero_pos, one_pos, ans)

def doOperations(i, j, arr, ans):
	zero_pos = []
	one_pos = []
	insertPosHelper(i, j, arr, zero_pos, one_pos)
	insertPosHelper(i + 1, j, arr, zero_pos, one_pos)
	insertPosHelper(i, j + 1, arr, zero_pos, one_pos)
	insertPosHelper(i + 1, j + 1, arr, zero_pos, one_pos)
	if len(one_pos) == 1:
		one1handler(zero_pos, one_pos, ans)
	elif len(one_pos) == 2:
		two1handler(zero_pos, one_pos, ans)
	elif len(one_pos) == 3:
		three1handler(zero_pos, one_pos, ans)
	elif len(one_pos) == 4:
		four1handler(zero_pos, one_pos, ans)
	arr[i][j] = 0
	arr[i + 1][j] = 0
	arr[i][j + 1] = 0
	arr[i + 1][j + 1] = 0

def insertPosHelper(i, j, arr, zero_pos, one_pos):
	if arr[i][j] == 0:
		zero_pos.append((i, j))
	else:
		one_pos.append((i, j))
t = int(input())
for i in range(t):
	[n, m] = list(map(int, input().split()))
	arr = []
	for j in range(n):
		arr.append(list(map(int, input())))
	i_row_iter = 0
	j_col_iter = 0
	ans = []
	while True:
		i_comp = i_row_iter if i_row_iter + 1 < n else i_row_iter - 1
		j_comp = j_col_iter if j_col_iter + 1 < m else j_col_iter - 1
		doOperations(i_comp, j_comp, arr, ans)
		j_next = j_col_iter + 2
		if j_next > m - 1:
			j_next = 0
			i_next = i_row_iter + 2
			if i_next > n - 1:
				break
		else:
			i_next = i_row_iter
		i_row_iter = i_next
		j_col_iter = j_next
	print(len(ans))
	for arrunit in ans:
		print(' '.join(list(map(str, arrunit))))


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = []
	for i in range(n):
		k = input()
		x = list(k)
		l.append(x)
	c = 0
	ans = []
	if m % 2 == 1:
		for i in range(0, n - 2):
			for j in range(0, m - 1, 2):
				if l[i][j] == '0' and l[i][j + 1] == '1':
					c += 1
					l[i][j] = '0'
					l[i][j + 1] = '0'
					ans.append([i, j + 1, i + 1, j, i + 1, j + 1])
					if l[i + 1][j + 1] == '1':
						l[i + 1][j + 1] = '0'
					else:
						l[i + 1][j + 1] = '1'
					if l[i + 1][j] == '1':
						l[i + 1][j] = '0'
					else:
						l[i + 1][j] = '1'
				elif l[i][j] == '1' and l[i][j + 1] == '0':
					c += 1
					l[i][j] = '0'
					l[i][j + 1] = '0'
					ans.append([i, j, i + 1, j, i + 1, j + 1])
					if l[i + 1][j + 1] == '1':
						l[i + 1][j + 1] = '0'
					else:
						l[i + 1][j + 1] = '1'
					if l[i + 1][j] == '1':
						l[i + 1][j] = '0'
					else:
						l[i + 1][j] = '1'
				elif l[i][j] == '1' and l[i][j + 1] == '1':
					c += 1
					l[i][j] = '0'
					l[i][j + 1] = '0'
					ans.append([i, j, i, j + 1, i + 1, j + 1])
					if l[i + 1][j + 1] == '1':
						l[i + 1][j + 1] = '0'
					else:
						l[i + 1][j + 1] = '1'
			j = m - 2
			if l[i][j] == '0' and l[i][j + 1] == '1':
				c += 1
				l[i][j] = '0'
				l[i][j + 1] = '0'
				ans.append([i, j + 1, i + 1, j, i + 1, j + 1])
				if l[i + 1][j + 1] == '1':
					l[i + 1][j + 1] = '0'
				else:
					l[i + 1][j + 1] = '1'
				if l[i + 1][j] == '1':
					l[i + 1][j] = '0'
				else:
					l[i + 1][j] = '1'
			elif l[i][j] == '1' and l[i][j + 1] == '0':
				c += 1
				l[i][j] = '0'
				l[i][j + 1] = '0'
				ans.append([i, j, i + 1, j, i + 1, j + 1])
				if l[i + 1][j + 1] == '1':
					l[i + 1][j + 1] = '0'
				else:
					l[i + 1][j + 1] = '1'
				if l[i + 1][j] == '1':
					l[i + 1][j] = '0'
				else:
					l[i + 1][j] = '1'
			elif l[i][j] == '1' and l[i][j + 1] == '1':
				c += 1
				l[i][j] = '0'
				l[i][j + 1] = '0'
				ans.append([i, j, i, j + 1, i + 1, j + 1])
				if l[i + 1][j + 1] == '1':
					l[i + 1][j + 1] = '0'
				else:
					l[i + 1][j + 1] = '1'
		i = n - 2
		for j in range(0, m - 1, 2):
			c0 = []
			c1 = []
			if l[i][j] == '1':
				c1.append([i, j])
			else:
				c0.append([i, j])
			if l[i][j + 1] == '1':
				c1.append([i, j + 1])
			else:
				c0.append([i, j + 1])
			if l[i + 1][j] == '1':
				c1.append([i + 1, j])
			else:
				c0.append([i + 1, j])
			if l[i + 1][j + 1] == '1':
				c1.append([i + 1, j + 1])
			else:
				c0.append([i + 1, j + 1])
			l0 = len(c0)
			l1 = len(c1)
			if l1 == 1:
				c += 1
				z = []
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[0][0])
				z.append(c0[0][1])
				z.append(c0[1][0])
				z.append(c0[1][1])
				ans.append(z)
				z = []
				c += 1
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[0][0])
				z.append(c0[0][1])
				z.append(c0[2][0])
				z.append(c0[2][1])
				ans.append(z)
				c += 1
				z = []
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[2][0])
				z.append(c0[2][1])
				z.append(c0[1][0])
				z.append(c0[1][1])
				ans.append(z)
			elif l1 == 2:
				z = []
				c += 1
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[0][0])
				z.append(c0[0][1])
				z.append(c0[1][0])
				z.append(c0[1][1])
				ans.append(z)
				z = []
				c += 1
				z.append(c0[0][0])
				z.append(c0[0][1])
				z.append(c0[1][0])
				z.append(c0[1][1])
				z.append(c1[1][0])
				z.append(c1[1][1])
				ans.append(z)
			elif l1 == 3:
				z = []
				c += 1
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c1[1][0])
				z.append(c1[1][1])
				z.append(c1[2][0])
				z.append(c1[2][1])
				ans.append(z)
			elif l1 == 4:
				c += 1
				c0 = []
				z = []
				for b in range(l1 - 1):
					c0.append(c1[b])
					z.append(c1[b][0])
					z.append(c1[b][1])
				c1 = c1[-1:]
				ans.append(z)
				c += 1
				z = []
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[0][0])
				z.append(c0[0][1])
				z.append(c0[1][0])
				z.append(c0[1][1])
				ans.append(z)
				z = []
				c += 1
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[0][0])
				z.append(c0[0][1])
				z.append(c0[2][0])
				z.append(c0[2][1])
				ans.append(z)
				c += 1
				z = []
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[2][0])
				z.append(c0[2][1])
				z.append(c0[1][0])
				z.append(c0[1][1])
				ans.append(z)
		i = n - 2
		j = m - 2
		c0 = []
		c1 = []
		l[n - 2][m - 2] = '0'
		l[n - 1][m - 2] = '0'
		if l[i][j] == '1':
			c1.append([i, j])
		else:
			c0.append([i, j])
		if l[i][j + 1] == '1':
			c1.append([i, j + 1])
		else:
			c0.append([i, j + 1])
		if l[i + 1][j] == '1':
			c1.append([i + 1, j])
		else:
			c0.append([i + 1, j])
		if l[i + 1][j + 1] == '1':
			c1.append([i + 1, j + 1])
		else:
			c0.append([i + 1, j + 1])
		l0 = len(c0)
		l1 = len(c1)
		if l1 == 1:
			c += 1
			z = []
			z.append(c1[0][0])
			z.append(c1[0][1])
			z.append(c0[0][0])
			z.append(c0[0][1])
			z.append(c0[1][0])
			z.append(c0[1][1])
			ans.append(z)
			z = []
			c += 1
			z.append(c1[0][0])
			z.append(c1[0][1])
			z.append(c0[0][0])
			z.append(c0[0][1])
			z.append(c0[2][0])
			z.append(c0[2][1])
			ans.append(z)
			c += 1
			z = []
			z.append(c1[0][0])
			z.append(c1[0][1])
			z.append(c0[2][0])
			z.append(c0[2][1])
			z.append(c0[1][0])
			z.append(c0[1][1])
			ans.append(z)
		elif l1 == 2:
			z = []
			c += 1
			z.append(c1[0][0])
			z.append(c1[0][1])
			z.append(c0[0][0])
			z.append(c0[0][1])
			z.append(c0[1][0])
			z.append(c0[1][1])
			ans.append(z)
			z = []
			c += 1
			z.append(c0[0][0])
			z.append(c0[0][1])
			z.append(c0[1][0])
			z.append(c0[1][1])
			z.append(c1[1][0])
			z.append(c1[1][1])
			ans.append(z)
		elif l1 == 3:
			z = []
			c += 1
			z.append(c1[0][0])
			z.append(c1[0][1])
			z.append(c1[1][0])
			z.append(c1[1][1])
			z.append(c1[2][0])
			z.append(c1[2][1])
			ans.append(z)
		elif l1 == 4:
			c0 = []
			z = []
			c += 1
			for b in range(0, l1 - 1):
				c0.append(c1[b])
				z.append(c1[b][0])
				z.append(c1[b][1])
			c1 = c1[:-1]
			c += 1
			ans.append(z)
			z = []
			z.append(c1[0][0])
			z.append(c1[0][1])
			z.append(c0[0][0])
			z.append(c0[0][1])
			z.append(c0[1][0])
			z.append(c0[1][1])
			ans.append(z)
			z = []
			c += 1
			z.append(c1[0][0])
			z.append(c1[0][1])
			z.append(c0[0][0])
			z.append(c0[0][1])
			z.append(c0[2][0])
			z.append(c0[2][1])
			ans.append(z)
			c += 1
			z = []
			z.append(c1[0][0])
			z.append(c1[0][1])
			z.append(c0[2][0])
			z.append(c0[2][1])
			z.append(c0[1][0])
			z.append(c0[1][1])
			ans.append(z)
	else:
		for i in range(0, n - 2):
			for j in range(0, m, 2):
				if l[i][j] == '0' and l[i][j + 1] == '1':
					c += 1
					l[i][j] = '0'
					l[i][j + 1] = '0'
					ans.append([i, j + 1, i + 1, j, i + 1, j + 1])
					if l[i + 1][j + 1] == '1':
						l[i + 1][j + 1] = '0'
					else:
						l[i + 1][j + 1] = '1'
					if l[i + 1][j] == '1':
						l[i + 1][j] = '0'
					else:
						l[i + 1][j] = '1'
				elif l[i][j] == '1' and l[i][j + 1] == '0':
					c += 1
					l[i][j] = '0'
					l[i][j + 1] = '0'
					ans.append([i, j, i + 1, j, i + 1, j + 1])
					if l[i + 1][j + 1] == '1':
						l[i + 1][j + 1] = '0'
					else:
						l[i + 1][j + 1] = '1'
					if l[i + 1][j] == '1':
						l[i + 1][j] = '0'
					else:
						l[i + 1][j] = '1'
				elif l[i][j] == '1' and l[i][j + 1] == '1':
					c += 1
					l[i][j] = '0'
					l[i][j + 1] = '0'
					ans.append([i, j, i, j + 1, i + 1, j + 1])
					if l[i + 1][j + 1] == '1':
						l[i + 1][j + 1] = '0'
					else:
						l[i + 1][j + 1] = '1'
		i = n - 2
		for j in range(0, m, 2):
			c0 = []
			c1 = []
			if l[i][j] == '1':
				c1.append([i, j])
			else:
				c0.append([i, j])
			if l[i][j + 1] == '1':
				c1.append([i, j + 1])
			else:
				c0.append([i, j + 1])
			if l[i + 1][j] == '1':
				c1.append([i + 1, j])
			else:
				c0.append([i + 1, j])
			if l[i + 1][j + 1] == '1':
				c1.append([i + 1, j + 1])
			else:
				c0.append([i + 1, j + 1])
			l0 = len(c0)
			l1 = len(c1)
			if l1 == 1:
				c += 1
				z = []
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[0][0])
				z.append(c0[0][1])
				z.append(c0[1][0])
				z.append(c0[1][1])
				ans.append(z)
				z = []
				c += 1
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[0][0])
				z.append(c0[0][1])
				z.append(c0[2][0])
				z.append(c0[2][1])
				ans.append(z)
				c += 1
				z = []
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[2][0])
				z.append(c0[2][1])
				z.append(c0[1][0])
				z.append(c0[1][1])
				ans.append(z)
			elif l1 == 2:
				z = []
				c += 1
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[0][0])
				z.append(c0[0][1])
				z.append(c0[1][0])
				z.append(c0[1][1])
				ans.append(z)
				z = []
				c += 1
				z.append(c0[0][0])
				z.append(c0[0][1])
				z.append(c0[1][0])
				z.append(c0[1][1])
				z.append(c1[1][0])
				z.append(c1[1][1])
				ans.append(z)
			elif l1 == 3:
				z = []
				c += 1
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c1[1][0])
				z.append(c1[1][1])
				z.append(c1[2][0])
				z.append(c1[2][1])
				ans.append(z)
			elif l1 == 4:
				c0 = []
				z = []
				c += 1
				for b in range(l1 - 1):
					c0.append(c1[b])
					z.append(c1[b][0])
					z.append(c1[b][1])
				c1 = c1[-1:]
				ans.append(z)
				c += 1
				z = []
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[0][0])
				z.append(c0[0][1])
				z.append(c0[1][0])
				z.append(c0[1][1])
				ans.append(z)
				z = []
				c += 1
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[0][0])
				z.append(c0[0][1])
				z.append(c0[2][0])
				z.append(c0[2][1])
				ans.append(z)
				c += 1
				z = []
				z.append(c1[0][0])
				z.append(c1[0][1])
				z.append(c0[2][0])
				z.append(c0[2][1])
				z.append(c0[1][0])
				z.append(c0[1][1])
				ans.append(z)
	if c == 0:
		ans.append([0, 0, 1, 0, 1, 1])
		ans.append([0, 0, 1, 0, 1, 1])
		c = 2
	print(c)
	for i in ans:
		k = []
		for j in i:
			k.append(j + 1)
		print(*k)


for _ in range(int(input())):
	(n, m) = map(int, input().split(' '))
	a = [list(map(int, list(input()))) for x in range(n)]
	ans = []

	def getone(arr):
		for xx in range(2):
			for yy in range(2):
				if arr[xx][yy] == 1:
					return (xx, yy)

	def getzero(arr):
		for xx in range(2):
			for yy in range(2):
				if arr[xx][yy] == 0:
					return (xx, yy)
	nn = n
	mm = m
	for x in range(0, nn, 2):
		for y in range(0, mm, 2):
			if n % 2 != 0 and x + 1 == n:
				x -= 1
			if m % 2 != 0 and y + 1 == m:
				y -= 1
			if x + 1 < n and y + 1 < m:
				temp = list([a[x][y:y + 2], a[x + 1][y:y + 2]])
				sm = 0
				for xxx in temp:
					sm = sm + sum(xxx)
				if sm == 0:
					continue
				if sm == 1:
					(xx, yy) = getone(temp)
					ans.append([x + 1 + xx, y + 1 + yy])
					ans.append([x + 1 + (xx + 1) % 2, y + 1 + yy])
					ans.append([x + 1 + xx, y + 1 + (yy + 1) % 2])
					tempx = xx
					xx = (xx + 1) % 2
					ans.append([x + 1 + xx, y + 1 + yy])
					ans.append([x + 1 + (xx + 1) % 2, y + 1 + yy])
					ans.append([x + 1 + xx, y + 1 + (yy + 1) % 2])
					yy = (yy + 1) % 2
					xx = (xx + 1) % 2
					ans.append([x + 1 + xx, y + 1 + yy])
					ans.append([x + 1 + (xx + 1) % 2, y + 1 + yy])
					ans.append([x + 1 + xx, y + 1 + (yy + 1) % 2])
					a[x][y] = 0
					a[x + 1][y] = 0
					a[x][y + 1] = 0
					a[x + 1][y + 1] = 0
				elif sm == 3:
					for xx in range(2):
						for yy in range(2):
							if temp[xx][yy] == 1:
								ans.append([x + 1 + xx, y + 1 + yy])
					a[x][y] = 0
					a[x + 1][y] = 0
					a[x][y + 1] = 0
					a[x + 1][y + 1] = 0
				elif sm == 4:
					for xx in range(2):
						for yy in range(2):
							if temp[xx][yy] == 1:
								ans.append([x + 1 + xx, y + 1 + yy])
					ans.pop()
					(xx, yy) = (1, 1)
					ans.append([x + 1 + xx, y + 1 + yy])
					ans.append([x + 1 + (xx + 1) % 2, y + 1 + yy])
					ans.append([x + 1 + xx, y + 1 + (yy + 1) % 2])
					tempx = xx
					xx = (xx + 1) % 2
					ans.append([x + 1 + xx, y + 1 + yy])
					ans.append([x + 1 + (xx + 1) % 2, y + 1 + yy])
					ans.append([x + 1 + xx, y + 1 + (yy + 1) % 2])
					yy = (yy + 1) % 2
					xx = (xx + 1) % 2
					ans.append([x + 1 + xx, y + 1 + yy])
					ans.append([x + 1 + (xx + 1) % 2, y + 1 + yy])
					ans.append([x + 1 + xx, y + 1 + (yy + 1) % 2])
					a[x][y] = 0
					a[x + 1][y] = 0
					a[x][y + 1] = 0
					a[x + 1][y + 1] = 0
				elif sm == 2:
					for xx in range(2):
						for yy in range(2):
							if temp[xx][yy] == 1:
								tempx = xx
								tempy = yy
							elif temp[xx][yy] == 0:
								ans.append([x + xx + 1, y + yy + 1])
								temp[xx][yy] = 1
					temp[tempx][tempy] = 0
					ans.append([x + tempx + 1, y + tempy + 1])
					for xx in range(2):
						for yy in range(2):
							if temp[xx][yy] == 1:
								ans.append([x + xx + 1, y + yy + 1])
					a[x][y] = 0
					a[x + 1][y] = 0
					a[x][y + 1] = 0
					a[x + 1][y + 1] = 0
	print(len(ans) // 3)
	cnt = 0
	for x in range(len(ans)):
		print(*ans[x], end=' ')
		cnt += 1
		if cnt == 3:
			print()
			cnt = 0


import sys
import math
from collections import defaultdict, Counter
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	l = []
	for j in range(n):
		s = list(input())
		l.append(s)
	ans = []
	for j in range(n - 1):
		if j & 1 and j != n - 2:
			continue
		for k in range(m - 1):
			if k & 1 and k != m - 2:
				continue
			cur = []
			if l[j][k] == '1':
				cur.extend([j + 1, k + 1])
			if l[j][k + 1] == '1':
				cur.extend([j + 1, k + 2])
			if l[j + 1][k] == '1':
				cur.extend([j + 2, k + 1])
			if l[j + 1][k + 1] == '1':
				cur.extend([j + 2, k + 2])
			if len(cur) == 6:
				ans.append(cur)
			elif len(cur) == 2:
				cur1 = []
				if l[j][k] == '1' or l[j + 1][k] == '1':
					cur1.extend([j + 1, k + 1])
					cur1.extend([j + 2, k + 1])
					cur1.extend([j + 2, k + 2])
					ans.append(cur1)
					cur1 = []
					cur1.extend([j + 1, k + 1])
					cur1.extend([j + 2, k + 1])
					cur1.extend([j + 1, k + 2])
					ans.append(cur1)
					cur1 = []
					cur1.extend([j + 2, k + 2])
					cur1.extend([j + 1, k + 2])
					if l[j][k] == '1':
						cur1.extend([j + 1, k + 1])
					else:
						cur1.extend([j + 2, k + 1])
					ans.append(cur1)
				else:
					cur1.extend([j + 1, k + 1])
					cur1.extend([j + 1, k + 2])
					cur1.extend([j + 2, k + 2])
					ans.append(cur1)
					cur1 = []
					cur1.extend([j + 2, k + 1])
					cur1.extend([j + 2, k + 2])
					cur1.extend([j + 1, k + 2])
					ans.append(cur1)
					cur1 = []
					cur1.extend([j + 2, k + 1])
					cur1.extend([j + 1, k + 1])
					if l[j][k + 1] == '1':
						cur1.extend([j + 1, k + 2])
					else:
						cur1.extend([j + 2, k + 2])
					ans.append(cur1)
			elif len(cur) == 8:
				cur1 = []
				cur1.extend([j + 2, k + 2])
				cur1.extend([j + 1, k + 2])
				cur1.extend([j + 2, k + 1])
				ans.append(cur1)
				cur1 = []
				cur1.extend([j + 1, k + 1])
				cur1.extend([j + 2, k + 1])
				cur1.extend([j + 2, k + 2])
				ans.append(cur1)
				cur1 = []
				cur1.extend([j + 1, k + 1])
				cur1.extend([j + 2, k + 1])
				cur1.extend([j + 1, k + 2])
				ans.append(cur1)
				cur1 = []
				cur1.extend([j + 2, k + 2])
				cur1.extend([j + 1, k + 2])
				cur1.extend([j + 1, k + 1])
				ans.append(cur1)
			elif len(cur) == 4:
				if l[j][k] == '1':
					if l[j][k + 1] == '1':
						cur1 = []
						cur1.extend([j + 1, k + 1])
						cur1.extend([j + 2, k + 1])
						cur1.extend([j + 2, k + 2])
						ans.append(cur1)
						cur1 = []
						cur1.extend([j + 2, k + 1])
						cur1.extend([j + 2, k + 2])
						cur1.extend([j + 1, k + 2])
						ans.append(cur1)
					elif l[j + 1][k] == '1':
						cur1 = []
						cur1.extend([j + 1, k + 1])
						cur1.extend([j + 2, k + 2])
						cur1.extend([j + 1, k + 2])
						ans.append(cur1)
						cur1 = []
						cur1.extend([j + 2, k + 1])
						cur1.extend([j + 2, k + 2])
						cur1.extend([j + 1, k + 2])
						ans.append(cur1)
					else:
						cur1 = []
						cur1.extend([j + 2, k + 1])
						cur1.extend([j + 2, k + 2])
						cur1.extend([j + 1, k + 2])
						ans.append(cur1)
						cur1 = []
						cur1.extend([j + 1, k + 1])
						cur1.extend([j + 2, k + 1])
						cur1.extend([j + 1, k + 2])
						ans.append(cur1)
				elif l[j + 1][k + 1] == '1':
					if l[j][k + 1] == '1':
						cur1 = []
						cur1.extend([j + 1, k + 1])
						cur1.extend([j + 2, k + 1])
						cur1.extend([j + 1, k + 2])
						ans.append(cur1)
						cur1 = []
						cur1.extend([j + 2, k + 1])
						cur1.extend([j + 2, k + 2])
						cur1.extend([j + 1, k + 1])
						ans.append(cur1)
					else:
						cur1 = []
						cur1.extend([j + 1, k + 1])
						cur1.extend([j + 2, k + 1])
						cur1.extend([j + 1, k + 2])
						ans.append(cur1)
						cur1 = []
						cur1.extend([j + 2, k + 2])
						cur1.extend([j + 1, k + 2])
						cur1.extend([j + 1, k + 1])
						ans.append(cur1)
				else:
					cur1 = []
					cur1.extend([j + 1, k + 1])
					cur1.extend([j + 2, k + 2])
					cur1.extend([j + 1, k + 2])
					ans.append(cur1)
					cur1 = []
					cur1.extend([j + 2, k + 1])
					cur1.extend([j + 2, k + 2])
					cur1.extend([j + 1, k + 1])
					ans.append(cur1)
			l[j][k] = '0'
			l[j][k + 1] = '0'
			l[j + 1][k + 1] = '0'
			l[j + 1][k] = '0'
	print(len(ans))
	for i in ans:
		print(*i)


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def change(a, b):
	(p, q, r, s, t, u) = b
	a[p - 1][q - 1] ^= 1
	a[r - 1][s - 1] ^= 1
	a[t - 1][u - 1] ^= 1
	return a

def solve():
	(n, m) = li()
	a = []
	for i in range(n):
		a.append(list(input().strip()))
	for i in range(n):
		for j in range(m):
			a[i][j] = int(a[i][j])
	ans = []
	for i in range(n - 1):
		for j in range(m):
			if a[i][j] == 1:
				if j != m - 1:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
				else:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j])
				a = change(a, ans[len(ans) - 1])
	i = n - 1
	for j in range(m - 1):
		if a[n - 1][j] == 1 and a[n - 1][j + 1] == 1:
			ans.append([i, j + 1, i, j + 2, i + 1, j + 2])
			a = change(a, ans[len(ans) - 1])
			ans.append([i + 1, j + 1, i, j + 1, i, j + 2])
			a = change(a, ans[len(ans) - 1])
		elif a[n - 1][j] == 1 and a[n - 1][j + 1] == 0:
			ans.append([i + 1, j + 1, i + 1, j + 2, i, j + 2])
			a = change(a, ans[len(ans) - 1])
			ans.append([i, j + 1, i + 1, j + 1, i + 1, j + 2])
			a = change(a, ans[len(ans) - 1])
			ans.append([i + 1, j + 1, i, j + 1, i, j + 2])
			a = change(a, ans[len(ans) - 1])
		elif a[n - 1][j] == 0 and a[n - 1][j + 1] == 1:
			ans.append([i + 1, j + 1, i, j + 1, i + 1, j + 2])
			a = change(a, ans[len(ans) - 1])
			ans.append([i, j + 1, i, j + 2, i + 1, j + 2])
			a = change(a, ans[len(ans) - 1])
			ans.append([i + 1, j + 1, i + 1, j + 2, i, j + 2])
			a = change(a, ans[len(ans) - 1])
	print(len(ans))
	for i in ans:
		print(*i)
for _ in range(I()):
	solve()


for t in range(int(input())):
	(n, m) = map(int, input().split())
	s = []
	for i in range(n):
		s.append(list(input()))
	a = []
	c = 0
	ans = 0
	for i in range(n - 1):
		for j in range(m - 1):
			while not (s[i][j] == '0' and s[i + 1][j] == '0' and (s[i][j + 1] == '0') and (s[i + 1][j + 1] == '0')):
				c = 0
				if s[i][j] == '1':
					c += 1
				if s[i][j + 1] == '1':
					c += 1
				if s[i + 1][j] == '1':
					c += 1
				if s[i + 1][j + 1] == '1':
					c += 1
				if c == 4:
					ans += 1
					a.append([i + 2, j + 1, i + 1, j + 2, i + 2, j + 2])
					s[i + 1][j] = '0'
					s[i][j + 1] = '0'
					s[i + 1][j + 1] = '0'
				if c == 3:
					ans += 1
					x = []
					if s[i][j] == '1':
						x.append(i + 1)
						x.append(j + 1)
						s[i][j] = '0'
					if s[i][j + 1] == '1':
						x.append(i + 1)
						x.append(j + 2)
						s[i][j + 1] = '0'
					if s[i + 1][j] == '1':
						x.append(i + 2)
						x.append(j + 1)
						s[i + 1][j] = '0'
					if s[i + 1][j + 1] == '1':
						x.append(i + 2)
						x.append(j + 2)
						s[i + 1][j + 1] = '0'
					a.append(x)
				elif c == 2:
					ans += 1
					x = []
					if s[i][j] == '1':
						x.append(i + 1)
						x.append(j + 1)
						s[i][j] = '0'
						if s[i + 1][j] == '0':
							x.append(i + 2)
							x.append(j + 1)
							s[i + 1][j] = '1'
						if s[i + 1][j + 1] == '0':
							x.append(i + 2)
							x.append(j + 2)
							s[i + 1][j + 1] = '1'
						if s[i][j + 1] == '0':
							x.append(i + 1)
							x.append(j + 2)
							s[i][j + 1] = '1'
					elif s[i][j + 1] == '1':
						x.append(i + 1)
						x.append(j + 2)
						s[i][j + 1] = '0'
						if s[i + 1][j] == '0':
							x.append(i + 2)
							x.append(j + 1)
							s[i + 1][j] = '1'
						if s[i + 1][j + 1] == '0':
							x.append(i + 2)
							x.append(j + 2)
							s[i + 1][j + 1] = '1'
						if s[i][j] == '0':
							x.append(i + 1)
							x.append(j + 1)
							s[i][j] = '1'
					elif s[i + 1][j] == '1':
						x.append(i + 2)
						x.append(j + 1)
						s[i + 1][j] = '0'
						if s[i][j + 1] == '0':
							x.append(i + 1)
							x.append(j + 2)
							s[i][j + 1] = '1'
						if s[i + 1][j + 1] == '0':
							x.append(i + 2)
							x.append(j + 2)
							s[i + 1][j + 1] = '1'
						if s[i][j] == '0':
							x.append(i + 1)
							x.append(j + 1)
							s[i][j] = '1'
					a.append(x)
				else:
					ans += 1
					x = []
					if s[i][j] == '1':
						x.append(i + 1)
						x.append(j + 1)
						s[i][j] = '0'
						s[i + 1][j + 1] = '1'
						x.append(i + 2)
						x.append(j + 2)
						if i + 2 < n:
							x.append(i + 2)
							x.append(j + 1)
							s[i + 1][j] = '1'
						else:
							x.append(i + 1)
							x.append(j + 2)
							s[i][j + 1] = '1'
						a.append(x)
					elif s[i][j + 1] == '1':
						a.append([i + 1, j + 2, i + 1, j + 1, i + 2, j + 1])
						s[i][j + 1] = '0'
						s[i][j] = '1'
						s[i + 1][j] = '1'
					elif s[i + 1][j] == '1':
						a.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
						s[i][j] = '1'
						s[i + 1][j] = '0'
						s[i + 1][j + 1] = '1'
					elif s[i + 1][j + 1] == '1':
						a.append([i + 1, j + 1, i + 2, j + 2, i + 2, j + 1])
						s[i][j] = '1'
						s[i + 1][j + 1] = '0'
						s[i + 1][j] = '1'
	print(ans)
	for i in range(len(a)):
		for j in range(len(a[i])):
			print(a[i][j], end=' ')
		print('')


import io, os
from math import *
J = input
input = lambda : J().encode()
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = [[0 if c == ord('0') else 1 for c in input()] for _ in range(n)]
	r = []
	for i in range(0, n, 2):
		i = min(i, n - 2)
		for j in range(0, m, 2):
			j = min(j, m - 2)
			t = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
			amt = t.count(1)
			z = [x for x in range(4) if t[x] == 0]
			o = z = [x for x in range(4) if t[x] == 1]

			def do(m):
				global r, t, amt, z, o
				r += [m]
				(x1, y1, x2, y2, x3, y3) = m
				a[x1 - 1][y1 - 1] ^= 1
				a[x2 - 1][y2 - 1] ^= 1
				a[x3 - 1][y3 - 1] ^= 1
				t = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
				amt = t.count(1)
				z = [x for x in range(4) if t[x] == 0]
				o = [x for x in range(4) if t[x] == 1]
			UR = lambda : do([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
			UL = lambda : do([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
			LR = lambda : do([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
			LL = lambda : do([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
			z = [x for x in range(4) if t[x] == 0]
			if amt == 4:
				LR()
			if amt == 1:
				[UL, UR, LL, LR][o[0]]()
			if amt == 2:
				[LR, LL, UR, UL][o[0]]()
			if amt == 3:
				[LR, LL, UR, UL][z[0]]()
	print(len(r))
	for x in r:
		print(*x)


import math
import sys
from collections import defaultdict
from functools import lru_cache
t = int(input())
for _ in range(t):
	(n, m) = [int(i) for i in input().split()]
	matrix = []
	for i in range(n):
		matrix.append([int(i) for i in input()])
	ans = []
	for i in range(n - 1):
		for j in range(m):
			if j != m - 1:
				if matrix[i][j] == 1:
					ans.append([i, j, i + 1, j, i + 1, j + 1])
					ans.append([i + 1, j, i + 1, j + 1, i, j + 1])
					matrix[i][j] = 0
					matrix[i][j + 1] = (matrix[i][j + 1] + 1) % 2
			elif matrix[i][j] == 1:
				ans.append([i, j, i, j - 1, i + 1, j])
				ans.append([i, j - 1, i + 1, j, i + 1, j - 1])
				matrix[i][j] = 0
				matrix[i + 1][j - 1] = (matrix[i + 1][j - 1] + 1) % 2
	for i in range(m - 1):
		if matrix[n - 1][i] == 1:
			ans.append([n - 1, i, n - 2, i, n - 2, i + 1])
			ans.append([n - 2, i, n - 2, i + 1, n - 1, i + 1])
			matrix[n - 1][i] = 0
			matrix[n - 1][i + 1] = (matrix[n - 1][i + 1] + 1) % 2
	if matrix[n - 1][m - 1] == 1:
		ans.append([n - 2, m - 2, n - 1, m - 2, n - 1, m - 1])
		ans.append([n - 1, m - 2, n - 1, m - 1, n - 2, m - 1])
		matrix[n - 2][m - 2] = (matrix[n - 2][m - 2] + 1) % 2
		matrix[n - 2][m - 1] = (matrix[n - 2][m - 1] + 1) % 2
		ans.append([n - 1, m - 1, n - 2, m - 1, n - 2, m - 2])
	a1 = []
	for i in range(len(ans)):
		a2 = [i + 1 for i in ans[i]]
		a1.append(a2)
	print(len(ans))
	for i in range(len(a1)):
		print(*a1[i])


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = []
	for i in range(n):
		a.append([int(i) for i in list(input())])
	res = []
	for i in range(n):
		for j in range(m):
			if a[i][j] == 1:
				if i != 0 and j != 0:
					res.append([i, j + 1, i + 1, j + 1, i + 1, j])
					res.append([i, j, i + 1, j + 1, i + 1, j])
					res.append([i, j + 1, i + 1, j + 1, i, j])
				elif i != n - 1 and j != m - 1:
					res.append([i + 1, j + 2, i + 1, j + 1, i + 2, j + 1])
					res.append([i + 2, j + 2, i + 1, j + 1, i + 2, j + 1])
					res.append([i + 1, j + 2, i + 1, j + 1, i + 2, j + 2])
				elif i == n - 1:
					res.append([i + 1, j + 2, i + 1, j + 1, i, j + 1])
					res.append([i, j + 2, i + 1, j + 1, i, j + 1])
					res.append([i + 1, j + 2, i + 1, j + 1, i, j + 2])
				else:
					res.append([i + 2, j + 1, i + 1, j + 1, i + 1, j])
					res.append([i + 2, j, i + 1, j + 1, i + 1, j])
					res.append([i + 2, j + 1, i + 1, j + 1, i + 2, j])
	print(len(res))
	for i in range(len(res)):
		print(*res[i])


import sys
from math import gcd, sqrt, ceil, log2
from collections import defaultdict, Counter, deque
from bisect import bisect_left, bisect_right
import math
sys.setrecursionlimit(2 * 10 ** 5 + 10)
import heapq
from itertools import permutations
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
aa = 'abcdefghijklmnopqrstuvwxyz'

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def get_sum(bit, i):
	s = 0
	i += 1
	while i > 0:
		s += bit[i]
		i -= i & -i
	return s

def update(bit, n, i, v):
	i += 1
	while i <= n:
		bit[i] += v
		i += i & -i

def modInverse(b, m):
	g = math.gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def primeFactors(n):
	sa = []
	while n % 2 == 0:
		sa.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			sa.append(i)
			n = n // i
	if n > 2:
		sa.append(n)
	return sa

def seive(n):
	pri = [True] * (n + 1)
	p = 2
	while p * p <= n:
		if pri[p] == True:
			for i in range(p * p, n + 1, p):
				pri[i] = False
		p += 1
	return pri

def check_prim(n):
	if n < 0:
		return False
	for i in range(2, int(sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

def getZarr(string, z):
	n = len(string)
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and string[r - l] == string[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and string[r - l] == string[r]:
					r += 1
				z[i] = r - l
				r -= 1

def search(text, pattern):
	concat = pattern + '$' + text
	l = len(concat)
	z = [0] * l
	getZarr(concat, z)
	ha = []
	for i in range(l):
		if z[i] == len(pattern):
			ha.append(i - len(pattern) - 1)
	return ha

def mergeIntervals(arr):
	arr.sort(key=lambda x: x[0])
	m = []
	s = -10000
	max = -100000
	for i in range(len(arr)):
		a = arr[i]
		if a[0] > max:
			if i != 0:
				m.append([s, max])
			max = a[1]
			s = a[0]
		elif a[1] >= max:
			max = a[1]
	if max != -100000 and [s, max] not in m:
		m.append([s, max])
	return m

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def sol(n):
	seti = set()
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			seti.add(n // i)
			seti.add(i)
	return seti

def lcm(a, b):
	return a * b // gcd(a, b)

def moore_voting(l):
	count1 = 0
	count2 = 0
	first = 10 ** 18
	second = 10 ** 18
	n = len(l)
	for i in range(n):
		if l[i] == first:
			count1 += 1
		elif l[i] == second:
			count2 += 1
		elif count1 == 0:
			count1 += 1
			first = l[i]
		elif count2 == 0:
			count2 += 1
			second = l[i]
		else:
			count1 -= 1
			count2 -= 1
	for i in range(n):
		if l[i] == first:
			count1 += 1
		elif l[i] == second:
			count2 += 1
	if count1 > n // 3:
		return first
	if count2 > n // 3:
		return second
	return -1

def find_parent(u, parent):
	if u != parent[u]:
		parent[u] = find_parent(parent[u], parent)
	return parent[u]

def dis_union(n):
	par = [i for i in range(n + 1)]
	rank = [1] * (n + 1)
	k = int(input())
	for i in range(k):
		(a, b) = map(int, input().split())
		(z1, z2) = (find_parent(a, par), find_parent(b, par))
		if z1 != z2:
			par[z1] = z2
			rank[z2] += rank[z1]

def dijkstra(n, tot, hash):
	hea = [[0, n]]
	dis = [10 ** 18] * (tot + 1)
	dis[n] = 0
	boo = defaultdict(bool)
	check = defaultdict(int)
	while hea:
		(a, b) = heapq.heappop(hea)
		if boo[b]:
			continue
		boo[b] = True
		for (i, w) in hash[b]:
			if b == 1:
				c = 0
				if (1, i, w) in nodes:
					c = nodes[1, i, w]
					del nodes[1, i, w]
				if dis[b] + w < dis[i]:
					dis[i] = dis[b] + w
					check[i] = c
				elif dis[b] + w == dis[i] and c == 0:
					dis[i] = dis[b] + w
					check[i] = c
			elif dis[b] + w <= dis[i]:
				dis[i] = dis[b] + w
				check[i] = check[b]
			heapq.heappush(hea, [dis[i], i])
	return check

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res *= x
		x = x * x
		y = y >> 1
	return res
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	l = []
	for i in range(n):
		s = list(input())
		l.append(s)
	ans = []
	for x in range(n):
		for y in range(m):
			if x + 1 < n and y + 1 < m:
				cnt = 0
				(i, j) = (x, y)
				(a, b, c, d) = (l[i][j], l[i + 1][j], l[i][j + 1], l[i + 1][j + 1])
				k = [a, b, c, d]
				co = [(i, j), (i + 1, j), (i, j + 1), (i + 1, j + 1)]
				if k.count('1') == 3:
					for i in co:
						(a, b) = i
						if l[a][b] == '1':
							l[a][b] = '0'
							ans.append((a + 1, b + 1))
				elif k.count('1') == 2:
					flag = 0
					flag1 = 0
					for i in co:
						(a, b) = i
						if l[a][b] == '1' and flag == 0:
							l[a][b] = '0'
							ans.append((a + 1, b + 1))
							flag = 1
						elif l[a][b] == '0' and flag1 < 2:
							l[a][b] = '1'
							ans.append((a + 1, b + 1))
							flag1 += 1
					for i in co:
						(a, b) = i
						if l[a][b] == '1':
							l[a][b] = '0'
							ans.append((a + 1, b + 1))
				elif k.count('1') == 1:
					flag = 0
					for i in co:
						(a, b) = i
						if l[a][b] == '1':
							l[a][b] = '0'
							ans.append((a + 1, b + 1))
						elif l[a][b] == '0' and flag < 2:
							l[a][b] = '1'
							ans.append((a + 1, b + 1))
							flag += 1
					flag = 0
					flag1 = 0
					for i in co:
						(a, b) = i
						if l[a][b] == '1' and flag == 0:
							l[a][b] = '0'
							ans.append((a + 1, b + 1))
							flag = 1
						elif l[a][b] == '0' and flag1 < 2:
							l[a][b] = '1'
							ans.append((a + 1, b + 1))
							flag1 += 1
					for i in co:
						(a, b) = i
						if l[a][b] == '1':
							l[a][b] = '0'
							ans.append((a + 1, b + 1))
				elif k.count('1') == 4:
					flag = 0
					for i in co:
						(a, b) = i
						if l[a][b] == '1' and flag <= 2:
							l[a][b] = '0'
							flag += 1
							ans.append((a + 1, b + 1))
					flag = 0
					for i in co:
						(a, b) = i
						if l[a][b] == '1':
							l[a][b] = '0'
							ans.append((a + 1, b + 1))
						elif l[a][b] == '0' and flag < 2:
							l[a][b] = '1'
							ans.append((a + 1, b + 1))
							flag += 1
					flag = 0
					flag1 = 0
					for i in co:
						(a, b) = i
						if l[a][b] == '1' and flag == 0:
							l[a][b] = '0'
							ans.append((a + 1, b + 1))
							flag = 1
						elif l[a][b] == '0' and flag1 < 2:
							l[a][b] = '1'
							ans.append((a + 1, b + 1))
							flag1 += 1
					for i in co:
						(a, b) = i
						if l[a][b] == '1':
							l[a][b] = '0'
							ans.append((a + 1, b + 1))
	print(len(ans) // 3)
	for i in range(0, len(ans), 3):
		(a, b, c) = (ans[i], ans[i + 1], ans[i + 2])
		print(*a, *b, *c)


def fu1(A, i, j, kkk):
	o = 0
	z = 0
	ans = []
	for k in range(i, i + 2):
		for m in range(j, j + 2):
			if A[k][m] == '0' and z < 2:
				z += 1
				A[k][m] = '1'
				ans.append([k + 1, m + 1])
			elif A[k][m] == '1':
				A[k][m] = '0'
				ans.append([k + 1, m + 1])
	kkk.append(ans[:])
	fu2(A, i, j, kkk)

def fu2(A, i, j, kkk):
	o = 0
	ans = []
	for k in range(i, i + 2):
		for m in range(j, j + 2):
			if A[k][m] == '0':
				A[k][m] = '1'
				ans.append([k + 1, m + 1])
			elif A[k][m] == '1' and o == 0:
				o = 1
				A[k][m] = '0'
				ans.append([k + 1, m + 1])
	kkk.append(ans[:])
	fu3(A, i, j, kkk)

def fu3(A, i, j, kkk):
	ans = []
	if A[i][j] == '1':
		ans.append([i + 1, j + 1])
		A[i][j] = '0'
	if A[i + 1][j] == '1':
		ans.append([i + 2, j + 1])
		A[i + 1][j] = '0'
	if A[i][j + 1] == '1':
		ans.append([i + 1, j + 2])
		A[i][j + 1] = '0'
	if A[i + 1][j + 1] == '1':
		ans.append([i + 2, j + 2])
		A[i + 1][j + 1] = '0'
	kkk.append(ans[:])
	return

def fu4(A, i, j, kkk):
	ans = []
	ans.append([i + 1, j + 1])
	ans.append([i + 1, j + 2])
	ans.append([i + 2, j + 1])
	A[i][j] = '0'
	A[i + 1][j] = '0'
	A[i][j + 1] = '0'
	kkk.append(ans[:])
	fu1(A, i, j, kkk)

def fu(A, i, j, ans):
	t = int(A[i][j]) + int(A[i + 1][j]) + int(A[i][j + 1]) + int(A[i + 1][j + 1])
	if t == 4:
		fu4(A, i, j, ans)
	elif t == 3:
		fu3(A, i, j, ans)
	elif t == 2:
		fu2(A, i, j, ans)
	elif t == 1:
		fu1(A, i, j, ans)
for i in range(int(input())):
	(n, m) = map(int, input().split())
	l = []
	for i in range(n):
		t = list(input())
		l.append(t[:])
	ans = []
	for i in range(0, n - 1):
		for j in range(0, m - 1):
			fu(l, i, j, ans)
	print(len(ans))
	for i in ans:
		for j in i:
			print(j[0], j[1], end=' ')
		print()


from collections import Counter
from collections import OrderedDict
from collections import defaultdict
import bisect
import math
from sys import setrecursionlimit

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def fre_count(mylist):
	return Counter(mylist)
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	grid = [[int(x) for x in input()] for x in range(n)]
	ans = []
	for i in range(n):
		for j in range(m):
			count = 0
			if i + 1 < n and j + 1 < m:
				p1 = []
				p0 = []
				if grid[i][j] == 1:
					p1.append([i + 1, j + 1])
					grid[i][j] = 0
				else:
					p0.append([i + 1, j + 1])
				if grid[i + 1][j] == 1:
					p1.append([i + 2, j + 1])
					grid[i + 1][j] = 0
				else:
					p0.append([i + 1 + 1, j + 1])
				if grid[i][j + 1] == 1:
					p1.append([i + 1, j + 2])
					grid[i][j + 1] = 0
				else:
					p0.append([i + 1, j + 2])
				if grid[i + 1][j + 1] == 1:
					p1.append([i + 2, j + 2])
					grid[i + 1][j + 1] = 0
				else:
					p0.append([i + 2, j + 2])
				if len(p1) == 4:
					for k in range(3):
						ans.append(p1[k])
					for k in range(3, 0, -1):
						ans.append(p1[k])
					ans.append(p1[3])
					ans.append(p1[2])
					ans.append(p1[0])
					ans.append(p1[1])
					ans.append(p1[0])
					ans.append(p1[3])
				elif len(p1) == 3:
					for k in range(3):
						ans.append(p1[k])
				elif len(p1) == 2:
					ans.append(p1[0])
					ans.append(p0[0])
					ans.append(p0[1])
					ans.append(p1[1])
					ans.append(p0[0])
					ans.append(p0[1])
				elif len(p1) == 1:
					ans.append(p1[0])
					ans.append(p0[0])
					ans.append(p0[1])
					ans.append(p0[0])
					ans.append(p1[0])
					ans.append(p0[2])
					ans.append(p1[0])
					ans.append(p0[1])
					ans.append(p0[2])
	print(len(ans) // 3)
	for i in range(len(ans)):
		print(*ans[i], end=' ')
		if (i + 1) % 3 == 0 and i != 0:
			print()


t = int(input())
for _ in range(t):
	(N, M) = map(int, input().split(' '))
	A = [list(input()) for _ in range(N)]
	res = []

	def solve(B):
		count_of_one = 0
		dict = {1: [], 0: []}
		for i in B:
			if A[i[0]][i[1]] == '1':
				count_of_one += 1
				dict[1].append(i)
			else:
				dict[0].append(i)
		if count_of_one == 0:
			return
		if count_of_one == 3:
			var = []
			for i in dict[1]:
				var.append(i[0] + 1)
				var.append(i[1] + 1)
				A[i[0]][i[1]] = '0'
			res.append(var)
			return
		if count_of_one == 2:
			var = []
			for i in dict[0]:
				var.append(i[0] + 1)
				var.append(i[1] + 1)
				A[i[0]][i[1]] = '1'
			x = dict[1][0]
			A[x[0]][x[1]] = '0'
			var.append(x[0] + 1)
			var.append(x[1] + 1)
			res.append(var)
			solve(B)
		if count_of_one == 1:
			var = []
			for j in range(2):
				i = dict[0][j]
				var.append(i[0] + 1)
				var.append(i[1] + 1)
				A[i[0]][i[1]] = '1'
			x = dict[1][0]
			A[x[0]][x[1]] = '0'
			var.append(x[0] + 1)
			var.append(x[1] + 1)
			res.append(var)
			solve(B)
		if count_of_one == 4:
			var = []
			for i in range(3):
				x = dict[1][i]
				A[x[0]][x[1]] = '0'
				var.append(x[0] + 1)
				var.append(x[1] + 1)
			res.append(var)
			solve(B)
	for i in range(0, N - 1):
		for j in range(0, M - 1):
			solve([[i, j], [i, j + 1], [i + 1, j], [i + 1, j + 1]])
	print(len(res))
	for i in res:
		print(' '.join(list(map(str, i))))


import os
import sys
from io import BytesIO, IOBase
_str = str
BUFSIZE = 8192

def str(x=b''):
	return x if type(x) is bytes else _str(x).encode()

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def inp():
	return sys.stdin.readline().rstrip()

def mpint():
	return map(int, inp().split(' '))

def itg():
	return int(inp())
output = []
from copy import deepcopy

def left_top(i, j):
	output.append([i, j, i, j + 1, i + 1, j])
	output.append([i, j, i, j + 1, i + 1, j + 1])
	output.append([i, j, i + 1, j, i + 1, j + 1])

def left_bottom(i, j):
	output.append([i, j + 1, i + 1, j, i + 1, j + 1])
	output.append([i, j, i + 1, j, i + 1, j + 1])
	output.append([i, j, i, j + 1, i + 1, j])

def right_top(i, j):
	output.append([i, j, i, j + 1, i + 1, j + 1])
	output.append([i, j, i, j + 1, i + 1, j])
	output.append([i + 1, j, i + 1, j + 1, i, j + 1])

def right_bottom(i, j):
	output.append([i + 1, j, i + 1, j + 1, i, j])
	output.append([i + 1, j, i + 1, j + 1, i, j + 1])
	output.append([i, j, i, j + 1, i + 1, j + 1])

def solve():
	(n, m) = mpint()
	matrix = [list(inp()) for _ in range(n)]
	m2 = deepcopy(matrix)
	output.clear()
	for i in range(n - 1):
		for j in range(m - 1):
			if matrix[i][j] == '1':
				left_top(i, j)
				matrix[i][j] = '0'
	if matrix[0][0] == '1':
		left_top(0, 0)
		matrix[0][0] = '0'
	if matrix[n - 1][0] == '1':
		left_bottom(n - 2, 0)
		matrix[n - 1][0] = '0'
	if matrix[0][m - 1] == '1':
		right_top(0, m - 2)
		matrix[0][m - 1] = '0'
	if matrix[n - 1][m - 1] == '1':
		right_bottom(n - 2, m - 2)
		matrix[n - 1][m - 1] = '0'
	for i in range(1, m - 1):
		if matrix[n - 1][i] == '1':
			left_bottom(n - 2, i)
			matrix[n - 1][i] = '0'
	for i in range(1, n - 1):
		if matrix[i][m - 1] == '1':
			right_top(i, m - 2)
			matrix[i][m - 1] = '0'
	print(len(output))
	for op in output:
		print(*map(lambda x: x + 1, op))

def main():
	for _ in range(itg()):
		solve()
DEBUG = 0
URL = ''
if DEBUG == 1:
	import requests
	from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
	runner = TestCaseRunner(main, URL, 2)
	inp = runner.input_stream
	print = runner.output_stream
	runner.checking()
elif DEBUG == 2:
	main()
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	main()


t = int(input())
while t:
	t -= 1
	(n, m) = map(int, input().split())
	li = [list(map(int, list(input()))) for __ in range(n)]
	ope = []
	for i in range(n - 1):
		for j in range(m):
			if li[i][j] == 1:
				if j == 0:
					ope.append([i, j, i + 1, j, i + 1, j + 1])
					li[i][j] = 1 - li[i][j]
					li[i + 1][j] = 1 - li[i + 1][j]
					li[i + 1][j + 1] = 1 - li[i + 1][j + 1]
				else:
					ope.append([i, j, i + 1, j, i + 1, j - 1])
					li[i][j] = 1 - li[i][j]
					li[i + 1][j] = 1 - li[i + 1][j]
					li[i + 1][j - 1] = 1 - li[i + 1][j - 1]
	k = n - 1
	for cl in range(m):
		if li[k][cl] == 1:
			if cl == 0:
				ope.append([k, cl, k - 1, cl, k - 1, cl + 1])
				li[k][cl] = 1 - li[k][cl]
				ope.append([k, cl, k, cl + 1, k - 1, cl + 1])
				ope.append([k - 1, cl, k, cl, k, cl + 1])
			else:
				ope.append([k, cl, k, cl - 1, k - 1, cl - 1])
				li[k][cl] = 1 - li[k][cl]
				ope.append([k, cl, k - 1, cl, k - 1, cl - 1])
				ope.append([k, cl - 1, k, cl, k - 1, cl])
	print(len(ope))
	for ele in ope:
		print(*list(map(lambda i: i + 1, ele)))


for y in range(int(input())):
	(n, k) = map(int, input().split())
	lst = []
	ans = []
	for i in range(n):
		lst.append(list(input()))
	for i in range(n):
		for j in range(k):
			if i == n - 1:
				if j == k - 1:
					if lst[i][j] == '0':
						pass
					else:
						ans.append([i, j, i - 1, j, i - 1, j - 1])
						ans.append([i, j, i - 1, j, i, j - 1])
						ans.append([i, j, i - 1, j - 1, i, j - 1])
				elif lst[i][j] == '0':
					pass
				else:
					ans.append([i, j, i - 1, j, i - 1, j + 1])
					ans.append([i, j + 1, i - 1, j, i - 1, j + 1])
					lst[i][j] = '0'
					if lst[i][j + 1] == '1':
						lst[i][j + 1] = '0'
					else:
						lst[i][j + 1] = '1'
			elif j == k - 1:
				if lst[i][j] == '0':
					pass
				elif lst[i][j - 1] == '0':
					ans.append([i, j, i + 1, j, i + 1, j - 1])
					lst[i][j] = '0'
					if lst[i + 1][j] == '1':
						lst[i + 1][j] = '0'
					else:
						lst[i + 1][j] = '1'
					if lst[i + 1][j - 1] == '1':
						lst[i + 1][j - 1] = '0'
					else:
						lst[i + 1][j - 1] = '1'
			elif lst[i][j] == '0':
				pass
			elif lst[i][j + 1] == '0':
				ans.append([i, j, i + 1, j, i + 1, j + 1])
				lst[i][j] = '0'
				if lst[i + 1][j] == '1':
					lst[i + 1][j] = '0'
				else:
					lst[i + 1][j] = '1'
				if lst[i + 1][j + 1] == '1':
					lst[i + 1][j + 1] = '0'
				else:
					lst[i + 1][j + 1] = '1'
			else:
				ans.append([i, j, i, j + 1, i + 1, j + 1])
				lst[i][j] = '0'
				if lst[i][j + 1] == '1':
					lst[i][j + 1] = '0'
				else:
					lst[i][j + 1] = '1'
				if lst[i + 1][j + 1] == '1':
					lst[i + 1][j + 1] = '0'
				else:
					lst[i + 1][j + 1] = '1'
	print(len(ans))
	for i in ans:
		for j in i:
			print(j + 1, end=' ')
		print()


import io
import os

def solve():
	(n, m) = map(int, input().split())
	vals = []
	for _ in range(n):
		vals.append(list(map(int, input())))
	ops = []

	def flip(x1, y1, x2, y2, x3, y3):
		for (a, b) in ([x1, y1], [x2, y2], [x3, y3]):
			vals[b][a] = 1 - vals[b][a]
		ops.append((y1 + 1, x1 + 1, y2 + 1, x2 + 1, y3 + 1, x3 + 1))
	for y in range(n - 2):
		for x in range(m):
			if x == m - 1:
				if vals[y][x]:
					flip(x, y, x, y + 1, x - 1, y + 1)
			elif vals[y][x]:
				flip(x, y, x, y + 1, x + 1, y + 1)
	for x in range(m - 2):
		if vals[n - 2][x]:
			flip(x, n - 2, x + 1, n - 2, x + 1, n - 1)
		if vals[n - 1][x]:
			flip(x, n - 1, x + 1, n - 2, x + 1, n - 1)
	a = (m - 2, n - 2)
	b = (m - 1, n - 2)
	c = (m - 2, n - 1)
	d = (m - 1, n - 1)

	def last():
		return [vals[y][x] for (x, y) in [a, b, c, d]]
	if sum(last()) == 4:
		flip(*a, *b, *c)
	if sum(last()) == 1:
		i = last().index(1)
		if i == 0:
			flip(*a, *b, *c)
		elif i == 1:
			flip(*b, *d, *c)
		elif i == 2:
			flip(*a, *c, *d)
		else:
			flip(*b, *d, *c)
	if sum(last()) == 2:
		if last() == [1, 1, 0, 0]:
			flip(*a, *c, *d)
		elif last() == [1, 0, 1, 0]:
			flip(*a, *b, *d)
		elif last() == [1, 0, 0, 1]:
			flip(*a, *b, *c)
		elif last() == [0, 1, 1, 0]:
			flip(*a, *b, *d)
		elif last() == [0, 1, 0, 1]:
			flip(*a, *b, *c)
		else:
			flip(*a, *b, *c)
	if sum(last()) == 3:
		i = last().index(0)
		if i == 0:
			flip(*b, *c, *d)
		elif i == 1:
			flip(*a, *c, *d)
		elif i == 2:
			flip(*a, *b, *d)
		else:
			flip(*a, *b, *c)
	for row in vals:
		assert not any(row)
	print(len(ops))
	for op in ops:
		print(*op)
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	arr = [[*input()] for _ in range(n)]
	ans = []
	for i in range(n - 1):
		for j in range(m):
			if arr[i][j] == '1':
				ans.append([i, j, i + 1, j, i + 1, j + 1 if j == 0 else j - 1])
				arr[i][j] = '0'
				arr[i + 1][j] = '1' if arr[i + 1][j] == '0' else '0'
				arr[i + 1][j + 1 if j == 0 else j - 1] = '1' if arr[i + 1][j + 1 if j == 0 else j - 1] == '0' else '0'
	i = n - 1
	j = 0
	if arr[-1][0] == '1':
		ans.append([i, j, i, j + 1, i - 1, j + 1])
		arr[i][j] = '1' if arr[i][j] == '0' else '0'
		arr[i][j + 1] = '1' if arr[i][j + 1] == '0' else '0'
		arr[i - 1][j + 1] = '1' if arr[i - 1][j + 1] == '0' else '0'
		ans.append([i, j, i - 1, j, i, j + 1])
		arr[i][j] = '1' if arr[i][j] == '0' else '0'
		arr[i - 1][j] = '1' if arr[i - 1][j] == '0' else '0'
		arr[i][j + 1] = '1' if arr[i][j + 1] == '0' else '0'
		ans.append([i, j, i - 1, j, i - 1, j + 1])
		arr[i][j] = '1' if arr[i][j] == '0' else '0'
		arr[i - 1][j] = '1' if arr[i - 1][j] == '0' else '0'
		arr[i - 1][j + 1] = '1' if arr[i - 1][j + 1] == '0' else '0'
	for j in range(1, m):
		if arr[-1][j] == '1':
			ans.append([i, j, i, j - 1, i - 1, j - 1])
			arr[i][j] = '1' if arr[i][j] == '0' else '0'
			arr[i][j - 1] = '1' if arr[i][j - 1] == '0' else '0'
			arr[i - 1][j - 1] = '1' if arr[i - 1][j - 1] == '0' else '0'
			ans.append([i, j, i - 1, j, i - 1, j - 1])
			arr[i][j] = '1' if arr[i][j] == '0' else '0'
			arr[i - 1][j] = '1' if arr[i - 1][j] == '0' else '0'
			arr[i - 1][j - 1] = '1' if arr[i - 1][j - 1] == '0' else '0'
			ans.append([i, j, i, j - 1, i - 1, j])
			arr[i][j] = '1' if arr[i][j] == '0' else '0'
			arr[i][j - 1] = '1' if arr[i][j - 1] == '0' else '0'
			arr[i - 1][j] = '1' if arr[i - 1][j] == '0' else '0'
	print(len(ans))
	for i in ans:
		i = [j + 1 for j in i]
		print(*i)


dic = {'1000': [1, 1, 1, 2, 2, 2], '0100': [1, 1, 1, 2, 2, 2], '0010': [1, 1, 1, 2, 2, 1], '0001': [1, 1, 1, 2, 2, 2], '0011': [1, 1, 1, 2, 2, 1], '0110': [1, 1, 1, 2, 2, 2], '0101': [1, 1, 1, 2, 2, 1], '1001': [1, 1, 1, 2, 2, 1], '1010': [1, 1, 1, 2, 2, 2], '1100': [1, 1, 2, 1, 2, 2], '0111': [1, 2, 2, 1, 2, 2], '1011': [1, 1, 2, 1, 2, 2], '1101': [1, 1, 1, 2, 2, 2], '1110': [1, 1, 1, 2, 2, 1], '1111': [1, 1, 1, 2, 2, 1]}
dic1 = {'1000': '0101', '0100': '1001', '0010': '1100', '0001': '1100', '0011': '1101', '0110': '1011', '0101': '1011', '1001': '0111', '1010': '0111', '1100': '0111', '0111': '0000', '1011': '0000', '1101': '0000', '1110': '0000', '1111': '0001'}
T = int(input())
while T > 0:
	(n, m) = map(int, input().split())
	matrix = []
	res = []
	for i in range(n):
		matrix.append(input())
	count = 0
	for i in range(n // 2):
		row = i * 2
		for j in range(m // 2):
			col = j * 2
			temp = matrix[row][col] + matrix[row][col + 1] + matrix[row + 1][col] + matrix[row + 1][col + 1]
			while temp != '0000':
				xx = dic[temp].copy()
				(xx[0], xx[2], xx[4]) = (xx[0] + row, xx[2] + row, xx[4] + row)
				(xx[1], xx[3], xx[5]) = (xx[1] + col, xx[3] + col, xx[5] + col)
				res.append(xx)
				temp = dic1[temp]
				count += 1
		if m % 2 == 1:
			temp = '0' + matrix[row][m - 1] + '0' + matrix[row + 1][m - 1]
			while temp != '0000':
				xx = dic[temp].copy()
				(xx[0], xx[2], xx[4]) = (xx[0] + row, xx[2] + row, xx[4] + row)
				(xx[1], xx[3], xx[5]) = (xx[1] + m - 2, xx[3] + m - 2, xx[5] + m - 2)
				res.append(xx)
				temp = dic1[temp]
				count += 1
	if n % 2 == 1:
		for i in range(m // 2):
			col = i * 2
			temp = '0' + '0' + matrix[n - 1][col] + matrix[n - 1][col + 1]
			while temp != '0000':
				xx = dic[temp].copy()
				(xx[0], xx[2], xx[4]) = (xx[0] + n - 2, xx[2] + n - 2, xx[4] + n - 2)
				(xx[1], xx[3], xx[5]) = (xx[1] + col, xx[3] + col, xx[5] + col)
				res.append(xx)
				temp = dic1[temp]
				count += 1
		if m % 2 == 1 and matrix[n - 1][m - 1] == '1':
			temp = '0001'
			while temp != '0000':
				xx = dic[temp].copy()
				(xx[0], xx[2], xx[4]) = (xx[0] + n - 2, xx[2] + n - 2, xx[4] + n - 2)
				(xx[1], xx[3], xx[5]) = (xx[1] + m - 2, xx[3] + m - 2, xx[5] + m - 2)
				res.append(xx)
				temp = dic1[temp]
				count += 1
	print(count)
	for r in res:
		print(*r)
	T -= 1


def solve(n, m, mat):
	ops = []
	for i in range(n):
		for j in range(m):
			if mat[i][j] == '1':
				mat[i][j] = 1
			else:
				mat[i][j] = 0
	for i in range(n - 1, 1, -1):
		for j in range(m - 1, -1, -1):
			if mat[i][j] == 1:
				ops.append([i, j, i - 1, j, i - 1, j - 1 if j else j + 1])
				mat[i][j] ^= 1
				mat[i - 1][j] ^= 1
				mat[i - 1][j - 1 if j else j + 1] ^= 1
	for j in range(m - 1, 1, -1):
		if mat[0][j] and mat[1][j]:
			ops.append([0, j, 1, j, 0, j - 1])
			mat[0][j] ^= 1
			mat[1][j] ^= 1
			mat[0][j - 1] ^= 1
		elif mat[0][j]:
			ops.append([0, j, 0, j - 1, 1, j - 1])
			mat[0][j] ^= 1
			mat[0][j - 1] ^= 1
			mat[1][j - 1] ^= 1
		elif mat[1][j]:
			ops.append([1, j, 0, j - 1, 1, j - 1])
			mat[1][j] ^= 1
			mat[0][j - 1] ^= 1
			mat[1][j - 1] ^= 1
	if mat[0][0] ^ mat[0][1] ^ mat[1][0]:
		ops.append([0, 0, 0, 1, 1, 0])
	if mat[0][0] ^ mat[0][1] ^ mat[1][1]:
		ops.append([0, 0, 0, 1, 1, 1])
	if mat[0][0] ^ mat[1][0] ^ mat[1][1]:
		ops.append([0, 0, 1, 1, 1, 0])
	if mat[0][1] ^ mat[1][0] ^ mat[1][1]:
		ops.append([0, 1, 1, 0, 1, 1])
	for op in ops:
		for z in range(6):
			op[z] += 1
	return ops
t = int(input())
while t:
	(n, m) = map(int, input().split())
	res = []
	for _ in range(n):
		res.append(list(input()))
	k = solve(n, m, res)
	print(len(k))
	for x in k:
		print(*x)
	t -= 1


from sys import stdin, stdout
input = stdin.readline

def f1(a, b, c, d):
	global ans
	ans.append((a[1], a[2], b[1], b[2], c[1], c[2]))
	ans.append((a[1], a[2], b[1], b[2], d[1], d[2]))
	ans.append((a[1], a[2], d[1], d[2], c[1], c[2]))

def f2(a, b, c, d):
	global ans
	ans.append((a[1], a[2], b[1], b[2], d[1], d[2]))
	ans.append((a[1], a[2], d[1], d[2], c[1], c[2]))

def f3(a, b, c, d):
	global ans
	ans.append((a[1], a[2], d[1], d[2], c[1], c[2]))
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	mat = [[0 for x in range(m)] for y in range(n)]
	for i in range(n):
		x = input()
		for j in range(m):
			mat[i][j] = int(x[j])
	ans = []
	for i in range(n - 1):
		for j in range(m - 1):
			if i == n - 2 and j == m - 2:
				arr = [(mat[i][j], i, j), (mat[i + 1][j], i + 1, j), (mat[i][j + 1], i, j + 1), (mat[i + 1][j + 1], i + 1, j + 1)]
				arr = sorted(arr, key=lambda x: x[0], reverse=True)
				c = 0
				for k in arr:
					if k[0]:
						c += 1
				if c == 0:
					continue
				elif c == 1:
					f1(arr[0], arr[1], arr[2], arr[3])
				elif c == 2:
					f2(arr[3], arr[0], arr[1], arr[2])
				elif c == 3:
					f3(arr[2], arr[3], arr[1], arr[0])
				else:
					ans.append((i + 1, j + 1, i, j + 1, i + 1, j))
					f1(arr[0], arr[1], arr[2], arr[3])
				continue
			if j == m - 2:
				if mat[i][j] and mat[i][j + 1]:
					ans.append((i, j, i, j + 1, i + 1, j))
				elif not mat[i][j] and mat[i][j + 1]:
					ans.append((i + 1, j + 1, i, j + 1, i + 1, j))
				elif mat[i][j] and (not mat[i][j + 1]):
					ans.append((i, j, i + 1, j, i + 1, j + 1))
				else:
					continue
				for k in range(0, 6, 2):
					mat[ans[-1][k]][ans[-1][k + 1]] ^= 1
				continue
			if i == n - 2:
				if mat[i][j] and mat[i + 1][j]:
					ans.append((i, j, i + 1, j, i, j + 1))
				elif not mat[i][j] and mat[i + 1][j]:
					ans.append((i + 1, j + 1, i, j + 1, i + 1, j))
				elif mat[i][j] and (not mat[i + 1][j]):
					ans.append((i, j, i, j + 1, i + 1, j + 1))
				else:
					continue
				for k in range(0, 6, 2):
					mat[ans[-1][k]][ans[-1][k + 1]] ^= 1
				continue
			if mat[i][j]:
				ans.append((i, j, i, j + 1, i + 1, j))
			else:
				continue
			for k in range(0, 6, 2):
				mat[ans[-1][k]][ans[-1][k + 1]] ^= 1
	stdout.write(str(len(ans)) + '\n')
	for i in ans:
		for j in i:
			stdout.write(str(j + 1) + ' ')
		stdout.write('\n')


t = int(input())

def put(i, j):
	print(i, j, i + 1, j, i, j + 1)
	print(i, j, i + 1, j + 1, i, j + 1)
	print(i, j, i + 1, j + 1, i + 1, j)

def put2(i, j):
	print(i, j, i - 1, j, i, j + 1)
	print(i, j, i - 1, j + 1, i, j + 1)
	print(i, j, i - 1, j + 1, i - 1, j)

def put3(i, j):
	print(i, j, i + 1, j, i, j - 1)
	print(i, j, i + 1, j - 1, i, j - 1)
	print(i, j, i + 1, j - 1, i + 1, j)

def put4(i, j):
	print(i, j, i - 1, j, i, j - 1)
	print(i, j, i - 1, j - 1, i, j - 1)
	print(i, j, i - 1, j - 1, i - 1, j)
for _ in range(t):
	(n, m) = map(int, input().split())
	mat = []
	n1 = 0
	for l in range(n):
		mat.append(input())
		n1 += sum([int(x) for x in mat[l]])
	print(3 * n1)
	for i in range(n - 1):
		for j in range(m - 1):
			if mat[i][j] == '1':
				put(i + 1, j + 1)
	i = n - 1
	for j in range(m - 1):
		if mat[i][j] == '1':
			put2(i + 1, j + 1)
	j = m - 1
	for i in range(n - 1):
		if mat[i][j] == '1':
			put3(i + 1, j + 1)
	if mat[n - 1][m - 1] == '1':
		put4(n, m)


for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	mat = []
	for i in range(n):
		s = list(input())
		for i in range(len(s)):
			s[i] = int(s[i])
		mat.append(s)
	ops = []
	for i in range(n - 1):
		for j in range(m - 1):
			if j != m - 2:
				if mat[i][j] == 0 and mat[i + 1][j] == 0:
					continue
				elif mat[i][j] == 1 and mat[i + 1][j] == 1:
					ops.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					mat[i][j] = 0
					mat[i + 1][j] = 0
					mat[i][j + 1] ^= 1
				elif mat[i][j] == 1:
					ops.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					mat[i][j] = 0
					mat[i][j + 1] ^= 1
					mat[i + 1][j + 1] ^= 1
				elif mat[i + 1][j] == 1:
					ops.append([i + 2, j + 1, i + 1, j + 2, i + 2, j + 2])
					mat[i + 1][j] = 0
					mat[i][j + 1] ^= 1
					mat[i + 1][j + 1] ^= 1
			else:
				(a, b, c, d) = (mat[i][j], mat[i][j + 1], mat[i + 1][j], mat[i + 1][j + 1])
				cnt = 0
				for k in [a, b, c, d]:
					if k == 1:
						cnt += 1
				if cnt == 3:
					temp = []
					if mat[i][j] == 1:
						temp += [i + 1, j + 1]
					if mat[i + 1][j] == 1:
						temp += [i + 2, j + 1]
					if mat[i][j + 1] == 1:
						temp += [i + 1, j + 2]
					if mat[i + 1][j + 1] == 1:
						temp += [i + 2, j + 2]
					ops.append(temp)
					mat[i][j] = mat[i + 1][j] = mat[i][j + 1] = mat[i + 1][j + 1] = 0
				elif cnt == 2:
					(x, y, z1, z2) = ([], [], [], [])
					if mat[i][j] == 0:
						if x == []:
							x = [i + 1, j + 1]
						else:
							y = [i + 1, j + 1]
					if mat[i + 1][j] == 0:
						if x == []:
							x = [i + 2, j + 1]
						else:
							y = [i + 2, j + 1]
					if mat[i][j + 1] == 0:
						if x == []:
							x = [i + 1, j + 2]
						else:
							y = [i + 1, j + 2]
					if mat[i + 1][j + 1] == 0:
						if x == []:
							x = [i + 2, j + 2]
						else:
							y = [i + 2, j + 2]
					if mat[i][j] == 1:
						if z1 == []:
							z1 = [i + 1, j + 1]
						else:
							z2 = [i + 1, j + 1]
					if mat[i + 1][j] == 1:
						if z1 == []:
							z1 = [i + 2, j + 1]
						else:
							z2 = [i + 2, j + 1]
					if mat[i][j + 1] == 1:
						if z1 == []:
							z1 = [i + 1, j + 2]
						else:
							z2 = [i + 1, j + 2]
					if mat[i + 1][j + 1] == 1:
						if z1 == []:
							z1 = [i + 2, j + 2]
						else:
							z2 = [i + 2, j + 2]
					mat[i][j] = mat[i + 1][j] = mat[i][j + 1] = mat[i + 1][j + 1] = 0
					temp = x + y + z1
					ops.append(temp)
					temp = x + y + z2
					ops.append(temp)
				elif cnt == 1:
					if mat[i][j] == 1:
						x = [i + 1, j + 1]
						(z1, z2, z3) = ([i + 2, j + 1], [i + 1, j + 2], [i + 2, j + 2])
					if mat[i + 1][j] == 1:
						x = [i + 2, j + 1]
						(z1, z2, z3) = ([i + 1, j + 1], [i + 1, j + 2], [i + 2, j + 2])
					if mat[i + 1][j + 1] == 1:
						x = [i + 2, j + 2]
						(z1, z2, z3) = ([i + 2, j + 1], [i + 1, j + 2], [i + 1, j + 1])
					if mat[i][j + 1] == 1:
						x = [i + 1, j + 2]
						(z1, z2, z3) = ([i + 2, j + 1], [i + 2, j + 2], [i + 1, j + 1])
					temp = z1 + z2 + x
					ops.append(temp)
					temp = z1 + z3 + x
					ops.append(temp)
					temp = z2 + z3 + x
					ops.append(temp)
					mat[i][j] = mat[i + 1][j] = mat[i][j + 1] = mat[i + 1][j + 1] = 0
				elif cnt == 4:
					ops.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					mat[i][j] = 0
					mat[i][j + 1] = 0
					mat[i + 1][j] = 0
					if mat[i][j] == 1:
						x = [i + 1, j + 1]
						(z1, z2, z3) = ([i + 2, j + 1], [i + 1, j + 2], [i + 2, j + 2])
					if mat[i + 1][j] == 1:
						x = [i + 2, j + 1]
						(z1, z2, z3) = ([i + 1, j + 1], [i + 1, j + 2], [i + 2, j + 2])
					if mat[i + 1][j + 1] == 1:
						x = [i + 2, j + 2]
						(z1, z2, z3) = ([i + 2, j + 1], [i + 1, j + 2], [i + 1, j + 1])
					if mat[i][j + 1] == 1:
						x = [i + 1, j + 2]
						(z1, z2, z3) = ([i + 2, j + 1], [i + 2, j + 2], [i + 1, j + 1])
					temp = z1 + z2 + x
					ops.append(temp)
					temp = z1 + z3 + x
					ops.append(temp)
					temp = z2 + z3 + x
					ops.append(temp)
					mat[i][j] = mat[i + 1][j] = mat[i][j + 1] = mat[i + 1][j + 1] = 0
	print(len(ops))
	for i in ops:
		print(*i)


t = int(input())
for test in range(t):
	[n, m] = list(map(int, input().rstrip().split()))
	g = [[0 for _ in range(m)] for _ in range(n)]
	for i in range(n):
		s = input()
		for j in range(m):
			g[i][j] = int(s[j])
	o = 0
	op = list()
	for i in range(n - 2):
		for j in range(m - 2):
			if g[i][j] == 1:
				o += 1
				op.append([i, j, i + 1, j, i, j + 1])
				g[i + 1][j] = 1 - g[i + 1][j]
				g[i][j + 1] = 1 - g[i][j + 1]
	for i in range(n - 2):
		if g[i][m - 2] == 1:
			o += 1
			if g[i][m - 1] == 1:
				op.append([i, m - 2, i, m - 1, i + 1, m - 2])
				g[i + 1][m - 2] = 1 - g[i + 1][m - 2]
			else:
				op.append([i, m - 2, i + 1, m - 1, i + 1, m - 2])
				g[i + 1][m - 2] = 1 - g[i + 1][m - 2]
				g[i + 1][m - 1] = 1 - g[i + 1][m - 1]
		elif g[i][m - 1] == 1:
			o += 1
			op.append([i, m - 1, i + 1, m - 2, i + 1, m - 1])
			g[i + 1][m - 2] = 1 - g[i + 1][m - 2]
			g[i + 1][m - 1] = 1 - g[i + 1][m - 1]
	for j in range(m - 2):
		if g[n - 2][j] == 1:
			o += 1
			if g[n - 1][j] == 1:
				op.append([n - 2, j, n - 1, j, n - 2, j + 1])
				g[n - 2][j + 1] = 1 - g[n - 2][j + 1]
			else:
				op.append([n - 2, j, n - 1, j + 1, n - 2, j + 1])
				g[n - 2][j + 1] = 1 - g[n - 2][j + 1]
				g[n - 1][j + 1] = 1 - g[n - 1][j + 1]
		elif g[n - 1][j] == 1:
			o += 1
			op.append([n - 1, j, n - 2, j + 1, n - 1, j + 1])
			g[n - 2][j + 1] = 1 - g[n - 2][j + 1]
			g[n - 1][j + 1] = 1 - g[n - 1][j + 1]
	d = {0: [], 1: []}
	for i in range(n - 2, n):
		for j in range(m - 2, m):
			d[g[i][j]].append([i, j])
	if len(d[1]) == 4:
		o += 4
		op.append([n - 2, m - 2, n - 1, m - 2, n - 2, m - 1])
		op.append([n - 1, m - 1, n - 2, m - 2, n - 1, m - 2])
		op.append([n - 2, m - 1, n - 1, m - 1, n - 2, m - 2])
		op.append([n - 1, m - 2, n - 2, m - 1, n - 1, m - 1])
	elif len(d[1]) == 3:
		o += 1
		oper = []
		l = d[1]
		for (x, y) in l:
			oper.append(x)
			oper.append(y)
		op.append(oper)
	elif len(d[1]) == 2:
		o += 2
		oper = []
		l = d[0]
		for (x, y) in l:
			oper.append(x)
			oper.append(y)
		oper.append(d[1][0][0])
		oper.append(d[1][0][1])
		op.append(oper)
		oper = []
		for (x, y) in l:
			oper.append(x)
			oper.append(y)
		oper.append(d[1][1][0])
		oper.append(d[1][1][1])
		op.append(oper)
	elif len(d[1]) == 1:
		o += 3
		oper = []
		oper.append(d[1][0][0])
		oper.append(d[1][0][1])
		oper.append(d[0][0][0])
		oper.append(d[0][0][1])
		oper.append(d[0][1][0])
		oper.append(d[0][1][1])
		op.append(oper)
		newd = {0: [d[1][0], d[0][2]], 1: [d[0][0], d[0][1]]}
		d = newd
		oper = []
		l = d[0]
		for (x, y) in l:
			oper.append(x)
			oper.append(y)
		oper.append(d[1][0][0])
		oper.append(d[1][0][1])
		op.append(oper)
		oper = []
		for (x, y) in l:
			oper.append(x)
			oper.append(y)
		oper.append(d[1][1][0])
		oper.append(d[1][1][1])
		op.append(oper)
	print(o)
	for i in range(o):
		k = [x + 1 for x in op[i]]
		print(' '.join(map(str, k)))


t = int(input())
for tt in range(t):
	(n, m) = [int(x) for x in input().split(' ')]
	a = []
	ones = 0
	for i in range(n):
		ls = [int(x == '1') for x in input()]
		a.append(list(ls))
		ones += ls.count(1)
	print(3 * ones)
	for i in range(n):
		for j in range(m):
			if a[i][j] == 1:
				b1 = i == n - 1
				b2 = j == m - 1
				if b1 and b2:
					print(n, m, n, m - 1, n - 1, m)
					print(n, m, n, m - 1, n - 1, m - 1)
					print(n, m, n - 1, m, n - 1, m - 1)
				elif b1:
					print(n, j + 1, n, j + 2, n - 1, j + 1)
					print(n, j + 1, n, j + 2, n - 1, j + 2)
					print(n, j + 1, n - 1, j + 1, n - 1, j + 2)
				elif b2:
					print(i + 1, m, i + 1, m - 1, i + 2, m)
					print(i + 1, m, i + 1, m - 1, i + 2, m - 1)
					print(i + 1, m, i + 2, m, i + 2, m - 1)
				else:
					print(i + 1, j + 1, i + 1, j + 2, i + 2, j + 1)
					print(i + 1, j + 1, i + 1, j + 2, i + 2, j + 2)
					print(i + 1, j + 1, i + 2, j + 1, i + 2, j + 2)


import sys
t = int(input())
for test in range(t):
	[n, m] = list(map(int, input().split(' ')))
	a = []
	for i in range(n):
		a.append(input())
	res = []
	r = 0
	for i in range(n - 1):
		for j in range(m - 1):
			if a[i][j] == '1':
				res.append([str(i + 1), str(j + 1), str(i + 2), str(j + 1), str(i + 1), str(j + 2)])
				res.append([str(i + 1), str(j + 1), str(i + 2), str(j + 1), str(i + 2), str(j + 2)])
				res.append([str(i + 1), str(j + 1), str(i + 2), str(j + 2), str(i + 1), str(j + 2)])
				r = r + 3
		if a[i][m - 1] == '1':
			res.append([str(i + 1), str(m), str(i + 2), str(m), str(i + 1), str(m - 1)])
			res.append([str(i + 1), str(m), str(i + 2), str(m), str(i + 2), str(m - 1)])
			res.append([str(i + 1), str(m), str(i + 2), str(m - 1), str(i + 1), str(m - 1)])
			r = r + 3
	for j in range(m - 1):
		if a[n - 1][j] == '1':
			res.append([str(n), str(j + 1), str(n - 1), str(j + 1), str(n), str(j + 2)])
			res.append([str(n), str(j + 1), str(n - 1), str(j + 1), str(n - 1), str(j + 2)])
			res.append([str(n), str(j + 1), str(n - 1), str(j + 2), str(n), str(j + 2)])
			r = r + 3
	if a[n - 1][m - 1] == '1':
		res.append([str(n), str(m), str(n - 1), str(m), str(n), str(m - 1)])
		res.append([str(n), str(m), str(n - 1), str(m), str(n - 1), str(m - 1)])
		res.append([str(n), str(m), str(n - 1), str(m - 1), str(n), str(m - 1)])
		r = r + 3
	print(r)
	for i in range(r):
		print(' '.join(res[i]))


global mat

def calc(a, b, c, d, e, f):
	mat[a - 1][b - 1] = str(int(mat[a - 1][b - 1]) ^ 1)
	mat[c - 1][d - 1] = str(int(mat[c - 1][d - 1]) ^ 1)
	mat[e - 1][f - 1] = str(int(mat[e - 1][f - 1]) ^ 1)
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	mat = []
	for i in range(n):
		row = list(input())
		mat.append(row)
	k = 0
	ans = []
	for i in range(1, n):
		for j in range(1, m):
			if i == n - 1 and j == m - 1:
				temp = int(mat[i - 1][j - 1]) + int(mat[i][j - 1]) + int(mat[i - 1][j]) + int(mat[i][j])
				tlist = [[i - 1, j - 1], [i, j - 1], [i - 1, j], [i, j]]
				if temp != 0:
					if temp == 4:
						k += 1
						ans.append([i, j, i + 1, j, i, j + 1])
						calc(i, j, i + 1, j, i, j + 1)
						temp = 1
					if temp == 1:
						k += 1
						tans = []
						zc = 0
						for x in tlist:
							if mat[x[0]][x[1]] == '1':
								tans.append(x[0] + 1)
								tans.append(x[1] + 1)
							elif zc < 2:
								tans.append(x[0] + 1)
								tans.append(x[1] + 1)
								zc += 1
						ans.append(tans)
						calc(tans[0], tans[1], tans[2], tans[3], tans[4], tans[5])
						temp = 2
					if temp == 2:
						k += 1
						tans = []
						zo = 0
						for x in tlist:
							if mat[x[0]][x[1]] == '1' and zo < 1:
								tans.append(x[0] + 1)
								tans.append(x[1] + 1)
								zo += 1
							elif mat[x[0]][x[1]] == '0':
								tans.append(x[0] + 1)
								tans.append(x[1] + 1)
						ans.append(tans)
						calc(tans[0], tans[1], tans[2], tans[3], tans[4], tans[5])
						temp = 3
					if temp == 3:
						k += 1
						tans = []
						for x in tlist:
							if mat[x[0]][x[1]] == '1':
								tans.append(x[0] + 1)
								tans.append(x[1] + 1)
						ans.append(tans)
						calc(tans[0], tans[1], tans[2], tans[3], tans[4], tans[5])
			elif i == n - 1:
				if mat[i - 1][j - 1] == '1' and mat[i][j - 1] == '1':
					k += 1
					ans.append([i, j, i + 1, j, i, j + 1])
					calc(i, j, i + 1, j, i, j + 1)
				elif mat[i - 1][j - 1] == '1' and mat[i][j - 1] == '0':
					k += 1
					ans.append([i, j, i, j + 1, i + 1, j + 1])
					calc(i, j, i, j + 1, i + 1, j + 1)
				elif mat[i - 1][j - 1] == '0' and mat[i][j - 1] == '1':
					k += 1
					ans.append([i + 1, j, i, j + 1, i + 1, j + 1])
					calc(i + 1, j, i, j + 1, i + 1, j + 1)
			elif j == m - 1:
				if mat[i - 1][j - 1] == '1' and mat[i - 1][j] == '1':
					k += 1
					ans.append([i, j, i, j + 1, i + 1, j])
					calc(i, j, i, j + 1, i + 1, j)
				elif mat[i - 1][j - 1] == '1' and mat[i - 1][j] == '0':
					k += 1
					ans.append([i, j, i + 1, j, i + 1, j + 1])
					calc(i, j, i + 1, j, i + 1, j + 1)
				elif mat[i - 1][j - 1] == '0' and mat[i - 1][j] == '1':
					k += 1
					ans.append([i, j + 1, i + 1, j, i + 1, j + 1])
					calc(i, j + 1, i + 1, j, i + 1, j + 1)
			elif mat[i - 1][j - 1] == '1':
				k += 1
				ans.append([i, j, i, j + 1, i + 1, j])
				calc(i, j, i, j + 1, i + 1, j)
	print(k)
	for i in ans:
		for j in i:
			print(j, end=' ')
		print()


import sys
import os
from io import BytesIO, IOBase
from typing import AsyncIterable
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	return [[default for j in range(m)] for i in range(n)]

def arr3D(n, m, r, default=0):
	return [[[default for k in range(r)] for j in range(m)] for i in range(n)]

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

class DSU:

	def __init__(self, length):
		self.length = length
		self.parent = [-1] * self.length

	def getParent(self, node, start):
		if node >= self.length:
			return False
		if self.parent[node] < 0:
			if start != node:
				self.parent[start] = node
			return node
		return self.getParent(self.parent[node], start)

	def union(self, node1, node2):
		parent1 = self.getParent(node1, node1)
		parent2 = self.getParent(node2, node2)
		if parent1 == parent2:
			return False
		elif self.parent[parent1] <= self.parent[parent2]:
			self.parent[parent1] += self.parent[parent2]
			self.parent[parent2] = parent1
		else:
			self.parent[parent2] += self.parent[parent1]
			self.parent[parent1] = parent2
		return True

	def getCount(self, node):
		return -self.parent[self.getParent(node, node)]

def solve(n, m, lst):
	ans = []
	for i in range(n):
		for j in range(m):
			if lst[i][j] == '1':
				a = i + 1
				b = j + 1
				if i != 0 and j != m - 1:
					ans.append([a, b, a - 1, b, a - 1, b + 1])
					ans.append([a, b, a, b + 1, a - 1, b + 1])
					ans.append([a, b, a - 1, b, a, b + 1])
					lst[i][j] = '0'
				elif i != 0 and j != 0:
					ans.append([a, b, a - 1, b - 1, a, b - 1])
					ans.append([a, b, a - 1, b, a - 1, b - 1])
					ans.append([a, b, a - 1, b, a, b - 1])
					lst[i][j] = '0'
				elif i != n - 1 and j != m - 1:
					ans.append([a, b, a, b + 1, a + 1, b + 1])
					ans.append([a, b, a + 1, b, a + 1, b + 1])
					ans.append([a, b, a + 1, b, a, b + 1])
					lst[i][j] = '0'
				elif i != n - 1 and j != 0:
					ans.append([a, b, a + 1, b, a + 1, b - 1])
					ans.append([a, b, a, b - 1, a + 1, b - 1])
					ans.append([a, b, a + 1, b, a, b - 1])
					lst[i][j] = '0'
	print(len(ans))
	for i in ans:
		print(i[0], i[1], i[2], i[3], i[4], i[5])
for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	lst = []
	for i in range(n):
		lst.append(list(input()))
	solve(n, m, lst)


def nei(i, j):
	ans = []
	ans += [i + 1 if i % 2 else i - 1, j]
	ans += [i, j + 1 if j % 2 else j - 1]
	return ans

def nei2(i, j):
	ans = []
	ans += [i + 1 if i % 2 else i - 1, j]
	ans += [i, j + 1 if not j % 2 else j - 1]
	return ans

def nei3(i, j):
	ans = []
	ans += [i + 1 if not i % 2 else i - 1, j]
	ans += [i, j + 1 if j % 2 else j - 1]
	return ans
for _ in range(int(input())):
	(m, n) = map(int, input().split())
	A = [[0] * (n + 1)] + [[0] + list(map(int, list(input()))) for _ in range(m)]
	ans = []
	for i in range(1, m, 2):
		for j in range(1, n, 2):
			tmp = [A[i][j:j + 2], A[i + 1][j:j + 2]]
			if tmp == [[0, 0], [0, 0]]:
				continue
			if tmp == [[1, 1], [1, 1]]:
				ans.append([i, j, i, j + 1, i + 1, j])
				ans.append([i, j, i + 1, j, i + 1, j + 1])
				ans.append([i + 1, j, i + 1, j + 1, i, j + 1])
				ans.append([i, j, i, j + 1, i + 1, j + 1])
			elif tmp[0].count(1) + tmp[1].count(1) == 3:
				for (x, y) in [(i, j), (i, j + 1), (i + 1, j), (i + 1, j + 1)]:
					if not A[x][y]:
						cur = [[i, j], [i, j + 1], [i + 1, j], [i + 1, j + 1]]
						cur.remove([x, y])
						cur2 = []
						for c in cur:
							cur2 += c
						ans.append(cur2)
						break
			elif tmp[0].count(1) + tmp[1].count(1) == 1:
				for (x, y) in [(i, j), (i, j + 1), (i + 1, j), (i + 1, j + 1)]:
					if A[x][y]:
						nn = nei(x, y)
						(a, b, c, d) = nn
						ans.append([x, y] + nn)
						ans.append([a, b] + nei(a, b))
						ans.append([c, d] + nei(c, d))
						break
			elif tmp[0].count(1) + tmp[1].count(1) == 2:
				if sum(tmp[0]) == 1 and tmp[0][0] + tmp[1][0] == 1:
					if tmp[0][0]:
						ans.append([i, j, i + 1, j, i, j + 1])
						ans.append([i, j + 1, i + 1, j, i + 1, j + 1])
					else:
						ans.append([i, j, i, j + 1, i + 1, j + 1])
						ans.append([i, j, i + 1, j, i + 1, j + 1])
				else:
					for (x, y) in [(i, j), (i, j + 1), (i + 1, j), (i + 1, j + 1)]:
						if not A[x][y]:
							nn = nei(x, y)
							ans.append([x, y] + nn)
	if n % 2:
		j = n - 1
		for i in range(1, m, 2):
			tmp = [[0, A[i][-1]], [0, A[i + 1][-1]]]
			A[i][-2] = A[i + 1][-2] = 0
			if tmp == [[0, 0], [0, 0]]:
				continue
			elif tmp[0].count(1) + tmp[1].count(1) == 1:
				for (x, y) in [(i, j), (i, j + 1), (i + 1, j), (i + 1, j + 1)]:
					if A[x][y]:
						nn = nei2(x, y)
						(a, b, c, d) = nn
						ans.append([x, y] + nn)
						ans.append([a, b] + nei2(a, b))
						ans.append([c, d] + nei2(c, d))
						break
			elif tmp[0].count(1) + tmp[1].count(1) == 2:
				if sum(tmp[0]) == 1 and tmp[0][0] + tmp[1][0] == 1:
					if tmp[0][0]:
						ans.append([i, j, i + 1, j, i, j + 1])
						ans.append([i, j + 1, i + 1, j, i + 1, j + 1])
					else:
						ans.append([i, j, i, j + 1, i + 1, j + 1])
						ans.append([i, j, i + 1, j, i + 1, j + 1])
				else:
					for (x, y) in [(i, j), (i, j + 1), (i + 1, j), (i + 1, j + 1)]:
						if not A[x][y]:
							nn = nei2(x, y)
							ans.append([x, y] + nn)
	if m % 2:
		i = m - 1
		for j in range(1, n, 2):
			tmp = [[0, 0], A[m][j:j + 2]]
			A[-2][j] = A[-2][j + 1] = 0
			if tmp == [[0, 0], [0, 0]]:
				continue
			elif tmp[0].count(1) + tmp[1].count(1) == 1:
				for (x, y) in [(i, j), (i, j + 1), (i + 1, j), (i + 1, j + 1)]:
					if A[x][y]:
						nn = nei3(x, y)
						(a, b, c, d) = nn
						ans.append([x, y] + nn)
						ans.append([a, b] + nei3(a, b))
						ans.append([c, d] + nei3(c, d))
						break
			elif tmp[0].count(1) + tmp[1].count(1) == 2:
				if sum(tmp[0]) == 1 and tmp[0][0] + tmp[1][0] == 1:
					if tmp[0][0]:
						ans.append([i, j, i + 1, j, i, j + 1])
						ans.append([i, j + 1, i + 1, j, i + 1, j + 1])
					else:
						ans.append([i, j, i, j + 1, i + 1, j + 1])
						ans.append([i, j, i + 1, j, i + 1, j + 1])
				else:
					for (x, y) in [(i, j), (i, j + 1), (i + 1, j), (i + 1, j + 1)]:
						if not A[x][y]:
							nn = nei3(x, y)
							ans.append([x, y] + nn)
	if n % 2 and m % 2 and A[-1][-1]:
		ans.append([m, n, m - 1, n, m, n - 1])
		ans.append([m - 1, n - 1, m - 1, n, m, n])
		ans.append([m - 1, n - 1, m, n - 1, m, n])
	print(len(ans))
	for a in ans:
		print(*a)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	arr = []
	ans = []
	for i in range(n):
		arr.append([])
		temp = input().rstrip()
		for char in temp:
			if char == '1':
				arr[i].append(True)
			else:
				arr[i].append(False)
	for i in range(n - 1, 1, -1):
		for j in range(m):
			if arr[i][j]:
				if j == m - 1:
					arr[i][j] = not arr[i][j]
					arr[i - 1][j] = not arr[i - 1][j]
					arr[i - 1][j - 1] = not arr[i - 1][j - 1]
					ans.append((i, j, i - 1, j, i - 1, j - 1))
				else:
					arr[i][j] = not arr[i][j]
					arr[i - 1][j] = not arr[i - 1][j]
					arr[i - 1][j + 1] = not arr[i - 1][j + 1]
					ans.append((i, j, i - 1, j, i - 1, j + 1))
	if m & 1:
		if arr[0][m - 1]:
			arr[0][m - 1] = not arr[0][m - 1]
			arr[0][m - 2] = not arr[0][m - 2]
			arr[1][m - 2] = not arr[1][m - 2]
			ans.append((0, m - 1, 0, m - 2, 1, m - 2))
		if arr[1][m - 1]:
			arr[1][m - 1] = not arr[1][m - 1]
			arr[1][m - 2] = not arr[1][m - 2]
			arr[0][m - 2] = not arr[0][m - 2]
			ans.append((1, m - 1, 1, m - 2, 0, m - 2))
	for i in range(0, m - 1, 2):
		count1 = 0
		ones = []
		for x in range(2):
			for y in range(2):
				if arr[y][i + x]:
					ones.append((y, i + x))
					count1 += 1
		if count1 == 0:
			pass
		if count1 == 1:
			x = ones[0][0]
			y = ones[0][1]
			count1 = 2
			ans.append((x, y, x ^ 1, y, x, y ^ 1))
			ones = [(x ^ 1, y), (x, y ^ 1)]
			arr[x][y] = not arr[x][y]
			arr[x ^ 1][y] = not arr[x ^ 1][y]
			arr[x][y ^ 1] = not arr[x][y ^ 1]
		if count1 == 2:
			if ones[0][0] == ones[1][0] or ones[0][1] == ones[1][1]:
				ones = [(ones[0][0] ^ 1, ones[0][1] ^ 1), (ones[1][0] ^ 1, ones[1][1] ^ 1)]
			for one in ones:
				x = one[0]
				y = one[1]
				arr[x][y] = not arr[x][y]
				arr[x ^ 1][y] = not arr[x ^ 1][y]
				arr[x][y ^ 1] = not arr[x][y ^ 1]
				ans.append((x, y, x ^ 1, y, x, y ^ 1))
		if count1 == 3:
			arr[ones[0][0]][ones[0][1]] = not arr[ones[0][0]][ones[0][1]]
			arr[ones[1][0]][ones[1][1]] = not arr[ones[1][0]][ones[1][1]]
			arr[ones[2][0]][ones[2][1]] = not arr[ones[2][0]][ones[2][1]]
			ans.append((ones[0][0], ones[0][1], ones[1][0], ones[1][1], ones[2][0], ones[2][1]))
		if count1 == 4:
			for x in range(2):
				for y in range(2):
					arr[x][y + i] = not arr[x][y + i]
					arr[x ^ 1][y + i] = not arr[x ^ 1][y + i]
					arr[x][y ^ 1 + i] = not arr[x][y ^ 1 + i]
					ans.append((x, y + i, x ^ 1, y + i, x, y ^ 1 + i))
	print(len(ans))
	for move in ans:
		for ele in move:
			print(ele + 1, end=' ')
		print()


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = [[-1 for i in range(m)] for j in range(n)]
	for z in range(n):
		a[z] = list(input())
	c = 0
	ans = []
	for z in range(n):
		for y in range(m):
			if a[z][y] == '1':
				c += 1
				if y + 1 < m:
					s = 1
				else:
					s = -1
				if z + 1 < n:
					d = 1
				else:
					d = -1
				ans.append([z + 1, y + 1, z + 1, y + s + 1, z + d + 1, y + 1])
				ans.append([z + 1, y + 1, z + 1, y + s + 1, z + d + 1, y + s + 1])
				ans.append([z + 1, y + 1, z + d + 1, y + 1, z + d + 1, y + s + 1])
	print(3 * c)
	for z in range(3 * c):
		print(*ans[z])


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	li = [list(map(int, list(input()))) for __ in range(a)]
	ope = []
	for i in range(a - 1):
		for j in range(b):
			if li[i][j] == 1:
				if j == 0:
					ope.append([i, j, i + 1, j, i + 1, j + 1])
					li[i][j] = 1 - li[i][j]
					li[i + 1][j] = 1 - li[i + 1][j]
					li[i + 1][j + 1] = 1 - li[i + 1][j + 1]
				else:
					ope.append([i, j, i + 1, j, i + 1, j - 1])
					li[i][j] = 1 - li[i][j]
					li[i + 1][j] = 1 - li[i + 1][j]
					li[i + 1][j - 1] = 1 - li[i + 1][j - 1]
	t = a - 1
	for cl in range(b):
		if li[t][cl] == 1:
			if cl == 0:
				ope.append([t, cl, t - 1, cl, t - 1, cl + 1])
				li[t][cl] = 1 - li[t][cl]
				ope.append([t, cl, t, cl + 1, t - 1, cl + 1])
				ope.append([t - 1, cl, t, cl, t, cl + 1])
			else:
				ope.append([t, cl, t, cl - 1, t - 1, cl - 1])
				li[t][cl] = 1 - li[t][cl]
				ope.append([t, cl, t - 1, cl, t - 1, cl - 1])
				ope.append([t, cl - 1, t, cl, t - 1, cl])
	print(len(ope))
	for ele in ope:
		print(*list(map(lambda i: i + 1, ele)))


def ans(i, j):
	x = i + 1 if i < n else i - 1
	y = j + 1 if j < m else j - 1
	print(i, j, i, y, x, y)
	print(i, j, x, j, x, y)
	print(i, j, i, y, x, j)
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = [list(map(int, list(input()))) for i in range(n)]
	print(sum((sum(z) for z in l)) * 3)
	for i in range(n):
		for j in range(m):
			if l[i][j] == 1:
				ans(i + 1, j + 1)


a = []

def solve(i, j):
	if a[i][j] == 0 and a[i + 1][j + 1] == 0 and (a[i + 1][j] == 0) and (a[i][j + 1] == 0):
		return []
	if a[i][j] == 1 and a[i + 1][j + 1] == 0 and (a[i + 1][j] == 0) and (a[i][j + 1] == 0):
		return [(0, 0, 0, 1, 1, 0), (0, 0, 0, 1, 1, 1), (0, 0, 1, 0, 1, 1)]
	if a[i][j] == 0 and a[i + 1][j + 1] == 0 and (a[i + 1][j] == 0) and (a[i][j + 1] == 1):
		return [(0, 0, 0, 1, 1, 1), (1, 0, 0, 0, 0, 1), (1, 1, 1, 0, 0, 1)]
	if a[i][j] == 0 and a[i + 1][j + 1] == 0 and (a[i + 1][j] == 1) and (a[i][j + 1] == 0):
		return [(0, 0, 1, 0, 1, 1), (0, 0, 0, 1, 1, 0), (0, 1, 1, 0, 1, 1)]
	if a[i][j] == 0 and a[i + 1][j + 1] == 1 and (a[i + 1][j] == 0) and (a[i][j + 1] == 0):
		return [(0, 1, 1, 0, 1, 1), (0, 0, 0, 1, 1, 1), (0, 0, 1, 0, 1, 1)]
	if a[i][j] == 1 and a[i + 1][j + 1] == 0 and (a[i + 1][j] == 0) and (a[i][j + 1] == 1):
		return [(0, 1, 1, 0, 1, 1), (0, 0, 1, 0, 1, 1)]
	if a[i][j] == 0 and a[i + 1][j + 1] == 1 and (a[i + 1][j] == 1) and (a[i][j + 1] == 0):
		return [(0, 0, 0, 1, 1, 0), (0, 0, 0, 1, 1, 1)]
	if a[i][j] == 1 and a[i + 1][j + 1] == 0 and (a[i + 1][j] == 1) and (a[i][j + 1] == 0):
		return [(0, 0, 0, 1, 1, 1), (0, 1, 1, 0, 1, 1)]
	if a[i][j] == 0 and a[i + 1][j + 1] == 1 and (a[i + 1][j] == 0) and (a[i][j + 1] == 1):
		return [(0, 0, 1, 0, 1, 1), (0, 0, 0, 1, 1, 0)]
	if a[i][j] == 1 and a[i + 1][j + 1] == 1 and (a[i + 1][j] == 0) and (a[i][j + 1] == 0):
		return [(0, 1, 1, 0, 1, 1), (0, 0, 0, 1, 1, 0)]
	if a[i][j] == 0 and a[i + 1][j + 1] == 0 and (a[i + 1][j] == 1) and (a[i][j + 1] == 1):
		return [(0, 0, 1, 0, 1, 1), (0, 0, 0, 1, 1, 1)]
	if a[i][j] == 1 and a[i + 1][j + 1] == 1 and (a[i + 1][j] == 1) and (a[i][j + 1] == 0):
		return [(0, 0, 1, 0, 1, 1)]
	if a[i][j] == 1 and a[i + 1][j + 1] == 1 and (a[i + 1][j] == 0) and (a[i][j + 1] == 1):
		return [(0, 0, 1, 1, 0, 1)]
	if a[i][j] == 1 and a[i + 1][j + 1] == 0 and (a[i + 1][j] == 1) and (a[i][j + 1] == 1):
		return [(0, 0, 1, 0, 0, 1)]
	if a[i][j] == 0 and a[i + 1][j + 1] == 1 and (a[i + 1][j] == 1) and (a[i][j + 1] == 1):
		return [(0, 1, 1, 0, 1, 1)]
	if a[i][j] == 1 and a[i + 1][j + 1] == 1 and (a[i + 1][j] == 1) and (a[i][j + 1] == 1):
		return [(0, 0, 0, 1, 1, 1), (0, 0, 1, 0, 1, 1), (0, 0, 0, 1, 1, 0), (0, 1, 1, 0, 1, 1)]
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = []
	for i in range(n):
		b = input().strip()
		b = [int(j) for j in b]
		a.append(b)
	res = []
	for i in range(0, n - 1, 2):
		for j in range(0, m - 1, 2):
			val = solve(i, j)
			for tp in val:
				res.append([tp[0] + i + 1, tp[1] + j + 1, tp[2] + i + 1, tp[3] + j + 1, tp[4] + i + 1, tp[5] + j + 1])
				a[tp[0] + i][tp[1] + j] ^= 1
				a[tp[2] + i][tp[3] + j] ^= 1
				a[tp[4] + i][tp[5] + j] ^= 1
	j = m - 2
	for i in range(0, n - 1, 2):
		val = solve(i, m - 2)
		for tp in val:
			res.append([tp[0] + i + 1, tp[1] + j + 1, tp[2] + i + 1, tp[3] + j + 1, tp[4] + i + 1, tp[5] + j + 1])
			a[tp[0] + i][tp[1] + j] ^= 1
			a[tp[2] + i][tp[3] + j] ^= 1
			a[tp[4] + i][tp[5] + j] ^= 1
	i = n - 2
	for j in range(0, m - 1):
		val = solve(n - 2, j)
		for tp in val:
			res.append([tp[0] + i + 1, tp[1] + j + 1, tp[2] + i + 1, tp[3] + j + 1, tp[4] + i + 1, tp[5] + j + 1])
			a[tp[0] + i][tp[1] + j] ^= 1
			a[tp[2] + i][tp[3] + j] ^= 1
			a[tp[4] + i][tp[5] + j] ^= 1
	print(len(res))
	for i in res:
		print(*i)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(list(input()))
		for j in range(m):
			l[-1][j] = int(l[-1][j])
	ans = []
	for i in range(n):
		for j in range(m):
			if i == n - 1:
				if j == m - 1:
					if l[i][j] == 0:
						pass
					else:
						ans.append([i, j, i - 1, j, i - 1, j - 1])
						l[i - 1][j] ^= 1
						l[i][j] ^= 1
						l[i - 1][j - 1] ^= 1
						ans.append([i, j, i, j - 1, i - 1, j - 1])
						l[i][j] ^= 1
						l[i][j - 1] ^= 1
						l[i - 1][j - 1] ^= 1
						ans.append([i, j, i - 1, j, i, j - 1])
						l[i][j] ^= 1
						l[i][j - 1] ^= 1
						l[i - 1][j] ^= 1
				elif l[i][j] == 0:
					pass
				else:
					ans.append([i, j, i - 1, j, i - 1, j + 1])
					l[i - 1][j] ^= 1
					l[i][j] ^= 1
					l[i - 1][j + 1] ^= 1
					ans.append([i, j, i, j + 1, i - 1, j + 1])
					l[i][j] ^= 1
					l[i][j + 1] ^= 1
					l[i - 1][j + 1] ^= 1
					ans.append([i, j, i - 1, j, i, j + 1])
					l[i][j] ^= 1
					l[i][j + 1] ^= 1
					l[i - 1][j] ^= 1
			elif j == m - 1:
				if l[i][j] == 0:
					pass
				else:
					ans.append([i, j, i + 1, j, i + 1, j - 1])
					l[i + 1][j] ^= 1
					l[i][j] ^= 1
					l[i + 1][j - 1] ^= 1
					ans.append([i, j, i, j - 1, i + 1, j - 1])
					l[i][j] ^= 1
					l[i][j - 1] ^= 1
					l[i + 1][j - 1] ^= 1
					ans.append([i, j, i + 1, j, i, j - 1])
					l[i][j] ^= 1
					l[i][j - 1] ^= 1
					l[i + 1][j] ^= 1
			elif l[i][j] == 0:
				pass
			else:
				ans.append([i, j, i + 1, j, i + 1, j + 1])
				l[i + 1][j] ^= 1
				l[i][j] ^= 1
				l[i + 1][j + 1] ^= 1
				ans.append([i, j, i, j + 1, i + 1, j + 1])
				l[i][j] ^= 1
				l[i][j + 1] ^= 1
				l[i + 1][j + 1] ^= 1
				ans.append([i, j, i + 1, j, i, j + 1])
				l[i][j] ^= 1
				l[i][j + 1] ^= 1
				l[i + 1][j] ^= 1
	print(len(ans))
	for i in ans:
		for j in range(6):
			if j == 5:
				print(i[j] + 1, end='')
			else:
				print(i[j] + 1, end=' ')
		print()


for _ in range(int(input())):
	(rows, cols) = map(int, input().split())
	ss = [list(map(int, input())) for _ in range(rows)]
	count = 0
	res = []
	for y in range(rows - 1):
		if y % 2 and y < rows - 2:
			continue
		fixed = False
		for x in range(cols - 1):
			if not ss[y][x] and (not ss[y + 1][x]) and (x < cols - 2):
				fixed = False
				continue
			elif fixed and x < cols - 2:
				fixed = False
				continue
			var = sum(ss[y][x:x + 2]) + sum(ss[y + 1][x:x + 2])
			if var == 0:
				fixed = True
				continue
			if var == 4:
				count += 1
				ss[y + 1][x + 1] = 0
				ss[y][x + 1] = 0
				ss[y + 1][x] = 0
				res.append([y + 2, x + 2, y + 1, x + 2, y + 2, x + 1])
				var = 1
			if var == 1:
				if ss[y][x] == 1:
					cx = x
					cy = y
				elif ss[y][x + 1] == 1:
					cx = x + 1
					cy = y
				elif ss[y + 1][x] == 1:
					cx = x
					cy = y + 1
				else:
					cx = x + 1
					cy = y + 1
				ss[cy][cx] = 0
				ss[y + (1 - (cy - y))][cx] = 1
				ss[y + (1 - (cy - y))][x + (1 - (cx - x))] = 1
				count += 1
				res.append([cy + 1, cx + 1, y + (1 - (cy - y)) + 1, cx + 1, y + (1 - (cy - y)) + 1, x + (1 - (cx - x)) + 1])
				var = 2
			if var == 2:
				if ss[y][x] == 1:
					cx = x
					cy = y
				elif ss[y][x + 1] == 1:
					cx = x + 1
					cy = y
				elif ss[y + 1][x] == 1:
					cx = x
					cy = y + 1
				else:
					cx = x + 1
					cy = y + 1
				cres = []
				for cy2 in range(y, y + 2):
					for cx2 in range(x, x + 2):
						if ss[cy2][cx2] == 0:
							cres += [cy2 + 1, cx2 + 1]
							ss[cy2][cx2] = 1
				count += 1
				ss[cy][cx] = 0
				cres += [cy + 1, cx + 1]
				res.append(cres)
			count += 1
			cres = []
			for cy in range(y, y + 2):
				for cx in range(x, x + 2):
					if ss[cy][cx] == 1:
						cres += [cy + 1, cx + 1]
						ss[cy][cx] = 0
			res.append(cres)
			fixed = True
	print(count)
	for r in res:
		print(*r)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(list(input()))
	o = 0
	a = []
	for i in range(n):
		for j in range(m):
			if l[i][j] == '1' and j != m - 1 and (i != n - 1):
				o += 3
				a.append([i + 1, j + 1, i + 1, j + 1 + 1, i + 1 + 1, j + 1])
				a.append([i + 1, j + 1, i + 1, j + 1 + 1, i + 1 + 1, j + 1 + 1])
				a.append([i + 1, j + 1, i + 1 + 1, j + 1, i + 1 + 1, j + 1 + 1])
			elif l[i][j] == '1' and j == m - 1 and (i != n - 1):
				o += 3
				a.append([i + 1, j + 1, i + 1, j, i + 2, j + 1])
				a.append([i + 1, j + 1, i + 1, j, i + 2, j])
				a.append([i + 1, j + 1, i + 2, j + 1, i + 2, j])
			elif l[i][j] == '1' and i == n - 1 and (j != m - 1):
				o += 3
				a.append([i + 1, j + 1, i, j + 1, i + 1, j + 2])
				a.append([i + 1, j + 1, i + 1, j + 2, i, j + 2])
				a.append([i + 1, j + 1, i, j + 1, i, j + 2])
			elif l[i][j] == '1' and i == n - 1 and (j == m - 1):
				o += 3
				a.append([i + 1, j + 1, i + 1, j, i, j + 1])
				a.append([i + 1, j + 1, i + 1, j, i, j])
				a.append([i + 1, j + 1, i, j + 1, i, j])
	print(o)
	for i in range(len(a)):
		for j in range(6):
			print(a[i][j], end=' ')
		print('')


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(A, B, C, D) = ([0, 0], [0, 1], [1, 0], [1, 1])

def func(a, b, c, d):
	(a, b, c, d) = (int(a), int(b), int(c), int(d))
	x = a + b + c + d
	zz = A * (not a) + B * (not b) + C * (not c) + D * (not d)
	oo = A * a + B * b + C * c + D * d
	pr = []
	if x == 1:
		pr.append(oo + zz[2:])
		pr.append(oo + zz[:2] + zz[4:])
		pr.append(oo + zz[:4])
	elif x == 2:
		pr.append(zz + oo[:2])
		pr.append(zz + oo[2:])
	elif x == 3:
		pr.append(A * a + B * b + C * c + D * d)
	elif x == 4:
		pr.append(oo[2:])
		zz = oo[2:]
		oo = oo[:2]
		pr.append(oo + zz[2:])
		pr.append(oo + zz[:2] + zz[4:])
		pr.append(oo + zz[:4])
	return pr
(t,) = I()
for _ in range(t):
	(n, m) = I()
	l = []
	for i in range(n):
		l.append(list(input().strip()))
	an = []
	for i in range(0, n - 1):
		for j in range(0, m - 1):
			x = func(l[i][j], l[i][j + 1], l[i + 1][j], l[i + 1][j + 1])
			for pp in x:
				for k in range(6):
					pp[k] += 1 + i * (k % 2 == 0) + j * (k % 2)
			an += x
			(l[i][j], l[i][j + 1], l[i + 1][j], l[i + 1][j + 1]) = list('0000')
	print(len(an))
	for i in an:
		print(*i)


import io
import os
from collections import Counter, defaultdict, deque

def solve(R, C, grid):
	out = [[[0 for c in range(C)] for r in range(R)] for i in range(4)]

	def toggle(dir, coord):
		(r, c) = coord
		out[dir][r][c] ^= 1
		if dir == 0:
			grid[r][c] ^= 1
			grid[r][c + 1] ^= 1
			grid[r + 1][c] ^= 1
		if dir == 1:
			grid[r][c] ^= 1
			grid[r][c - 1] ^= 1
			grid[r + 1][c] ^= 1
		if dir == 2:
			grid[r][c] ^= 1
			grid[r - 1][c] ^= 1
			grid[r][c + 1] ^= 1
		if dir == 3:
			grid[r][c] ^= 1
			grid[r - 1][c] ^= 1
			grid[r][c - 1] ^= 1

	def toStr(dir, coord):
		(r, c) = coord
		r += 1
		c += 1
		line = []
		if dir == 0:
			line += [r, c]
			line += [r, c + 1]
			line += [r + 1, c]
		elif dir == 1:
			line += [r, c]
			line += [r, c - 1]
			line += [r + 1, c]
		elif dir == 2:
			line += [r, c]
			line += [r - 1, c]
			line += [r, c + 1]
		elif dir == 3:
			line += [r, c]
			line += [r - 1, c]
			line += [r, c - 1]
		return ' '.join(map(str, line))

	def fix(a, b, c, d):
		if grid[a[0]][a[1]]:
			toggle(0, a)
			toggle(1, b)
			toggle(2, c)
		if grid[b[0]][b[1]]:
			toggle(0, a)
			toggle(1, b)
			toggle(3, d)
		if grid[c[0]][c[1]]:
			toggle(0, a)
			toggle(2, c)
			toggle(3, d)
		if grid[d[0]][d[1]]:
			toggle(1, b)
			toggle(2, c)
			toggle(3, d)
	for r in range(R - 1):
		for c in range(C - 1):
			fix((r, c), (r, c + 1), (r + 1, c), (r + 1, c + 1))
	ans = []
	for i in range(4):
		for r in range(R):
			for c in range(C):
				if out[i][r][c]:
					ans.append(toStr(i, (r, c)))
	assert len(ans) <= 3 * R * C
	return str(len(ans)) + '\n' + '\n'.join(ans)
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N, M) = [int(x) for x in input().split()]
	grid = [[int(d) for d in input().decode().rstrip()] for i in range(N)]
	ans = solve(N, M, grid)
	print(ans)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	(cnt, A) = (0, [])
	for i in range(n):
		A.append(input())
		cnt += A[i].count('1')
	print(3 * cnt)
	for i in range(n):
		for j in range(m):
			if A[i][j] == '1':
				(x, y) = (1, 1)
				if i == n - 1:
					x = -1
				if j == m - 1:
					y = -1
				print(i + 1, j + 1, i + x + 1, j + 1, i + 1, j + y + 1)
				print(i + 1, j + 1, i + x + 1, j + y + 1, i + 1, j + y + 1)
				print(i + 1, j + 1, i + x + 1, j + 1, i + x + 1, j + y + 1)


from collections import defaultdict
t = int(input().strip())

def return_pos(x1, y1, x2, y2, x3, y3, x4, y4):
	return [[x1, y1, x2, y2, x3, y3], [x1, y1, x2, y2, x4, y4], [x1, y1, x3, y3, x4, y4]]
for _ in range(t):
	(n, m) = map(int, input().strip().split())
	nums = []
	for i in range(n):
		nums.append(input().strip())
	ans = []
	for i in range(n):
		for j in range(m):
			(x1, y1) = (i, j)
			if nums[i][j] == '1':
				if i >= n - 1:
					(x2, y2) = (i - 1, j)
					if j >= m - 1:
						(x3, y3) = (i - 1, j - 1)
						(x4, y4) = (i, j - 1)
					else:
						(x3, y3) = (i - 1, j + 1)
						(x4, y4) = (i, j + 1)
				elif j >= m - 1:
					(x2, y2) = (i, j - 1)
					(x3, y3) = (i + 1, j - 1)
					(x4, y4) = (i + 1, j)
				else:
					(x2, y2) = (i + 1, j)
					(x3, y3) = (i, j + 1)
					(x4, y4) = (i + 1, j + 1)
				ans.extend(return_pos(x1, y1, x2, y2, x3, y3, x4, y4))
	print(len(ans))
	for i in ans:
		print(' '.join((str(j + 1) for j in i)))


import sys
import math
t = int(input())

def fn(l, i, j, l2):
	if l == [1, 1, 1, 1]:
		l1 = [[i, j, i + 1, j, i, j + 1], [i, j + 1, i + 1, j + 1, i + 1, j], [i, j, i, j + 1, i + 1, j + 1], [i, j, i + 1, j, i + 1, j + 1]]
		l2 += l1
		return 4
	elif l == [1, 0, 0, 0]:
		l1 = [[i, j, i, j + 1, i + 1, j], [i, j, i, j + 1, i + 1, j + 1], [i, j, i + 1, j, i + 1, j + 1]]
		l2 += l1
		return 3
	elif l == [0, 1, 0, 0]:
		l1 = [[i, j, i, j + 1, i + 1, j + 1], [i, j, i, j + 1, i + 1, j], [i + 1, j, i, j + 1, i + 1, j + 1]]
		l2 += l1
		return 3
	elif l == [0, 0, 1, 0]:
		l1 = [[i, j, i + 1, j + 1, i + 1, j], [i, j, i, j + 1, i + 1, j], [i + 1, j, i, j + 1, i + 1, j + 1]]
		l2 += l1
		return 3
	elif l == [0, 0, 0, 1]:
		l1 = [[i + 1, j, i, j + 1, i + 1, j + 1], [i, j, i, j + 1, i + 1, j + 1], [i, j, i + 1, j, i + 1, j + 1]]
		l2 += l1
		return 3
	elif l == [1, 1, 0, 0]:
		l1 = [[i, j, i + 1, j, i + 1, j + 1], [i + 1, j, i + 1, j + 1, i, j + 1]]
		l2 += l1
		return 2
	elif l == [1, 0, 1, 0]:
		l1 = [[i, j, i, j + 1, i + 1, j + 1], [i + 1, j, i, j + 1, i + 1, j + 1]]
		l2 += l1
		return 2
	elif l == [1, 0, 0, 1]:
		l1 = [[i, j, i + 1, j, i, j + 1], [i + 1, j, i, j + 1, i + 1, j + 1]]
		l2 += l1
		return 2
	elif l == [0, 1, 1, 0]:
		l1 = [[i, j, i, j + 1, i + 1, j + 1], [i, j, i + 1, j, i + 1, j + 1]]
		l2 += l1
		return 2
	elif l == [0, 1, 0, 1]:
		l1 = [[i, j, i + 1, j, i, j + 1], [i, j, i + 1, j, i + 1, j + 1]]
		l2 += l1
		return 2
	elif l == [0, 0, 1, 1]:
		l1 = [[i, j, i + 1, j, i, j + 1], [i, j, i, j + 1, i + 1, j + 1]]
		l2 += l1
		return 2
	elif l == [0, 1, 1, 1]:
		l1 = [[i, j + 1, i + 1, j, i + 1, j + 1]]
		l2 += l1
		return 1
	elif l == [1, 0, 1, 1]:
		l1 = [[i, j, i + 1, j, i + 1, j + 1]]
		l2 += l1
		return 1
	elif l == [1, 1, 0, 1]:
		l1 = [[i, j, i, j + 1, i + 1, j + 1]]
		l2 += l1
		return 1
	elif l == [1, 1, 1, 0]:
		l1 = [[i, j, i + 1, j, i, j + 1]]
		l2 += l1
		return 1
	else:
		return 0
for w in range(t):
	(n, m) = (int(i) for i in input().split())
	l = []
	l2 = []
	s = 0
	for i in range(n):
		l.append(list(input()))
	if n % 2 == 0:
		if m % 2 == 0:
			for i in range(0, n, 2):
				for j in range(0, m, 2):
					l1 = [int(l[i][j]), int(l[i][j + 1]), int(l[i + 1][j]), int(l[i + 1][j + 1])]
					s += fn(l1, i + 1, j + 1, l2)
					(l[i][j], l[i + 1][j], l[i][j + 1], l[i + 1][j + 1]) = ('0', '0', '0', '0')
		else:
			for i in range(0, n, 2):
				for j in range(0, m - 1, 2):
					l1 = [int(l[i][j]), int(l[i][j + 1]), int(l[i + 1][j]), int(l[i + 1][j + 1])]
					s += fn(l1, i + 1, j + 1, l2)
					(l[i][j], l[i + 1][j], l[i][j + 1], l[i + 1][j + 1]) = ('0', '0', '0', '0')
				j += 1
				l1 = [int(l[i][j]), int(l[i][j + 1]), int(l[i + 1][j]), int(l[i + 1][j + 1])]
				s += fn(l1, i + 1, j + 1, l2)
				(l[i][j], l[i + 1][j], l[i][j + 1], l[i + 1][j + 1]) = ('0', '0', '0', '0')
	elif m % 2 == 0:
		for i in range(0, n - 1, 2):
			for j in range(0, m, 2):
				l1 = [int(l[i][j]), int(l[i][j + 1]), int(l[i + 1][j]), int(l[i + 1][j + 1])]
				s += fn(l1, i + 1, j + 1, l2)
				(l[i][j], l[i + 1][j], l[i][j + 1], l[i + 1][j + 1]) = ('0', '0', '0', '0')
		i += 1
		for j in range(0, m, 2):
			l1 = [int(l[i][j]), int(l[i][j + 1]), int(l[i + 1][j]), int(l[i + 1][j + 1])]
			s += fn(l1, i + 1, j + 1, l2)
			(l[i][j], l[i + 1][j], l[i][j + 1], l[i + 1][j + 1]) = ('0', '0', '0', '0')
	else:
		for i in range(0, n - 1, 2):
			for j in range(0, m - 1, 2):
				l1 = [int(l[i][j]), int(l[i][j + 1]), int(l[i + 1][j]), int(l[i + 1][j + 1])]
				s += fn(l1, i + 1, j + 1, l2)
				(l[i][j], l[i + 1][j], l[i][j + 1], l[i + 1][j + 1]) = ('0', '0', '0', '0')
			j += 1
			l1 = [int(l[i][j]), int(l[i][j + 1]), int(l[i + 1][j]), int(l[i + 1][j + 1])]
			s += fn(l1, i + 1, j + 1, l2)
			(l[i][j], l[i + 1][j], l[i][j + 1], l[i + 1][j + 1]) = ('0', '0', '0', '0')
		i += 1
		for j in range(0, m - 1, 2):
			l1 = [int(l[i][j]), int(l[i][j + 1]), int(l[i + 1][j]), int(l[i + 1][j + 1])]
			s += fn(l1, i + 1, j + 1, l2)
			(l[i][j], l[i + 1][j], l[i][j + 1], l[i + 1][j + 1]) = ('0', '0', '0', '0')
		j += 1
		l1 = [int(l[i][j]), int(l[i][j + 1]), int(l[i + 1][j]), int(l[i + 1][j + 1])]
		s += fn(l1, i + 1, j + 1, l2)
		(l[i][j], l[i + 1][j], l[i][j + 1], l[i + 1][j + 1]) = ('0', '0', '0', '0')
	print(s)
	for i in range(s):
		print(*l2[i])


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
mod2 = 998244353
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = ip()
for _ in range(t):
	(n, m) = inp()
	matrix = []
	for i in range(n):
		matrix.append([int(i) for i in input()])
	ans = []
	for i in range(n - 1):
		for j in range(m):
			if j != m - 1:
				if matrix[i][j] == 1:
					ans.append([i, j, i + 1, j, i + 1, j + 1])
					ans.append([i + 1, j, i + 1, j + 1, i, j + 1])
					matrix[i][j] = 0
					matrix[i][j + 1] = (matrix[i][j + 1] + 1) % 2
			elif matrix[i][j] == 1:
				ans.append([i, j, i, j - 1, i + 1, j])
				ans.append([i, j - 1, i + 1, j, i + 1, j - 1])
				matrix[i][j] = 0
				matrix[i + 1][j - 1] = (matrix[i + 1][j - 1] + 1) % 2
	for i in range(m - 1):
		if matrix[n - 1][i] == 1:
			ans.append([n - 1, i, n - 2, i, n - 2, i + 1])
			ans.append([n - 2, i, n - 2, i + 1, n - 1, i + 1])
			matrix[n - 1][i] = 0
			matrix[n - 1][i + 1] = (matrix[n - 1][i + 1] + 1) % 2
	if matrix[n - 1][m - 1] == 1:
		ans.append([n - 2, m - 2, n - 1, m - 2, n - 1, m - 1])
		ans.append([n - 1, m - 2, n - 1, m - 1, n - 2, m - 1])
		matrix[n - 2][m - 2] = (matrix[n - 2][m - 2] + 1) % 2
		matrix[n - 2][m - 1] = (matrix[n - 2][m - 1] + 1) % 2
		ans.append([n - 1, m - 1, n - 2, m - 1, n - 2, m - 2])
	a1 = []
	for i in range(len(ans)):
		a2 = [i + 1 for i in ans[i]]
		a1.append(a2)
	print(len(ans))
	for i in range(len(a1)):
		print(*a1[i])


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return [int(x) for x in input().split()]

def sapin():
	return [int(x) for x in input()]
t = int(input())
for _ in range(t):
	(n, m) = mapin()
	d = []
	for i in range(n):
		temp = sapin()
		d.append(temp)
	ans = []
	for i in range(n - 1):
		for j in range(m):
			if d[i][j]:
				if j == m - 1:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j])
					d[i][j] = 0
					d[i + 1][j] ^= 1
					d[i + 1][j - 1] ^= 1
				else:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					d[i][j] = 0
					d[i + 1][j] ^= 1
					d[i + 1][j + 1] ^= 1
	i = n - 1
	for j in range(m):
		if d[-1][j]:
			if j == m - 1:
				ans.append([i + 1, j + 1, i + 1, j, i, j + 1])
				d[i][j] ^= 1
				d[i][j - 1] ^= 1
				d[i - 1][j] ^= 1
				ans.append([i + 1, j + 1, i, j, i, j + 1])
				d[i][j] ^= 1
				d[i - 1][j - 1] ^= 1
				d[i - 1][j] ^= 1
				ans.append([i + 1, j + 1, i + 1, j, i, j])
				d[i][j] ^= 1
				d[i][j - 1] ^= 1
				d[i - 1][j - 1] ^= 1
			else:
				ans.append([i + 1, j + 1, i, j + 1, i, j + 2])
				d[i][j] = 0
				d[i - 1][j] ^= 1
				d[i - 1][j + 1] ^= 1
				ans.append([i + 1, j + 2, i, j + 1, i, j + 2])
				d[i][j + 1] ^= 1
				d[i - 1][j] ^= 1
				d[i - 1][j + 1] ^= 1
	print(len(ans))
	for i in ans:
		print(*i)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = list()
	for i in range(n):
		l.append(input())
	ans = list()
	for i in range(n):
		for j in range(m):
			if l[i][j] == '1':
				if i != n - 1 and j != m - 1:
					ans.append((i, j, i + 1, j, i + 1, j + 1))
					ans.append((i, j, i, j + 1, i + 1, j + 1))
					ans.append((i, j, i + 1, j, i, j + 1))
				elif i == n - 1 and j != m - 1:
					ans.append((i, j, i, j + 1, i - 1, j + 1))
					ans.append((i, j, i - 1, j, i - 1, j + 1))
					ans.append((i, j, i - 1, j, i, j + 1))
				elif i != n - 1 and j == m - 1:
					ans.append((i, j, i, j - 1, i + 1, j - 1))
					ans.append((i, j, i + 1, j, i + 1, j - 1))
					ans.append((i, j, i, j - 1, i + 1, j))
				else:
					ans.append((i, j, i, j - 1, i - 1, j - 1))
					ans.append((i, j, i - 1, j, i - 1, j - 1))
					ans.append((i, j, i, j - 1, i - 1, j))
	print(len(ans))
	for i in range(len(ans)):
		for j in range(6):
			print(ans[i][j] + 1, end=' ')
		print()


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass

def pmat(A):
	for ele in A:
		print(*ele, end='\n')

def seive():
	prime = [1 for i in range(10 ** 6 + 1)]
	prime[0] = 0
	prime[1] = 0
	for i in range(10 ** 6 + 1):
		if prime[i]:
			for j in range(2 * i, 10 ** 6 + 1, i):
				prime[j] = 0
	return prime
for _ in range(int(input())):
	(m, n) = map(int, input().split())
	A = [[0] * (n + 1)] + [[0] + list(map(int, list(input()))) for _ in range(m)]
	ans = []
	for i in range(1, m - 1):
		for j in range(1, n + 1):
			if A[i][j]:
				ans.append([i, j, i + 1, j, i + 1, j + 1 if j < n else j - 1])
				A[i][j] ^= 1
				A[i + 1][j] ^= 1
				A[i + 1][j + 1 if j < n else j - 1] ^= 1
	for j in range(1, n - 1):
		if A[m - 1][j] and A[m][j]:
			ans.append([m - 1, j, m, j, m - 1, j + 1])
			A[m - 1][j] ^= 1
			A[m][j] ^= 1
			A[m - 1][j + 1] ^= 1
		elif A[m - 1][j]:
			ans.append([m - 1, j, m - 1, j + 1, m, j + 1])
			A[m - 1][j] ^= 1
			A[m - 1][j + 1] ^= 1
			A[m][j + 1] ^= 1
		elif A[m][j]:
			ans.append([m, j, m, j + 1, m - 1, j + 1])
			A[m][j] ^= 1
			A[m][j + 1] ^= 1
			A[m - 1][j + 1] ^= 1
	if A[m - 1][n - 1] ^ A[m - 1][n] ^ A[m][n - 1]:
		ans.append([m - 1, n - 1, m - 1, n, m, n - 1])
	if A[m - 1][n - 1] ^ A[m][n - 1] ^ A[m][n]:
		ans.append([m - 1, n - 1, m, n - 1, m, n])
	if A[m][n - 1] ^ A[m][n] ^ A[m - 1][n]:
		ans.append([m, n - 1, m, n, m - 1, n])
	if A[m][n] ^ A[m - 1][n] ^ A[m - 1][n - 1]:
		ans.append([m, n, m - 1, n, m - 1, n - 1])
	print(len(ans))
	for a in ans:
		print(*a)
endtime = time.time()


def main():
	(n, m) = map(int, input().split())
	a = [[] for i in range(n)]
	for i in range(n):
		a[i] = list(input())
	x1 = []
	x2 = []
	x3 = []
	y1 = []
	y2 = []
	y3 = []
	if a[0][m - 1] == '1':
		y1.append(0)
		x1.append(m - 1)
		y2.append(0)
		x2.append(m - 2)
		y3.append(1)
		x3.append(m - 1)
		y1.append(0)
		x1.append(m - 1)
		y2.append(0)
		x2.append(m - 2)
		y3.append(1)
		x3.append(m - 2)
		y1.append(0)
		x1.append(m - 1)
		y2.append(1)
		x2.append(m - 2)
		y3.append(1)
		x3.append(m - 1)
	if a[n - 1][0] == '1':
		y1.append(n - 1)
		x1.append(0)
		x2.append(0)
		y2.append(n - 2)
		x3.append(1)
		y3.append(n - 1)
		y1.append(n - 1)
		x1.append(0)
		x2.append(0)
		y2.append(n - 2)
		x3.append(1)
		y3.append(n - 2)
		y1.append(n - 1)
		x1.append(0)
		x2.append(1)
		y2.append(n - 2)
		x3.append(1)
		y3.append(n - 1)
	for i in range(n):
		for j in range(m):
			if i == 0 and j == m - 1 or (i == n - 1 and j == 0):
				continue
			if a[i][j] == '0':
				continue
			if i == n - 1 or j == m - 1:
				y1.append(i)
				x1.append(j)
				y2.append(i - 1)
				x2.append(j)
				y3.append(i)
				x3.append(j - 1)
				y1.append(i)
				x1.append(j)
				y2.append(i - 1)
				x2.append(j)
				y3.append(i - 1)
				x3.append(j - 1)
				y1.append(i)
				x1.append(j)
				y2.append(i)
				x2.append(j - 1)
				y3.append(i - 1)
				x3.append(j - 1)
				continue
			y1.append(i)
			x1.append(j)
			y2.append(i + 1)
			x2.append(j)
			y3.append(i)
			x3.append(j + 1)
			y1.append(i)
			x1.append(j)
			y2.append(i + 1)
			x2.append(j)
			y3.append(i + 1)
			x3.append(j + 1)
			y1.append(i)
			x1.append(j)
			y2.append(i)
			x2.append(j + 1)
			y3.append(i + 1)
			x3.append(j + 1)
	print(len(x1))
	for i in range(len(x1)):
		print(y1[i] + 1, x1[i] + 1, y2[i] + 1, x2[i] + 1, y3[i] + 1, x3[i] + 1)
rw = int(input())
for wewq in range(rw):
	main()


def op(x1, y1, x2, y2, x3, y3):
	ans.append((x1 + 1, y1 + 1, x2 + 1, y2 + 1, x3 + 1, y3 + 1))
	a[x1][y1] = 1 - a[x1][y1]
	a[x2][y2] = 1 - a[x2][y2]
	a[x3][y3] = 1 - a[x3][y3]
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = [list(map(int, input())) for _ in range(n)]
	ans = []
	for i in range(n - 1):
		for j in range(m - 1):
			mat = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
			if mat == [1, 1, 1, 1]:
				op(i, j, i + 1, j, i, j + 1)
			mat = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
			if mat == [0, 0, 0, 1]:
				op(i, j + 1, i + 1, j, i + 1, j + 1)
			elif mat == [0, 0, 1, 0]:
				op(i, j, i + 1, j, i + 1, j + 1)
			elif mat == [0, 1, 0, 0]:
				op(i, j, i, j + 1, i + 1, j + 1)
			elif mat == [1, 0, 0, 0]:
				op(i, j, i + 1, j, i, j + 1)
			mat = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
			if mat == [1, 0, 0, 1]:
				op(i, j, i + 1, j, i, j + 1)
			elif mat == [0, 1, 1, 0]:
				op(i, j, i + 1, j, i + 1, j + 1)
			mat = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
			if mat == [1, 1, 0, 0]:
				op(i + 1, j, i + 1, j + 1, i, j + 1)
			elif mat == [1, 0, 1, 0]:
				op(i + 1, j, i + 1, j + 1, i, j + 1)
			elif mat == [0, 1, 0, 1]:
				op(i, j, i + 1, j, i + 1, j + 1)
			elif mat == [0, 0, 1, 1]:
				op(i, j, i, j + 1, i + 1, j + 1)
			mat = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
			if mat == [1, 1, 1, 0]:
				op(i, j, i, j + 1, i + 1, j)
			elif mat == [1, 1, 0, 1]:
				op(i, j, i, j + 1, i + 1, j + 1)
			elif mat == [1, 0, 1, 1]:
				op(i, j, i + 1, j, i + 1, j + 1)
			elif mat == [0, 1, 1, 1]:
				op(i, j + 1, i + 1, j, i + 1, j + 1)
			mat = [a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1]]
	print(len(ans))
	for i in ans:
		print(*i)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def testcase(t):
	for pp in range(t):
		solve(pp)

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	y %= p - 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

def solve(case):
	(n, m) = sep()
	g = []
	for i in range(n):
		temp = list(inp())
		for j in range(len(temp)):
			temp[j] = int(temp[j])
		g.append(temp)
	ans = []
	for i in range(n - 1):
		for j in range(m):
			if g[i][j] == 1:
				if j != m - 1:
					ans.append((i, i, i + 1, j, j + 1, j))
					g[i + 1][j] = 1 - g[i + 1][j]
					g[i][j + 1] = 1 - g[i][j + 1]
				else:
					ans.append((i, i + 1, i + 1, j, j, j - 1))
					g[i + 1][j] = 1 - g[i + 1][j]
					g[i + 1][j - 1] = 1 - g[i + 1][j - 1]
	i = n - 1
	for j in range(m - 2):
		if g[i][j] == 1:
			ans.append((i, i, i - 1, j, j + 1, j + 1))
			ans.append((i - 1, i, i, j + 1, j + 1, j + 2))
			g[i][j + 2] = 1 - g[i][j + 2]
	if g[i][m - 2] == 1 and g[i][m - 1] == 1:
		ans.append((i, i - 1, i - 1, m - 2, m - 2, m - 1))
		ans.append((i - 1, i - 1, i, m - 2, m - 1, m - 1))
	if g[i][m - 2] == 0 and g[i][m - 1] == 1:
		ans.append((i - 1, i, i, m - 2, m - 2, m - 1))
		ans.append((i - 1, i - 1, i, m - 2, m - 1, m - 1))
		ans.append((i - 1, i, i, m - 1, m - 1, m - 2))
	if g[i][m - 2] == 1 and g[i][m - 1] == 0:
		ans.append((i - 1, i, i, m - 2, m - 2, m - 1))
		ans.append((i - 1, i, i, m - 1, m - 1, m - 2))
		ans.append((i - 1, i - 1, i, m - 2, m - 1, m - 2))
	print(len(ans))
	for i in range(len(ans)):
		print(ans[i][0] + 1, ans[i][3] + 1, ans[i][1] + 1, ans[i][4] + 1, ans[i][2] + 1, ans[i][5] + 1)
testcase(int(inp()))


import bisect
import math

def solve(n, m, grid):
	answer = []
	for x in range(n - 1):
		for y in range(m - 1):
			if [grid[x][y], grid[x][y + 1], grid[x + 1][y], grid[x + 1][y + 1]].count('1') == 4:
				grid[x][y] = '0'
				grid[x][y + 1] = '0'
				grid[x + 1][y] = '0'
				answer.append([x + 1, y + 1, x + 1, y + 2, x + 2, y + 1])
				pass
			if [grid[x][y], grid[x][y + 1], grid[x + 1][y], grid[x + 1][y + 1]].count('1') == 1:
				if grid[x][y] == '1':
					grid[x][y] = '0'
					grid[x][y + 1] = '1'
					grid[x + 1][y] = '1'
					answer.append([x + 1, y + 1, x + 1, y + 2, x + 2, y + 1])
				elif grid[x + 1][y] == '1':
					grid[x + 1][y] = '0'
					grid[x][y + 1] = '1'
					grid[x + 1][y + 1] = '1'
					answer.append([x + 2, y + 1, x + 1, y + 2, x + 2, y + 2])
				elif grid[x][y + 1] == '1':
					grid[x][y + 1] = '0'
					grid[x + 1][y + 1] = '1'
					grid[x + 1][y] = '1'
					answer.append([x + 1, y + 2, x + 2, y + 2, x + 2, y + 1])
				elif grid[x + 1][y + 1] == '1':
					grid[x + 1][y + 1] = '0'
					grid[x][y + 1] = '1'
					grid[x][y] = '1'
					answer.append([x + 2, y + 2, x + 1, y + 2, x + 1, y + 1])
					pass
				pass
			if [grid[x][y], grid[x][y + 1], grid[x + 1][y], grid[x + 1][y + 1]].count('1') == 2:
				a = []
				gotone = False
				if grid[x][y] == '0':
					a.append(x + 1)
					a.append(y + 1)
					grid[x][y] = '1'
				elif gotone:
					pass
				else:
					gotone = True
					a.append(x + 1)
					a.append(y + 1)
					grid[x][y] = '0'
				if grid[x][y + 1] == '0':
					a.append(x + 1)
					a.append(y + 2)
					grid[x][y + 1] = '1'
				elif gotone:
					pass
				else:
					gotone = True
					a.append(x + 1)
					a.append(y + 2)
					grid[x][y + 1] = '0'
				if grid[x + 1][y] == '0':
					a.append(x + 2)
					a.append(y + 1)
					grid[x + 1][y] = '1'
				elif gotone:
					pass
				else:
					gotone = True
					a.append(x + 2)
					a.append(y + 1)
					grid[x + 1][y] = '0'
				if grid[x + 1][y + 1] == '0':
					a.append(x + 2)
					a.append(y + 2)
					grid[x + 1][y + 1] = '1'
				elif gotone:
					pass
				else:
					gotone = True
					a.append(x + 2)
					a.append(y + 2)
					grid[x + 1][y + 1] = '0'
				answer.append(a)
				pass
			if [grid[x][y], grid[x][y + 1], grid[x + 1][y], grid[x + 1][y + 1]].count('1') == 3:
				a = []
				if grid[x][y] == '1':
					a.append(x + 1)
					a.append(y + 1)
					grid[x][y] = '0'
				if grid[x][y + 1] == '1':
					a.append(x + 1)
					a.append(y + 2)
					grid[x][y + 1] = '0'
				if grid[x + 1][y] == '1':
					a.append(x + 2)
					a.append(y + 1)
					grid[x + 1][y] = '0'
				if grid[x + 1][y + 1] == '1':
					a.append(x + 2)
					a.append(y + 2)
					grid[x + 1][y + 1] = '0'
				answer.append(a)
				pass
			else:
				continue
	return answer
t = int(input())
ans = []
for i in range(t):
	nm = [int(x) for x in input().split()]
	n = nm[0]
	m = nm[1]
	grid = []
	for j in range(n):
		grid.append(list(input()))
	ans.append(solve(n, m, grid))
for test in ans:
	print(len(test))
	for z in range(len(test)):
		for ele in test[z]:
			print(ele, end=' ')
		print()


field = []
(n, m) = (0, 0)
opers = 0
ans = []

def sa(i, g):
	return str(i + 1) + ' ' + str(g + 1) + ' '

def do(i, g, am):
	if am == 0:
		return 0
	global opers
	global ans
	if am == 4:
		field[i][g] = False
		field[i + 1][g] = False
		field[i][g + 1] = False
		ans.append(sa(i, g) + sa(i + 1, g) + sa(i, g + 1))
		do(i, g, 1)
	else:
		poses = [[], []]
		for j in range(i, i + 2):
			for k in range(g, g + 2):
				if field[j][k]:
					poses[1].append((j, k))
				else:
					poses[0].append((j, k))
		if am == 1:
			field[poses[1][0][0]][poses[1][0][1]] = False
			field[poses[0][0][0]][poses[0][0][1]] = True
			field[poses[0][1][0]][poses[0][1][1]] = True
			ans.append(sa(poses[1][0][0], poses[1][0][1]) + sa(poses[0][0][0], poses[0][0][1]) + sa(poses[0][1][0], poses[0][1][1]))
			do(i, g, 2)
		elif am == 2:
			field[poses[0][0][0]][poses[0][0][1]] = True
			field[poses[0][1][0]][poses[0][1][1]] = True
			field[poses[1][0][0]][poses[1][0][1]] = False
			ans.append(sa(poses[0][0][0], poses[0][0][1]) + sa(poses[0][1][0], poses[0][1][1]) + sa(poses[1][0][0], poses[1][0][1]))
			do(i, g, 3)
		elif am == 3:
			field[poses[1][0][0]][poses[1][0][1]] = False
			field[poses[1][1][0]][poses[1][1][1]] = False
			field[poses[1][2][0]][poses[1][2][1]] = False
			ans.append(sa(poses[1][0][0], poses[1][0][1]) + sa(poses[1][1][0], poses[1][1][1]) + sa(poses[1][2][0], poses[1][2][1]))
	opers += 1
for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	opers = 0
	ans = []
	field = []
	for i in range(n):
		field.append(list(map(bool, map(int, list(input())))))
	am = 0
	for j in range(n - 2, n):
		for k in range(m - 2, m):
			am += field[j][k]
	do(n - 2, m - 2, am)
	am = 0
	for j in range(0, 2):
		for k in range(0, 2):
			am += field[j][k]
	do(0, 0, am)
	am = 0
	for j in range(n - 2, n):
		for k in range(0, 2):
			am += field[j][k]
	do(n - 2, 0, am)
	am = 0
	for j in range(0, 2):
		for k in range(m - 2, m):
			am += field[j][k]
	do(0, m - 2, am)
	for i in range(0, n - 1, 2):
		for g in range(0, m - 1, 2):
			am = 0
			for j in range(i, i + 2):
				for k in range(g, g + 2):
					am += field[j][k]
			do(i, g, am)
	for i in range(0, n - 1, 2):
		am = 0
		for j in range(i, i + 2):
			for k in range(m - 2, m):
				am += field[j][k]
		do(i, m - 2, am)
	for g in range(0, m - 1, 2):
		am = 0
		for j in range(n - 2, n):
			for k in range(g, g + 2):
				am += field[j][k]
		do(n - 2, g, am)
	print(opers)
	print(*ans, sep='\n')


from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, itertools, math
sys.setrecursionlimit(10 ** 5)
input = sys.stdin.readline
sqrt = math.sqrt

def LI():
	return list(map(int, input().split()))

def LF():
	return list(map(float, input().split()))

def LI_():
	return list(map(lambda x: int(x) - 1, input().split()))

def II():
	return int(input())

def IF():
	return float(input())

def S():
	return input().rstrip()

def LS():
	return S().split()

def IR(n):
	res = [None] * n
	for i in range(n):
		res[i] = II()
	return res

def LIR(n):
	res = [None] * n
	for i in range(n):
		res[i] = LI()
	return res

def FR(n):
	res = [None] * n
	for i in range(n):
		res[i] = IF()
	return res

def LIR(n):
	res = [None] * n
	for i in range(n):
		res[i] = IF()
	return res

def LIR_(n):
	res = [None] * n
	for i in range(n):
		res[i] = LI_()
	return res

def SR(n):
	res = [None] * n
	for i in range(n):
		res[i] = S()
	return res

def LSR(n):
	res = [None] * n
	for i in range(n):
		res[i] = LS()
	return res
mod = 1000000007
inf = float('INF')

def solve():
	t = II()
	kuso = {'1': '0', '0': '1'}
	f = {1: 3, 2: 2, 3: 1, 4: 4}

	def fff(g, a):
		one = a.count('1')
		if one == 0:
			return
		tmp = []
		if one == 1 or one == 3:
			for i in range(4):
				if a[i] == '1':
					tmp.append(i)
			for i in range(4):
				if len(tmp) == 3:
					break
				if a[i] == '0':
					tmp.append(i)
			for i in range(3):
				a[tmp[i]] = kuso[a[tmp[i]]]
				tmp[i] = ' '.join(map(str, g[tmp[i]]))
			ans.append(' '.join(tmp))
			fff(g, a)
			return
		else:
			for i in range(4):
				if a[i] == '0':
					tmp.append(i)
			for i in range(4):
				if len(tmp) == 3:
					break
				if a[i] == '1':
					tmp.append(i)
			for i in range(3):
				a[tmp[i]] = kuso[a[tmp[i]]]
				tmp[i] = ' '.join(map(str, g[tmp[i]]))
			ans.append(' '.join(tmp))
			fff(g, a)
			return
	for i in range(t):
		(n, m) = LI()
		s = [list(input()) for i in range(n)]
		ans = []
		for y in range(0, n - 1, 2):
			for x in range(0, m - 1, 2):
				a0 = s[y][x]
				a1 = s[y][x + 1]
				a2 = s[y + 1][x]
				a3 = s[y + 1][x + 1]
				s[y][x] = '0'
				s[y + 1][x] = '0'
				s[y][x + 1] = '0'
				s[y + 1][x + 1] = '0'
				g = [[y + 1, x + 1], [y + 1, x + 2], [y + 2, x + 1], [y + 2, x + 2]]
				a = [a0, a1, a2, a3]
				c = a.count('1')
				if c == 0:
					continue
				fff(g, a)
			x = m - 2
			a0 = s[y][x]
			a1 = s[y][x + 1]
			a2 = s[y + 1][x]
			a3 = s[y + 1][x + 1]
			s[y][x] = '0'
			s[y + 1][x] = '0'
			s[y][x + 1] = '0'
			s[y + 1][x + 1] = '0'
			g = [[y + 1, x + 1], [y + 1, x + 2], [y + 2, x + 1], [y + 2, x + 2]]
			a = [a0, a1, a2, a3]
			c = a.count('1')
			if c == 0:
				continue
			fff(g, a)
		y = n - 2
		for x in range(0, m - 1, 2):
			a0 = s[y][x]
			a1 = s[y][x + 1]
			a2 = s[y + 1][x]
			a3 = s[y + 1][x + 1]
			s[y][x] = '0'
			s[y + 1][x] = '0'
			s[y][x + 1] = '0'
			s[y + 1][x + 1] = '0'
			g = [[y + 1, x + 1], [y + 1, x + 2], [y + 2, x + 1], [y + 2, x + 2]]
			a = [a0, a1, a2, a3]
			c = a.count('1')
			if c == 0:
				continue
			fff(g, a)
		x = m - 2
		a0 = s[y][x]
		a1 = s[y][x + 1]
		a2 = s[y + 1][x]
		a3 = s[y + 1][x + 1]
		s[y][x] = '0'
		s[y + 1][x] = '0'
		s[y][x + 1] = '0'
		s[y + 1][x + 1] = '0'
		g = [[y + 1, x + 1], [y + 1, x + 2], [y + 2, x + 1], [y + 2, x + 2]]
		a = [a0, a1, a2, a3]
		c = a.count('1')
		if c != 0:
			fff(g, a)
		print(len(ans))
		for i in ans:
			print(i)
	return
solve()


import itertools
import math
import sys
import os
from collections import defaultdict

def is_debug():
	return 'PYPY3_HOME' not in os.environ

def stdin_wrapper():
	data = '1\n2 2\n11\n11\n3 3\n011\n101\n110\n4 4\n1111\n0110\n0110\n1111\n5 5\n01011\n11001\n00010\n11011\n10000\n2 3\n011\n101\n\n'
	for line in data.split('\n'):
		yield line
if not is_debug():

	def stdin_wrapper():
		while True:
			yield sys.stdin.readline()
inputs = stdin_wrapper()

def input_wrapper():
	return next(inputs)

def get_str():
	if is_debug():
		return input_wrapper()
	return input()

def get(_type):
	if _type == str:
		return get_str()
	return _type(input_wrapper())

def get_arr(_type):
	return [_type(x) for x in input_wrapper().split()]

def tuplerize(method):

	def wrap(*args, **kwargs):
		res = method(*args, **kwargs)
		if not isinstance(res, (tuple, list)):
			res = (res,)
		return res
	return wrap

@tuplerize
def solve(n, m, mx):
	for i in range(n):
		mx[i] = [int(x) for x in mx[i]]
	y_starts = [2 * i for i in range(n // 2)]
	if n % 2:
		y_starts.append(n - 2)
	x_starts = [2 * i for i in range(m // 2)]
	if m % 2:
		x_starts.append(m - 2)
	result = []

	def show(mx):
		print('\n'.join([''.join([str(x) for x in row]) for row in mx]))

	def to_absolute(op, x, y):
		return (op[1] + y, op[0] + x, op[3] + y, op[2] + x, op[5] + y, op[4] + x)

	def apply(mx, op):
		for i in range(3):
			mx[op[2 * i]][op[2 * i + 1]] = 1 - mx[op[2 * i]][op[2 * i + 1]]
	for y in y_starts:
		for x in x_starts:
			coords = [(_x, _y) for _x in range(2) for _y in range(2)]
			colors = [mx[y + _y][x + _x] for (_x, _y) in coords]
			ones = sum(colors)
			if ones == 0:
				continue
			if ones == 4:
				c = coords[0]
				op = (*c, *(1 - c[0], c[1]), *(c[0], 1 - c[1]))
				result.append(to_absolute(op, x, y))
				apply(mx, result[-1])
			colors = [mx[y + _y][x + _x] for (_x, _y) in coords]
			ones = sum(colors)
			if ones == 1:
				one_index = colors.index(1)
				c = coords[one_index]
				op = (*c, *(1 - c[0], c[1]), *(c[0], 1 - c[1]))
				result.append(to_absolute(op, x, y))
				apply(mx, result[-1])
			colors = [mx[y + _y][x + _x] for (_x, _y) in coords]
			ones = sum(colors)
			if ones == 2:
				if colors[1] == colors[2]:
					one_index = colors.index(1)
					c = coords[one_index]
					op = (*c, *(1 - c[0], c[1]), *(c[0], 1 - c[1]))
					result.append(to_absolute(op, x, y))
					apply(mx, result[-1])
				else:
					op = []
					for i in range(len(colors)):
						if colors[i] == 0:
							op += [*coords[i]]
					one_index = colors.index(1)
					c = coords[one_index]
					op += [*c]
					op = tuple(op)
					result.append(to_absolute(op, x, y))
					apply(mx, result[-1])
			colors = [mx[y + _y][x + _x] for (_x, _y) in coords]
			ones = sum(colors)
			if ones == 3:
				zero_index = colors.index(0)
				c = coords[zero_index]
				op = (*(1 - c[0], 1 - c[1]), *(c[0], 1 - c[1]), *(1 - c[0], c[1]))
				result.append(to_absolute(op, x, y))
				apply(mx, result[-1])
	return str(len(result)) + '\n' + '\n'.join([' '.join([str(x + 1) for x in row]) for row in result])
t = get(int)
for _ in range(t):
	(n, m) = get_arr(int)
	mx = [get_str() for _ in range(n)]
	print(*solve(n, m, mx))


def f(c):
	if c == '1':
		return '0'
	return '1'
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	arr = [list(input()) for i in range(n)]

	def do(x):
		res.append(x)
		arr[x[0] - 1][x[1] - 1] = f(arr[x[0] - 1][x[1] - 1])
		arr[x[2] - 1][x[3] - 1] = f(arr[x[2] - 1][x[3] - 1])
		arr[x[4] - 1][x[5] - 1] = f(arr[x[4] - 1][x[5] - 1])
	res = []
	if n % 2 == 1:
		for j in range(m - 1):
			if arr[n - 1][j] == '0':
				continue
			do((n, j + 1, n - 1, j + 1, n - 1, j + 2))
		if arr[n - 1][m - 1] == '1':
			do((n, m, n - 1, m, n - 1, m - 1))
		n -= 1
	if m % 2 == 1:
		for i in range(n - 1):
			if arr[i][m - 1] == '0':
				continue
			do((i + 1, m, i + 1, m - 1, i + 2, m - 1))
		if arr[n - 1][m - 1] == '1':
			do((n, m, n, m - 1, n - 1, m - 1))
		m -= 1
	for i in range(0, n, 2):
		for j in range(0, m, 2):
			a = [arr[i][j], arr[i + 1][j], arr[i][j + 1], arr[i + 1][j + 1]]
			if a.count('0') == 4:
				continue
			if a.count('1') == 4:
				do((i + 1, j + 1, i + 2, j + 1, i + 1, j + 2))
				do((i + 2, j + 2, i + 2, j + 1, i + 1, j + 2))
				do((i + 1, j + 1, i + 2, j + 2, i + 1, j + 2))
				do((i + 1, j + 1, i + 2, j + 1, i + 2, j + 2))
				continue
			if a.count('1') == 2:
				do((i + 1, j + 1, i + 2, j + 1, i + 1, j + 2))
				a = [arr[i][j], arr[i + 1][j], arr[i][j + 1], arr[i + 1][j + 1]]
			if a.count('1') == 3:
				if arr[i][j] == '0':
					do((i + 1, j + 2, i + 2, j + 1, i + 2, j + 2))
				elif arr[i + 1][j] == '0':
					do((i + 1, j + 2, i + 1, j + 1, i + 2, j + 2))
				elif arr[i][j + 1] == '0':
					do((i + 1, j + 1, i + 2, j + 1, i + 2, j + 2))
				elif arr[i + 1][j + 1] == '0':
					do((i + 1, j + 2, i + 2, j + 1, i + 1, j + 1))
			if a.count('1') == 1:
				if arr[i][j] == '1':
					do((i + 1, j + 2, i + 2, j + 1, i + 1, j + 1))
					do((i + 1, j + 2, i + 1, j + 1, i + 2, j + 2))
					do((i + 1, j + 1, i + 2, j + 1, i + 2, j + 2))
				elif arr[i + 1][j + 1] == '1':
					do((i + 1, j + 2, i + 2, j + 1, i + 2, j + 2))
					do((i + 1, j + 2, i + 1, j + 1, i + 2, j + 2))
					do((i + 1, j + 1, i + 2, j + 1, i + 2, j + 2))
				elif arr[i + 1][j] == '1':
					do((i + 1, j + 1, i + 2, j + 1, i + 2, j + 2))
					do((i + 1, j + 2, i + 2, j + 1, i + 2, j + 2))
					do((i + 1, j + 2, i + 2, j + 1, i + 1, j + 1))
				elif arr[i][j + 1] == '1':
					do((i + 1, j + 2, i + 1, j + 1, i + 2, j + 2))
					do((i + 1, j + 2, i + 2, j + 1, i + 2, j + 2))
					do((i + 1, j + 2, i + 2, j + 1, i + 1, j + 1))
	print(len(res))
	for e in res:
		print(*e)


import sys
import math

def int_arr():
	return list(map(int, sys.stdin.readline().split()))

def str_arr():
	return list(map(str, sys.stdin.readline().split()))

def input():
	return sys.stdin.readline()

def out(x):
	return sys.stdout.write(str(x))

def one1(one, zero):
	ans.append(one[0] + zero[0] + zero[1])
	ans.append(one[0] + zero[0] + zero[2])
	ans.append(one[0] + zero[1] + zero[2])

def one2(one, zero):
	ans.append(one[0] + zero[0] + zero[1])
	ans.append(one[1] + zero[0] + zero[1])

def one3(one, zero):
	ans.append(one[0] + one[1] + one[2])

def one4(one, zero):
	ans.append(one[0] + one[2] + one[1])
	ans.append(one[0] + one[3] + one[1])
	ans.append(one[3] + one[2] + one[1])
	ans.append(one[0] + one[2] + one[3])
for _ in range(int(input())):
	(n, m) = int_arr()
	arr = []
	ans = []
	for i in range(n):
		str = input()
		arr.append(' '.join(str).split())
	c = 0
	for i in range(1, n):
		for j in range(1, m):
			a = [arr[i][j], arr[i][j - 1], arr[i - 1][j], arr[i - 1][j - 1]]
			arr[i][j] = arr[i][j - 1] = arr[i - 1][j] = arr[i - 1][j - 1] = '0'
			one = []
			zero = []
			for k in range(4):
				if a[k] == '1':
					if k == 0:
						one.append([i + 1, j + 1])
					elif k == 1:
						one.append([i + 1, j])
					elif k == 2:
						one.append([i, j + 1])
					else:
						one.append([i, j])
				elif k == 0:
					zero.append([i + 1, j + 1])
				elif k == 1:
					zero.append([i + 1, j])
				elif k == 2:
					zero.append([i, j + 1])
				else:
					zero.append([i, j])
			if len(one) == 1:
				one1(one, zero)
			if len(one) == 2:
				one2(one, zero)
			if len(one) == 3:
				one3(one, zero)
			if len(one) == 4:
				one4(one, zero)
	print(len(ans))
	for l in range(len(ans)):
		print(*ans[l])


def read_ints():
	return list(map(int, input().split()))
d = {'0': '1', '1': '0'}

def change(rest, a, b, c):
	v = list(rest)
	for i in (a, b, c):
		v[i] = d[v[i]]
	return ''.join(v)

def get_moves(*args):
	result = []
	for i in args:
		result.extend([i % 2, i // 2])
	return [result]
d_rest = {'0000': []}
while len(d_rest) < 16:
	known = list(d_rest)
	for rest in known:
		new = change(rest, 0, 1, 2)
		if new not in d_rest:
			d_rest[new] = d_rest[rest] + get_moves(0, 1, 2)
		new = change(rest, 0, 1, 3)
		if new not in d_rest:
			d_rest[new] = d_rest[rest] + get_moves(0, 1, 3)
		new = change(rest, 0, 2, 3)
		if new not in d_rest:
			d_rest[new] = d_rest[rest] + get_moves(0, 2, 3)
		new = change(rest, 1, 2, 3)
		if new not in d_rest:
			d_rest[new] = d_rest[rest] + get_moves(1, 2, 3)

def remove_row(v):
	y = len(v) - 1
	row = v[-1]
	row1 = v[-2]
	result = []
	x = len(row) - 1
	while x > 0:
		if row[x] == '0':
			x -= 1
			continue
		if row[x - 1] == '1':
			result.append([x, y, x - 1, y, x, y - 1])
			row1[x] = d[row1[x]]
			x -= 2
			continue
		result.append([x, y, x, y - 1, x - 1, y - 1])
		row1[x] = d[row1[x]]
		row1[x - 1] = d[row1[x - 1]]
		x -= 2
	if x == 0 and row[0] == '1':
		result.append([0, y, 0, y - 1, 1, y - 1])
		row1[0] = d[row1[0]]
		row1[1] = d[row1[1]]
	del v[-1]
	return result

def remove_col(v):
	x = len(v[0]) - 1
	col = v[0][-1] + v[1][-1]
	del v[0][-1]
	del v[1][-1]
	if col == '00':
		return []
	if col == '01':
		v[0][-1] = d[v[0][-1]]
		v[1][-1] = d[v[1][-1]]
		return [[x, 1, x - 1, 0, x - 1, 1]]
	if col == '10':
		v[0][-1] = d[v[0][-1]]
		v[1][-1] = d[v[1][-1]]
		return [[x, 0, x - 1, 0, x - 1, 1]]
	v[0][-1] = d[v[0][-1]]
	return [[x, 0, x - 1, 0, x, 1]]
(t,) = read_ints()
for i in range(t):
	(n, m) = read_ints()
	values = [list(input()) for j in range(n)]
	moves = []
	while len(values) > 2:
		moves.extend(remove_row(values))
	while len(values[0]) > 2:
		moves.extend(remove_col(values))
	rest = ''.join(values[0] + values[1])
	moves.extend(d_rest[rest])
	print(len(moves))
	for (a, b, c, d0, e, f) in moves:
		print(*(b + 1, a + 1, d0 + 1, c + 1, f + 1, e + 1))


from sys import stdin, stdout
from collections import *
from math import gcd, floor, ceil
from bisect import bisect_left as bl
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : map(int, stdin.readline().split())
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
mod = 1000000007
INF = float('inf')

def solve():
	(n, m) = mp()
	ans = []
	l = [st() for i in range(n)]
	for i in range(n):
		for j in range(m):
			if l[i][j] == '1':
				(x, y) = (1, 1)
				if i == n - 1:
					x *= -1
				if j == m - 1:
					y *= -1
				ans.append([i + 1, j + 1, i + x + 1, j + 1, i + x + 1, j + y + 1])
				ans.append([i + 1, j + 1, i + 1, j + y + 1, i + x + 1, j + y + 1])
				ans.append([i + 1, j + 1, i + x + 1, j + 1, i + 1, j + y + 1])
	pr(len(ans))
	for i in ans:
		print(*i)
for _ in range(inp()):
	solve()


from itertools import chain

def read_ints():
	return map(int, input().split())

def get_moves_to_invert_cell(x, y):
	inv_x = x - 1 if x != 0 else x + 1
	inv_y = y - 1 if y != 0 else y + 1
	return [((x, y), (inv_x, y), (inv_x, inv_y)), ((x, y), (x, inv_y), (inv_x, inv_y)), ((x, y), (inv_x, y), (x, inv_y))]
t = int(input())
for i_t in range(t):
	(n, m) = read_ints()
	matrix = tuple((input() for i in range(n)))
	print(''.join(matrix).count('1') * 3)
	for y in range(n):
		for x in range(m):
			if matrix[y][x] == '1':
				moves = get_moves_to_invert_cell(x, y)
				for move in moves:
					move = map(lambda pos: (pos[1] + 1, pos[0] + 1), move)
					print(*chain.from_iterable(move))


import sys, math
input = sys.stdin.readline
t = int(input())

def f(i, j, n, m):
	x1 = []
	x2 = []
	x3 = []
	x4 = [i, j]
	if i + 1 < n and j + 1 < m:
		x1 = [i + 1, j]
		x2 = [i, j + 1]
		x3.append(i + 1)
		x3.append(j + 1)
	elif i - 1 >= 0 and j - 1 >= 0:
		x1 = [i - 1, j]
		x2 = [i, j - 1]
		x3.append(i - 1)
		x3.append(j - 1)
	elif i - 1 >= 0 and j + 1 < m:
		x1 = [i - 1, j]
		x2 = [i, j + 1]
		x3.append(i - 1)
		x3.append(j + 1)
	elif i + 1 < n and j - 1 >= 0:
		x1 = [i + 1, j]
		x2 = [i, j - 1]
		x3.append(i + 1)
		x3.append(j - 1)
	x = [[x1, x2, x4], [x1, x3, x4], [x2, x3, x4]]
	return x
for r in range(t):
	(n, m) = map(int, input().split())
	mat = []
	for i in range(n):
		temp = input().strip('\n')
		temp2 = []
		for j in temp:
			temp2.append(int(j))
		mat.append(temp2)
	ans = 0
	operations = []
	for i in range(n):
		for j in range(m):
			if mat[i][j] == 1:
				ans += 3
				x = f(i, j, n, m)
				operations.append(x[0])
				operations.append(x[1])
				operations.append(x[2])
	print(ans)
	for i in operations:
		for j in i:
			print(j[0] + 1, j[1] + 1, end=' ')
		print()


import sys

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	(n, m) = get_ints()
	c = []
	ans = []
	for i in range(n):
		b = input()
		b = [str(x) for x in b]
		c.append(b)
	for i in range(n - 1):
		for j in range(m - 1):
			zeros = []
			ones = []
			arr = []
			if c[i][j] == '1':
				ones.append([i + 1, j + 1])
			if c[i][j] == '0':
				zeros.append([i + 1, j + 1])
			if c[i][j + 1] == '1':
				ones.append([i + 1, j + 2])
			if c[i][j + 1] == '0':
				zeros.append([i + 1, j + 2])
			if c[i + 1][j] == '1':
				ones.append([i + 2, j + 1])
			if c[i + 1][j] == '0':
				zeros.append([i + 2, j + 1])
			if c[i + 1][j + 1] == '1':
				ones.append([i + 2, j + 2])
			if c[i + 1][j + 1] == '0':
				zeros.append([i + 2, j + 2])
			if len(ones) == 0:
				continue
			if len(ones) == 2:
				arr = [zeros[0][0], zeros[0][1], zeros[1][0], zeros[1][1], ones[0][0], ones[0][1]]
				ans.append(arr)
				arr = [zeros[0][0], zeros[0][1], zeros[1][0], zeros[1][1], ones[1][0], ones[1][1]]
				ans.append(arr)
			if len(ones) == 3:
				arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], zeros[0][0], zeros[0][1]]
				ans.append(arr)
				arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], ones[2][0], ones[2][1]]
				ans.append(arr)
				arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], zeros[0][0], zeros[0][1]]
				ans.append(arr)
			if len(ones) == 1:
				arr = [zeros[0][0], zeros[0][1], zeros[1][0], zeros[1][1], ones[0][0], ones[0][1]]
				ans.append(arr)
				arr = [zeros[0][0], zeros[0][1], zeros[2][0], zeros[2][1], ones[0][0], ones[0][1]]
				ans.append(arr)
				arr = [zeros[1][0], zeros[1][1], zeros[2][0], zeros[2][1], ones[0][0], ones[0][1]]
				ans.append(arr)
			if len(ones) == 4:
				arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], ones[2][0], ones[2][1]]
				ans.append(arr)
				arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], ones[3][0], ones[3][1]]
				ans.append(arr)
				arr = [ones[0][0], ones[0][1], ones[2][0], ones[2][1], ones[3][0], ones[3][1]]
				ans.append(arr)
				arr = [ones[1][0], ones[1][1], ones[2][0], ones[2][1], ones[3][0], ones[3][1]]
				ans.append(arr)
			(c[i][j], c[i + 1][j], c[i][j + 1], c[i + 1][j + 1]) = ('0', '0', '0', '0')
	print(len(ans))
	for i in range(len(ans)):
		print(*ans[i])


def generate(x1, x2, n1, n2):
	ans = []
	if x1 != n1 or x2 != n2:
		ans.append(x1)
		ans.append(x2)
	if x1 + 1 != n1 or x2 != n2:
		ans.append(x1 + 1)
		ans.append(x2)
	if x1 != n1 or x2 + 1 != n2:
		ans.append(x1)
		ans.append(x2 + 1)
	if x1 + 1 != n1 or x2 + 1 != n2:
		ans.append(x1 + 1)
		ans.append(x2 + 1)
	return ans

def gen_seq(x1, x2, c):
	ans = []
	x1 += 1
	x2 += 1
	for i in c:
		if i == 1:
			ans.append(generate(x1, x2, x1, x2))
		elif i == 2:
			ans.append(generate(x1, x2, x1, x2 + 1))
		elif i == 3:
			ans.append(generate(x1, x2, x1 + 1, x2))
		else:
			ans.append(generate(x1, x2, x1 + 1, x2 + 1))
	return ans
for t in range(int(input())):
	(n, m) = map(int, input().split())
	arr = [list(map(int, list(input()))) for i in range(n)]
	ans = []
	count = 0
	for row in range(n):
		for col in range(m):
			r = row
			c = col
			if arr[row][col] == 0:
				continue
			if c == 0 and r == 0:
				for i in gen_seq(r, c, (3, 2, 4)):
					ans.append(i)
			elif row == 0:
				for i in gen_seq(r, c - 1, (4, 1, 3)):
					ans.append(i)
			elif col == 0:
				for i in gen_seq(r - 1, c, (4, 1, 2)):
					ans.append(i)
			else:
				for i in gen_seq(r - 1, c - 1, (3, 2, 1)):
					ans.append(i)
			count += 3
	print(count)
	for i in ans:
		print(' '.join(map(str, i)))


from sys import stdin, stdout

def INI():
	return int(stdin.readline())

def INL():
	return [int(_) for _ in stdin.readline().split()]

def INS():
	return stdin.readline()

def MOD():
	return pow(10, 9) + 7

def OPS(ans):
	stdout.write(str(ans) + '\n')

def OPL(ans):
	[stdout.write(str(_) + ' ') for _ in ans]
	stdout.write('\n')

def one():
	if T[0] == '1':
		ANS.extend([I[0], I[1], I[2]])
		ANS.extend([I[0], I[2], I[3]])
		ANS.extend([I[0], I[1], I[3]])
	elif T[1] == '1':
		ANS.extend([I[0], I[1], I[2]])
		ANS.extend([I[2], I[1], I[3]])
		ANS.extend([I[3], I[1], I[0]])
	elif T[2] == '1':
		ANS.extend([I[0], I[2], I[1]])
		ANS.extend([I[1], I[2], I[3]])
		ANS.extend([I[3], I[2], I[0]])
	else:
		ANS.extend([I[0], I[3], I[1]])
		ANS.extend([I[1], I[3], I[2]])
		ANS.extend([I[2], I[3], I[0]])

def two():
	if T[0] == '1' and T[1] == '1':
		ANS.extend([I[0], I[2], I[3]])
		ANS.extend([I[1], I[2], I[3]])
	elif T[0] == '1' and T[2] == '1':
		ANS.extend([I[0], I[1], I[3]])
		ANS.extend([I[2], I[1], I[3]])
	elif T[0] == '1' and T[3] == '1':
		ANS.extend([I[0], I[1], I[2]])
		ANS.extend([I[3], I[1], I[2]])
	elif T[1] == '1' and T[2] == '1':
		ANS.extend([I[1], I[0], I[3]])
		ANS.extend([I[2], I[0], I[3]])
	elif T[1] == '1' and T[3] == '1':
		ANS.extend([I[1], I[0], I[2]])
		ANS.extend([I[3], I[0], I[2]])
	elif T[2] == '1' and T[3] == '1':
		ANS.extend([I[2], I[0], I[1]])
		ANS.extend([I[3], I[0], I[1]])

def three():
	if T[0] == '0':
		ANS.extend([I[1], I[2], I[3]])
	elif T[1] == '0':
		ANS.extend([I[0], I[2], I[3]])
	elif T[2] == '0':
		ANS.extend([I[0], I[1], I[3]])
	else:
		ANS.extend([I[0], I[1], I[2]])

def four():
	ANS.extend([I[1], I[2], I[3]])
	ANS.extend([I[0], I[1], I[2]])
	ANS.extend([I[0], I[2], I[3]])
	ANS.extend([I[0], I[1], I[3]])
for _ in range(INI()):
	(n, m) = INL()
	X = []
	for _ in range(n):
		x = input()
		X.append(list(x))
	ANS = []
	ans = 0
	for _ in range(n - 1):
		for __ in range(m - 1):
			T = [X[_][__], X[_][__ + 1], X[_ + 1][__], X[_ + 1][__ + 1]]
			I = [[_, __], [_, __ + 1], [_ + 1, __], [_ + 1, __ + 1]]
			if T.count('1') == 1:
				one()
			elif T.count('1') == 2:
				two()
			elif T.count('1') == 3:
				three()
			elif T.count('1') == 4:
				four()
			for i in I:
				X[i[0]][i[1]] = '0'
	ans = (len(ANS) + 2) // 3
	print(ans)
	for _ in range(0, len(ANS), 3):
		for __ in range(3):
			print(ANS[_ + __][0] + 1, ANS[_ + __][1] + 1, end=' ')
		print()


import sys, os
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

def solve_(g, n, m):
	og = [[x for x in row] for row in grid]
	(x, y) = (0, 0)
	res = []
	for y in range(n - 2):
		for x in range(m - 1):
			if g[y][x] == 0:
				continue
			if g[y][x] == 1 and g[y][x + 1] == 1:
				if g[y + 1][x] == 1:
					r = [(y, x), (y, x + 1), (y + 1, x)]
				else:
					r = [(y, x), (y, x + 1), (y + 1, x + 1)]
			if g[y][x] == 1 and g[y][x + 1] == 0:
				r = [(y, x), (y + 1, x), (y + 1, x + 1)]
			assert len(r) == 3
			assert len(set(r)) == 3
			res.append(r)
			for (yy, xx) in r:
				g[yy][xx] = 1 - g[yy][xx]
		assert x + 1 == m - 1
		if g[y][x] == 0 and g[y][x + 1] == 0:
			continue
		elif g[y][x] == 1 and g[y][x + 1] == 0:
			r = [(y, x), (y + 1, x), (y + 1, x + 1)]
		elif g[y][x] == 0 and g[y][x + 1] == 1:
			r = [(y, x + 1), (y + 1, x), (y + 1, x + 1)]
		elif g[y][x] == 1 and g[y][x + 1] == 1:
			r = [(y, x), (y, x + 1), (y + 1, x)]
		assert len(r) == 3
		assert len(set(r)) == 3
		res.append(r)
		for (yy, xx) in r:
			g[yy][xx] = 1 - g[yy][xx]
	for x in range(m - 2):
		if g[-2][x] == 0 and g[-1][x] == 0:
			continue
		if g[-2][x] == 1 and g[-1][x] == 0:
			r = [(-2, x), (-2, x + 1), (-1, x + 1)]
		if g[-2][x] == 0 and g[-1][x] == 1:
			r = [(-1, x), (-2, x + 1), (-1, x + 1)]
		if g[-2][x] == 1 and g[-1][x] == 1:
			if g[-2][x + 1] == 1:
				r = [(-2, x), (-1, x), (-2, x + 1)]
			else:
				r = [(-2, x), (-1, x), (-1, x + 1)]
		assert len(r) == 3
		assert len(set(r)) == 3
		res.append(r)
		for (yy, xx) in r:
			g[yy][xx] = 1 - g[yy][xx]
	l4 = [(-2, -2), (-2, -1), (-1, -2), (-1, -1)]
	if g[-2][-2] + g[-2][-1] + g[-1][-2] + g[-1][-1] == 4:
		console('resolve 4')
		r = l4[:3]
		assert len(r) == 3
		assert len(set(r)) == 3
		res.append(r)
		for (yy, xx) in r:
			g[yy][xx] = 1 - g[yy][xx]
	if g[-2][-2] + g[-2][-1] + g[-1][-2] + g[-1][-1] == 1:
		console('resolve 1')
		for (i, (y, x)) in enumerate(l4 + l4):
			if g[y][x] == 1:
				r = (l4 + l4)[i:i + 3]
				break
		assert len(r) == 3
		assert len(set(r)) == 3
		res.append(r)
		for (yy, xx) in r:
			g[yy][xx] = 1 - g[yy][xx]
	if g[-2][-2] + g[-2][-1] + g[-1][-2] + g[-1][-1] == 2:
		console('resolve 2')
		sett = set()
		for (i, (y, x)) in enumerate(l4 + l4 + l4):
			if g[y][x] == 0:
				sett.add((y, x))
			if len(sett) == 2:
				sett.add((y, x))
			if len(sett) == 3:
				break
		r = list(sett)
		assert len(r) == 3
		assert len(set(r)) == 3
		res.append(r)
		for (yy, xx) in r:
			g[yy][xx] = 1 - g[yy][xx]
	if g[-2][-2] + g[-2][-1] + g[-1][-2] + g[-1][-1] == 3:
		console('resolve 3')
		r = []
		for (i, (y, x)) in enumerate(l4):
			if g[y][x] == 1:
				r.append((y, x))
		assert len(r) == 3
		assert len(set(r)) == 3
		res.append(r)
		for (yy, xx) in r:
			g[yy][xx] = 1 - g[yy][xx]
	res2 = []
	for re in res:
		re2 = []
		for (a, b) in re:
			if a < 0:
				a = n + a
			if b < 0:
				b = m + b
			re2.extend((a + 1, b + 1))
		res2.append(re2)
	assert sum((sum(row) for row in grid)) == 0
	for (x1, y1, x2, y2, x3, y3) in res2:
		(x1, y1, x2, y2, x3, y3) = (x1 - 1, y1 - 1, x2 - 1, y2 - 1, x3 - 1, y3 - 1)
		og[x1][y1] = 1 - og[x1][y1]
		og[x2][y2] = 1 - og[x2][y2]
		og[x3][y3] = 1 - og[x3][y3]
	assert sum((sum(row) for row in og)) == 0
	assert len(res2) <= n * m
	return res2

def console(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
	pass
ONLINE_JUDGE = False
if os.path.exists('input.txt'):
	ONLINE_JUDGE = True
if ONLINE_JUDGE:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def console(*args):
		pass

def solve(*args):
	if not ONLINE_JUDGE:
		console('----- solving ------')
		console(*args)
		console('----- ------- ------')
	return solve_(*args)
if True:
	inp = iter(sys.stdin.readlines())
	input = lambda : next(inp)
else:
	input = sys.stdin.readline
for case_num in range(int(input())):
	(n, m) = list(map(int, input().split()))
	grid = []
	for _ in range(n):
		grid.append(list(map(int, list(input().strip()))))
	res = solve(grid, n, m)
	print(len(res))
	for r in res:
		print(*r)


t = int(input())
for i in range(t):
	(n, m) = input().strip().split(' ')
	(n, m) = [int(n), int(m)]
	p = []
	for i in range(n):
		s = input()
		p.append(list(s))
	ans = []
	for i in range(0, n - 1, 2):
		for j in range(0, m - 1, 2):
			a = int(p[i][j])
			b = int(p[i][j + 1])
			c = int(p[i + 1][j])
			d = int(p[i + 1][j + 1])
			rts = a + b + c + d
			if rts == 0:
				pass
			elif rts == 3:
				if a and b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
				elif a and c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					p[i][j] = '0'
					p[i + 1][j] = '0'
					p[i + 1][j + 1] = '0'
				elif a and b and d:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j + 1] = '0'
				elif b and c and d:
					ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
					p[i + 1][j + 1] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
			elif rts == 2:
				if a and b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					c = 1
					p[i + 1][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					c = 1
					p[i + 1][j] = '1'
				elif b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					c = 0
					p[i + 1][j] = '0'
					a = 1
					p[i][j] = '1'
					b = 1
					p[i][j + 1] = '1'
				elif b and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					c = 1
					p[i + 1][j] = '1'
				if a and b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
				elif a and c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					p[i][j] = '0'
					p[i + 1][j] = '0'
					p[i + 1][j + 1] = '0'
				elif a and b and d:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j + 1] = '0'
				elif b and c and d:
					ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
					p[i + 1][j + 1] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
			elif rts == 1:
				if a:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					b = 1
					c = 1
					p[i][j + 1] = '1'
					p[i][j] = '0'
					p[i + 1][j] = '1'
				elif b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 1
					b = 0
					c = 1
					p[i][j + 1] = '0'
					p[i][j] = '1'
					p[i + 1][j] = '1'
				elif c:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 1
					b = 1
					c = 0
					p[i][j + 1] = '1'
					p[i][j] = '1'
					p[i + 1][j] = '0'
				elif d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 1
					c = 1
					d = 0
					p[i + 1][j + 1] = '0'
					p[i][j] = '1'
					p[i + 1][j] = '1'
				if a and b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					c = 1
					p[i + 1][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					c = 1
					p[i + 1][j] = '1'
				elif b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					c = 0
					p[i + 1][j] = '0'
					a = 1
					p[i][j] = '1'
					b = 1
					p[i][j + 1] = '1'
				elif b and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					c = 1
					p[i + 1][j] = '1'
				if a and b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
				elif a and c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					p[i][j] = '0'
					p[i + 1][j] = '0'
					p[i + 1][j + 1] = '0'
				elif a and b and d:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j + 1] = '0'
				elif b and c and d:
					ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
					p[i + 1][j + 1] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
			elif rts == 4:
				ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
				a = 0
				b = 0
				c = 0
				p[i][j] = '0'
				p[i + 1][j] = '0'
				p[i][j + 1] = '0'
				if a:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					b = 1
					c = 1
					p[i][j + 1] = '1'
					p[i][j] = '0'
					p[i + 1][j] = '1'
				elif b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 1
					b = 0
					c = 1
					p[i][j + 1] = '0'
					p[i][j] = '1'
					p[i + 1][j] = '1'
				elif c:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 1
					b = 1
					c = 0
					p[i][j + 1] = '1'
					p[i][j] = '1'
					p[i + 1][j] = '0'
				elif d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 1
					c = 1
					d = 0
					p[i + 1][j + 1] = '0'
					p[i][j] = '1'
					p[i + 1][j] = '1'
				if a and b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					c = 1
					p[i + 1][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					c = 1
					p[i + 1][j] = '1'
				elif b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					c = 0
					p[i + 1][j] = '0'
					a = 1
					p[i][j] = '1'
					b = 1
					p[i][j + 1] = '1'
				elif b and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					c = 1
					p[i + 1][j] = '1'
				if a and b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
				elif a and c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					p[i][j] = '0'
					p[i + 1][j] = '0'
					p[i + 1][j + 1] = '0'
				elif a and b and d:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j + 1] = '0'
				elif b and c and d:
					ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
					p[i + 1][j + 1] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
	if n % 2:
		i = n - 2
		for j in range(0, m - 1):
			a = int(p[i][j])
			b = int(p[i][j + 1])
			c = int(p[i + 1][j])
			d = int(p[i + 1][j + 1])
			rts = a + b + c + d
			if rts == 0:
				pass
			elif rts == 3:
				if a and b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
				elif a and c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					p[i][j] = '0'
					p[i + 1][j] = '0'
					p[i + 1][j + 1] = '0'
				elif a and b and d:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j + 1] = '0'
				elif b and c and d:
					ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
					p[i + 1][j + 1] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
			elif rts == 2:
				if a and b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					c = 1
					p[i + 1][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					c = 1
					p[i + 1][j] = '1'
				elif b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					c = 0
					p[i + 1][j] = '0'
					a = 1
					p[i][j] = '1'
					b = 1
					p[i][j + 1] = '1'
				elif b and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					c = 1
					p[i + 1][j] = '1'
				if a and b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
				elif a and c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					p[i][j] = '0'
					p[i + 1][j] = '0'
					p[i + 1][j + 1] = '0'
				elif a and b and d:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j + 1] = '0'
				elif b and c and d:
					ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
					p[i + 1][j + 1] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
			elif rts == 1:
				if a:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					b = 1
					c = 1
					p[i][j + 1] = '1'
					p[i][j] = '0'
					p[i + 1][j] = '1'
				elif b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 1
					b = 0
					c = 1
					p[i][j + 1] = '0'
					p[i][j] = '1'
					p[i + 1][j] = '1'
				elif c:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 1
					b = 1
					c = 0
					p[i][j + 1] = '1'
					p[i][j] = '1'
					p[i + 1][j] = '0'
				elif d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 1
					c = 1
					d = 0
					p[i + 1][j + 1] = '0'
					p[i][j] = '1'
					p[i + 1][j] = '1'
				if a and b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					c = 1
					p[i + 1][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					c = 1
					p[i + 1][j] = '1'
				elif b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					c = 0
					p[i + 1][j] = '0'
					a = 1
					p[i][j] = '1'
					b = 1
					p[i][j + 1] = '1'
				elif b and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					c = 1
					p[i + 1][j] = '1'
				if a and b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
				elif a and c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					p[i][j] = '0'
					p[i + 1][j] = '0'
					p[i + 1][j + 1] = '0'
				elif a and b and d:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j + 1] = '0'
				elif b and c and d:
					ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
					p[i + 1][j + 1] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
			elif rts == 4:
				ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
				a = 0
				b = 0
				c = 0
				p[i][j] = '0'
				p[i + 1][j] = '0'
				p[i][j + 1] = '0'
				if a:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					b = 1
					c = 1
					p[i][j + 1] = '1'
					p[i][j] = '0'
					p[i + 1][j] = '1'
				elif b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 1
					b = 0
					c = 1
					p[i][j + 1] = '0'
					p[i][j] = '1'
					p[i + 1][j] = '1'
				elif c:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 1
					b = 1
					c = 0
					p[i][j + 1] = '1'
					p[i][j] = '1'
					p[i + 1][j] = '0'
				elif d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 1
					c = 1
					d = 0
					p[i + 1][j + 1] = '0'
					p[i][j] = '1'
					p[i + 1][j] = '1'
				if a and b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					c = 1
					p[i + 1][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					c = 1
					p[i + 1][j] = '1'
				elif b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					c = 0
					p[i + 1][j] = '0'
					a = 1
					p[i][j] = '1'
					b = 1
					p[i][j + 1] = '1'
				elif b and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					c = 1
					p[i + 1][j] = '1'
				if a and b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
				elif a and c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					p[i][j] = '0'
					p[i + 1][j] = '0'
					p[i + 1][j + 1] = '0'
				elif a and b and d:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j + 1] = '0'
				elif b and c and d:
					ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
					p[i + 1][j + 1] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
	if m % 2:
		j = m - 2
		for i in range(0, n - 1):
			a = int(p[i][j])
			b = int(p[i][j + 1])
			c = int(p[i + 1][j])
			d = int(p[i + 1][j + 1])
			rts = a + b + c + d
			if rts == 0:
				pass
			elif rts == 3:
				if a and b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
				elif a and c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					p[i][j] = '0'
					p[i + 1][j] = '0'
					p[i + 1][j + 1] = '0'
				elif a and b and d:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j + 1] = '0'
				elif b and c and d:
					ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
					p[i + 1][j + 1] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
			elif rts == 2:
				if a and b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					c = 1
					p[i + 1][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					c = 1
					p[i + 1][j] = '1'
				elif b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					c = 0
					p[i + 1][j] = '0'
					a = 1
					p[i][j] = '1'
					b = 1
					p[i][j + 1] = '1'
				elif b and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					c = 1
					p[i + 1][j] = '1'
				if a and b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
				elif a and c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					p[i][j] = '0'
					p[i + 1][j] = '0'
					p[i + 1][j + 1] = '0'
				elif a and b and d:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j + 1] = '0'
				elif b and c and d:
					ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
					p[i + 1][j + 1] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
			elif rts == 1:
				if a:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					b = 1
					c = 1
					p[i][j + 1] = '1'
					p[i][j] = '0'
					p[i + 1][j] = '1'
				elif b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 1
					b = 0
					c = 1
					p[i][j + 1] = '0'
					p[i][j] = '1'
					p[i + 1][j] = '1'
				elif c:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 1
					b = 1
					c = 0
					p[i][j + 1] = '1'
					p[i][j] = '1'
					p[i + 1][j] = '0'
				elif d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 1
					c = 1
					d = 0
					p[i + 1][j + 1] = '0'
					p[i][j] = '1'
					p[i + 1][j] = '1'
				if a and b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					c = 1
					p[i + 1][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					c = 1
					p[i + 1][j] = '1'
				elif b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					c = 0
					p[i + 1][j] = '0'
					a = 1
					p[i][j] = '1'
					b = 1
					p[i][j + 1] = '1'
				elif b and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					c = 1
					p[i + 1][j] = '1'
				if a and b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
				elif a and c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					p[i][j] = '0'
					p[i + 1][j] = '0'
					p[i + 1][j + 1] = '0'
				elif a and b and d:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j + 1] = '0'
				elif b and c and d:
					ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
					p[i + 1][j + 1] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
			elif rts == 4:
				ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
				a = 0
				b = 0
				c = 0
				p[i][j] = '0'
				p[i + 1][j] = '0'
				p[i][j + 1] = '0'
				if a:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					b = 1
					c = 1
					p[i][j + 1] = '1'
					p[i][j] = '0'
					p[i + 1][j] = '1'
				elif b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 1
					b = 0
					c = 1
					p[i][j + 1] = '0'
					p[i][j] = '1'
					p[i + 1][j] = '1'
				elif c:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 1
					b = 1
					c = 0
					p[i][j + 1] = '1'
					p[i][j] = '1'
					p[i + 1][j] = '0'
				elif d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 1
					c = 1
					d = 0
					p[i + 1][j + 1] = '0'
					p[i][j] = '1'
					p[i + 1][j] = '1'
				if a and b:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					c = 1
					p[i + 1][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif a and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					a = 0
					p[i][j] = '0'
					b = 1
					p[i][j + 1] = '1'
					c = 1
					p[i + 1][j] = '1'
				elif b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					d = 1
					p[i + 1][j + 1] = '1'
				elif c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					c = 0
					p[i + 1][j] = '0'
					a = 1
					p[i][j] = '1'
					b = 1
					p[i][j + 1] = '1'
				elif b and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 1, j + 2])
					b = 0
					p[i][j + 1] = '0'
					a = 1
					p[i][j] = '1'
					c = 1
					p[i + 1][j] = '1'
				if a and b and c:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 1])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
				elif a and c and d:
					ans.append([i + 1, j + 1, i + 2, j + 1, i + 2, j + 2])
					p[i][j] = '0'
					p[i + 1][j] = '0'
					p[i + 1][j + 1] = '0'
				elif a and b and d:
					ans.append([i + 1, j + 1, i + 1, j + 2, i + 2, j + 2])
					p[i][j] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j + 1] = '0'
				elif b and c and d:
					ans.append([i + 1, j + 2, i + 2, j + 1, i + 2, j + 2])
					p[i + 1][j + 1] = '0'
					p[i][j + 1] = '0'
					p[i + 1][j] = '0'
	print(len(ans))
	for j in ans:
		print(*j)


def get_nums():
	return [int(x) for x in input().split()]

def transform(mat, r, c):
	if mat[r][c] == mat[r + 1][c] == 0:
		return
	elif mat[r][c] == mat[r + 1][c] == 1:
		step = [(r, c), (r + 1, c)]
		if mat[r][c + 1] == 1:
			step.append((r, c + 1))
		else:
			step.append((r + 1, c + 1))
		return step
	elif mat[r][c] == 1:
		return [(r, c), (r, c + 1), (r + 1, c + 1)]
	else:
		return [(r + 1, c), (r, c + 1), (r + 1, c + 1)]

def dump(mat):
	return
	for row in mat:
		print(row)
	print('-' * 10)

def apply(mat, step):
	for (r, c) in step:
		mat[r][c] = 1 - mat[r][c]
	dump(mat)

def solve(R, C, mat):
	ans = []
	dump(mat)
	for r in range(R - 1):
		for c in range(C - 1):
			step = transform(mat, r, c)
			if step:
				apply(mat, step)
				ans.append(step)
		if r < R - 1 and mat[r][C - 1] == 1:
			fix = [(r, C - 1), (r + 1, C - 2), (r + 1, C - 1)]
			apply(mat, fix)
			ans.append(fix)
	fixes = []
	if mat[R - 1][C - 1] == mat[R - 1][C - 2] == 1:
		fixes = [[(R - 2, C - 2), (R - 2, C - 1), (R - 1, C - 2)], [(R - 2, C - 2), (R - 2, C - 1), (R - 1, C - 1)]]
	elif mat[R - 1][C - 2] == 1:
		fixes = [[(R - 2, C - 2), (R - 1, C - 1), (R - 1, C - 2)], [(R - 2, C - 2), (R - 2, C - 1), (R - 1, C - 2)], [(R - 2, C - 1), (R - 1, C - 2), (R - 1, C - 1)]]
	elif mat[R - 1][C - 1] == 1:
		fixes = [[(R - 2, C - 1), (R - 1, C - 1), (R - 1, C - 2)], [(R - 2, C - 2), (R - 2, C - 1), (R - 1, C - 1)], [(R - 2, C - 2), (R - 1, C - 2), (R - 1, C - 1)]]
	for f in fixes:
		apply(mat, f)
		ans.append(f)
	return ans
(T,) = get_nums()
for _ in range(T):
	mat = []
	(R, C) = get_nums()
	for _ in range(R):
		mat.append([int(x) for x in list(input())])
	ans = solve(R, C, mat)
	print(len(ans))
	for step in ans:
		print(' '.join((str(x + 1) for move in step for x in move)))


import io, os
from math import *

def move(i, j, n, m):
	if i < n - 1 and j < m - 1:
		return [(i + 1, j + 1, i + 2, j + 1, i + 1, j + 2), (i + 1, j + 1, i + 2, j + 1, i + 2, j + 2), (i + 1, j + 1, i + 2, j + 2, i + 1, j + 2)]
	elif i > 0 and j > 0:
		return [(i + 1, j + 1, i + 1, j, i, j + 1), (i, j, i + 1, j, i + 1, j + 1), (i, j, i + 1, j + 1, i, j + 1)]
	elif i == 0 and j == m - 1:
		return [(i + 1, j + 1, i + 2, j + 1, i + 2, j), (i + 1, j + 1, i + 1, j, i + 2, j), (i + 1, j + 1, i + 2, j + 1, i + 1, j)]
	else:
		return [(i + 1, j + 1, i, j + 1, i + 1, j + 2), (i + 1, j + 1, i + 1, j + 2, i, j + 2), (i + 1, j + 1, i, j + 1, i, j + 2)]

def main():
	t = int(input())
	for _ in range(t):
		(n, m) = list(map(int, input().split()))
		a = [input() for _ in range(n)]
		moves = []
		for i in range(n):
			for j in range(m):
				if a[i][j] == '1':
					moves.append(move(i, j, n, m))
		print(len(moves) * 3)
		for i in range(len(moves)):
			for m in moves[i]:
				print(str(m).replace(',', '').replace('(', '').replace(')', ''))
main()


def fun1(xx):
	global ar, ans, mai
	zer = []
	one = []
	for i in xx:
		if ar[i[0]][i[1]] == 0:
			zer.append([i[0], i[1]])
		else:
			one.append([i[0], i[1]])
	ans += 2
	mai.append([])
	for i in zer:
		mai[-1].append(i[0] + 1)
		mai[-1].append(i[1] + 1)
		ar[i[0]][i[1]] = 1
	for i in one:
		mai[-1].append(i[0] + 1)
		mai[-1].append(i[1] + 1)
		ar[i[0]][i[1]] = 0
		break
	del one[0]
	mai.append([])
	for i in zer:
		mai[-1].append(i[0] + 1)
		mai[-1].append(i[1] + 1)
		ar[i[0]][i[1]] = 0
	for i in one:
		mai[-1].append(i[0] + 1)
		mai[-1].append(i[1] + 1)
		ar[i[0]][i[1]] = 0

def fun2(xx):
	global ar, ans, mai
	zer = []
	one = []
	for i in xx:
		if ar[i[0]][i[1]] == 0:
			zer.append([i[0], i[1]])
		else:
			one.append([i[0], i[1]])
	ans += 1
	mai.append([])
	zer.pop()
	for i in zer:
		mai[-1].append(i[0] + 1)
		mai[-1].append(i[1] + 1)
		ar[i[0]][i[1]] = 1
	for i in one:
		mai[-1].append(i[0] + 1)
		mai[-1].append(i[1] + 1)
		ar[i[0]][i[1]] = 0
	fun1(xx)

def fun3(xx):
	global ar, ans, mai
	zer = []
	one = []
	for i in xx:
		if ar[i[0]][i[1]] == 0:
			zer.append([i[0], i[1]])
		else:
			one.append([i[0], i[1]])
	ans += 1
	mai.append([])
	for i in one:
		mai[-1].append(i[0] + 1)
		mai[-1].append(i[1] + 1)
		ar[i[0]][i[1]] = 0
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	ar = []
	ans = 0
	mai = []
	for i in range(n):
		ar.append(list(map(int, list(input()))))
	for i in range(n - 1):
		for j in range(m - 1):
			zero = 0
			one = 0
			if ar[i][j] == 0:
				zero += 1
			else:
				one += 1
			if ar[i + 1][j] == 0:
				zero += 1
			else:
				one += 1
			if ar[i][j + 1] == 0:
				zero += 1
			else:
				one += 1
			if ar[i + 1][j + 1] == 0:
				zero += 1
			else:
				one += 1
			if one == 4:
				ans += 1
				mai.append([])
				mai[-1].append(i + 1)
				mai[-1].append(j + 1)
				mai[-1].append(i + 2)
				mai[-1].append(j + 1)
				mai[-1].append(i + 1)
				mai[-1].append(j + 2)
				ar[i][j] = 0
				ar[i][j + 1] = 0
				ar[i + 1][j] = 0
			elif one == 2 and zero == 2:
				fun1([[i, j], [i, j + 1], [i + 1, j], [i + 1, j + 1]])
			elif zero == 3 and one == 1:
				fun2([[i, j], [i, j + 1], [i + 1, j], [i + 1, j + 1]])
			elif one == 3:
				fun3([[i, j], [i, j + 1], [i + 1, j], [i + 1, j + 1]])
	zero = 0
	one = 0
	if ar[i][j] == 0:
		zero += 1
	else:
		one += 1
	if ar[i + 1][j] == 0:
		zero += 1
	else:
		one += 1
	if ar[i][j + 1] == 0:
		zero += 1
	else:
		one += 1
	if ar[i + 1][j + 1] == 0:
		zero += 1
	else:
		one += 1
	if one == 4:
		ans += 1
		mai.append([])
		mai[-1].append(i + 1)
		mai[-1].append(j + 1)
		mai[-1].append(i + 2)
		mai[-1].append(j + 1)
		mai[-1].append(i + 1)
		mai[-1].append(j + 2)
		ar[i][j] = 0
		ar[i][j + 1] = 0
		ar[i + 1][j] = 0
	elif one == 2 and zero == 2:
		fun1([[i, j], [i, j + 1], [i + 1, j], [i + 1, j + 1]])
	elif zero == 3 and one == 1:
		fun2([[i, j], [i, j + 1], [i + 1, j], [i + 1, j + 1]])
	elif one == 3:
		fun3([[i, j], [i, j + 1], [i + 1, j], [i + 1, j + 1]])
	print(ans)
	for i in mai:
		print(*i)


d = {'0': 0, '1': 1}
cases = int(input())
for t in range(cases):
	(n, m) = list(map(int, input().split()))
	matrix = []
	for i in range(n):
		matrix.append([d[i] for i in input()])
	k = 0
	out = []
	for i in range(n - 1):
		for j in range(m):
			if matrix[i][j] == 1:
				k += 1
				if j < m - 1:
					matrix[i][j] = 0
					matrix[i + 1][j] = matrix[i + 1][j] ^ 1
					matrix[i + 1][j + 1] = matrix[i + 1][j + 1] ^ 1
					out.append([i, j, i + 1, j, i + 1, j + 1])
				elif j == m - 1:
					matrix[i][j] = 0
					matrix[i + 1][j] = matrix[i + 1][j] ^ 1
					matrix[i + 1][j - 1] = matrix[i + 1][j - 1] ^ 1
					out.append([i, j, i + 1, j, i + 1, j - 1])
	for i in range(0, m - 1, 2):
		(x1, x2, x3, x4) = (matrix[n - 2][i], matrix[n - 2][i + 1], matrix[n - 1][i], matrix[n - 1][i + 1])
		if x3 == 0 and x4 == 0:
			continue
		elif x3 == 1 and x4 == 1:
			out.append([n - 2, i, n - 2, i + 1, n - 1, i])
			out.append([n - 2, i, n - 2, i + 1, n - 1, i + 1])
			(matrix[n - 2][i], matrix[n - 2][i + 1], matrix[n - 1][i], matrix[n - 1][i + 1]) = (0, 0, 0, 0)
			k += 2
		elif x3 == 0 and x4 == 1:
			out.append([n - 2, i, n - 2, i + 1, n - 1, i + 1])
			out.append([n - 2, i, n - 1, i, n - 1, i + 1])
			out.append([n - 2, i + 1, n - 1, i, n - 1, i + 1])
			(matrix[n - 2][i], matrix[n - 2][i + 1], matrix[n - 1][i], matrix[n - 1][i + 1]) = (0, 0, 0, 0)
			k += 3
		elif x3 == 1 and x4 == 0:
			out.append([n - 2, i, n - 2, i + 1, n - 1, i])
			out.append([n - 2, i, n - 1, i, n - 1, i + 1])
			out.append([n - 2, i + 1, n - 1, i, n - 1, i + 1])
			(matrix[n - 2][i], matrix[n - 2][i + 1], matrix[n - 1][i], matrix[n - 1][i + 1]) = (0, 0, 0, 0)
			k += 3
	if m % 2 == 1 and matrix[-1][-1] == 1:
		out.append([n - 2, m - 2, n - 2, m - 1, n - 1, m - 1])
		out.append([n - 2, m - 2, n - 1, m - 2, n - 1, m - 1])
		out.append([n - 2, m - 1, n - 1, m - 2, n - 1, m - 1])
		k += 3
	print(k)
	for (x1, y1, x2, y2, x3, y3) in out:
		print(x1 + 1, y1 + 1, x2 + 1, y2 + 1, x3 + 1, y3 + 1)


def oneSolve(ones, zeros, M, ans):
	(x1, y1) = ones.pop()
	(x2, y2) = zeros.pop()
	(x3, y3) = zeros.pop()
	M[x1][y1] = 0
	M[x2][y2] = 1
	M[x3][y3] = 1
	zeros.append([x1, y1])
	ones.append([x2, y2])
	ones.append([x3, y3])
	ans.append([x1, y1, x2, y2, x3, y3])
	twoSolve(ones, zeros, M, ans)

def twoSolve(ones, zeros, M, ans):
	(x1, y1) = ones.pop()
	(x2, y2) = zeros.pop()
	(x3, y3) = zeros.pop()
	M[x1][y1] = 0
	M[x2][y2] = 1
	M[x3][y3] = 1
	zeros.append([x1, y1])
	ones.append([x2, y2])
	ones.append([x3, y3])
	ans.append([x1, y1, x2, y2, x3, y3])
	threeSolve(ones, zeros, M, ans)

def threeSolve(ones, zeros, M, ans):
	L = list()
	for _ in range(3):
		(x, y) = ones.pop()
		zeros.append([x, y])
		M[x][y] = 0
		L.append(x)
		L.append(y)
	ans.append(L)

def fourSolve(ones, zeros, M, ans):
	L = list()
	for _ in range(3):
		(x, y) = ones.pop()
		zeros.append([x, y])
		M[x][y] = 0
		L.append(x)
		L.append(y)
	ans.append(L)
	oneSolve(ones, zeros, M, ans)

def solver(x, y, M, ans):
	pos = [[x, y], [x + 1, y], [x, y + 1], [x + 1, y + 1]]
	ones = list()
	zeros = list()
	for (i, j) in pos:
		if M[i][j] == 1:
			ones.append([i, j])
		else:
			zeros.append([i, j])
	c = len(ones)
	if c == 0:
		return
	if c == 1:
		oneSolve(ones, zeros, M, ans)
	elif c == 2:
		twoSolve(ones, zeros, M, ans)
	elif c == 3:
		threeSolve(ones, zeros, M, ans)
	else:
		fourSolve(ones, zeros, M, ans)
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	M = list()
	for _ in range(n):
		s = input()
		l = list()
		for c in s:
			l.append(int(c))
		M.append(l)
	if n % 2 == 0:
		R = n
	else:
		R = n - 1
	if m % 2 == 0:
		C = m
	else:
		C = m - 1
	ans = list()
	i = 0
	while i < R:
		j = 0
		while j < C:
			solver(i, j, M, ans)
			j += 2
		i += 2
	if n % 2 == 1:
		j = 0
		while j < C:
			solver(n - 2, j, M, ans)
			j += 2
	if m % 2 == 1:
		i = 0
		while i < R:
			solver(i, m - 2, M, ans)
			i += 2
	if n % 2 == 1 and m % 2 == 1:
		solver(n - 2, m - 2, M, ans)
	print(len(ans))
	for line in ans:
		for pos in line:
			print(pos + 1, end=' ')
		print()


from sys import stdin
for _ in ' ' * int(input()):
	(a, b) = map(int, input().split())
	z = []
	for i in range(a):
		z.append(input().replace('', ' ').split())
	k = []
	s = 0
	for i in range(a - 1):
		for j in range(b - 1):
			o = (z[i][j] == '1') + (z[i][j + 1] == '1') + (z[i + 1][j] == '1') + (z[i + 1][j + 1] == '1')
			p = [[z[i][j], [i + 1, j + 1]], [z[i][j + 1], [i + 1, j + 2]], [z[i + 1][j], [i + 2, j + 1]], [z[i + 1][j + 1], [i + 2, j + 2]]]
			p.sort(reverse=True)
			if o == 4:
				k.append([p[1][1][0], p[1][1][1], p[2][1][0], p[2][1][1], p[3][1][0], p[3][1][1]])
				k.append([p[0][1][0], p[0][1][1], p[1][1][0], p[1][1][1], p[2][1][0], p[2][1][1]])
				k.append([p[0][1][0], p[0][1][1], p[1][1][0], p[1][1][1], p[3][1][0], p[3][1][1]])
				k.append([p[0][1][0], p[0][1][1], p[2][1][0], p[2][1][1], p[3][1][0], p[3][1][1]])
			if o == 3:
				k.append([p[0][1][0], p[0][1][1], p[1][1][0], p[1][1][1], p[2][1][0], p[2][1][1]])
			if o == 2:
				k.append([p[0][1][0], p[0][1][1], p[2][1][0], p[2][1][1], p[3][1][0], p[3][1][1]])
				k.append([p[1][1][0], p[1][1][1], p[2][1][0], p[2][1][1], p[3][1][0], p[3][1][1]])
			if o == 1:
				k.append([p[0][1][0], p[0][1][1], p[1][1][0], p[1][1][1], p[2][1][0], p[2][1][1]])
				k.append([p[0][1][0], p[0][1][1], p[1][1][0], p[1][1][1], p[3][1][0], p[3][1][1]])
				k.append([p[0][1][0], p[0][1][1], p[2][1][0], p[2][1][1], p[3][1][0], p[3][1][1]])
			z[i][j] = z[i][j + 1] = z[i + 1][j] = z[i + 1][j + 1] = '0'
	print(len(k))
	for i in k:
		print(*i)


d = {7: (0, 4, 4), 11: (1, 4, 4), 13: (2, 4, 4), 14: (3, 4, 4), 0: (4, 4, 4), 1: (0, 1, 2), 2: (0, 1, 3), 12: (0, 1, 4), 4: (0, 2, 3), 10: (0, 2, 4), 9: (0, 3, 4), 8: (1, 2, 3), 6: (1, 2, 4), 5: (1, 3, 4), 3: (2, 3, 4)}
dif = [(0, 0), (0, 1), (1, 0), (1, 1)]

def solve():
	(n, m) = map(int, input().split())
	saver = []
	data = [list(map(int, list(input()))) for i in range(n)]

	def output(index, i, j):
		if index == 4:
			return []
		else:
			res = []
			for k in range(4):
				if k != index:
					(dx, dy) = dif[k]
					res.append(i + 1 + dx)
					res.append(j + 1 + dy)
					data[i + dx][j + dy] ^= 1
			return tuple(res)

	def change(nx, ny, old_list, new_list, saver):
		command = 0
		for i in range(4):
			if old_list[-(i + 1)] != new_list[-(i + 1)]:
				command += 1 << i
		demand = d[command]
		for v in demand:
			result = output(v, nx, ny)
			if result:
				saver.append(result)
	for i in range(n - 1):
		for j in range(m - 1):
			if i == 0 and j == 0:
				old_list = [data[i + dx][j + dy] for (dx, dy) in dif]
				if sum(old_list) == 4:
					saver.append((1, 1, 1, 2, 2, 1))
					data[0][0] = 0
					data[0][1] = 0
					data[1][0] = 0
			old_list = [data[i + dx][j + dy] for (dx, dy) in dif]
			change(i, j, old_list, [0, 0, 0, 0], saver)
	print(len(saver))
	for v in saver:
		print(*v)
	return

def main():
	t = int(input())
	for i in range(t):
		solve()
	return
main()