//khodaya khodet komak kon
# include <bits/stdc++.h>

/*
// ordered_set
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
# define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
*/

using namespace std;

typedef long long                                        ll;
typedef long double                                      ld;
typedef pair <int, int>                                  pii;
typedef pair <pii, int>                                  ppi;
typedef pair <int, pii>                                  pip;
typedef pair <pii, pii>                                  ppp;
typedef pair <ll, ll>                                    pll;

# define A                                               first
# define B                                               second
# define endl                                            '\n'
# define sep                                             ' '
# define all(x)                                          x.begin(), x.end()
# define kill(x)                                         return cout << x << endl, 0
# define SZ(x)                                           int(x.size())
# define lc                                              id << 1
# define rc                                              id << 1 | 1
# define InTheNameOfGod                                  ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);

ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}

const int xn = 1e3 + 10;
const int xm = - 20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const int mod = 998244353;
const int base = 257;

int qq, n, m, a[xn][xn], ans[6][xn * xn], ptr;
string s;

void add(int x1, int y1, int x2, int y2, int x3, int y3){
	ans[0][ptr] = x1;
	ans[1][ptr] = y1;
	ans[2][ptr] = x2;
	ans[3][ptr] = y2;
	ans[4][ptr] = x3;
	ans[5][ptr] = y3;
	a[x1][y1] ^= 1;
	a[x2][y2] ^= 1;
	a[x3][y3] ^= 1;
	++ ptr;
}

int main(){
	//InTheNameOfGod;

	cin >> qq;
	while (qq --){
		cin >> n >> m;
		for (int i = 1; i <= n; ++ i){
			cin >> s;
			for (int j = 1; j <= m; ++ j)
				a[i][j] = s[j - 1] - '0';
		}
		ptr = 0;
		if (n % 2 || m % 2){
			if (a[n][m])
				add(n, m, n - 1, m, n, m - 1);
			if (n % 2){
				for (int i = 1; i < m; ++ i){
					if (a[n][i]){
						if (m == 2)
							add(n, i, n - 1, i, n - 1, i + 1);
						else if (i < m - 1)
							add(n, i, n - 1, i, n - 1, i + 1);
						else
							add(n, i, n - 1, i, n - 1, i - 1);
					}
				}
			}
			if (m % 2){
				for (int i = 1; i < n; ++ i){
					if (a[i][m]){
						if (n == 2)
							add(i, m, i, m - 1, i + 1, m - 1);
						else if (i < n - 1)
							add(i, m, i, m - 1, i + 1, m - 1);
						else
							add(i, m, i, m - 1, i - 1, m - 1);
					}
				}
			}
		}
		for (int i = 1; i <= n - n % 2; i += 2){
			for (int j = 1; j <= m - m % 2; j += 2){
				//cout << i << " : " << j << endl;
				while (true){
					int ted = a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1];
					//cout << ted << endl;
					if (!ted)
						break;
					if (ted == 4){
						add(i, j, i + 1, j, i, j + 1);
						continue;
					}
					vector <pii> vec[2];
					if (a[i][j])
						vec[1].push_back({i, j});
					else
						vec[0].push_back({i, j});
					if (a[i + 1][j])
						vec[1].push_back({i + 1, j});
					else
						vec[0].push_back({i + 1, j});
					if (a[i][j + 1])
						vec[1].push_back({i, j + 1});
					else
						vec[0].push_back({i, j + 1});
					if (a[i + 1][j + 1])
						vec[1].push_back({i + 1, j + 1});
					else
						vec[0].push_back({i + 1, j + 1});
					if (ted < 3)
						add(vec[1][0].A, vec[1][0].B, vec[0][0].A, vec[0][0].B, vec[0][1].A, vec[0][1].B);
					else
						add(vec[1][0].A, vec[1][0].B, vec[1][1].A, vec[1][1].B, vec[1][2].A, vec[1][2].B);
				}
			}
		}
		cout << ptr << endl;
		for (int i = 0; i < ptr; ++ i){
			for (int j = 0; j < 6; ++ j)
				cout << ans[j][i] << sep;
			cout << endl;
		}
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(x) memset(x,0,sizeof x)
int T,n,m;
bool tab[110][110];
char p[110];
struct op{
	int x,y,xx,yy,xxx,yyy;
}stk[1000010],A;
int nowp;
void ope(int x,int y,int xx,int yy,int xxx,int yyy){
	stk[++nowp]={x,y,xx,yy,xxx,yyy};
}
int main(){
	cin>>T;

	while(T--){
		nowp=0;
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			scanf("%s",p+1);
			for(int j=1;j<=m;j++) tab[i][j]=p[i]-'0';
		}
		for(int i=1;i<n;i++){
			for(int j=1;j<m;j++){
				if(tab[i][j]){
					ope(i,j,i+1,j,i,j+1);
					ope(i,j,i+1,j,i+1,j+1);
					ope(i,j,i,j+1,i+1,j+1);
				}
			}
		}
		for(int i=1;i<n;i++){
			if(tab[i][m]){
				ope(i,m,i+1,m,i,m-1);
				ope(i,m,i+1,m,i+1,m-1);
				ope(i,m,i,m-1,i+1,m-1);
			}
		}
		for(int j=1;j<m;j++){
			if(tab[n][j]){
				ope(n,j,n-1,j,n,j+1);
				ope(n,j,n-1,j,n-1,j+1);
				ope(n,j,n,j+1,n-1,j+1);
			}
		}
		if(tab[n][m]){
			ope(n,m,n-1,m-1,n,m-1);ope(n,m,n-1,m,n-1,m-1);ope(n,m,n,m-1,n-1,m-1);
		}
		cout<<nowp<<endl;
		for(int i=1;i<=nowp;i++){
			A=stk[i];
			cout<<A.x<<" "<<A.y<<" "<<A.xx<<" "<<A.yy<<" "<<A.xxx<<" "<<A.yyy<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(x) memset(x,0,sizeof x)
int T,n,m;
bool tab[110][110];
char p[110];
struct op{
	int x,y,xx,yy,xxx,yyy;
}stk[1000010],A;
int nowp;
void ope(int x,int y,int xx,int yy,int xxx,int yyy){
	stk[++nowp]={x,y,xx,yy,xxx,yyy};
}
int main(){
	cin>>T;

	while(T--){
		nowp=0;
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			scanf("%s",p+1);
			for(int j=1;j<=m;j++) tab[i][j]=p[i]-'0';
		}
		for(int i=1;i<n;i++){
			for(int j=1;j<m;j++){
				if(tab[i][j]){
					ope(i,j,i+1,j,i,j+1);
					ope(i,j,i+1,j,i+1,j+1);
					ope(i,j,i,j+1,i+1,j+1);
				}
			}
		}
		for(int i=1;i<n;i++){
			if(tab[i][m]){
				ope(i,m,i+1,m,i,m-1);
				ope(i,m,i+1,m,i+1,m-1);
				ope(i,m,i,m-1,i+1,m-1);
			}
		}
		for(int j=1;j<m;j++){
			if(tab[n][j]){
				ope(n,j,n-1,j,n,j+1);
				ope(n,j,n-1,j,n-1,j+1);
				ope(n,j,n,j+1,n-1,j+1);
			}
		}
		if(tab[n][m]){
			ope(n,m,n-1,m,n,m-1);
			ope(n,m,n-1,m,n-1,m-1);
			ope(n,m,n,m-1,n-1,m-1);
		}
		cout<<nowp<<endl;
		for(int i=1;i<=nowp;i++){
			A=stk[i];
			cout<<A.x<<" "<<A.y<<" "<<A.xx<<" "<<A.yy<<" "<<A.xxx<<" "<<A.yyy<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(x) memset(x,0,sizeof x)
int T,n,m;
bool tab[110][110];
char p[110];
struct op{
	int x,y,xx,yy,xxx,yyy;
}stk[1000010],A;
int nowp;
void ope(int x,int y,int xx,int yy,int xxx,int yyy){
	stk[++nowp]={x,y,xx,yy,xxx,yyy};
}
int main(){
	cin>>T;

	while(T--){
		nowp=0;
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			scanf("%s",p+1);
			for(int j=1;j<=m;j++) tab[i][j]=p[j]-'0';
		}
	//	cout<<tab[1][2]<<endl;
		for(int i=1;i<n;i++){
			for(int j=1;j<m;j++){
				if(tab[i][j]){
					ope(i,j,i+1,j,i,j+1);
					ope(i,j,i+1,j,i+1,j+1);
					ope(i,j,i,j+1,i+1,j+1);
				}
			}
		}
		for(int i=1;i<n;i++){
			if(tab[i][m]){
				ope(i,m,i+1,m,i,m-1);
				ope(i,m,i+1,m,i+1,m-1);
				ope(i,m,i,m-1,i+1,m-1);
			}
		}
		for(int j=1;j<m;j++){
			if(tab[n][j]){
				ope(n,j,n-1,j,n,j+1);
				ope(n,j,n-1,j,n-1,j+1);
				ope(n,j,n,j+1,n-1,j+1);
			}
		}
		if(tab[n][m]){
			ope(n,m,n-1,m,n,m-1);
			ope(n,m,n-1,m,n-1,m-1);
			ope(n,m,n,m-1,n-1,m-1);
		}
		cout<<nowp<<endl;
		for(int i=1;i<=nowp;i++){
			A=stk[i];
			cout<<A.x<<" "<<A.y<<" "<<A.xx<<" "<<A.yy<<" "<<A.xxx<<" "<<A.yyy<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std ;
#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )
#define rep( i, s, t ) for( register int i = (s); i <= (t); ++ i )
#define drep( i, s, t ) for( register int i = (t); i >= (s); -- i )
#define re register
int gi() {
	char cc = getchar() ; int cn = 0, flus = 1 ;
	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }
	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;
	return cn * flus ;
}
const int N = 100 + 5 ;
const int M = 1e5 + 5 ;
int T, n, m, cnt ;
char s[N][N] ;
struct node {
	int x, y ;
	node(int _x = 0, int _y = 0) { x = _x, y = _y ; }
};
struct Opt {
	node a[3] ;
} opt[M] ;
void Make(Opt &u, node x, node y, node z) {
	u.a[0] = x, u.a[1] = y, u.a[2] = z ;
	//puts("Maker") ;
	rep( i, 0, 2 ) s[u.a[i].x][u.a[i].y] ^= 1 ;
}
void solve() {
	n = gi(), m = gi(), cnt = 0 ;
	rep( i, 1, n ) scanf("%s", s[i] + 1 ) ;
	rep( i, 1, n ) rep( j, 1, m ) s[i][j] -= '0' ;
	rep( i, 1, n - 1 ) {
		for(re int j = 1; j < m; ++ j) {
	//		printf("qaq %d %d %d\n", i, j, s[i][j] ) ;
			if( s[i][j] && s[i][j + 1] )
				++ cnt, Make(opt[cnt], node(i, j), node(i, j + 1), node(i + 1, j + 1)) ;
			else if( s[i][j] ) ++ cnt, Make(opt[cnt], node(i, j), node(i + 1, j), node(i + 1, j + 1)) ;
		}
		if( s[i][m] ) ++ cnt, Make(opt[cnt], node(i, m), node(i + 1, m - 1), node(i + 1, m)) ;
	}
	for(re int j = 1; j <= m; ++ j) {
		if( s[n][j] && s[n][j + 1] )
			++ cnt, Make(opt[cnt], node(n, j), node(n - 1, j), node(n - 1, j + 1) ),
			++ cnt, Make(opt[cnt], node(n, j + 1), node(n - 1, j), node(n - 1, j + 1) ) ;
		else if( s[n][j] ) {
			if( j != m ) {
				++ cnt, Make(opt[cnt], node(n, j), node(n - 1, j), node(n - 1, j + 1) ),
				++ cnt, Make(opt[cnt], node(n - 1, j), node(n, j), node(n, j + 1) ),
				++ cnt, Make(opt[cnt], node(n - 1, j + 1), node(n, j), node(n, j + 1) ) ;
			}
			else {
				++ cnt, Make(opt[cnt], node(n, j), node(n - 1, j), node(n - 1, j - 1) ),
				++ cnt, Make(opt[cnt], node(n - 1, j), node(n, j), node(n, j - 1) ),
				++ cnt, Make(opt[cnt], node(n - 1, j - 1), node(n, j), node(n, j - 1) ) ;
			}
		}
		s[n][j] = 0 ;
	}
	printf("%d\n", cnt ) ;
	rep( i, 1, cnt ) {
		rep( j, 0, 2 ) printf("%d %d ", opt[i].a[j].x, opt[i].a[j].y ) ;
		puts("") ;
	}
}
signed main()
{
	T = gi() ;
	while( T-- ) solve() ;
	return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )
#define rep( i, s, t ) for( register int i = (s); i <= (t); ++ i )
#define drep( i, s, t ) for( register int i = (t); i >= (s); -- i )
#define re register
int gi() {
	char cc = getchar() ; int cn = 0, flus = 1 ;
	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }
	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;
	return cn * flus ;
}
const int N = 100 + 5 ;
const int M = 1e5 + 5 ;
int T, n, m, cnt ;
char s[N][N], f[N][N] ;
struct node {
	int x, y ;
	node(int _x = 0, int _y = 0) { x = _x, y = _y ; }
};
struct Opt {
	node a[3] ;
} opt[M] ;
void Make(Opt &u, node x, node y, node z) {
	u.a[0] = x, u.a[1] = y, u.a[2] = z ;
	rep( i, 0, 2 ) s[u.a[i].x][u.a[i].y] ^= 1 ;
}
void Solve() {
	cnt = 0 ;
	rep( i, 1, n - 1 ) {
		for(re int j = 1; j < m; ++ j) {
			if( s[i][j] && s[i][j + 1] )
				++ cnt, Make(opt[cnt], node(i, j), node(i, j + 1), node(i + 1, j + 1)) ;
			else if( s[i][j] ) ++ cnt, Make(opt[cnt], node(i, j), node(i + 1, j), node(i + 1, j + 1)) ;
		}
		if( s[i][m] ) ++ cnt, Make(opt[cnt], node(i, m), node(i + 1, m - 1), node(i + 1, m)) ;
	}
	for(re int j = 1; j <= m; ++ j) {
//		printf("now case %d (%d %d %d)\n", j, s[n][j], s[n][j + 1], s[n][j + 2] ) ;
		if( s[n][j] && s[n][j + 1] && s[n][j + 2] )
			++ cnt, Make(opt[cnt], node(n, j), node(n - 1, j + 1), node(n, j + 1) ),
			++ cnt, Make(opt[cnt], node(n, j + 2), node(n, j + 1), node(n - 1, j + 2) ),
			++ cnt, Make(opt[cnt], node(n, j + 1), node(n - 1, j + 2), node(n - 1, j + 1) ) ;
		else if( s[n][j] && (!s[n][j + 1]) && s[n][j + 2] )
			++ cnt, Make(opt[cnt], node(n, j), node(n - 1, j + 1), node(n, j + 1) ),
			++ cnt, Make(opt[cnt], node(n, j + 2), node(n - 1, j + 1), node(n, j + 1) ) ;
		else if( s[n][j] && s[n][j + 1] )
			++ cnt, Make(opt[cnt], node(n, j), node(n - 1, j), node(n - 1, j + 1) ),
			++ cnt, Make(opt[cnt], node(n, j + 1), node(n - 1, j), node(n - 1, j + 1) ) ;
		else if( s[n][j] ) {
			if( j != m ) {
				++ cnt, Make(opt[cnt], node(n, j), node(n - 1, j), node(n - 1, j + 1) ),
				++ cnt, Make(opt[cnt], node(n - 1, j), node(n, j), node(n, j + 1) ),
				++ cnt, Make(opt[cnt], node(n - 1, j + 1), node(n, j), node(n, j + 1) ) ;
			}
			else {
				++ cnt, Make(opt[cnt], node(n, j), node(n - 1, j), node(n - 1, j - 1) ),
				++ cnt, Make(opt[cnt], node(n - 1, j), node(n, j), node(n, j - 1) ),
				++ cnt, Make(opt[cnt], node(n - 1, j - 1), node(n, j), node(n, j - 1) ) ;
			}
		}
		s[n][j] = 0 ;
	}
}
void solve() {
	n = gi(), m = gi(), cnt = 0 ;
	rep( i, 1, n ) scanf("%s", f[i] + 1 ) ;
	rep( i, 1, n ) rep( j, 1, m ) f[i][j] -= '0' ;
	rep( i, 1, n ) rep( j, 1, m ) s[i][j] = f[i][j] ;
	int flag = 0 ;
	if( n < m ) {
		swap( n, m ) ;
		rep( i, 1, n ) rep( j, 1, m ) s[i][j] = f[j][i] ;
		flag = 1 ;
	}
	Solve() ;
	printf("%d\n", cnt ) ;
	rep( i, 1, cnt ) {
		if( !flag ) {
			rep( j, 0, 2 ) printf("%d %d ", opt[i].a[j].x, opt[i].a[j].y ) ;
			puts("") ;
		}
		else {
			rep( j, 0, 2 ) printf("%d %d ", opt[i].a[j].y, opt[i].a[j].x ) ;
			puts("") ;
		}
	}
}
signed main()
{
	T = gi() ;
	while( T-- ) solve() ;
	return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )
#define rep( i, s, t ) for( register int i = (s); i <= (t); ++ i )
#define drep( i, s, t ) for( register int i = (t); i >= (s); -- i )
#define re register
int gi() {
	char cc = getchar() ; int cn = 0, flus = 1 ;
	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }
	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;
	return cn * flus ;
}
const int N = 100 + 5 ;
const int M = 1e5 + 5 ;
int T, n, m, cnt ;
char s[N][N] ;
struct node {
	int x, y ;
	node(int _x = 0, int _y = 0) { x = _x, y = _y ; }
};
struct Opt {
	node a[3] ;
} opt[M] ;
void Make(Opt &u, node x, node y, node z) {
	u.a[0] = x, u.a[1] = y, u.a[2] = z ;
	//puts("Maker") ;
	//rep( i, 0, 2 ) s[u.a[i].x][u.a[i].y] ^= 1, printf("Make (%d %d)\n", u.a[i].x, u.a[i].y ) ;
}
void solve() {
	n = gi(), m = gi(), cnt = 0 ;
	rep( i, 1, n ) scanf("%s", s[i] + 1 ) ;
	rep( i, 1, n ) rep( j, 1, m ) s[i][j] -= '0' ;
	rep( i, 1, n - 1 ) {
		for(re int j = 1; j < m; ++ j) {
		//	printf("qaq %d %d %d\n", i, j, s[i][j] ) ;
			if( s[i][j] && s[i][j + 1] )
				++ cnt, Make(opt[cnt], node(i, j), node(i, j + 1), node(i + 1, j + 1)) ;
			else if( s[i][j] ) ++ cnt, Make(opt[cnt], node(i, j), node(i + 1, j), node(i + 1, j + 1)) ;
			s[i][j] = 0 ;
		}
		if( s[i][m] ) ++ cnt, Make(opt[cnt], node(i, m), node(i + 1, m - 1), node(i + 1, m)) ;
	}
	for(re int j = 1; j <= m; ++ j) {
		if( s[n][j] && s[n][j + 1] )
			++ cnt, Make(opt[cnt], node(n, j), node(n - 1, j), node(n - 1, j + 1) ),
			++ cnt, Make(opt[cnt], node(n, j + 1), node(n - 1, j), node(n - 1, j + 1) ) ;
		else if( s[n][j] ) {
			if( j != m ) {
				++ cnt, Make(opt[cnt], node(n, j), node(n - 1, j), node(n - 1, j + 1) ),
				++ cnt, Make(opt[cnt], node(n - 1, j), node(n, j), node(n, j + 1) ),
				++ cnt, Make(opt[cnt], node(n - 1, j + 1), node(n, j), node(n, j + 1) ) ;
			}
			else {
				++ cnt, Make(opt[cnt], node(n, j), node(n - 1, j), node(n - 1, j - 1) ),
				++ cnt, Make(opt[cnt], node(n - 1, j), node(n, j), node(n, j - 1) ),
				++ cnt, Make(opt[cnt], node(n - 1, j - 1), node(n, j), node(n, j - 1) ) ;
			}
		}
		s[n][j] = 0 ;
	}
	printf("%d\n", cnt ) ;
	rep( i, 1, cnt ) {
		rep( j, 0, 2 ) printf("%d %d ", opt[i].a[j].x, opt[i].a[j].y ) ;
		puts("") ;
	}
}
signed main()
{
	T = gi() ;
	while( T-- ) solve() ;
	return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
char s[110][110];
int main(){
    int t; scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
        int ans=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(s[i][j]=='1') ans++;
        printf("%d\n",ans*3);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(s[i][j]=='1'){
                    if(i<n&&j<m){
                        printf("%d %d %d %d %d %d\n",i,j,i,j+1,i+1,j);
                        printf("%d %d %d %d %d %d\n",i,j,i,j+1,i+1,j+1);
                        printf("%d %d %d %d %d %d\n",i,j,i+1,j,i+1,j+1);
                    } else if(i<n&&j==m){
                        printf("%d %d %d %d %d %d\n",i,j,i,j-1,i+1,j);
                        printf("%d %d %d %d %d %d\n",i,j,i,j-1,i+1,j-1);
                        printf("%d %d %d %d %d %d\n",i,j,i+1,j,i+1,j-1);
                    } else if(i==n&&j<m){
                        printf("%d %d %d %d %d %d\n",i,j,i,j+1,i-1,j);
                        printf("%d %d %d %d %d %d\n",i,j,i,j+1,i-1,j+1);
                        printf("%d %d %d %d %d %d\n",i,j,i-1,j,i-1,j+1);
                    } else if(i==n&&j==m){
                        printf("%d %d %d %d %d %d\n",i,j,i,j-1,i-1,j);
                        printf("%d %d %d %d %d %d\n",i,j,i,j-1,i-1,j-1);
                        printf("%d %d %d %d %d %d\n",i,j,i-1,j,i-1,j-1);
                    }
                }
            }
        }
    }
    return 0;
}
/*
给定一个 n\times m 的 01 矩阵，
每次操作可以将某个 2\times2 的矩阵内的 3 个数取反，
请在 3\times n\times m 步内将矩阵变为全 0。
*/


#include<bits/stdc++.h>
using namespace std;
int n,m;
char s[110][110];
int main(){
    int t; scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
        int ans=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(s[i][j]) ans++;
        printf("%d\n",ans*2);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(i<n&&j<m){
                    printf("%d %d %d %d %d %d\n",i,j,i,j+1,i+1,j);
                    printf("%d %d %d %d %d %d\n",i,j,i,j+1,i+1,j+1);
                    printf("%d %d %d %d %d %d\n",i,j,i+1,j,i+1,j+1);
                } else if(i<n&&j==m){
                    printf("%d %d %d %d %d %d\n",i,j,i,j-1,i+1,j);
                    printf("%d %d %d %d %d %d\n",i,j,i,j-1,i+1,j-1);
                    printf("%d %d %d %d %d %d\n",i,j,i+1,j,i+1,j-1);
                } else if(i==n&&j<m){
                    printf("%d %d %d %d %d %d\n",i,j,i,j+1,i-1,j);
                    printf("%d %d %d %d %d %d\n",i,j,i,j+1,i-1,j+1);
                    printf("%d %d %d %d %d %d\n",i,j,i-1,j,i-1,j+1);
                } else if(i==n&&j==m){
                    printf("%d %d %d %d %d %d\n",i,j,i,j-1,i-1,j);
                    printf("%d %d %d %d %d %d\n",i,j,i,j-1,i-1,j-1);
                    printf("%d %d %d %d %d %d\n",i,j,i-1,j,i-1,j-1);
                }
            }
        }
    }
    return 0;
}
/*
给定一个 n\times m 的 01 矩阵，
每次操作可以将某个 2\times2 的矩阵内的 3 个数取反，
请在 3\times n\times m 步内将矩阵变为全 0。
*/


//#pragma GCC optimize(2)   //O2优化
//#pragma GCC optimize(3,"Ofast","inline")   //O3优化
#include<stdio.h>
#include<stdlib.h>
#include<bits/stdc++.h>
/****************宏定义*****************/
#define Fup(i,n) for(int i=1;i<=n;++i)
#define Fwr(i,n) for(int i=n;i>0;--i)
#define fup(i,n) for(int i=0;i<n;++i)
#define fwr(i,n) for(int i=n-1;i>=0;--i)
#define met(a,b) memset(a,b,sizeof(a))
#define Met(a,b,len) memset(a,b,sizeof(a[0])*(len+3))
#define pb push_back
#define in insert
#define fr first
#define se second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define continue(a) {a;continue;}
#define break(a) {a;break;}
#define Return(a) {a;return;}
#define MP(a,b) make_pair(a,b)
#define Mid ((l+r)>>1)
#define lowbit(x) ((x)&(-x))
#define ls rt<<1
#define rs rt<<1|1
#define pow2(x) ((x)*(x))
/****************文件流*****************/
#define ios std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)       //关闭流同步
#define fcin freopen("/home/wade/Desktop/input.txt","r",stdin)
#define fcout freopen("/home/wade/Desktop/output.txt","w",stdout)
#define Anoyer void Main();int main(){if(IOS)ios;Main();TIME;return 0;}void Main
#define times int T;scanf("%d",&T);while(T--)
/****************DEBUG*****************/
#ifdef LOCAL
#define debug(fmt, ...) fprintf(stderr, "[%s] " fmt "\n", __func__, ##__VA_ARGS__)
#define TIME cout << "RunTime: " << clock() << "ms\n", 0
#define __int128 ll
#define DEBUG(...) cout << ">> " << #__VA_ARGS__ << " =", PRT(__VA_ARGS__), cout << endl;
#define DEBUGS(...) cout << ">> " << #__VA_ARGS__ << " =", PRTS(__VA_ARGS__), cout << endl;
void PRT() {}
void PRTS() {}
template <typename S, typename... T>
void PRT(S x, T ... y){
	std::cout << " " << x;
	PRT(y...);
}
template <typename S>
void PRTS(S x){
	for (auto v : x)
		std::cout << " " << v;
}
#else
#define debug(...)((void)0) ;
#define DEBUG(...) ((void)0) ;
#define DEBUGS(...) ((void)0) ;
#define TIME 0
#define __int128 __int128
#endif
/****************变量名*****************/
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;   //点集
typedef pair<ll, ll>PLL;
typedef pair<double, double>PDD;
typedef vector<PII>VPII;
typedef vector<PLL>VPLL;
typedef vector<PDD>VPDD;
typedef vector<ll>VLL;
int dir[8][2] = { -1, 0, 1, 0, 0, -1, 0, 1, -1, -1, -1, 1, 1 - 1, 1, 1 };   //方向数组
/**************普通函数*****************/
int sgn(double x){if(fabs(x)<1e-8)return 0;if(x<0)return -1;else return 1;}
ll qpow(ll a, ll b, ll m){ ll ans = 1; while (b){ if (b & 1)ans = ans*a%m; a = a*a%m, b >>= 1; }return ans; }
ll qpow(ll a, ll b){ ll ans = 1; while (b){ if (b & 1)ans = ans*a; a = a*a, b >>= 1; }return ans; }
ll qmul(ll a, ll b, ll m){ ll res = 0; while (b > 0){ if (b & 1) res = (res + a) % m; a = (a + a) % m; b >>= 1; }return res; }
ll gcd(ll a, ll b){ return b == 0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b){ return a / gcd(a, b)*b; }
ll inv(ll t, ll p){ return t == 1 ? 1 : (p - p / t)*inv(p%t, p) % p; }
ll exgcd(ll a, ll b, ll&x, ll&y){ if (b == 0){ x = 1; y = 0; return a; }ll r = exgcd(b, a%b, y, x); y -= (a / b)*x; return r; }//最大公约数
ll inv1(ll a, ll p){ ll d, x, y; d = exgcd(a, p, x, y); return d == 1 ? (x % p + p) % p : -1; }//如果不存在，返回-1  a,p互质
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<typename S, typename T> inline bool Min(S &a, const T &b){ return a > b ? a = b, true : false; }
template<typename S, typename T> inline bool Max(S &a, const T &b){ return a < b ? a = b, true : false; }
template<typename T> inline bool IsPri(T x) { if (x < 2) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; }
template<typename T> inline int BitCnt(T x){ int cnt = 0; while (x)++cnt, x &= x - 1; return cnt; } //x二进制中1的个数
template<typename T> inline int BitNum(T x){int cnt = 0; while (x)++cnt, x>>=1; return cnt;}    //2进制位数
/***************读写函数***************/
template<class T> void _sf(T &x) { cin >> x; }
void _sf(int &x) { scanf("%d", &x); }
void _sf(ll &x) { scanf("%lld", &x); }
void _sf(double &x) { scanf("%lf", &x); }
void _sf(char &x) { scanf(" %c", &x); }
void _sf(char *x) { scanf("%s", x); }
void sf(){}
template<class T, class... U> void sf(T &head, U &... tail) { _sf(head); sf(tail...); }
template<class T> void _pf(const T &x) { cout << x; }
void _pf(const int &x) { printf("%d", x); }
void _pf(const ll &x) { printf("%lld", x); }
void _pf(const double &x) { printf("%.16f", x); }
void _pf(const char &x) { putchar(x); }
void _pf(const char *x) { printf("%s", x); }
template<class T,class U> void _pf(const pair<T,U> &x) {pf(x.F); putchar(' '); pf(x.S);}
template<class T> void _pf(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _pf(*i++)) if (i != x.cbegin()) putchar(' '); }
void pf() {}
template<class T, class... U> void pf(const T &head, const U &... tail) { _pf(head); putchar(sizeof...(tail) ? ' ' : '\n'); pf(tail...); }
template<typename T>inline void read(T &x){ x = 0; static int p; p = 1; static char c; c = getchar(); while (!isdigit(c)){ if (c == '-')p = -1; c = getchar(); }while (isdigit(c)) { x = (x << 1) + (x << 3) + (c - 48); c = getchar(); }x *= p; }
/**********************常量**********************/
const bool IOS=false;        //关闭流同步
const double PI = acos(-1);
const double eps = 1e-8;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const ll INV2=500000004;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 2e6+10;
/********************CODE*********************/
int a[200][200];
int d[6][4]={0,0,0,0,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,};   //  1 2 3 4
vector<vector<PII>>vec;
void get(int i,int j){
    int cnta=0,cntb=0;
    vector<PII>q;
    for(int x=i;x<=i+1;++x){
        for(int y=j;y<=j+1;++y){
            if(a[x][y])q.pb(MP(x,y));
        }
    }
    cnta=q.size();
    for(int x=i;x<=i+1;++x){
        for(int y=j;y<=j+1;++y){
            if(a[x][y]==0)q.pb(MP(x,y));
        }
    }
    cntb=q.size()-cnta;;
    while(cntb!=4){
        vector<PII>tmp;
        for(int x=0;x<q.size();++x){
            if(d[cnta][x]){
                tmp.pb(q[x]);
                a[q[x].fr][q[x].se]=!a[q[x].fr][q[x].se];
            }
        }
        vec.pb(tmp);
        q.clear();
        for(int x=i;x<=i+1;++x){
            for(int y=j;y<=j+1;++y){
                if(a[x][y])q.pb(MP(x,y));
            }
        }
        cnta=q.size();
        for(int x=i;x<=i+1;++x){
            for(int y=j;y<=j+1;++y){
                if(a[x][y]==0)q.pb(MP(x,y));
            }
        }
        cntb=q.size()-cnta;;
    }
}
Anoyer(){
#ifdef LOCAL
	fcin;
	//fcout;
#endif
    times{
        int n,m;
        sf(n,m);
        vec.clear();
        Fup(i,n){
            Fup(j,m)scanf("%1d",&a[i][j]);
        }
        for(int i=1;i<=n;i+=2){
            for(int j=1;j<=m;j+=2){
                get(i,j);
            }
        }
        if(n%2){
            for(int j=1;j<m;j+=2){
                get(n-1,j);
            }
        }
        if(m%2){
            for(int i=1;i<n;i+=2){
                get(i,m-1);
            }
        }
        if((n&1)&&(m&1)){
            get(n-1,m-1);
        }
//        pf("------------");
//        for(int i=1;i<=n;++i){
//            for(int j=1;j<=m;++j)printf("%d",a[i][j]);
//            pf("");
//        }
//        pf("------------");
        pf(vec.size());
        for(auto q:vec){
            for(auto it:q){
                printf("%d %d ",it.fr,it.se);
            }
            pf("");
        }
    }

}


//#pragma GCC optimize(2)   //O2优化
//#pragma GCC optimize(3,"Ofast","inline")   //O3优化
#include<stdio.h>
#include<stdlib.h>
#include<bits/stdc++.h>
/****************宏定义*****************/
#define Fup(i,n) for(int i=1;i<=n;++i)
#define Fwr(i,n) for(int i=n;i>0;--i)
#define fup(i,n) for(int i=0;i<n;++i)
#define fwr(i,n) for(int i=n-1;i>=0;--i)
#define met(a,b) memset(a,b,sizeof(a))
#define Met(a,b,len) memset(a,b,sizeof(a[0])*(len+3))
#define pb push_back
#define in insert
#define fr first
#define se second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define continue(a) {a;continue;}
#define break(a) {a;break;}
#define Return(a) {a;return;}
#define MP(a,b) make_pair(a,b)
#define Mid ((l+r)>>1)
#define lowbit(x) ((x)&(-x))
#define ls rt<<1
#define rs rt<<1|1
#define pow2(x) ((x)*(x))
/****************文件流*****************/
#define ios std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)       //关闭流同步
#define fcin freopen("/home/wade/Desktop/input.txt","r",stdin)
#define fcout freopen("/home/wade/Desktop/output.txt","w",stdout)
#define Anoyer void Main();int main(){if(IOS)ios;Main();TIME;return 0;}void Main
#define times int T;scanf("%d",&T);while(T--)
/****************DEBUG*****************/
#ifdef LOCAL
#define debug(fmt, ...) fprintf(stderr, "[%s] " fmt "\n", __func__, ##__VA_ARGS__)
#define TIME cout << "RunTime: " << clock() << "ms\n", 0
#define __int128 ll
#define DEBUG(...) cout << ">> " << #__VA_ARGS__ << " =", PRT(__VA_ARGS__), cout << endl;
#define DEBUGS(...) cout << ">> " << #__VA_ARGS__ << " =", PRTS(__VA_ARGS__), cout << endl;
void PRT() {}
void PRTS() {}
template <typename S, typename... T>
void PRT(S x, T ... y){
	std::cout << " " << x;
	PRT(y...);
}
template <typename S>
void PRTS(S x){
	for (auto v : x)
		std::cout << " " << v;
}
#else
#define debug(...)((void)0) ;
#define DEBUG(...) ((void)0) ;
#define DEBUGS(...) ((void)0) ;
#define TIME 0
#define __int128 __int128
#endif
/****************变量名*****************/
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;   //点集
typedef pair<ll, ll>PLL;
typedef pair<double, double>PDD;
typedef vector<PII>VPII;
typedef vector<PLL>VPLL;
typedef vector<PDD>VPDD;
typedef vector<ll>VLL;
int dir[8][2] = { -1, 0, 1, 0, 0, -1, 0, 1, -1, -1, -1, 1, 1 - 1, 1, 1 };   //方向数组
/**************普通函数*****************/
int sgn(double x){if(fabs(x)<1e-8)return 0;if(x<0)return -1;else return 1;}
ll qpow(ll a, ll b, ll m){ ll ans = 1; while (b){ if (b & 1)ans = ans*a%m; a = a*a%m, b >>= 1; }return ans; }
ll qpow(ll a, ll b){ ll ans = 1; while (b){ if (b & 1)ans = ans*a; a = a*a, b >>= 1; }return ans; }
ll qmul(ll a, ll b, ll m){ ll res = 0; while (b > 0){ if (b & 1) res = (res + a) % m; a = (a + a) % m; b >>= 1; }return res; }
ll gcd(ll a, ll b){ return b == 0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b){ return a / gcd(a, b)*b; }
ll inv(ll t, ll p){ return t == 1 ? 1 : (p - p / t)*inv(p%t, p) % p; }
ll exgcd(ll a, ll b, ll&x, ll&y){ if (b == 0){ x = 1; y = 0; return a; }ll r = exgcd(b, a%b, y, x); y -= (a / b)*x; return r; }//最大公约数
ll inv1(ll a, ll p){ ll d, x, y; d = exgcd(a, p, x, y); return d == 1 ? (x % p + p) % p : -1; }//如果不存在，返回-1  a,p互质
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<typename S, typename T> inline bool Min(S &a, const T &b){ return a > b ? a = b, true : false; }
template<typename S, typename T> inline bool Max(S &a, const T &b){ return a < b ? a = b, true : false; }
template<typename T> inline bool IsPri(T x) { if (x < 2) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; }
template<typename T> inline int BitCnt(T x){ int cnt = 0; while (x)++cnt, x &= x - 1; return cnt; } //x二进制中1的个数
template<typename T> inline int BitNum(T x){int cnt = 0; while (x)++cnt, x>>=1; return cnt;}    //2进制位数
/***************读写函数***************/
template<class T> void _sf(T &x) { cin >> x; }
void _sf(int &x) { scanf("%d", &x); }
void _sf(ll &x) { scanf("%lld", &x); }
void _sf(double &x) { scanf("%lf", &x); }
void _sf(char &x) { scanf(" %c", &x); }
void _sf(char *x) { scanf("%s", x); }
void sf(){}
template<class T, class... U> void sf(T &head, U &... tail) { _sf(head); sf(tail...); }
template<class T> void _pf(const T &x) { cout << x; }
void _pf(const int &x) { printf("%d", x); }
void _pf(const ll &x) { printf("%lld", x); }
void _pf(const double &x) { printf("%.16f", x); }
void _pf(const char &x) { putchar(x); }
void _pf(const char *x) { printf("%s", x); }
template<class T,class U> void _pf(const pair<T,U> &x) {pf(x.F); putchar(' '); pf(x.S);}
template<class T> void _pf(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _pf(*i++)) if (i != x.cbegin()) putchar(' '); }
void pf() {}
template<class T, class... U> void pf(const T &head, const U &... tail) { _pf(head); putchar(sizeof...(tail) ? ' ' : '\n'); pf(tail...); }
template<typename T>inline void read(T &x){ x = 0; static int p; p = 1; static char c; c = getchar(); while (!isdigit(c)){ if (c == '-')p = -1; c = getchar(); }while (isdigit(c)) { x = (x << 1) + (x << 3) + (c - 48); c = getchar(); }x *= p; }
/**********************常量**********************/
const bool IOS=false;        //关闭流同步
const double PI = acos(-1);
const double eps = 1e-8;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const ll INV2=500000004;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 2e6+10;
/********************CODE*********************/
int a[200][200];
vector<vector<PII>>vec;
Anoyer(){
#ifdef LOCAL
	fcin;
	//fcout;
#endif
    times{
        int n,m;
        sf(n,m);
        vec.clear();
        Fup(i,n){
            Fup(j,m)scanf("%1d",&a[i][j]);
        }
        for(int i=1;i<n;++i){
            for(int j=1;j<m;++j){
                int cnt=0;
                vector<PII>q;
                for(int x=0;x<=1&&q.size()<3;++x){
                    for(int y=0;y<=1&&q.size()<3;++y){
                        if(a[i+x][j+y]==1)q.pb(MP(i+x,j+y));
                    }
                }
                if(q.size()<3){
                    for(int x=0;x<=1&&q.size()<3;++x){
                        for(int y=0;y<=1&&q.size()<3;++y){
                            if(x==0&&y==0)continue;
                            if(a[i+x][j+y]==0)q.pb(MP(i+x,j+y));
                        }
                    }
                }
                for(auto it:q){
                    a[it.fr][it.se]=!a[it.fr][it.se];
                }
                vec.pb(q);
            }
        }
        pf(vec.size());
        for(auto q:vec){
            for(auto it:q){
                printf("%d %d ",it.fr,it.se);
            }
            pf("");
        }
    }

}


//#pragma GCC optimize(2)   //O2优化
//#pragma GCC optimize(3,"Ofast","inline")   //O3优化
#include<stdio.h>
#include<stdlib.h>
#include<bits/stdc++.h>
/****************宏定义*****************/
#define Fup(i,n) for(int i=1;i<=n;++i)
#define Fwr(i,n) for(int i=n;i>0;--i)
#define fup(i,n) for(int i=0;i<n;++i)
#define fwr(i,n) for(int i=n-1;i>=0;--i)
#define met(a,b) memset(a,b,sizeof(a))
#define Met(a,b,len) memset(a,b,sizeof(a[0])*(len+3))
#define pb push_back
#define in insert
#define fr first
#define se second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define continue(a) {a;continue;}
#define break(a) {a;break;}
#define Return(a) {a;return;}
#define MP(a,b) make_pair(a,b)
#define Mid ((l+r)>>1)
#define lowbit(x) ((x)&(-x))
#define ls rt<<1
#define rs rt<<1|1
#define pow2(x) ((x)*(x))
/****************文件流*****************/
#define ios std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)       //关闭流同步
#define fcin freopen("/home/wade/Desktop/input.txt","r",stdin)
#define fcout freopen("/home/wade/Desktop/output.txt","w",stdout)
#define Anoyer void Main();int main(){if(IOS)ios;Main();TIME;return 0;}void Main
#define times int T;scanf("%d",&T);while(T--)
/****************DEBUG*****************/
#ifdef LOCAL
#define debug(fmt, ...) fprintf(stderr, "[%s] " fmt "\n", __func__, ##__VA_ARGS__)
#define TIME cout << "RunTime: " << clock() << "ms\n", 0
#define __int128 ll
#define DEBUG(...) cout << ">> " << #__VA_ARGS__ << " =", PRT(__VA_ARGS__), cout << endl;
#define DEBUGS(...) cout << ">> " << #__VA_ARGS__ << " =", PRTS(__VA_ARGS__), cout << endl;
void PRT() {}
void PRTS() {}
template <typename S, typename... T>
void PRT(S x, T ... y){
	std::cout << " " << x;
	PRT(y...);
}
template <typename S>
void PRTS(S x){
	for (auto v : x)
		std::cout << " " << v;
}
#else
#define debug(...)((void)0) ;
#define DEBUG(...) ((void)0) ;
#define DEBUGS(...) ((void)0) ;
#define TIME 0
#define __int128 __int128
#endif
/****************变量名*****************/
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;   //点集
typedef pair<ll, ll>PLL;
typedef pair<double, double>PDD;
typedef vector<PII>VPII;
typedef vector<PLL>VPLL;
typedef vector<PDD>VPDD;
typedef vector<ll>VLL;
int dir[8][2] = { -1, 0, 1, 0, 0, -1, 0, 1, -1, -1, -1, 1, 1 - 1, 1, 1 };   //方向数组
/**************普通函数*****************/
int sgn(double x){if(fabs(x)<1e-8)return 0;if(x<0)return -1;else return 1;}
ll qpow(ll a, ll b, ll m){ ll ans = 1; while (b){ if (b & 1)ans = ans*a%m; a = a*a%m, b >>= 1; }return ans; }
ll qpow(ll a, ll b){ ll ans = 1; while (b){ if (b & 1)ans = ans*a; a = a*a, b >>= 1; }return ans; }
ll qmul(ll a, ll b, ll m){ ll res = 0; while (b > 0){ if (b & 1) res = (res + a) % m; a = (a + a) % m; b >>= 1; }return res; }
ll gcd(ll a, ll b){ return b == 0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b){ return a / gcd(a, b)*b; }
ll inv(ll t, ll p){ return t == 1 ? 1 : (p - p / t)*inv(p%t, p) % p; }
ll exgcd(ll a, ll b, ll&x, ll&y){ if (b == 0){ x = 1; y = 0; return a; }ll r = exgcd(b, a%b, y, x); y -= (a / b)*x; return r; }//最大公约数
ll inv1(ll a, ll p){ ll d, x, y; d = exgcd(a, p, x, y); return d == 1 ? (x % p + p) % p : -1; }//如果不存在，返回-1  a,p互质
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<typename S, typename T> inline bool Min(S &a, const T &b){ return a > b ? a = b, true : false; }
template<typename S, typename T> inline bool Max(S &a, const T &b){ return a < b ? a = b, true : false; }
template<typename T> inline bool IsPri(T x) { if (x < 2) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; }
template<typename T> inline int BitCnt(T x){ int cnt = 0; while (x)++cnt, x &= x - 1; return cnt; } //x二进制中1的个数
template<typename T> inline int BitNum(T x){int cnt = 0; while (x)++cnt, x>>=1; return cnt;}    //2进制位数
/***************读写函数***************/
template<class T> void _sf(T &x) { cin >> x; }
void _sf(int &x) { scanf("%d", &x); }
void _sf(ll &x) { scanf("%lld", &x); }
void _sf(double &x) { scanf("%lf", &x); }
void _sf(char &x) { scanf(" %c", &x); }
void _sf(char *x) { scanf("%s", x); }
void sf(){}
template<class T, class... U> void sf(T &head, U &... tail) { _sf(head); sf(tail...); }
template<class T> void _pf(const T &x) { cout << x; }
void _pf(const int &x) { printf("%d", x); }
void _pf(const ll &x) { printf("%lld", x); }
void _pf(const double &x) { printf("%.16f", x); }
void _pf(const char &x) { putchar(x); }
void _pf(const char *x) { printf("%s", x); }
template<class T,class U> void _pf(const pair<T,U> &x) {pf(x.F); putchar(' '); pf(x.S);}
template<class T> void _pf(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _pf(*i++)) if (i != x.cbegin()) putchar(' '); }
void pf() {}
template<class T, class... U> void pf(const T &head, const U &... tail) { _pf(head); putchar(sizeof...(tail) ? ' ' : '\n'); pf(tail...); }
template<typename T>inline void read(T &x){ x = 0; static int p; p = 1; static char c; c = getchar(); while (!isdigit(c)){ if (c == '-')p = -1; c = getchar(); }while (isdigit(c)) { x = (x << 1) + (x << 3) + (c - 48); c = getchar(); }x *= p; }
/**********************常量**********************/
const bool IOS=false;        //关闭流同步
const double PI = acos(-1);
const double eps = 1e-8;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const ll INV2=500000004;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 2e6+10;
/********************CODE*********************/
int a[200][200];
int d[6][4]={0,0,0,0,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,};   //  1 2 3 4
vector<vector<PII>>vec;
void get(int i,int j){
    int cnta=0,cntb=0;
    vector<PII>q;
    for(int x=i;x<=i+1;++x){
        for(int y=j;y<=j+1;++y){
            if(a[x][y])q.pb(MP(x,y));
        }
    }
    cnta=q.size();
    for(int x=i;x<=i+1;++x){
        for(int y=j;y<=j+1;++y){
            if(a[x][y]==0)q.pb(MP(x,y));
        }
    }
    cntb=q.size()-cnta;;
    while(cntb!=4){
        vector<PII>tmp;
        for(int x=0;x<q.size();++x){
            if(d[cnta][x]){
                tmp.pb(q[x]);
                a[q[x].fr][q[x].se]=!a[q[x].fr][q[x].se];
            }
        }
        vec.pb(tmp);
        q.clear();
        for(int x=i;x<=i+1;++x){
            for(int y=j;y<=j+1;++y){
                if(a[x][y])q.pb(MP(x,y));
            }
        }
        cnta=q.size();
        for(int x=i;x<=i+1;++x){
            for(int y=j;y<=j+1;++y){
                if(a[x][y]==0)q.pb(MP(x,y));
            }
        }
        cntb=q.size()-cnta;;
    }
}
int dd[200][200];
Anoyer(){
#ifdef LOCAL
	fcin;
	//fcout;
#endif
    times{
        int n,m;
        sf(n,m);
        vec.clear();
        Fup(i,n){
            Fup(j,m)scanf("%1d",&a[i][j]);
        }
        Fup(i,n){
            Fup(j,m)dd[i][j]=a[i][j];
        }
        for(int i=1;i+1<=n;i+=2){
            for(int j=1;j+1<=m;j+=2){
                get(i,j);
            }
        }
        if(n%2){
            for(int j=1;j<m;j+=2){
                get(n-1,j);
            }
        }
        if(m%2){
            for(int i=1;i<n;i+=2){
                get(i,m-1);
            }
        }
        if((n&1)&&(m&1)){
            get(n-1,m-1);
        }
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j)assert(a[i][j]==0);
        }
        pf(vec.size());
        for(auto q:vec){
            for(auto it:q){
                dd[it.fr][it.se]^=dd[it.fr][it.se];
                printf("%d %d ",it.fr,it.se);
            }
            pf("");
        }
        assert(vec.size()<=n*m);
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j)assert(dd[i][j]==0);
        }
    }

}


//#pragma GCC optimize(2)   //O2优化
//#pragma GCC optimize(3,"Ofast","inline")   //O3优化
#include<stdio.h>
#include<stdlib.h>
#include<bits/stdc++.h>
/****************宏定义*****************/
#define Fup(i,n) for(int i=1;i<=n;++i)
#define Fwr(i,n) for(int i=n;i>0;--i)
#define fup(i,n) for(int i=0;i<n;++i)
#define fwr(i,n) for(int i=n-1;i>=0;--i)
#define met(a,b) memset(a,b,sizeof(a))
#define Met(a,b,len) memset(a,b,sizeof(a[0])*(len+3))
#define pb push_back
#define in insert
#define fr first
#define se second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define continue(a) {a;continue;}
#define break(a) {a;break;}
#define Return(a) {a;return;}
#define MP(a,b) make_pair(a,b)
#define Mid ((l+r)>>1)
#define lowbit(x) ((x)&(-x))
#define ls rt<<1
#define rs rt<<1|1
#define pow2(x) ((x)*(x))
/****************文件流*****************/
#define ios std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)       //关闭流同步
#define fcin freopen("/home/wade/Desktop/input.txt","r",stdin)
#define fcout freopen("/home/wade/Desktop/output.txt","w",stdout)
#define Anoyer void Main();int main(){if(IOS)ios;Main();TIME;return 0;}void Main
#define times int T;scanf("%d",&T);while(T--)
/****************DEBUG*****************/
#ifdef LOCAL
#define debug(fmt, ...) fprintf(stderr, "[%s] " fmt "\n", __func__, ##__VA_ARGS__)
#define TIME cout << "RunTime: " << clock() << "ms\n", 0
#define __int128 ll
#define DEBUG(...) cout << ">> " << #__VA_ARGS__ << " =", PRT(__VA_ARGS__), cout << endl;
#define DEBUGS(...) cout << ">> " << #__VA_ARGS__ << " =", PRTS(__VA_ARGS__), cout << endl;
void PRT() {}
void PRTS() {}
template <typename S, typename... T>
void PRT(S x, T ... y){
	std::cout << " " << x;
	PRT(y...);
}
template <typename S>
void PRTS(S x){
	for (auto v : x)
		std::cout << " " << v;
}
#else
#define debug(...)((void)0) ;
#define DEBUG(...) ((void)0) ;
#define DEBUGS(...) ((void)0) ;
#define TIME 0
#define __int128 __int128
#endif
/****************变量名*****************/
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;   //点集
typedef pair<ll, ll>PLL;
typedef pair<double, double>PDD;
typedef vector<PII>VPII;
typedef vector<PLL>VPLL;
typedef vector<PDD>VPDD;
typedef vector<ll>VLL;
int dir[8][2] = { -1, 0, 1, 0, 0, -1, 0, 1, -1, -1, -1, 1, 1 - 1, 1, 1 };   //方向数组
/**************普通函数*****************/
int sgn(double x){if(fabs(x)<1e-8)return 0;if(x<0)return -1;else return 1;}
ll qpow(ll a, ll b, ll m){ ll ans = 1; while (b){ if (b & 1)ans = ans*a%m; a = a*a%m, b >>= 1; }return ans; }
ll qpow(ll a, ll b){ ll ans = 1; while (b){ if (b & 1)ans = ans*a; a = a*a, b >>= 1; }return ans; }
ll qmul(ll a, ll b, ll m){ ll res = 0; while (b > 0){ if (b & 1) res = (res + a) % m; a = (a + a) % m; b >>= 1; }return res; }
ll gcd(ll a, ll b){ return b == 0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b){ return a / gcd(a, b)*b; }
ll inv(ll t, ll p){ return t == 1 ? 1 : (p - p / t)*inv(p%t, p) % p; }
ll exgcd(ll a, ll b, ll&x, ll&y){ if (b == 0){ x = 1; y = 0; return a; }ll r = exgcd(b, a%b, y, x); y -= (a / b)*x; return r; }//最大公约数
ll inv1(ll a, ll p){ ll d, x, y; d = exgcd(a, p, x, y); return d == 1 ? (x % p + p) % p : -1; }//如果不存在，返回-1  a,p互质
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<typename S, typename T> inline bool Min(S &a, const T &b){ return a > b ? a = b, true : false; }
template<typename S, typename T> inline bool Max(S &a, const T &b){ return a < b ? a = b, true : false; }
template<typename T> inline bool IsPri(T x) { if (x < 2) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; }
template<typename T> inline int BitCnt(T x){ int cnt = 0; while (x)++cnt, x &= x - 1; return cnt; } //x二进制中1的个数
template<typename T> inline int BitNum(T x){int cnt = 0; while (x)++cnt, x>>=1; return cnt;}    //2进制位数
/***************读写函数***************/
template<class T> void _sf(T &x) { cin >> x; }
void _sf(int &x) { scanf("%d", &x); }
void _sf(ll &x) { scanf("%lld", &x); }
void _sf(double &x) { scanf("%lf", &x); }
void _sf(char &x) { scanf(" %c", &x); }
void _sf(char *x) { scanf("%s", x); }
void sf(){}
template<class T, class... U> void sf(T &head, U &... tail) { _sf(head); sf(tail...); }
template<class T> void _pf(const T &x) { cout << x; }
void _pf(const int &x) { printf("%d", x); }
void _pf(const ll &x) { printf("%lld", x); }
void _pf(const double &x) { printf("%.16f", x); }
void _pf(const char &x) { putchar(x); }
void _pf(const char *x) { printf("%s", x); }
template<class T,class U> void _pf(const pair<T,U> &x) {pf(x.F); putchar(' '); pf(x.S);}
template<class T> void _pf(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _pf(*i++)) if (i != x.cbegin()) putchar(' '); }
void pf() {}
template<class T, class... U> void pf(const T &head, const U &... tail) { _pf(head); putchar(sizeof...(tail) ? ' ' : '\n'); pf(tail...); }
template<typename T>inline void read(T &x){ x = 0; static int p; p = 1; static char c; c = getchar(); while (!isdigit(c)){ if (c == '-')p = -1; c = getchar(); }while (isdigit(c)) { x = (x << 1) + (x << 3) + (c - 48); c = getchar(); }x *= p; }
/**********************常量**********************/
const bool IOS=false;        //关闭流同步
const double PI = acos(-1);
const double eps = 1e-8;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const ll INV2=500000004;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 2e6+10;
/********************CODE*********************/
int a[200][200];
int d[6][4]={0,0,0,0,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,};   //  1 2 3 4
vector<vector<PII>>vec;
void get(int i,int j){
    int cnta=0,cntb=0;
    vector<PII>q;
    for(int x=i;x<=i+1;++x){
        for(int y=j;y<=j+1;++y){
            if(a[x][y])q.pb(MP(x,y));
        }
    }
    cnta=q.size();
    for(int x=i;x<=i+1;++x){
        for(int y=j;y<=j+1;++y){
            if(a[x][y]==0)q.pb(MP(x,y));
        }
    }
    cntb=q.size()-cnta;;
    while(cntb!=4){
        vector<PII>tmp;
        for(int x=0;x<q.size();++x){
            if(d[cnta][x]){
                tmp.pb(q[x]);
                a[q[x].fr][q[x].se]=!a[q[x].fr][q[x].se];
            }
        }
        vec.pb(tmp);
        q.clear();
        for(int x=i;x<=i+1;++x){
            for(int y=j;y<=j+1;++y){
                if(a[x][y])q.pb(MP(x,y));
            }
        }
        cnta=q.size();
        for(int x=i;x<=i+1;++x){
            for(int y=j;y<=j+1;++y){
                if(a[x][y]==0)q.pb(MP(x,y));
            }
        }
        cntb=q.size()-cnta;;
    }
}
int dd[200][200];
Anoyer(){
#ifdef LOCAL
	fcin;
	//fcout;
#endif
    times{
        int n,m;
        sf(n,m);
        vec.clear();
        Fup(i,n){
            Fup(j,m)scanf("%1d",&a[i][j]);
        }
        Fup(i,n){
            Fup(j,m)dd[i][j]=a[i][j];
        }
        for(int i=1;i<=n;i+=2){
            for(int j=1;j<=m;j+=2){
                get(i,j);
            }
        }
        if(n%2){
            for(int j=1;j<m;j+=2){
                get(n-1,j);
            }
        }
        if(m%2){
            for(int i=1;i<n;i+=2){
                get(i,m-1);
            }
        }
        if((n&1)&&(m&1)){
            get(n-1,m-1);
        }
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j)assert(a[i][j]==0);
        }
        pf(vec.size());
        for(auto q:vec){
            for(auto it:q){
                dd[it.fr][it.se]^=dd[it.fr][it.se];
                printf("%d %d ",it.fr,it.se);
            }
            pf("");
        }
        assert(vec.size()<=n*m);
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j)assert(dd[i][j]==0);
        }
    }

}


//#pragma GCC optimize(2)   //O2优化
//#pragma GCC optimize(3,"Ofast","inline")   //O3优化
#include<stdio.h>
#include<stdlib.h>
#include<bits/stdc++.h>
/****************宏定义*****************/
#define Fup(i,n) for(int i=1;i<=n;++i)
#define Fwr(i,n) for(int i=n;i>0;--i)
#define fup(i,n) for(int i=0;i<n;++i)
#define fwr(i,n) for(int i=n-1;i>=0;--i)
#define met(a,b) memset(a,b,sizeof(a))
#define Met(a,b,len) memset(a,b,sizeof(a[0])*(len+3))
#define pb push_back
#define in insert
#define fr first
#define se second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define continue(a) {a;continue;}
#define break(a) {a;break;}
#define Return(a) {a;return;}
#define MP(a,b) make_pair(a,b)
#define Mid ((l+r)>>1)
#define lowbit(x) ((x)&(-x))
#define ls rt<<1
#define rs rt<<1|1
#define pow2(x) ((x)*(x))
/****************文件流*****************/
#define ios std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)       //关闭流同步
#define fcin freopen("/home/wade/Desktop/input.txt","r",stdin)
#define fcout freopen("/home/wade/Desktop/output.txt","w",stdout)
#define Anoyer void Main();int main(){if(IOS)ios;Main();TIME;return 0;}void Main
#define times int T;scanf("%d",&T);while(T--)
/****************DEBUG*****************/
#ifdef LOCAL
#define debug(fmt, ...) fprintf(stderr, "[%s] " fmt "\n", __func__, ##__VA_ARGS__)
#define TIME cout << "RunTime: " << clock() << "ms\n", 0
#define __int128 ll
#define DEBUG(...) cout << ">> " << #__VA_ARGS__ << " =", PRT(__VA_ARGS__), cout << endl;
#define DEBUGS(...) cout << ">> " << #__VA_ARGS__ << " =", PRTS(__VA_ARGS__), cout << endl;
void PRT() {}
void PRTS() {}
template <typename S, typename... T>
void PRT(S x, T ... y){
	std::cout << " " << x;
	PRT(y...);
}
template <typename S>
void PRTS(S x){
	for (auto v : x)
		std::cout << " " << v;
}
#else
#define debug(...)((void)0) ;
#define DEBUG(...) ((void)0) ;
#define DEBUGS(...) ((void)0) ;
#define TIME 0
#define __int128 __int128
#endif
/****************变量名*****************/
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;   //点集
typedef pair<ll, ll>PLL;
typedef pair<double, double>PDD;
typedef vector<PII>VPII;
typedef vector<PLL>VPLL;
typedef vector<PDD>VPDD;
typedef vector<ll>VLL;
int dir[8][2] = { -1, 0, 1, 0, 0, -1, 0, 1, -1, -1, -1, 1, 1 - 1, 1, 1 };   //方向数组
/**************普通函数*****************/
int sgn(double x){if(fabs(x)<1e-8)return 0;if(x<0)return -1;else return 1;}
ll qpow(ll a, ll b, ll m){ ll ans = 1; while (b){ if (b & 1)ans = ans*a%m; a = a*a%m, b >>= 1; }return ans; }
ll qpow(ll a, ll b){ ll ans = 1; while (b){ if (b & 1)ans = ans*a; a = a*a, b >>= 1; }return ans; }
ll qmul(ll a, ll b, ll m){ ll res = 0; while (b > 0){ if (b & 1) res = (res + a) % m; a = (a + a) % m; b >>= 1; }return res; }
ll gcd(ll a, ll b){ return b == 0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b){ return a / gcd(a, b)*b; }
ll inv(ll t, ll p){ return t == 1 ? 1 : (p - p / t)*inv(p%t, p) % p; }
ll exgcd(ll a, ll b, ll&x, ll&y){ if (b == 0){ x = 1; y = 0; return a; }ll r = exgcd(b, a%b, y, x); y -= (a / b)*x; return r; }//最大公约数
ll inv1(ll a, ll p){ ll d, x, y; d = exgcd(a, p, x, y); return d == 1 ? (x % p + p) % p : -1; }//如果不存在，返回-1  a,p互质
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<typename S, typename T> inline bool Min(S &a, const T &b){ return a > b ? a = b, true : false; }
template<typename S, typename T> inline bool Max(S &a, const T &b){ return a < b ? a = b, true : false; }
template<typename T> inline bool IsPri(T x) { if (x < 2) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; }
template<typename T> inline int BitCnt(T x){ int cnt = 0; while (x)++cnt, x &= x - 1; return cnt; } //x二进制中1的个数
template<typename T> inline int BitNum(T x){int cnt = 0; while (x)++cnt, x>>=1; return cnt;}    //2进制位数
/***************读写函数***************/
template<class T> void _sf(T &x) { cin >> x; }
void _sf(int &x) { scanf("%d", &x); }
void _sf(ll &x) { scanf("%lld", &x); }
void _sf(double &x) { scanf("%lf", &x); }
void _sf(char &x) { scanf(" %c", &x); }
void _sf(char *x) { scanf("%s", x); }
void sf(){}
template<class T, class... U> void sf(T &head, U &... tail) { _sf(head); sf(tail...); }
template<class T> void _pf(const T &x) { cout << x; }
void _pf(const int &x) { printf("%d", x); }
void _pf(const ll &x) { printf("%lld", x); }
void _pf(const double &x) { printf("%.16f", x); }
void _pf(const char &x) { putchar(x); }
void _pf(const char *x) { printf("%s", x); }
template<class T,class U> void _pf(const pair<T,U> &x) {pf(x.F); putchar(' '); pf(x.S);}
template<class T> void _pf(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _pf(*i++)) if (i != x.cbegin()) putchar(' '); }
void pf() {}
template<class T, class... U> void pf(const T &head, const U &... tail) { _pf(head); putchar(sizeof...(tail) ? ' ' : '\n'); pf(tail...); }
template<typename T>inline void read(T &x){ x = 0; static int p; p = 1; static char c; c = getchar(); while (!isdigit(c)){ if (c == '-')p = -1; c = getchar(); }while (isdigit(c)) { x = (x << 1) + (x << 3) + (c - 48); c = getchar(); }x *= p; }
/**********************常量**********************/
const bool IOS=false;        //关闭流同步
const double PI = acos(-1);
const double eps = 1e-8;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const ll INV2=500000004;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 2e6+10;
/********************CODE*********************/
int a[200][200];
int d[6][4]={0,0,0,0,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,};   //  1 2 3 4
vector<vector<PII>>vec;
void get(int i,int j){
    int cnta=0,cntb=0;
    vector<PII>q;
    for(int x=i;x<=i+1;++x){
        for(int y=j;y<=j+1;++y){
            if(a[x][y])q.pb(MP(x,y));
        }
    }
    cnta=q.size();
    for(int x=i;x<=i+1;++x){
        for(int y=j;y<=j+1;++y){
            if(a[x][y]==0)q.pb(MP(x,y));
        }
    }
    cntb=q.size()-cnta;;
    while(cntb!=4){
        vector<PII>tmp;
        for(int x=0;x<q.size();++x){
            if(d[cnta][x]){
                tmp.pb(q[x]);
                a[q[x].fr][q[x].se]=!a[q[x].fr][q[x].se];
            }
        }
        vec.pb(tmp);
        q.clear();
        for(int x=i;x<=i+1;++x){
            for(int y=j;y<=j+1;++y){
                if(a[x][y])q.pb(MP(x,y));
            }
        }
        cnta=q.size();
        for(int x=i;x<=i+1;++x){
            for(int y=j;y<=j+1;++y){
                if(a[x][y]==0)q.pb(MP(x,y));
            }
        }
        cntb=q.size()-cnta;;
    }
}
Anoyer(){
#ifdef LOCAL
	fcin;
	//fcout;
#endif
    times{
        int n,m;
        sf(n,m);
        vec.clear();
        Fup(i,n){
            Fup(j,m)scanf("%1d",&a[i][j]);
        }
        for(int i=1;i+1<=n;i+=2){
            for(int j=1;j+1<=m;j+=2){
                get(i,j);
            }
        }
        if(n%2){
            for(int j=1;j<m;j+=2){
                get(n-1,j);
            }
        }
        if(m%2){
            for(int i=1;i<n;i+=2){
                get(i,m-1);
            }
        }
        if((n&1)&&(m&1)){
            get(n-1,m-1);
        }
        pf(vec.size());
        for(auto q:vec){
            for(auto it:q){
                printf("%d %d ",it.fr,it.se);
            }
            pf("");
        }
    }

}


#include<bits/stdc++.h>
using namespace std;
int n,m,a[105][105];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                char x=getchar();
                while(!isdigit(x)) x=getchar();
                a[i][j]=x-'0';
            }
        }
        vector<pair<int,int>> V;
        for (int i=1;i<n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                if (i==n-1&&j==m-1) break;
                if (a[i][j]==1)
                {
                    V.push_back(make_pair(i,j));
                    V.push_back(make_pair(i+1,j));
                    a[i][j]^=1;
                    a[i+1][j]^=1;
                    if (j!=m) a[i+1][j+1]^=1,V.push_back(make_pair(i+1,j+1));
                    else a[i+1][j-1]^=1,V.push_back(make_pair(i+1,j-1));
                }
            }
        }
        for (int j=1;j<m-1;j++)
        {
            if (a[n-1][j]==1&&a[n][j]==1)
            {
                V.push_back(make_pair(n-1,j));
                V.push_back(make_pair(n,j));
                V.push_back(make_pair(n,j+1));
                a[n-1][j]^=1,a[n][j]^=1,a[n][j+1]^=1;
                continue;
            }
            if (a[n-1][j]==0&&a[n][j]==0) continue;
            if (a[n-1][j]==1)
            {
                V.push_back(make_pair(n-1,j));
                V.push_back(make_pair(n-1,j+1));
                V.push_back(make_pair(n,j+1));
                a[n-1][j]^=1,a[n][j+1]^=1,a[n-1][j+1]^=1;
            }
            if (a[n][j]==1)
            {
                V.push_back(make_pair(n,j));
                V.push_back(make_pair(n-1,j+1));
                V.push_back(make_pair(n,j+1));
                a[n][j]^=1,a[n-1][j+1]^=1,a[n][j+1]^=1;
            }
        }
        vector<pair<int,int>> tmp;
        for (int i=n-1;i<=n;i++)
            for (int j=m-1;j<=m;j++)
                if (a[i][j]==1) tmp.push_back(make_pair(i,j));
        if (tmp.size()==1)
        {
            vector<pair<int,int>> tmpp;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (tmpp.size()<2&&a[i][j]==0)
                    {
                        a[i][j]^=1;
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                    }
                    else V.push_back(make_pair(i,j)),a[i][j]^=1;
                }
            tmpp.clear();
            bool flag=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (a[i][j]==1&&!flag)
                    {
                        flag=1;
                        a[i][j]^=1;
                        V.push_back(make_pair(i,j));
                    }
                    else
                    {
                        tmpp.push_back(make_pair(i,j));
                        a[i][j]^=1;
                    }
                }
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) V.push_back(make_pair(i,j));
        }
        if (tmp.size()==2)
        {
            vector<pair<int,int>> tmpp;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==0) tmpp.push_back(make_pair(i,j));
            V.push_back(tmpp[0]);
            V.push_back(tmpp[1]);
            V.push_back(tmp[0]);
            V.push_back(tmpp[0]);
            V.push_back(tmpp[1]);
            V.push_back(tmp[1]);
        }
        if (tmp.size()==3)
        {
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) V.push_back(make_pair(i,j));
        }
        if (tmp.size()==4)
        {
            vector<pair<int,int>> tmpp;
            V.push_back(make_pair(n-1,m-1));
            V.push_back(make_pair(n,m-1));
            V.push_back(make_pair(n,m));
            a[n-1][m-1]=a[n][m-1]=a[n][m]=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (tmpp.size()<2&&a[i][j]==0)
                    {
                        a[i][j]^=1;
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                    }
                    else V.push_back(make_pair(i,j)),a[i][j]^=1;
                }
            tmpp.clear();
            bool flag=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (a[i][j]==1&&!flag)
                    {
                        flag=1;
                        a[i][j]^=1;
                        V.push_back(make_pair(i,j));
                    }
                    else
                    {
                        tmpp.push_back(make_pair(i,j));
                        a[i][j]^=1;
                    }
                }
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) V.push_back(make_pair(i,j));
        }
        cout<<V.size()/3<<endl;
        for (int i=0;i<V.size();i+=3)
        {
            cout<<V[i].first<<' '<<V[i].second<<' '<<V[i+1].first<<' '<<
                V[i+1].second<<' '<<V[i+2].first<<' '<<V[i+2].second<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int n,m,a[105][105],b[105][105];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                char x=getchar();
                while(!isdigit(x)) x=getchar();
                a[i][j]=x-'0';
                b[i][j]=a[i][j];
            }
        }
        vector<pair<int,int>> V;
        for (int i=1;i<n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                if (i==n-1&&j==m-1) break;
                if (a[i][j]==1)
                {
                    V.push_back(make_pair(i,j));
                    V.push_back(make_pair(i+1,j));
                    a[i][j]^=1;
                    a[i+1][j]^=1;
                    if (j!=m) a[i+1][j+1]^=1,V.push_back(make_pair(i+1,j+1));
                    else a[i+1][j-1]^=1,V.push_back(make_pair(i+1,j-1));
                }
            }
        }
        for (int j=1;j<m-1;j++)
        {
            if (a[n-1][j]==1&&a[n][j]==1)
            {
                V.push_back(make_pair(n-1,j));
                V.push_back(make_pair(n,j));
                V.push_back(make_pair(n,j+1));
                a[n-1][j]^=1,a[n][j]^=1,a[n][j+1]^=1;
                continue;
            }
            if (a[n-1][j]==0&&a[n][j]==0) continue;
            if (a[n-1][j]==1)
            {
                V.push_back(make_pair(n-1,j));
                V.push_back(make_pair(n-1,j+1));
                V.push_back(make_pair(n,j+1));
                a[n-1][j]^=1,a[n][j+1]^=1,a[n-1][j+1]^=1;
                continue;
            }
            if (a[n][j]==1)
            {
                V.push_back(make_pair(n,j));
                V.push_back(make_pair(n-1,j+1));
                V.push_back(make_pair(n,j+1));
                a[n][j]^=1,a[n-1][j+1]^=1,a[n][j+1]^=1;
                continue;
            }
        }
        vector<pair<int,int>> tmp;
        for (int i=n-1;i<=n;i++)
            for (int j=m-1;j<=m;j++)
                if (a[i][j]==1) tmp.push_back(make_pair(i,j));
        if (tmp.size()==1)
        {
            vector<pair<int,int>> tmpp;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (tmpp.size()<2&&a[i][j]==0)
                    {
                        a[i][j]^=1;
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                    }
                    else V.push_back(make_pair(i,j)),a[i][j]^=1;
                }
            tmpp.clear();
            bool flag=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (a[i][j]==1&&!flag)
                    {
                        flag=1;
                        a[i][j]^=1;
                        V.push_back(make_pair(i,j));
                    }
                    else
                    {
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                        a[i][j]^=1;
                    }
                }
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        if (tmp.size()==2)
        {
            vector<pair<int,int>> tmpp;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==0) tmpp.push_back(make_pair(i,j));
            V.push_back(tmpp[0]);
            V.push_back(tmpp[1]);
            V.push_back(tmp[0]);
            V.push_back(tmpp[0]);
            V.push_back(tmpp[1]);
            V.push_back(tmp[1]);
        }
        if (tmp.size()==3)
        {
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        if (tmp.size()==4)
        {
            vector<pair<int,int>> tmpp;
            V.push_back(make_pair(n-1,m-1));
            V.push_back(make_pair(n,m-1));
            V.push_back(make_pair(n,m));
            a[n-1][m-1]=a[n][m-1]=a[n][m]=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (tmpp.size()<2&&a[i][j]==0)
                    {
                        a[i][j]^=1;
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                    }
                    else if (a[i][j]==1)
                        V.push_back(make_pair(i,j)),a[i][j]^=1;
                }
            tmpp.clear();
            bool flag=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (a[i][j]==1&&!flag)
                    {
                        flag=1;
                        a[i][j]^=1;
                        V.push_back(make_pair(i,j));
                    }
                    else
                    {
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                        a[i][j]^=1;
                    }
                }
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        assert(V.size()%3==0);
        cout<<V.size()/3<<endl;
        for (int i=0;i<V.size();i+=3)
        {
            cout<<V[i].first<<' '<<V[i].second<<' '<<V[i+1].first<<' '<<
                V[i+1].second<<' '<<V[i+2].first<<' '<<V[i+2].second<<endl;
            b[V[i].first][V[i].second]^=1;
            b[V[i+1].first][V[i+1].second]^=1;
            b[V[i+2].first][V[i+2].second]^=1;
        }
        for (int i=1;i<=n;i++)
            for (int j=1;j<=m;j++)
                assert(!b[i][j]);
    }
}


#include<bits/stdc++.h>
using namespace std;
int n,m,a[105][105],b[105][105];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                char x=getchar();
                while(!isdigit(x)) x=getchar();
                a[i][j]=x-'0';
                b[i][j]=a[i][j];
            }
        }
        vector<pair<int,int>> V;
        for (int i=1;i<n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                if (i==n-1&&j==m-1) break;
                if (a[i][j]==1)
                {
                    V.push_back(make_pair(i,j));
                    V.push_back(make_pair(i+1,j));
                    a[i][j]^=1;
                    a[i+1][j]^=1;
                    if (j!=m) a[i+1][j+1]^=1,V.push_back(make_pair(i+1,j+1));
                    else a[i+1][j-1]^=1,V.push_back(make_pair(i+1,j-1));
                }
            }
        }
        for (int j=1;j<m-1;j++)
        {
            if (a[n-1][j]==1&&a[n][j]==1)
            {
                V.push_back(make_pair(n-1,j));
                V.push_back(make_pair(n,j));
                V.push_back(make_pair(n,j+1));
                a[n-1][j]^=1,a[n][j]^=1,a[n][j+1]^=1;
                continue;
            }
            if (a[n-1][j]==0&&a[n][j]==0) continue;
            if (a[n-1][j]==1)
            {
                V.push_back(make_pair(n-1,j));
                V.push_back(make_pair(n-1,j+1));
                V.push_back(make_pair(n,j+1));
                a[n-1][j]^=1,a[n][j+1]^=1,a[n-1][j+1]^=1;
                continue;
            }
            if (a[n][j]==1)
            {
                V.push_back(make_pair(n,j));
                V.push_back(make_pair(n-1,j+1));
                V.push_back(make_pair(n,j+1));
                a[n][j]^=1,a[n-1][j+1]^=1,a[n][j+1]^=1;
                continue;
            }
        }
        vector<pair<int,int>> tmp;
        for (int i=n-1;i<=n;i++)
            for (int j=m-1;j<=m;j++)
                if (a[i][j]==1) tmp.push_back(make_pair(i,j));
        if (tmp.size()==1)
        {
            vector<pair<int,int>> tmpp;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (tmpp.size()<2&&a[i][j]==0)
                    {
                        a[i][j]^=1;
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                    }
                    else if (a[i][j]==1)
                        V.push_back(make_pair(i,j)),a[i][j]^=1;
                }
            tmpp.clear();
            bool flag=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (a[i][j]==1&&!flag)
                    {
                        flag=1;
                        a[i][j]^=1;
                        V.push_back(make_pair(i,j));
                    }
                    else if (!a[i][j])
                    {
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                        a[i][j]^=1;
                    }
                }
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        if (tmp.size()==2)
        {
            vector<pair<int,int>> tmpp;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==0) tmpp.push_back(make_pair(i,j));
            V.push_back(tmpp[0]);
            V.push_back(tmpp[1]);
            V.push_back(tmp[0]);
            V.push_back(tmpp[0]);
            V.push_back(tmpp[1]);
            V.push_back(tmp[1]);
        }
        if (tmp.size()==3)
        {
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        if (tmp.size()==4)
        {
            vector<pair<int,int>> tmpp;
            V.push_back(make_pair(n-1,m-1));
            V.push_back(make_pair(n,m-1));
            V.push_back(make_pair(n,m));
            a[n-1][m-1]=a[n][m-1]=a[n][m]=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (tmpp.size()<2&&a[i][j]==0)
                    {
                        a[i][j]^=1;
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                    }
                    else if (a[i][j]==1)
                        V.push_back(make_pair(i,j)),a[i][j]^=1;
                }
            tmpp.clear();
            bool flag=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (a[i][j]==1&&!flag)
                    {
                        flag=1;
                        a[i][j]^=1;
                        V.push_back(make_pair(i,j));
                    }
                    else if (!a[i][j])
                    {
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                        a[i][j]^=1;
                    }
                }
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        assert(V.size()%3==0);
        cout<<V.size()/3<<endl;
        for (int i=0;i<V.size();i+=3)
        {
            cout<<V[i].first<<' '<<V[i].second<<' '<<V[i+1].first<<' '<<
                V[i+1].second<<' '<<V[i+2].first<<' '<<V[i+2].second<<endl;
            b[V[i].first][V[i].second]^=1;
            b[V[i+1].first][V[i+1].second]^=1;
            b[V[i+2].first][V[i+2].second]^=1;
        }
        for (int i=1;i<=n;i++)
            for (int j=1;j<=m;j++)
                assert(!b[i][j]);
    }
}


#include<bits/stdc++.h>
using namespace std;
int n,m,a[105][105],b[105][105];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                char x=getchar();
                while(!isdigit(x)) x=getchar();
                a[i][j]=x-'0';
                b[i][j]=a[i][j];
            }
        }
        vector<pair<int,int>> V;
        for (int i=1;i<n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                if (i==n-1&&j==m-1) break;
                if (a[i][j]==1)
                {
                    V.push_back(make_pair(i,j));
                    V.push_back(make_pair(i+1,j));
                    a[i][j]^=1;
                    a[i+1][j]^=1;
                    if (j!=m) a[i+1][j+1]^=1,V.push_back(make_pair(i+1,j+1));
                    else a[i+1][j-1]^=1,V.push_back(make_pair(i+1,j-1));
                }
            }
        }
        for (int j=1;j<m-1;j++)
        {
            if (a[n-1][j]==1&&a[n][j]==1)
            {
                V.push_back(make_pair(n-1,j));
                V.push_back(make_pair(n,j));
                V.push_back(make_pair(n,j+1));
                a[n-1][j]^=1,a[n][j]^=1,a[n][j+1]^=1;
                continue;
            }
            if (a[n-1][j]==0&&a[n][j]==0) continue;
            if (a[n-1][j]==1)
            {
                V.push_back(make_pair(n-1,j));
                V.push_back(make_pair(n-1,j+1));
                V.push_back(make_pair(n,j+1));
                a[n-1][j]^=1,a[n][j+1]^=1,a[n-1][j+1]^=1;
                continue;
            }
            if (a[n][j]==1)
            {
                V.push_back(make_pair(n,j));
                V.push_back(make_pair(n-1,j+1));
                V.push_back(make_pair(n,j+1));
                a[n][j]^=1,a[n-1][j+1]^=1,a[n][j+1]^=1;
                continue;
            }
        }
        vector<pair<int,int>> tmp;
        for (int i=n-1;i<=n;i++)
            for (int j=m-1;j<=m;j++)
                if (a[i][j]==1) tmp.push_back(make_pair(i,j));
        if (tmp.size()==1)
        {
            vector<pair<int,int>> tmpp;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (tmpp.size()<2&&a[i][j]==0)
                    {
                        a[i][j]^=1;
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                    }
                    else V.push_back(make_pair(i,j)),a[i][j]^=1;
                }
            tmpp.clear();
            bool flag=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (a[i][j]==1&&!flag)
                    {
                        flag=1;
                        a[i][j]^=1;
                        V.push_back(make_pair(i,j));
                    }
                    else
                    {
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                        a[i][j]^=1;
                    }
                }
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        if (tmp.size()==2)
        {
            vector<pair<int,int>> tmpp;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==0) tmpp.push_back(make_pair(i,j));
            V.push_back(tmpp[0]);
            V.push_back(tmpp[1]);
            V.push_back(tmp[0]);
            V.push_back(tmpp[0]);
            V.push_back(tmpp[1]);
            V.push_back(tmp[1]);
        }
        if (tmp.size()==3)
        {
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        if (tmp.size()==4)
        {
            vector<pair<int,int>> tmpp;
            V.push_back(make_pair(n-1,m-1));
            V.push_back(make_pair(n,m-1));
            V.push_back(make_pair(n,m));
            a[n-1][m-1]=a[n][m-1]=a[n][m]=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (tmpp.size()<2&&a[i][j]==0)
                    {
                        a[i][j]^=1;
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                    }
                    else V.push_back(make_pair(i,j)),a[i][j]^=1;
                }
            tmpp.clear();
            bool flag=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (a[i][j]==1&&!flag)
                    {
                        flag=1;
                        a[i][j]^=1;
                        V.push_back(make_pair(i,j));
                    }
                    else
                    {
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                        a[i][j]^=1;
                    }
                }
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        cout<<V.size()/3<<endl;
        for (int i=0;i<V.size();i+=3)
        {
            cout<<V[i].first<<' '<<V[i].second<<' '<<V[i+1].first<<' '<<
                V[i+1].second<<' '<<V[i+2].first<<' '<<V[i+2].second<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int n,m,a[105][105];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                char x=getchar();
                while(!isdigit(x)) x=getchar();
                a[i][j]=x-'0';
            }
        }
        vector<pair<int,int>> V;
        for (int i=1;i<n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                if (i==n-1&&j==m-1) break;
                if (a[i][j]==1)
                {
                    V.push_back(make_pair(i,j));
                    V.push_back(make_pair(i+1,j));
                    a[i][j]^=1;
                    a[i+1][j]^=1;
                    if (j!=m) a[i+1][j+1]^=1,V.push_back(make_pair(i+1,j+1));
                    else a[i+1][j-1]^=1,V.push_back(make_pair(i+1,j-1));
                }
            }
        }
        for (int j=1;j<m-1;j++)
        {
            if (a[n-1][j]==1&&a[n][j]==1)
            {
                V.push_back(make_pair(n-1,j));
                V.push_back(make_pair(n,j));
                V.push_back(make_pair(n,j+1));
                a[n-1][j]^=1,a[n][j]^=1,a[n][j+1]^=1;
                continue;
            }
            if (a[n-1][j]==0&&a[n][j]==0) continue;
            if (a[n-1][j]==1)
            {
                V.push_back(make_pair(n-1,j));
                V.push_back(make_pair(n-1,j+1));
                V.push_back(make_pair(n,j+1));
                a[n-1][j]^=1,a[n][j+1]^=1,a[n-1][j+1]^=1;
            }
            if (a[n][j]==1)
            {
                V.push_back(make_pair(n,j));
                V.push_back(make_pair(n-1,j+1));
                V.push_back(make_pair(n,j+1));
                a[n][j]^=1,a[n-1][j+1]^=1,a[n][j+1]^=1;
            }
        }
        vector<pair<int,int>> tmp;
        for (int i=n-1;i<=n;i++)
            for (int j=m-1;j<=m;j++)
                if (a[i][j]==1) tmp.push_back(make_pair(i,j));
        if (tmp.size()==1)
        {
            vector<pair<int,int>> tmpp;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (tmpp.size()<2&&a[i][j]==0)
                    {
                        a[i][j]^=1;
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                    }
                    else V.push_back(make_pair(i,j)),a[i][j]^=1;
                }
            tmpp.clear();
            bool flag=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (a[i][j]==1&&!flag)
                    {
                        flag=1;
                        a[i][j]^=1;
                        V.push_back(make_pair(i,j));
                    }
                    else
                    {
                        tmpp.push_back(make_pair(i,j));
                        a[i][j]^=1;
                    }
                }
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        if (tmp.size()==2)
        {
            vector<pair<int,int>> tmpp;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==0) tmpp.push_back(make_pair(i,j));
            V.push_back(tmpp[0]);
            V.push_back(tmpp[1]);
            V.push_back(tmp[0]);
            V.push_back(tmpp[0]);
            V.push_back(tmpp[1]);
            V.push_back(tmp[1]);
        }
        if (tmp.size()==3)
        {
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        if (tmp.size()==4)
        {
            vector<pair<int,int>> tmpp;
            V.push_back(make_pair(n-1,m-1));
            V.push_back(make_pair(n,m-1));
            V.push_back(make_pair(n,m));
            a[n-1][m-1]=a[n][m-1]=a[n][m]=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (tmpp.size()<2&&a[i][j]==0)
                    {
                        a[i][j]^=1;
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                    }
                    else V.push_back(make_pair(i,j)),a[i][j]^=1;
                }
            tmpp.clear();
            bool flag=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (a[i][j]==1&&!flag)
                    {
                        flag=1;
                        a[i][j]^=1;
                        V.push_back(make_pair(i,j));
                    }
                    else
                    {
                        tmpp.push_back(make_pair(i,j));
                        a[i][j]^=1;
                    }
                }
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        cout<<V.size()/3<<endl;
        for (int i=0;i<V.size();i+=3)
        {
            cout<<V[i].first<<' '<<V[i].second<<' '<<V[i+1].first<<' '<<
                V[i+1].second<<' '<<V[i+2].first<<' '<<V[i+2].second<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int n,m,a[105][105],b[105][105];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                char x=getchar();
                while(!isdigit(x)) x=getchar();
                a[i][j]=x-'0';
                b[i][j]=a[i][j];
            }
        }
        vector<pair<int,int>> V;
        for (int i=1;i<n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                if (i==n-1&&j==m-1) break;
                if (a[i][j]==1)
                {
                    V.push_back(make_pair(i,j));
                    V.push_back(make_pair(i+1,j));
                    a[i][j]^=1;
                    a[i+1][j]^=1;
                    if (j!=m) a[i+1][j+1]^=1,V.push_back(make_pair(i+1,j+1));
                    else a[i+1][j-1]^=1,V.push_back(make_pair(i+1,j-1));
                }
            }
        }
        for (int j=1;j<m-1;j++)
        {
            if (a[n-1][j]==1&&a[n][j]==1)
            {
                V.push_back(make_pair(n-1,j));
                V.push_back(make_pair(n,j));
                V.push_back(make_pair(n,j+1));
                a[n-1][j]^=1,a[n][j]^=1,a[n][j+1]^=1;
                continue;
            }
            if (a[n-1][j]==0&&a[n][j]==0) continue;
            if (a[n-1][j]==1)
            {
                V.push_back(make_pair(n-1,j));
                V.push_back(make_pair(n-1,j+1));
                V.push_back(make_pair(n,j+1));
                a[n-1][j]^=1,a[n][j+1]^=1,a[n-1][j+1]^=1;
                continue;
            }
            if (a[n][j]==1)
            {
                V.push_back(make_pair(n,j));
                V.push_back(make_pair(n-1,j+1));
                V.push_back(make_pair(n,j+1));
                a[n][j]^=1,a[n-1][j+1]^=1,a[n][j+1]^=1;
                continue;
            }
        }
        vector<pair<int,int>> tmp;
        for (int i=n-1;i<=n;i++)
            for (int j=m-1;j<=m;j++)
                if (a[i][j]==1) tmp.push_back(make_pair(i,j));
        if (tmp.size()==1)
        {
            vector<pair<int,int>> tmpp;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (tmpp.size()<2&&a[i][j]==0)
                    {
                        a[i][j]^=1;
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                    }
                    else if (a[i][j]==1)
                        V.push_back(make_pair(i,j)),a[i][j]^=1;
                }
            tmpp.clear();
            bool flag=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (a[i][j]==1&&!flag)
                    {
                        flag=1;
                        a[i][j]^=1;
                        V.push_back(make_pair(i,j));
                    }
                    else
                    {
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                        a[i][j]^=1;
                    }
                }
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        if (tmp.size()==2)
        {
            vector<pair<int,int>> tmpp;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==0) tmpp.push_back(make_pair(i,j));
            V.push_back(tmpp[0]);
            V.push_back(tmpp[1]);
            V.push_back(tmp[0]);
            V.push_back(tmpp[0]);
            V.push_back(tmpp[1]);
            V.push_back(tmp[1]);
        }
        if (tmp.size()==3)
        {
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        if (tmp.size()==4)
        {
            vector<pair<int,int>> tmpp;
            V.push_back(make_pair(n-1,m-1));
            V.push_back(make_pair(n,m-1));
            V.push_back(make_pair(n,m));
            a[n-1][m-1]=a[n][m-1]=a[n][m]=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (tmpp.size()<2&&a[i][j]==0)
                    {
                        a[i][j]^=1;
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                    }
                    else if (a[i][j]==1)
                        V.push_back(make_pair(i,j)),a[i][j]^=1;
                }
            tmpp.clear();
            bool flag=0;
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                {
                    if (a[i][j]==1&&!flag)
                    {
                        flag=1;
                        a[i][j]^=1;
                        V.push_back(make_pair(i,j));
                    }
                    else
                    {
                        tmpp.push_back(make_pair(i,j));
                        V.push_back(make_pair(i,j));
                        a[i][j]^=1;
                    }
                }
            for (int i=n-1;i<=n;i++)
                for (int j=m-1;j<=m;j++)
                    if (a[i][j]==1) a[i][j]^=1,V.push_back(make_pair(i,j));
        }
        assert(V.size()%3==0);
        cout<<V.size()/3<<endl;
        for (int i=0;i<V.size();i+=3)
        {
            cout<<V[i].first<<' '<<V[i].second<<' '<<V[i+1].first<<' '<<
                V[i+1].second<<' '<<V[i+2].first<<' '<<V[i+2].second<<endl;
            b[V[i].first][V[i].second]^=1;
            b[V[i+1].first][V[i+1].second]^=1;
            b[V[i+2].first][V[i+2].second]^=1;
        }
        for (int i=1;i<=n;i++)
            for (int j=1;j<=m;j++)
                assert(!b[i][j]);
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

struct out{
pair<int,int>one;
pair<int,int>two;
pair<int,int>three;
};

int t,n,m;
vector<out>ans;
bool a[105][105];

void add(int i,int j,int x,int y)
{
    vector<pair<int,int> >r;
    for(int q=0;q<2;q++)
        for(int p=0;p<2;p++)
            if(q!=x || p!=y)
                r.push_back({i+q,j+p});
    ans.push_back({r[0],r[1],r[2]});
}
void change(int i,int j,int x,int y)
{
    for(int q=0;q<2;q++)
        for(int p=0;p<2;p++)
            if(q!=x || p!=y)
                a[i+q][j+p]^=1;
}
void mini_res(int i,int j)
{
    int cnt=0;
    if(a[i][j])cnt++;
    if(a[i+1][j])cnt++;
    if(a[i][j+1])cnt++;
    if(a[i+1][j+1])cnt++;

    if(!cnt) return ;

    if(cnt==4)
    {
        add(i,j,0,0);
        add(i,j,1,1);
        add(i,j,0,1);
        add(i,j,1,0);
        return ;
    }
    if(cnt==3)
    {
        for(int q=0;q<2;q++)
            for(int p=0;p<2;p++)
                if(!a[i+q][j+p])
                    add(i,j,p,q);
        return ;
    }
    if(cnt==2)
    {
        for(int q=0;q<2;q++)
            for(int p=0;p<2;p++)
                if(a[i+q][j+p])
                    add(i,j,p,q);
        return ;
    }
    for(int q=0;q<2;q++)
            for(int p=0;p<2;p++)
                if(!a[i+q][j+p])
                    add(i,j,p,q);
}
void answer(bool xs=0,bool ys=0)
{
    for(int i=xs;i<n;i+=2)
    {
        for(int j=ys;j<m;j+=2)
        {
            mini_res(i,j);
        }
    }
}

void print(out q)
{
    cout<<q.one.first<<" "<<q.one.second<<" "<<q.two.first<<" "<<q.two.second<<" "<<q.three.first<<" "<<q.three.second<<'\n';
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);

    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=0;i<n;i++)
        {
            string s;
            cin>>s;
            for(int j=0;j<m;j++)
                a[i][j]=(s[j]=='1');
        }
        bool l=0,k=0;

        if(m%2)
        {
            for(int i=0;i<n-1;i++)
            {
                if(a[i][0]==a[i+1][0])
                {
                    if(a[i][0])
                    {add(i,0,1,1); change(i,0,1,1);}
                }
                else if(a[i][0])
                {add(i,0,1,0); change(i,0,1,0);}
                else
                {add(i,0,0,0); change(i,0,0,0);}
            }
            l=true;
        }
        if(n%2)
        {
            for(int j=l;j<m-1;j++)
            {
                if(a[0][j]==a[0][j+1])
                {
                    if(a[j][0])
                    {add(0,j,1,1); change(0,j,1,1);}
                }
                else if(a[0][j])
                {add(0,j,0,1); change(0,j,0,1);}
                else
                {add(0,j,0,0); change(0,j,0,0);}
            }
            k=true;
        }

        answer(l,k);

        cout<<ans.size()<<'\n';
        for(out q:ans)
            print(q);

        ans.clear();
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

struct out{
pair<int,int>one;
pair<int,int>two;
pair<int,int>three;
};
void print(out );

int t,n,m;
vector<out>ans;
bool a[105][105];

void add(int i,int j,int x,int y)
{
    vector<pair<int,int> >r;
    for(int q=0;q<2;q++)
        for(int p=0;p<2;p++)
            if(q!=x || p!=y)
                r.push_back({i+q,j+p});
    ans.push_back({r[0],r[1],r[2]});

}
void change(int i,int j,int x,int y)
{
    for(int q=0;q<2;q++)
        for(int p=0;p<2;p++)
            if(q!=x || p!=y)
                a[i+q][j+p]^=1;
}
void mini_res(int i,int j)
{
    int cnt=0;
    if(a[i][j])cnt++;
    if(a[i+1][j])cnt++;
    if(a[i][j+1])cnt++;
    if(a[i+1][j+1])cnt++;

    if(!cnt) return ;

    if(cnt==4)
    {
        add(i,j,0,0);
        add(i,j,1,1);
        add(i,j,0,1);
        add(i,j,1,0);
        return ;
    }
    if(cnt==3)
    {
        for(int q=0;q<2;q++)
            for(int p=0;p<2;p++)
                if(!a[i+q][j+p])
                    add(i,j,q,p);
        return ;
    }
    if(cnt==2)
    {
        for(int q=0;q<2;q++)
            for(int p=0;p<2;p++)
                if(a[i+q][j+p])
                    add(i,j,q,p);
        return ;
    }
    for(int q=0;q<2;q++)
            for(int p=0;p<2;p++)
                if(!a[i+q][j+p])
                    add(i,j,q,p);
}
void answer(bool xs,bool ys)
{
    for(int i=xs;i<n;i+=2)
    {
        for(int j=ys;j<m;j+=2)
        {
            mini_res(i,j);
        }
    }
}

void print(out q)
{
    cout<<q.one.first+1<<" "<<q.one.second+1<<" "<<q.two.first+1<<" "<<q.two.second+1<<" "<<q.three.first+1<<" "<<q.three.second+1<<'\n';
}

void the()
{
    for(int i=0;i<n;i++)
    {
        for(int  j=0;j<m;j++)
            cout<<a[i][j];
        cout<<'\n';
    }
    cout<<"///////////////////////////////"<<'\n';
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);

    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=0;i<n;i++)
        {
            string s;
            cin>>s;
            for(int j=0;j<m;j++)
                a[i][j]=(s[j]=='1');
        }
        bool l=0,k=0;

        if(m%2)
        {
            for(int i=0;i<n-1;i++)
            {
                if(a[i][0]==a[i+1][0])
                {
                    if(a[i][0])
                    {add(i,0,1,1); change(i,0,1,1);}
                }
                else if(a[i][0])
                {add(i,0,1,0); change(i,0,1,0);}
                else
                {add(i,0,0,0); change(i,0,0,0);}
            }
            l=true;
        }
        if(n%2)
        {
            for(int j=l;j<m-1;j++)
            {
                if(a[0][j]==a[0][j+1])
                {
                    if(a[0][j])
                    {add(0,j,1,1); change(0,j,1,1);}
                }
                else if(a[0][j])
                {add(0,j,0,1); change(0,j,0,1);}
                else
                {add(0,j,0,0); change(0,j,0,0);}
            }
            k=true;
        }

        answer(k,l);

        cout<<ans.size()<<'\n';
        for(out q:ans)
            print(q);

        ans.clear();
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

struct out{
pair<int,int>one;
pair<int,int>two;
pair<int,int>three;
};

int t,n,m;
vector<out>ans;
bool a[105][105];

void add(int i,int j,int x,int y)
{
    vector<pair<int,int> >r;
    for(int q=0;q<2;q++)
        for(int p=0;p<2;p++)
            if(q!=x || p!=y)
                r.push_back({i+q,j+p});
    ans.push_back({r[0],r[1],r[2]});
}
void change(int i,int j,int x,int y)
{
    for(int q=0;q<2;q++)
        for(int p=0;p<2;p++)
            if(q!=x || p!=y)
                a[i+q][j+p]^=1;
}
void mini_res(int i,int j)
{
    int cnt=0;
    if(a[i][j])cnt++;
    if(a[i+1][j])cnt++;
    if(a[i][j+1])cnt++;
    if(a[i+1][j+1])cnt++;

    if(!cnt) return ;

    if(cnt==4)
    {
        add(i,j,0,0);
        add(i,j,1,1);
        add(i,j,0,1);
        add(i,j,1,0);
        return ;
    }
    if(cnt==3)
    {
        for(int q=0;q<2;q++)
            for(int p=0;p<2;p++)
                if(!a[i+q][j+p])
                    add(i,j,q,p);
        return ;
    }
    if(cnt==2)
    {
        for(int q=0;q<2;q++)
            for(int p=0;p<2;p++)
                if(a[i+q][j+p])
                    add(i,j,q,p);
        return ;
    }
    for(int q=0;q<2;q++)
            for(int p=0;p<2;p++)
                if(!a[i+q][j+p])
                    add(i,j,q,p);
}
void answer(bool xs=0,bool ys=0)
{
    for(int i=xs;i<n;i+=2)
    {
        for(int j=ys;j<m;j+=2)
        {
            mini_res(i,j);
        }
    }
}

void print(out q)
{
    cout<<q.one.first+1<<" "<<q.one.second+1<<" "<<q.two.first+1<<" "<<q.two.second+1<<" "<<q.three.first+1<<" "<<q.three.second+1<<'\n';
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);

    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=0;i<n;i++)
        {
            string s;
            cin>>s;
            for(int j=0;j<m;j++)
                a[i][j]=(s[j]=='1');
        }
        bool l=0,k=0;

        if(m%2)
        {
            for(int i=0;i<n-1;i++)
            {
                if(a[i][0]==a[i+1][0])
                {
                    if(a[i][0])
                    {add(i,0,1,1); change(i,0,1,1);}
                }
                else if(a[i][0])
                {add(i,0,1,0); change(i,0,1,0);}
                else
                {add(i,0,0,0); change(i,0,0,0);}
            }
            l=true;
        }
        if(n%2)
        {
            for(int j=l;j<m-1;j++)
            {
                if(a[0][j]==a[0][j+1])
                {
                    if(a[j][0])
                    {add(0,j,1,1); change(0,j,1,1);}
                }
                else if(a[0][j])
                {add(0,j,0,1); change(0,j,0,1);}
                else
                {add(0,j,0,0); change(0,j,0,0);}
            }
            k=true;
        }

        answer(l,k);

        cout<<ans.size()<<'\n';
        for(out q:ans)
            print(q);

        ans.clear();
    }
    return 0;
}