#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include "stdio.h"

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define mp(x,y) make_pair(x,y)

// input

void read(ll a[], int s, int n) {
	for (int i = 0 + s; i < n + s; ++i) {
		scanf("%ll", &a[i]);
	}
}

void read(int a[], int s, int n) {
	for (int i = 0 + s; i < n + s; ++i) {
		scanf("%d", &a[i]);
	}
}

// fenwick

ll getSum(ll bits[], int index)
{
	ll sum = 0;
	while (index > 0)
	{
		sum += bits[index];
		index -= index & (-index);
	}
	return sum;
}

void updateAt(ll bits[], int n, int index, ll val)
{
	while (index <= n)
	{
		bits[index] += val;
		index += index & (-index);
	}
}

static const int MX_VAL = 1e6 + 2;
ll bits[MX_VAL + 1] = {};

// inversions

ll getInvCnt(ll a[], int n)
{
	ll cnt = 0;
	memset(bits, 0, sizeof(bits));
	for (int i = n - 1; i >= 0; i--)
	{
		cnt += getSum(bits, a[i] - 1);
		updateAt(bits, MX_VAL, a[i], 1);
	}

	return cnt;
}

const int N = 200;
string s[N];
vector<vector<int>> res;
void invert(int x, int y) {
	if (res.empty() || res.back().size() == 6)
		res.push_back({});
	if (s[x][y] == '0')
		s[x][y] = '1';
	else
		s[x][y] = '0';

	res.back().push_back(x);
	res.back().push_back(y);
}

void solve_case()
{
	int n, m;
	cin >> n >> m;
	res.clear();
	for (int i = 0; i < n; ++i)
		cin >> s[i];

	for (int i = 0; i < n-1; i++)
		for (int j = 0; j < m - 1; j++) {
			if (s[i][j] == '0' && s[i + 1][j] == '0') continue;
			if (s[i][j] == '1' && s[i + 1][j] == '1') {
				invert(i, j);
				invert(i + 1, j);
				invert(i + 1, j + 1);
			}
			if (s[i][j] == '0' && s[i + 1][j] == '1') {
				invert(i, j+1);
				invert(i + 1, j);
				invert(i + 1, j + 1);
			}
			if (s[i][j] == '1' && s[i + 1][j] == '0') {
				invert(i, j);
				invert(i, j+1);
				invert(i + 1, j + 1);
			}
		}
	int j = n - 2;
	for (int i = 0; i < n - 1; i++)
	{
		if (s[i][j] == '0' && s[i][j+1] == '0') continue;
		if (s[i][j] == '1' && s[i][j+1] == '1') {
			invert(i, j);
			invert(i, j + 1);
			invert(i + 1, j + 1);
		}
		if (s[i][j] == '0' && s[i][j+1] == '1') {
			invert(i, j + 1);
			invert(i + 1, j);
			invert(i + 1, j + 1);
		}
		if (s[i][j] == '1' && s[i][j+1] == '0') {
			invert(i, j);
			invert(i + 1, j);
			invert(i + 1, j + 1);
		}
	}
	int i = n - 1;
	j = m - 2;
	if (s[i][j] == '0' && s[i][j + 1] == '0') {
	}
	else  if (s[i][j] == '1' && s[i][j + 1] == '1') {
		invert(i-1, j);
		invert(i-1, j+1);
		invert(i, j);

		invert(i - 1, j);
		invert(i - 1, j + 1);
		invert(i, j+1);
	}
	if (s[i][j] == '0' && s[i][j + 1] == '1') {
		invert(i, j+1);
		invert(i - 1, j + 1);
		invert(i - 1, j);

		invert(i, j);
		invert(i, j + 1);
		invert(i - 1, j + 1);

		invert(i, j);
		invert(i, j + 1);
		invert(i-1, j);

	}
	if (s[i][j] == '1' && s[i][j + 1] == '0') {
		invert(i, j );
		invert(i - 1, j + 1);
		invert(i - 1, j);

		invert(i, j);
		invert(i, j + 1);
		invert(i - 1, j + 1);

		invert(i, j);
		invert(i, j + 1);
		invert(i - 1, j);
	}

	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			if (s[i][j] == '1')
				throw 34;
	cout << res.size() << endl;
	for (int i = 0; i < res.size(); ++i) {
		for (auto x : res[i]) cout << x + 1<< " ";
		cout << endl;
	}
	cout << endl;
}

void prepare()
{

}

int main()
{
#ifdef __OLIMP__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int T = 1;
	scanf("%d", &T);
	//prepare();
	for (int tc = 1; tc <= T; tc++)
		solve_case();

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include "stdio.h"

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define mp(x,y) make_pair(x,y)

// input

void read(ll a[], int s, int n) {
	for (int i = 0 + s; i < n + s; ++i) {
		scanf("%ll", &a[i]);
	}
}

void read(int a[], int s, int n) {
	for (int i = 0 + s; i < n + s; ++i) {
		scanf("%d", &a[i]);
	}
}

// fenwick

ll getSum(ll bits[], int index)
{
	ll sum = 0;
	while (index > 0)
	{
		sum += bits[index];
		index -= index & (-index);
	}
	return sum;
}

void updateAt(ll bits[], int n, int index, ll val)
{
	while (index <= n)
	{
		bits[index] += val;
		index += index & (-index);
	}
}

static const int MX_VAL = 1e6 + 2;
ll bits[MX_VAL + 1] = {};

// inversions

ll getInvCnt(ll a[], int n)
{
	ll cnt = 0;
	memset(bits, 0, sizeof(bits));
	for (int i = n - 1; i >= 0; i--)
	{
		cnt += getSum(bits, a[i] - 1);
		updateAt(bits, MX_VAL, a[i], 1);
	}

	return cnt;
}

const int N = 200;
string s[N];
vector<vector<int>> res;
void invert(int x, int y) {
	if (res.empty() || res.back().size() == 6)
		res.push_back({});
	if (s[x][y] == '0')
		s[x][y] = '1';
	else
		s[x][y] = '0';

	res.back().push_back(x);
	res.back().push_back(y);
}

void solve_case()
{
	int n, m;
	cin >> n >> m;
	res.clear();
	for (int i = 0; i < n; ++i)
		cin >> s[i];

	for (int i = 0; i < n-1; i++)
		for (int j = 0; j < m - 1; j++) {
			if (s[i][j] == '0' && s[i + 1][j] == '0') continue;
			if (s[i][j] == '1' && s[i + 1][j] == '1') {
				invert(i, j);
				invert(i + 1, j);
				invert(i + 1, j + 1);
			}
			if (s[i][j] == '0' && s[i + 1][j] == '1') {
				invert(i, j+1);
				invert(i + 1, j);
				invert(i + 1, j + 1);
			}
			if (s[i][j] == '1' && s[i + 1][j] == '0') {
				invert(i, j+1);
				invert(i + 1, j);
				invert(i + 1, j + 1);
			}
		}
	int j = n - 2;
	for (int i = 0; i < n - 1; i++)
	{
		if (s[i][j] == '0' && s[i][j+1] == '0') continue;
		if (s[i][j] == '1' && s[i][j+1] == '1') {
			invert(i, j);
			invert(i, j + 1);
			invert(i + 1, j + 1);
		}
		if (s[i][j] == '0' && s[i][j+1] == '1') {
			invert(i, j + 1);
			invert(i + 1, j);
			invert(i + 1, j + 1);
		}
		if (s[i][j] == '1' && s[i][j+1] == '0') {
			invert(i, j);
			invert(i + 1, j);
			invert(i + 1, j + 1);
		}
	}
	int i = n - 1;
	j = m - 2;
	if (s[i][j] == '0' && s[i][j + 1] == '0') {
	}
	else  if (s[i][j] == '1' && s[i][j + 1] == '1') {
		invert(i-1, j);
		invert(i-1, j+1);
		invert(i, j);

		invert(i - 1, j);
		invert(i - 1, j + 1);
		invert(i, j+1);
	}
	if (s[i][j] == '0' && s[i][j + 1] == '1') {
		invert(i, j+1);
		invert(i - 1, j + 1);
		invert(i - 1, j);

		invert(i, j);
		invert(i, j + 1);
		invert(i - 1, j + 1);

		invert(i, j);
		invert(i, j + 1);
		invert(i-1, j);

	}
	if (s[i][j] == '1' && s[i][j + 1] == '0') {
		invert(i, j );
		invert(i - 1, j + 1);
		invert(i - 1, j);

		invert(i, j);
		invert(i, j + 1);
		invert(i - 1, j + 1);

		invert(i, j);
		invert(i, j + 1);
		invert(i - 1, j);
	}

	for (int i = 0; i < n; ++i)
		for (int j = 0; j < 0; ++j)
			if (s[i][j] == '1')
				throw 34;
	cout << res.size() << endl;
	for (int i = 0; i < res.size(); ++i) {
		for (auto x : res[i]) cout << x << " ";
		cout << endl;
	}
	cout << endl;
}

void prepare()
{

}

int main()
{
#ifdef __OLIMP__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int T = 1;
	scanf("%d", &T);
	//prepare();
	for (int tc = 1; tc <= T; tc++)
		solve_case();

	return 0;
}


#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define ll long long
#define mod 1e9 + 7
#define MAXN 100005
using namespace std;
ll gcd(ll a, ll b)
{
    return (!b) ? a : gcd(b, a % b);
}
ll qpow(ll a, ll b, ll m)
{
    ll temp = a % m;
    a = 1;
    while (b)
    {
        if (b % 2 != 0)
            a = (a * temp) % m;
        temp = (temp * temp) % m;
        b >>= 1;
    }
    return (ll)(a % m);
}
ll lowbit(ll x)
{
    return x & (-x);
}
struct operation
{
    int x1, y1, x2, y2, x3, y3;
} g;
char num[105][105];
queue<operation> q;
void operate(int i, int j)
{
    int t;
    //x1
    g.x2 = g.x1 = i;
    g.y3 = g.y1 = j;
    if (i != 1)
        g.x3 = i - 1;
    else
        g.x3 = i + 1;
    if (j != 1)
        g.y2 = j - 1;
    else
        g.y2 = j + 1;
    q.push(g);
    //x2
    t = g.y3;
    g.y3 = g.y2;
    q.push(g);
    g.y3 = t;
    g.x2 = g.x3;
    q.push(g);
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        getchar();
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                scanf("%c", &num[i][j]);
                if (num[i][j] == '1')
                {
                    operate(i, j);
                }
            }
            getchar();
        }
        cout << q.size() << endl;
        while(!q.empty()){
            g = q.front();
            q.pop();
            printf("%d %d %d %d %d %d\n", g.x1, g.y1, g.x2, g.y2, g.x3, g.y3);
        }
    }

    return 0;
}


#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define ll long long
#define mod 1e9 + 7
#define MAXN 100005
using namespace std;
ll gcd(ll a, ll b)
{
    return (!b) ? a : gcd(b, a % b);
}
ll qpow(ll a, ll b, ll m)
{
    ll temp = a % m;
    a = 1;
    while (b)
    {
        if (b % 2 != 0)
            a = (a * temp) % m;
        temp = (temp * temp) % m;
        b >>= 1;
    }
    return (ll)(a % m);
}
ll lowbit(ll x)
{
    return x & (-x);
}
struct operation
{
    int x1, y1, x2, y2, x3, y3;
} g;
char num[105][105];
queue<operation> q;
void operate(int i, int j)
{
    int t;
    //x1
    g.x2 = g.x1 = i;
    g.y3 = g.y1 = j;
    if (i != 1)
        g.x3 = i - 1;
    else
        g.x3 = i + 1;
    if (j != 1)
        g.y2 = j - 1;
    else
        g.y2 = j + 1;
    q.push(g);
    //x2
    t = g.y3;
    g.y3 = g.y2;
    q.push(g);
    g.y3 = t;
    g.x2 = g.y3;
    q.push(g);
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        getchar();
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                scanf("%c", &num[i][j]);
                if (num[i][j] == '1')
                {
                    operate(i, j);
                }
            }
            getchar();
        }
        cout << q.size() << endl;
        while(!q.empty()){
            g = q.front();
            q.pop();
            printf("%d %d %d %d %d %d\n", g.x1, g.y1, g.x2, g.y2, g.x3, g.y3);
        }
    }

    return 0;
}


//#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
//#include <fstream>
//#include <sstream>
//#include <bitset>
//#include <time.h>
//#include <random>

using namespace std;

void Swap(int i, int j, int i1, int j1, int i2, int j2, vector <vector <int>> &s, vector <vector <int>> &ans) {
	s[i][j] = (s[i][j] + 1) % 2;
	s[i1][j1] = (s[i1][j1] + 1) % 2;
	s[i2][j2] = (s[i2][j2] + 1) % 2;
	ans.push_back(vector<int> {i, j, i1, j1, i2, j2});
}

void solve() {
	int n, m;
	cin >> n >> m;
	vector <vector <int>> s(n, vector <int>(m, 0));
	for (int i = 0; i < n; i++) {
		string st;
		cin >> st;
		for (int j = 0; j < m; j++) {
			if (st[j] == '1') {
				s[i][j] = 1;
			}
		}
	}
	vector <vector <int>> ans;
	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < m - 1; j++) {
			if (s[i][j]) {
				s[i][j] = (s[i][j] + 1) % 2;
				s[i + 1][j + 1] = (s[i + 1][j + 1] + 1) % 2;
				s[i + 1][j] = (s[i + 1][j] + 1) % 2;
				ans.push_back(vector<int> {i, j, i + 1, j, i + 1, j + 1});
			}
		}
		if (s[i][m - 1]) {
			s[i][m - 1] = (s[i][m - 1] + 1) % 2;
			s[i + 1][m - 2] = (s[i + 1][m - 2] + 1) % 2;
			s[i + 1][m - 1] = (s[i + 1][m - 1] + 1) % 2;
			ans.push_back(vector<int> {i, m - 1, i + 1, m - 2, i + 1, m - 1});
		}
	}

	int i = n - 1;
	for (int j = 0; j < m - 2; j++)
		if (s[i][j] == 1)
		{
			Swap(i, j, i, j + 1, i - 1, j + 1, s, ans);
			Swap(i - 1, j + 1, i, j + 1, i, j + 2, s, ans);
		}
	int j = m - 2;
	if (s[i][j] == 1)
		Swap(i, j, i, j + 1, i - 1, j + 1, s, ans);
	i = n - 1;
	j = m - 1;
	if (s[i][j] == 1 || s[i - 1][j] == 1)
	{
		if (s[i][j] + s[i - 1][j] == 1)
		{
			if (s[i][j] == 1)
				Swap(i, j, i, j - 1, i - 1, j - 1, s, ans);
			else
				Swap(i - 1, j, i, j - 1, i - 1, j - 1, s, ans);
			Swap(i, j, i - 1, j, i - 1, j - 1, s, ans);
			Swap(i, j, i - 1, j, i, j - 1, s, ans);
		}
		else
		{
			Swap(i - 1, j, i, j - 1, i - 1, j - 1, s, ans);
			Swap(i, j, i, j - 1, i - 1, j - 1, s, ans);
		}
	}

	cout << ans.size() << endl;
	for (auto i : ans) {
		for (auto j : i) {
			cout << j + 1 << " ";
		}
		cout << endl;
	}

}

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		solve();
	}
}


#include <iostream>
#include<queue>
//#include <unordered_map >
#include <set>
#include <map>
#include <vector>
#include <string>
#include<functional>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include <list>
using namespace std;

char str[105][105];
int k = 0;
vector<int> gai;
void gaiThree(int x1, int y1, int x2, int y2, int x3, int y3);
void gaiOne(int i, int j, int x11, int y11);
void gaiTwo(int i, int j, int x11, int y11, int x22, int y22);
void gaiFour(int i, int j);

void reverse(int i, int j)
{
    if (str[i][j] == '0')
        str[i][j] = '1';
    else
        str[i][j] = '0';
    gai.push_back(i);
    gai.push_back(j);
}

void gaiOne(int i, int j, int x11, int y11)
{
    int x1, y1, x2, y2;
    int co = 0;
    for (int ii = i; ii < i + 2; ii++)
        for (int jj = j; jj < j + 2; jj++)
        {
            if (str[ii][jj] == '0')
            {
                if (co == 0)
                {
                    x1 = ii, y1 = jj;
                    reverse(x1, y1);
                }
                else if (co == 1)
                {
                    x2 = ii, y2 = jj;
                    reverse(x2, y2);
                }
                co++;
            }
        }
    reverse(x11, y11);
    gaiTwo(i, j, x1, y1, x2, y2);
}

void gaiTwo(int i,int j,int x11, int y11, int x22, int y22)
{
    int x1, y1, x2, y2, x3, y3;
    int co = 0;
    for (int ii = i; ii < i + 2; ii++)
        for (int jj = j; jj < j + 2; jj++)
        {
            if (str[ii][jj] == '0')
            {
                if (co == 0)
                {
                    x1 = ii, y1 = jj;
                    reverse(x1, y1);
                }
                else if (co == 1)
                {
                    x2 = ii, y2 = jj;
                    reverse(x2, y2);
                }
                co++;
            }
        }
    reverse(x11, y11);
    gaiThree(x1, y1, x2, y2, x22,y22);
}

void gaiThree(int x1, int y1, int x2, int y2, int x3, int y3)
{
    reverse(x1, y1);
    reverse(x2, y2);
    reverse(x3, y3);
}

void gaiFour(int i,int j)
{
    reverse(i, j);
    reverse(i+1, j);
    reverse(i, j+1);
    gaiOne(i, j,i+1 , j+1);
}

void analysis(int i, int j)
{
    int x1, y1, x2, y2, x3, y3;
    int co = 0;
    for(int ii=i;ii<i+2;ii++)
        for (int jj = j; jj < j + 2; jj++)
        {
            if (str[ii][jj] == '1')
            {
                if (co == 0)
                {
                    x1 = ii, y1 = jj;

                }
                else if (co == 1)
                {
                    x2 = ii, y2 = jj;
                }
                else if (co == 2)
                {
                    x3 = ii, y3 = jj;
                }
                co++;
            }
        }
    if (co == 3)
    {
        k++;
        gaiThree(x1, y1, x2, y2, x3, y3);
    }
    if (co == 2)
    {
        k += 2;
        gaiTwo(i,j,x1, y1, x2, y2);
    }
    if (co == 1)
    {
        k += 3;
        gaiOne(i, j, x1, y1);
    }
    if (co == 4)
    {
        k += 4;
        gaiFour(i, j);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        k = 0;
        gai.clear();
        vector <int>().swap(gai);
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
            {
                cin >> str[i][j];
            }
        for (int i = 1; i < n; i++)
            for (int j = 1; j < m; j++)
            {
                if (j == m - 1)
                {
                    analysis(i, j);
                }
                if (str[i][j] == '0' && str[i + 1][j] == '0')
                {
                    continue;
                }
                if (str[i][j] == '1' && str[i + 1][j] == '1')
                {
                    k++;
                    reverse(i, j);
                    reverse(i+1, j);
                    reverse(i+1, j+1);
                    continue;
                }
                if (str[i][j] == '1' && str[i + 1][j] == '0')
                {
                    k++;
                    reverse(i, j);
                    reverse(i, j + 1);
                    reverse(i + 1, j + 1);
                    continue;
                }
                if (str[i][j] == '0' && str[i + 1][j] == '1')
                {
                    k++;
                    reverse(i + 1, j);
                    reverse(i, j + 1);
                    reverse(i + 1, j + 1);
                    continue;
                }
            }
        cout << k << '\n';
        for (int i = 0; i < gai.size(); i++)
        {
            cout << gai[i];
            if ((i + 1) % 6 == 0)
                cout << '\n';
            else
                cout << ' ';
        }
    }
    return 0;
}


#include <iostream>
#include<queue>
//#include <unordered_map >
#include <set>
#include <map>
#include <vector>
#include <string>
#include<functional>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include <list>
using namespace std;

char str[105][105];
int k = 0;
vector<int> gai;
void gaiThree(int x1, int y1, int x2, int y2, int x3, int y3);
void gaiOne(int i, int j, int x11, int y11);
void gaiTwo(int i, int j, int x11, int y11, int x22, int y22);
void gaiFour(int i, int j);

void reverse(int i, int j)
{
    if (str[i][j] == '0')
        str[i][j] = '1';
    else
        str[i][j] = '0';
    gai.push_back(i);
    gai.push_back(j);
}

void gaiOne(int i, int j, int x11, int y11)
{
    int x1, y1, x2, y2;
    int co = 0;
    for (int ii = i; ii < i + 2; ii++)
        for (int jj = j; jj < j + 2; jj++)
        {
            if (str[ii][jj] == '0')
            {
                if (co == 0)
                {
                    x1 = ii, y1 = jj;
                    reverse(x1, y1);
                }
                else if (co == 1)
                {
                    x2 = ii, y2 = jj;
                    reverse(x2, y2);
                }
                co++;
            }
        }
    reverse(x11, y11);
    gaiTwo(i, j, x1, y1, x2, y2);
}

void gaiTwo(int i,int j,int x11, int y11, int x22, int y22)
{
    int x1, y1, x2, y2, x3, y3;
    int co = 0;
    for (int ii = i; ii < i + 2; ii++)
        for (int jj = j; jj < j + 2; jj++)
        {
            if (str[ii][jj] == '0')
            {
                if (co == 0)
                {
                    x1 = ii, y1 = jj;
                    reverse(x1, y1);
                }
                else if (co == 1)
                {
                    x2 = ii, y2 = jj;
                    reverse(x2, y2);
                }
                co++;
            }
        }
    reverse(x11, y11);
    gaiThree(x1, y1, x2, y2, x22,y22);
}

void gaiThree(int x1, int y1, int x2, int y2, int x3, int y3)
{
    reverse(x1, y1);
    reverse(x2, y2);
    reverse(x3, y3);
}

void gaiFour(int i,int j)
{
    reverse(i, j);
    reverse(i+1, j);
    reverse(i, j+1);
    gaiOne(i, j,i+1 , j+1);
}

void analysis(int i, int j)
{
    int x1, y1, x2, y2, x3, y3;
    int co = 0;
    for(int ii=i;ii<i+2;ii++)
        for (int jj = j; jj < j + 2; jj++)
        {
            if (str[ii][jj] == '1')
            {
                if (co == 0)
                {
                    x1 = ii, y1 = jj;

                }
                else if (co == 1)
                {
                    x2 = ii, y2 = jj;
                }
                else if (co == 2)
                {
                    x3 = ii, y3 = jj;
                }
                co++;
            }
        }
    if (co == 3)
    {
        k++;
        gaiThree(x1, y1, x2, y2, x3, y3);
    }
    if (co == 2)
    {
        k += 2;
        gaiTwo(i,j,x1, y1, x2, y2);
    }
    if (co == 1)
    {
        k += 3;
        gaiOne(i, j, x1, y1);
    }
    if (co == 4)
    {
        gaiFour(i, j);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        k = 0;
        gai.clear();
        vector <int>().swap(gai);
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
            {
                cin >> str[i][j];
            }
        for (int i = 1; i < n; i++)
            for (int j = 1; j < m; j++)
            {
                if (j == m - 1)
                {
                    analysis(i, j);
                }
                if (str[i][j] == '0' && str[i + 1][j] == '0')
                {
                    continue;
                }
                if (str[i][j] == '1' && str[i + 1][j] == '1')
                {
                    k++;
                    reverse(i, j);
                    reverse(i+1, j);
                    reverse(i+1, j+1);
                    continue;
                }
                if (str[i][j] == '1' && str[i + 1][j] == '0')
                {
                    k++;
                    reverse(i, j);
                    reverse(i, j + 1);
                    reverse(i + 1, j + 1);
                    continue;
                }
                if (str[i][j] == '0' && str[i + 1][j] == '1')
                {
                    k++;
                    reverse(i + 1, j);
                    reverse(i, j + 1);
                    reverse(i + 1, j + 1);
                    continue;
                }
            }
        cout << k << '\n';
        for (int i = 0; i < gai.size(); i++)
        {
            cout << gai[i];
            if ((i + 1) % 6 == 0)
                cout << '\n';
            else
                cout << ' ';
        }
    }
    return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,tune=native")
#pragma GCC target("avx,avx2")

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <cassert>
#include <cmath>
#include <stack>
#include <ctime>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <random>
//#include "optimization.h"
using namespace std;
const double PI = 3.141592653589793;
typedef long long ll;
typedef unsigned long long ull;

random_device dev;
mt19937 rng(dev());
ll mod = 1e9;

void solve() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>> a(n, vector<int>(m));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
		}
	}
	vector<vector<pair<int, int>>> ans;;

	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < m - 1; i++) {
			if (a[i][j]) {
				ans.push_back(vector<pair<int, int>>(0));
				ans.back().push_back({ i + 1 ,  j + 1 });
				ans.back().push_back({ i + 2 ,  j + 1 });
				ans.back().push_back({ i + 2 ,  j + 2 });
				a[i][j] ^= 1;
				a[i + 1][j] ^= 1;
				a[i + 1][j + 1] ^= 1;

			}
		}
		if (a[i][m - 1]) {
			ans.push_back(vector<pair<int, int>>(0));
			ans.back().push_back({ i + 1 ,  m });
			ans.back().push_back({ i + 2 ,  m });
			ans.back().push_back({ i + 2 ,  m - 1});
			a[i][m - 1] ^= 1;
			a[i + 1][m - 1] ^= 1;
			a[i + 1][m - 2] ^= 1;
		}
	}
	for (int j = 0; j < m - 1; j++) {
		if (a[n - 1][j]) {
			ans.push_back(vector<pair<int, int>>(0));
			ans.back().push_back({ n ,  j + 1 });
			ans.back().push_back({ n - 1 , j + 1  });
			ans.back().push_back({ n - 1 , j + 2 });
			a[n - 1][j] ^= 1;
			a[n - 2][j] ^= 1;
			a[n - 2][j + 1] ^= 1;

			ans.push_back(vector<pair<int, int>>(0));
			ans.back().push_back({ n ,  j + 2 });
			ans.back().push_back({ n - 1 , j + 2 });
			ans.back().push_back({ n - 1 , j + 1 });
			a[n - 1][j + 1] ^= 1;
			a[n - 2][j + 1] ^= 1;
			a[n - 2][j] ^= 1;
		}
	}

	if (a[n - 1][m - 1]) {
		ans.push_back(vector<pair<int, int>>(0));
		ans.back().push_back({ n ,  m });
		ans.back().push_back({ n - 1 , m });
		ans.back().push_back({ n , m - 1 });

		ans.push_back(vector<pair<int, int>>(0));
		ans.back().push_back({ n - 1,  m - 1 });
		ans.back().push_back({ n - 1 , m });
		ans.back().push_back({ n , m - 1 });
	}

	cout << ans.size() << "\n";
	for (int i = 0; i < ans.size(); i++) {
		for (int j = 0; j < ans[i].size(); j++) {
			cout << ans[i][j].first << " " << ans[i][j].second << " ";
		}
		cout << "\n";
	}
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t = 1;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,tune=native")
#pragma GCC target("avx,avx2")

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <cassert>
#include <cmath>
#include <stack>
#include <ctime>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <random>
//#include "optimization.h"
using namespace std;
const double PI = 3.141592653589793;
typedef long long ll;
typedef unsigned long long ull;

random_device dev;
mt19937 rng(dev());
ll mod = 1e9;

void solve() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>> a(n, vector<int>(m));
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		for (int j = 0; j < m; j++) {
			if (s[j] == '1') {
				a[i][j] = 1;
			}
			else {
				a[i][j] = 0;
			}
		}
	}
	vector<vector<pair<int, int>>> ans;;

	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < m - 1; j++) {
			if (a[i][j]) {
				ans.push_back(vector<pair<int, int>>(0));
				ans.back().push_back({ i + 1 ,  j + 1 });
				ans.back().push_back({ i + 2 ,  j + 1 });
				ans.back().push_back({ i + 2 ,  j + 2 });
				//cout << "HERE\n";
				a[i][j] ^= 1;
				a[i + 1][j] ^= 1;
				a[i + 1][j + 1] ^= 1;

			}
		}
		if (a[i][m - 1]) {
			ans.push_back(vector<pair<int, int>>(0));
			ans.back().push_back({ i + 1 ,  m });
			ans.back().push_back({ i + 2 ,  m });
			ans.back().push_back({ i + 2 ,  m - 1});
			a[i][m - 1] ^= 1;
			a[i + 1][m - 1] ^= 1;
			a[i + 1][m - 2] ^= 1;
		}
	}
	for (int j = 0; j < m - 1; j++) {
		if (a[n - 1][j]) {
			ans.push_back(vector<pair<int, int>>(0));
			ans.back().push_back({ n ,  j + 1 });
			ans.back().push_back({ n - 1 , j + 1  });
			ans.back().push_back({ n - 1 , j + 2 });
			a[n - 1][j] ^= 1;
			a[n - 2][j] ^= 1;
			a[n - 2][j + 1] ^= 1;

			ans.push_back(vector<pair<int, int>>(0));
			ans.back().push_back({ n ,  j + 2 });
			ans.back().push_back({ n - 1 , j + 2 });
			ans.back().push_back({ n - 1 , j + 1 });
			a[n - 1][j + 1] ^= 1;
			a[n - 2][j + 1] ^= 1;
			a[n - 2][j] ^= 1;
		}
	}

	if (a[n - 1][m - 1]) {
		ans.push_back(vector<pair<int, int>>(0));
		ans.back().push_back({ n ,  m });
		ans.back().push_back({ n - 1 , m });
		ans.back().push_back({ n , m - 1 });

		ans.push_back(vector<pair<int, int>>(0));
		ans.back().push_back({ n - 1,  m - 1 });
		ans.back().push_back({ n - 1 , m });
		ans.back().push_back({ n , m - 1 });
	}

	cout << ans.size() << "\n";
	for (int i = 0; i < ans.size(); i++) {
		for (int j = 0; j < ans[i].size(); j++) {
			cout << ans[i][j].first << " " << ans[i][j].second << " ";
		}
		cout << "\n";
	}
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t = 1;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,tune=native")
#pragma GCC target("avx,avx2")

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <cassert>
#include <cmath>
#include <stack>
#include <ctime>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <random>
//#include "optimization.h"
using namespace std;
const double PI = 3.141592653589793;
typedef long long ll;
typedef unsigned long long ull;

random_device dev;
mt19937 rng(dev());
ll mod = 1e9;

void solve() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>> a(n, vector<int>(m));
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		for (int j = 0; j < m; j++) {
			if (s[j] == '1') {
				a[i][j] = 1;
			}
			else {
				a[i][j] = 0;
			}
		}
	}
	vector<vector<pair<int, int>>> ans;;

	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < m - 1; j++) {
			if (a[i][j]) {
				ans.push_back(vector<pair<int, int>>(0));
				ans.back().push_back({ i + 1 ,  j + 1 });
				ans.back().push_back({ i + 2 ,  j + 1 });
				ans.back().push_back({ i + 2 ,  j + 2 });
				//cout << "HERE\n";
				a[i][j] ^= 1;
				a[i + 1][j] ^= 1;
				a[i + 1][j + 1] ^= 1;

			}
		}
		if (a[i][m - 1]) {
			ans.push_back(vector<pair<int, int>>(0));
			ans.back().push_back({ i + 1 ,  m });
			ans.back().push_back({ i + 2 ,  m });
			ans.back().push_back({ i + 2 ,  m - 1});
			a[i][m - 1] ^= 1;
			a[i + 1][m - 1] ^= 1;
			a[i + 1][m - 2] ^= 1;
		}
	}
	for (int j = 0; j < m - 1; j++) {
		if (a[n - 1][j]) {
			ans.push_back(vector<pair<int, int>>(0));
			ans.back().push_back({ n ,  j + 1 });
			ans.back().push_back({ n - 1 , j + 1  });
			ans.back().push_back({ n - 1 , j + 2 });
			a[n - 1][j] ^= 1;
			a[n - 2][j] ^= 1;
			a[n - 2][j + 1] ^= 1;

			ans.push_back(vector<pair<int, int>>(0));
			ans.back().push_back({ n ,  j + 2 });
			ans.back().push_back({ n - 1 , j + 2 });
			ans.back().push_back({ n - 1 , j + 1 });
			a[n - 1][j + 1] ^= 1;
			a[n - 2][j + 1] ^= 1;
			a[n - 2][j] ^= 1;
		}
	}
	//cout << "\n";
	//for (int i = 0; i < n; i++) {
	//	for (int j = 0; j < m; j++) {
	//		cout << a[i][j] << " ";
	//	}
	//	cout << "\n";
	//}
	//cout << "\n";
	if (a[n - 1][m - 1]) {
		ans.push_back(vector<pair<int, int>>(0));
		ans.back().push_back({ n ,  m });
		ans.back().push_back({ n - 1 , m });
		ans.back().push_back({ n , m - 1 });

		ans.push_back(vector<pair<int, int>>(0));
		ans.back().push_back({ n ,  m });
		ans.back().push_back({ n , m - 1 });
		ans.back().push_back({ n - 1 , m - 1 });

		ans.push_back(vector<pair<int, int>>(0));
		ans.back().push_back({ n - 1,  m - 1 });
		ans.back().push_back({ n - 1 , m });
		ans.back().push_back({ n , m - 1 });
	}

	cout << ans.size() << "\n";
	for (int i = 0; i < ans.size(); i++) {
		for (int j = 0; j < ans[i].size(); j++) {
			cout << ans[i][j].first << " " << ans[i][j].second << " ";
		}
		cout << "\n";
	}
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t = 1;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,tune=native")
#pragma GCC target("avx,avx2")

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <cassert>
#include <cmath>
#include <stack>
#include <ctime>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <random>
//#include "optimization.h"
using namespace std;
const double PI = 3.141592653589793;
typedef long long ll;
typedef unsigned long long ull;

random_device dev;
mt19937 rng(dev());
ll mod = 1e9;

void solve() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>> a(n, vector<int>(m));
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		for (int j = 0; j < m; j++) {
			if (s[j] == '1') {
				a[i][j] = 1;
			}
			else {
				a[i][j] = 0;
			}
		}
	}
	vector<vector<pair<int, int>>> ans;;

	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < m - 1; j++) {
			if (a[i][j]) {
				ans.push_back(vector<pair<int, int>>(0));
				ans.back().push_back({ i + 1 ,  j + 1 });
				ans.back().push_back({ i + 2 ,  j + 1 });
				ans.back().push_back({ i + 2 ,  j + 2 });
				//cout << "HERE\n";
				a[i][j] ^= 1;
				a[i + 1][j] ^= 1;
				a[i + 1][j + 1] ^= 1;

			}
		}
		if (a[i][m - 1]) {
			ans.push_back(vector<pair<int, int>>(0));
			ans.back().push_back({ i + 1 ,  m });
			ans.back().push_back({ i + 2 ,  m });
			ans.back().push_back({ i + 2 ,  m - 1});
			a[i][m - 1] ^= 1;
			a[i + 1][m - 1] ^= 1;
			a[i + 1][m - 2] ^= 1;
		}
	}
	for (int j = 0; j < m - 1; j++) {
		if (a[n - 1][j]) {
			ans.push_back(vector<pair<int, int>>(0));
			ans.back().push_back({ n ,  j + 1 });
			ans.back().push_back({ n - 1 , j + 1  });
			ans.back().push_back({ n - 1 , j + 2 });
			a[n - 1][j] ^= 1;
			a[n - 2][j] ^= 1;
			a[n - 2][j + 1] ^= 1;

			ans.push_back(vector<pair<int, int>>(0));
			ans.back().push_back({ n ,  j + 2 });
			ans.back().push_back({ n - 1 , j + 2 });
			ans.back().push_back({ n - 1 , j + 1 });
			a[n - 1][j + 1] ^= 1;
			a[n - 2][j + 1] ^= 1;
			a[n - 2][j] ^= 1;
		}
	}
	//cout << "\n";
	//for (int i = 0; i < n; i++) {
	//	for (int j = 0; j < m; j++) {
	//		cout << a[i][j] << " ";
	//	}
	//	cout << "\n";
	//}
	//cout << "\n";
	if (a[n - 1][m - 1]) {
		ans.push_back(vector<pair<int, int>>(0));
		ans.back().push_back({ n ,  m });
		ans.back().push_back({ n - 1 , m });
		ans.back().push_back({ n , m - 1 });

		ans.push_back(vector<pair<int, int>>(0));
		ans.back().push_back({ n ,  m });
		ans.back().push_back({ n , m - 1 });
		ans.back().push_back({ n - 1 , m - 1 });

		ans.push_back(vector<pair<int, int>>(0));
		ans.back().push_back({ n - 1,  m - 1 });
		ans.back().push_back({ n - 1, m });
		ans.back().push_back({ n , m  });
	}

	cout << ans.size() << "\n";
	for (int i = 0; i < ans.size(); i++) {
		for (int j = 0; j < ans[i].size(); j++) {
			cout << ans[i][j].first << " " << ans[i][j].second << " ";
		}
		cout << "\n";
	}
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t = 1;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vpii = vector<pii>; using vpll = vector<pll>;
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (const auto &x : v) os << x << " "; return os; }

void print(int i1, int j1, int i2, int j2, int i3, int j3) {
	cout << i1 << " " << j1 << " " << i2 << " " << j2 << " " << i3 << " " << j3 << endl;
}

void solve() {
	int n, m;
	cin >> n >> m;
	vector<string> ss(n);
	for (int i=0; i<n; i++)
		cin >> ss[i];
	int cnt = 0;
	for (int i=1; i<=n; i++) {
		for (int j=1; j<=m; j++) {
			if (ss[i-1][j-1]=='1')
				cnt++;
		}
	}
	cout << cnt << endl;
	for (int i=1; i<=n; i++) {
		for (int j=1; j<=m; j++) {
			if (ss[i-1][j-1]=='1') {
				int di = i<n ? 1 : -1;
				int dj = j<m ? 1 : -1;
				print(i,j, i+di,j, i, j+dj);
				print(i,j, i,j+dj, i+di, j+dj);
				print(i,j, i+di,j, i+di, j+dj);
			}
		}
	}
}

int main(int argc, char const *argv[]) {
	int T;
	cin >> T;
	for (int t=0; t<T; t++)
		solve();
	return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vpii = vector<pii>; using vpll = vector<pll>;
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (const auto &x : v) os << x << " "; return os; }

void print(int i1, int j1, int i2, int j2, int i3, int j3, vector<vector<int>>& ops) {
	//cout << i1 << " " << j1 << " " << i2 << " " << j2 << " " << i3 << " " << j3 << endl;
	vector<int> op = {i1, j1, i2, j2, i3, j3};
	ops.push_back(op);
}

int applyOps(vector<string>& ss, vector<vector<int>>& ops) {
	for (vector<int>& op : ops) {
		for (int k=0; k<op.size(); k+=2) {
			int i = op[k], j = op[k+1];
			ss[i-1][j-1] = (ss[i-1][j-1] == '0' ? '1' : '0');
		}
	}
	return 0;
}

void solve() {
	vector<vector<int>> ops;
	int n, m;
	cin >> n >> m;
	vector<string> ss(n);
	for (int i=0; i<n; i++)
		cin >> ss[i];
	int cnt = 0;
	for (int i=1; i<=n; i++) {
		for (int j=1; j<=m; j++) {
			if (ss[i-1][j-1]=='1')
				cnt++;
		}
	}
	//cout << cnt << endl;
	for (int i=1; i<=n; i++) {
		for (int j=1; j<=m; j++) {
			if (ss[i-1][j-1]=='1') {
				int di = i<n ? 1 : -1;
				int dj = j<m ? 1 : -1;
				print(i,j, i+di,j, i, j+dj, ops);
				print(i,j, i,j+dj, i+di, j+dj, ops);
				print(i,j, i+di,j, i+di, j+dj, ops);
			}
		}
	}
	//applyOps(ss, ops);
	cout << ops.size() << endl;
	for (vector<int>& op : ops) {
		cout << op << endl;
	}
}

int main(int argc, char const *argv[]) {
	int T;
	cin >> T;
	for (int t=0; t<T; t++)
		solve();
	return 0;
}


#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef pair<int,int> pii;
char a[110][110];
int n,m;
int po[31000][10];
int cnt=0;

void change(int i,int j){
	if (a[i][j]=='1')a[i][j]='0';
	else a[i][j]='1';
}
void add(vector<pii> cct){
	++cnt;
	for (int i=0;i<cct.size();++i){
		int ii=i+1;
		po[cnt][ii*2-1]=cct[i].first;
		po[cnt][ii*2]=cct[i].second;
		change(cct[i].first,cct[i].second);
	}return;
}

void Solve(int x,int y){
	vector<pii> cct;
	if (a[x][y]=='1')cct.push_back(pii(x,y));
	if (a[x+1][y]=='1')cct.push_back(pii(x+1,y));
	if (a[x+1][y+1]=='1')cct.push_back(pii(x+1,y+1));
	if (a[x][y+1]=='1')cct.push_back(pii(x,y+1));
	if (cct.size()==0)return;
	if (cct.size()==3){
		add(cct);
		return;
	}
	vector<pii> res;
	if (cct.size()==1){
		int xx = cct[0].first;
		int yy = cct[0].second;
		res.push_back(cct[0]);
		int ty;
		if (yy==y)ty=y+1;
		else ty=y;
		res.push_back(pii(x,ty));
		res.push_back(pii(x+1,ty));
		add(res);Solve(x,y);
		return;
	}
	if (cct.size()==4){
		res.push_back(pii(x,y));
		res.push_back(pii(x+1,y));
		res.push_back(pii(x+1,y+1));
		add(res);
		Solve(x,y);
		return;
	}
	if (cct.size()==2){

		if (cct[0].first!=cct[1].first&&cct[0].second!=cct[1].second){
			res.push_back(cct[0]);
			res.push_back(cct[1]);
			res.push_back(pii(cct[0].first,cct[1].second));
		}
		else {
			res.push_back(cct[0]);
			if (cct[0].first==cct[1].first){
				int tx;
				if (cct[0].first==x)tx=x+1;
				else tx=x;
				res.push_back(pii(tx,y));
				res.push_back(pii(tx,y+1));
			}
			else{
				int ty;
				if (cct[0].second==y)ty=y+1;
				else ty=y;
				res.push_back(pii(x,ty));
				res.push_back(pii(x+1,ty));
			}
		}add(res);
		Solve(x,y);
		return;
	}
}

int main(){
	ios::sync_with_stdio(0);
	int t;cin>>t;
	while (t--){
		cin>>n>>m;cnt=0;
		for (int i=1;i<=n;++i)
		for (int j=1;j<=m;++j)
		cin>>a[i][j];
		for (int i=1;i<n;i+=2){
			for (int j=1;j<m;j+=2){
				Solve(i,j);
			}
		}if (n&1){
			for (int j=1;j<m;j+=2)Solve(n-1,j);
		}
		if (m&1){
			for (int i=1;i<n;i+=2)Solve(i,m-1);
		}
		if ((n&1)&&(m&1))Solve(n-1,m-1);

		cout<<cnt<<endl;
		for (int i=1;i<=cnt;++i){
			for (int j=1;j<=6;++j)
			cout<<po[i][j]<<" ";
			cout<<endl;
		}
	}
}


#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef pair<int,int> pii;
char a[110][110];
int n,m;
int po[31000][10];
int cnt=0;

void change(int i,int j){
	if (a[i][j]=='1')a[i][j]='0';
	else a[i][j]='1';
}
void add(vector<pii> cct){
	++cnt;
	for (int i=0;i<cct.size();++i){
		int ii=i+1;
		po[cnt][ii*2-1]=cct[i].first;
		po[cnt][ii*2]=cct[i].second;
		change(cct[i].first,cct[i].second);
	}return;
}

void Solve(int x,int y){
	vector<pii> cct;
	if (a[x][y]=='1')cct.push_back(pii(x,y));
	if (a[x+1][y]=='1')cct.push_back(pii(x+1,y));
	if (a[x+1][y+1]=='1')cct.push_back(pii(x+1,y+1));
	if (a[x][y+1]=='1')cct.push_back(pii(x,y+1));
	if (cct.size()==0)return;
	if (cct.size()==3){
		add(cct);
		return;
	}
	vector<pii> res;
	if (cct.size()==1){
		cout<<x<<" "<<y<<endl;
		int xx = cct[0].first;
		int yy = cct[0].second;
		res.push_back(cct[0]);
		int ty;
		if (yy==y)ty=y+1;
		else ty=y;
		res.push_back(pii(x,ty));
		res.push_back(pii(x+1,ty));
		add(res);Solve(x,y);
		return;
	}
	if (cct.size()==4){
		res.push_back(pii(x,y));
		res.push_back(pii(x+1,y));
		res.push_back(pii(x+1,y+1));
		add(res);
		Solve(x,y);
		return;
	}
	if (cct.size()==2){

		if (cct[0].first!=cct[1].first&&cct[0].second!=cct[1].second){
			res.push_back(cct[0]);
			res.push_back(cct[1]);
			res.push_back(pii(cct[0].first,cct[1].second));
		}
		else {
			res.push_back(cct[0]);
			if (cct[0].first==cct[1].first){
				int tx;
				if (cct[0].first==x)tx=x+1;
				else tx=x;
				res.push_back(pii(tx,y));
				res.push_back(pii(tx,y+1));
			}
			else{
				int ty;
				if (cct[0].second==y)ty=y+1;
				else ty=y;
				res.push_back(pii(x,ty));
				res.push_back(pii(x+1,ty));
			}
		}add(res);
		Solve(x,y);
		return;
	}
}

int main(){
	ios::sync_with_stdio(0);
	int t;cin>>t;
	while (t--){
		cin>>n>>m;cnt=0;
		for (int i=1;i<=n;++i)
		for (int j=1;j<=m;++j)
		cin>>a[i][j];
		for (int i=1;i<n;i+=2){
			for (int j=1;j<m;j+=2){
				Solve(i,j);
			}
		}if (n&1){
			for (int j=1;j<m;j+=2)Solve(n-1,j);
		}
		if (m&1){
			for (int i=1;i<n;i+=2)Solve(i,m-1);
		}
		if ((n&1)&&(m&1))Solve(n-1,m-1);

		cout<<cnt<<endl;
		for (int i=1;i<=cnt;++i){
			for (int j=1;j<=6;++j)
			cout<<po[i][j]<<" ";
			cout<<endl;
		}
	}
}


#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef pair<int,int> pii;
char a[110][110];
int n,m;
int po[31000][10];
int cnt=0;

void change(int i,int j){
	if (a[i][j]=='1')a[i][j]='0';
	else a[i][j]='1';
}
void add(vector<pii> cct){
	++cnt;
	for (int i=0;i<cct.size();++i){
		int ii=i+1;
		po[cnt][ii*2-1]=cct[i].first;
		po[cnt][ii*2]=cct[i].second;
		change(cct[i].first,cct[i].second);
	}return;
}

void Solve(int x,int y){
	vector<pii> cct;
	if (a[x][y]=='1')cct.push_back(pii(x,y));
	if (a[x+1][y]=='1')cct.push_back(pii(x+1,y));
	if (a[x+1][y+1]=='1')cct.push_back(pii(x+1,y+1));
	if (a[x][y+1]=='1')cct.push_back(pii(x,y+1));
	if (cct.size()==0)return;
	if (cct.size()==3){
		add(cct);
		return;
	}
	vector<pii> res;
	if (cct.size()==1){
		cout<<x<<" "<<y<<endl;
		int xx = cct[0].first;
		int yy = cct[0].second;
		res.push_back(cct[0]);
		int ty;
		if (yy==y)ty=y+1;
		else ty=y;
		res.push_back(pii(x,ty));
		res.push_back(pii(x+1,ty));
		add(res);Solve(x,y);
		return;
	}
	if (cct.size()==4){
		res.push_back(pii(x,y));
		res.push_back(pii(x+1,y));
		res.push_back(pii(x+1,y+1));
		add(res);
		Solve(x,y);
		return;
	}
	if (cct.size()==2){

		if (cct[0].first!=cct[1].first&&cct[0].second!=cct[1].second){
			res.push_back(cct[0]);
			res.push_back(cct[1]);
			res.push_back(pii(cct[0].first,cct[1].second));
		}
		else {
			res.push_back(cct[0]);
			if (cct[0].first==cct[1].first){
				int tx;
				if (cct[0].first==x)tx=x+1;
				else tx=x;
				res.push_back(pii(tx,y));
				res.push_back(pii(tx,y+1));
			}
			else{
				int ty;
				if (cct[0].second==y)ty=y+1;
				else ty=y;
				res.push_back(pii(x,ty));
				res.push_back(pii(x+1,ty));
			}
		}add(res);
		Solve(x,y);
		return;
	}
}

int main(){
	ios::sync_with_stdio(0);
	int t;cin>>t;
	while (t--){
		cin>>n>>m;cnt=0;
		for (int i=1;i<=n;++i)
		for (int j=1;j<=m;++j)
		cin>>a[i][j];
		if (n&1){
			for (int j=1;j<m;j+=2)Solve(n-1,j);
		}
		if (m&1){
			for (int i=1;i<n;i+=2)Solve(i,m-1);
		}
		if ((n&1)&&(m&1))Solve(n-1,m-1);

		cout<<cnt<<endl;
		for (int i=1;i<=cnt;++i){
			for (int j=1;j<=6;++j)
			cout<<po[i][j]<<" ";
			cout<<endl;
		}
	}
}


#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef pair<int,int> pii;
char a[110][110];
int n,m;
int po[31000][10];
int cnt=0;

void change(int i,int j){
	if (a[i][j]=='1')a[i][j]='0';
	else a[i][j]='1';
}
void add(vector<pii> cct){
	++cnt;
	for (int i=0;i<cct.size();++i){
		int ii=i+1;
		po[cnt][ii*2-1]=cct[i].first;
		po[cnt][ii*2]=cct[i].second;
		change(cct[i].first,cct[i].second);
	}return;
}

void Solve(int x,int y){
	vector<pii> cct;
	if (a[x][y]=='1')cct.push_back(pii(x,y));
	if (a[x+1][y]=='1')cct.push_back(pii(x+1,y));
	if (a[x+1][y+1]=='1')cct.push_back(pii(x+1,y+1));
	if (a[x][y+1]=='1')cct.push_back(pii(x,y+1));
	if (cct.size()==0)return;
	if (cct.size()==3){
		add(cct);
		return;
	}
	vector<pii> res;
	if (cct.size()==1){
		cout<<x<<" "<<y<<endl;
		int xx = cct[0].first;
		int yy = cct[0].second;
		res.push_back(cct[0]);
		int ty;
		if (yy==y)ty=y+1;
		else ty=y;
		res.push_back(pii(x,ty));
		res.push_back(pii(x+1,ty));
		add(res);Solve(x,y);
		return;
	}
	if (cct.size()==4){
		res.push_back(pii(x,y));
		res.push_back(pii(x+1,y));
		res.push_back(pii(x+1,y+1));
		add(res);
		Solve(x,y);
		return;
	}
	if (cct.size()==2){

		if (cct[0].first!=cct[1].first&&cct[0].second!=cct[1].second){
			res.push_back(cct[0]);
			res.push_back(cct[1]);
			res.push_back(pii(cct[0].first,cct[1].second));
		}
		else {
			res.push_back(cct[0]);
			if (cct[0].first==cct[1].first){
				int tx;
				if (cct[0].first==x)tx=x+1;
				else tx=x;
				res.push_back(pii(tx,y));
				res.push_back(pii(tx,y+1));
			}
			else{
				int ty;
				if (cct[0].second==y)ty=y+1;
				else ty=y;
				res.push_back(pii(x,ty));
				res.push_back(pii(x+1,ty));
			}
		}add(res);
		Solve(x,y);
		return;
	}
}

int main(){
	ios::sync_with_stdio(0);
	int t;cin>>t;
	while (t--){
		cin>>n>>m;cnt=0;
		for (int i=1;i<=n;++i)
		for (int j=1;j<=m;++j)
		cin>>a[i][j];
		for (int i=1;i<n;i+=2){
			for (int j=1;j<m;j+=2){
				cout<<i<<" "<<j<<endl;
				Solve(i,j);
			}
		}if (n&1){
			for (int j=1;j<m;j+=2)Solve(n-1,j);
		}
		if (m&1){
			for (int i=1;i<n;i+=2)Solve(i,m-1);
		}
		if ((n&1)&&(m&1))Solve(n-1,m-1);

		cout<<cnt<<endl;
		for (int i=1;i<=cnt;++i){
			for (int j=1;j<=6;++j)
			cout<<po[i][j]<<" ";
			cout<<endl;
		}
	}
}


#include <iostream>
#include <vector>
#include <string>

using namespace std;

int t, n, m, cnt, k;
string a[101];
vector<pair<int, int>> v;

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> t;

	while (t--) {
		k = 0;
		v.clear();

		cin >> n >> m;
		for (int i = 1; i <= n; i++) cin >> a[i];

		for (int i = 1; i <= n; i += 2) {
			for (int j = 0; j < m; j += 2) {
				cnt = 0;
				if (i == n) i--;
				if (j == m - 1) j--;

				for (int x = 0; x < 2; x++) {
					for (int y = 0; y < 2; y++) {
						if (a[i + x][j + y] == '1') cnt++;
					}
				}

				int tmp1 = 2;
				int tmp2 = 1;
				switch (cnt)
				{
				case 4:
					k++;
					a[i][j] = a[i][j + 1] = a[i + 1][j] = '0';
					v.emplace_back(i, j + 1);
					v.emplace_back(i, j + 2);
					v.emplace_back(i + 1, j + 1);
				case 1:
					k++;
					for (int x = 0; x < 2; x++) {
						for (int y = 0; y < 2; y++) {
							if (a[i + x][j + y] == '1') {
								a[i + x][j + y] = '0';
								v.emplace_back(i + x, j + y + 1);
							}
							else if (tmp1) {
								tmp1--;
								a[i + x][j + y] = '1';
								v.emplace_back(i + x, j + y + 1);
							}
						}
					}
				case 2:
					k++;
					for (int x = 0; x < 2; x++) {
						for (int y = 0; y < 2; y++) {
							if (a[i + x][j + y] == '0') {
								a[i + x][j + y] = '1';
								v.emplace_back(i + x, j + y + 1);
							}
							else if (tmp2) {
								tmp2--;
								a[i + x][j + y] = '0';
								v.emplace_back(i + x, j + y + 1);
							}
						}
					}
				case 3:
					k++;
					for (int x = 0; x < 2; x++) {
						for (int y = 0; y < 2; y++) {
							if (a[i + x][j + y] == '1') {
								a[i + x][j + y] = '0';
								v.emplace_back(i + x, j + y + 1);
							}
						}
					}
					break;
				default:
					break;
				}

			}
		}

		cout << k << '\n';
		for (int i = 0; i < 3 * k; i++) {
			cout << v[i].first << ' ' << v[i].second << ' ';
			if (i % 3 == 2) cout << '\n';
		}
	}
}


#include <iostream>
#include <vector>
#include <string>

using namespace std;

int t, n, m, cnt, k;
string a[101];
vector<pair<int, int>> v;

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> t;

	while (t--) {
		k = 0;
		v.clear();

		cin >> n >> m;
		for (int i = 1; i <= n; i++) cin >> a[i];

		for (int i = 1; i <= n; i += 2) {
			for (int j = 0; j < m; j += 2) {
				cnt = 0;
				if (i == n) i--;
				if (j == m - 1) j--;

				for (int x = 0; x < 2; x++) {
					for (int y = 0; y < 2; y++) {
						if (a[i + x][j + y] == '1') cnt++;
					}
				}

				int tmp1 = 2;
				int tmp2 = 1;
				switch (cnt)
				{
				case 4:
					k++;
					a[i][j] = a[i][j + 1] = a[i + 1][j] = 0;
					v.emplace_back(i, j + 1);
					v.emplace_back(i, j + 2);
					v.emplace_back(i + 1, j + 1);
				case 1:
					k++;
					for (int x = 0; x < 2; x++) {
						for (int y = 0; y < 2; y++) {
							if (a[i + x][j + y] == '1') {
								a[i + x][j + y] = '0';
								v.emplace_back(i + x, j + y + 1);
							}
							else if (tmp1) {
								tmp1--;
								a[i + x][j + y] = '1';
								v.emplace_back(i + x, j + y + 1);
							}
						}
					}
				case 2:
					k++;
					for (int x = 0; x < 2; x++) {
						for (int y = 0; y < 2; y++) {
							if (a[i + x][j + y] == '0') {
								a[i + x][j + y] = '1';
								v.emplace_back(i + x, j + y + 1);
							}
							else if (tmp2) {
								tmp2--;
								a[i + x][j + y] = '0';
								v.emplace_back(i + x, j + y + 1);
							}
						}
					}
				case 3:
					k++;
					for (int x = 0; x < 2; x++) {
						for (int y = 0; y < 2; y++) {
							if (a[i + x][j + y] == '1') {
								a[i + x][j + y] = '0';
								v.emplace_back(i + x, j + y + 1);
							}
						}
					}
					break;
				default:
					break;
				}

			}
		}

		cout << k << '\n';
		for (int i = 0; i < 3 * k; i++) {
			cout << v[i].first << ' ' << v[i].second << ' ';
			if (i % 3 == 2) cout << '\n';
		}
	}
}


//Я ломаю монитор, быть собой, да сколько можно?
//За мной ангельский саппорт, это братья стали мод
//Худи, худи наизнанку, антистиль, мой антипод
//А что не так с этим мобом? Я включаю beast мод
//Она новая валюта, но ты полный банкрот
#include <iostream>
#include <locale>
#include <string>
#include <iomanip>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <vector>
#include <stdlib.h>
#include <clocale>
#include <algorithm>
#include <iterator>
#include <list>
#include <set>
#include <cstddef>
#include <cstdio>
#include <stdio.h>
#include <map>
#include <cmath>
#include <functional>
#include <stack>
#include <type_traits>
#include <queue>
#include <memory>
#include <memory.h>
#include <time.h>
#include <ctime>
#pragma warning(disable:4996)
#pragma optimize("", on)
using namespace std;
#define elizeyyy__ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define pf push_front
#define irt insert
#define ll long long
#define int long long
#define ull unsigned long long
#define str string
#define vi vector<int>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define msi multiset<int>
#define msll multiset<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define left false
#define right true
vector <tuple<int, int, int, int, int, int>> ans;
int matrix[113][113] = {};
void proc(int i, int j)
{
	if (matrix[i][j] + matrix[i+1][j] == 0)
		return;
	if (matrix[i][j] + matrix[i+1][j] == 2)
	{
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i + 2, j + 1);
		matrix[i][j] ^= 1;
		matrix[i][j+1] ^= 1;
		matrix[i+1][j] ^= 1;
		return;
	}
	if (matrix[i][j] == 1)
	{
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i + 2, j + 2);
		matrix[i][j] ^= 1;
		matrix[i][j+1] ^= 1;
		matrix[i + 1][j+1] ^= 1;
		return;
	}
	else
	{
		ans.emplace_back(i + 2, j + 1, i + 2, j + 2, i + 1, j + 2);
		matrix[i+1][j] ^= 1;
		matrix[i][j + 1] ^= 1;
		matrix[i + 1][j+1] ^= 1;
		return;
	}
}
void finproc(int i, int j)
{
	if (matrix[i][j] + matrix[i+1][j] == 0)
		return;
	if (matrix[i][j] + matrix[i+1][j] == 2)
	{
		ans.emplace_back(i + 1, j + 1, i+1, j, i+2, j + 1);
		matrix[i][j] ^= 1;
		matrix[i][j - 1] ^= 1;
		matrix[i + 1][j] ^= 1;
		ans.emplace_back(i+1, j + 1, i+2, j + 1, i + 2, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i + 1][j -1] ^= 1;
		return;
	}
	if (matrix[i][j] == 1)
	{
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i+1, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i][j - 1] ^= 1;
		ans.emplace_back(i + 1, j + 1, i+1 , j, i+2, j);
		matrix[i][j] ^= 1;
		matrix[i][j - 1] ^= 1;
		matrix[i + 1][j - 1] ^= 1;
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i + 2, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i + 1][j - 1] ^= 1;
		return;
	}
	else
	{
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i +2, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i + 1][j - 1] ^= 1;
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i+1, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i][j - 1] ^= 1;
		ans.emplace_back(i + 1, j, i + 2, j, i+2, j + 1);
		matrix[i][j-1] ^= 1;
		matrix[i+1][j - 1] ^= 1;
		matrix[i + 1][j] ^= 1;
		return;
	}
}
void solve()
{
	memset(matrix, 0, sizeof(matrix));
	ans.clear();
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	{
		str h;
		cin >> h;
		for (int j = 0; j < h.size(); ++j)
		{
			if (h[j] == '1')
				matrix[i][j] = 1;
		}
	}
	for (int i = 0; i < n - 1; i+=2)
	{
		for (int j = 0; j < m-1; j++)
		{
			proc(i, j);
		}
		finproc(i, m-1);
	}
	if (n % 2 == 1)
	{
		for (int j = 0; j < m - 1; j++)
		{
			proc(n-2, j);
		}
		finproc(n-2, m - 1);
	}
	cout << ans.size() << "\n";
	for (int i = 0; i < ans.size(); ++i)
	{
			cout << get<0>(ans[i]) << " " << get<1>(ans[i]) << " " << get<2>(ans[i]) << " " << get<3>(ans[i]) << " " << get<4>(ans[i]) << " " << get<5>(ans[i]);
			cout << "\n";
	}
}
signed main()
{
	elizeyyy__;
	int t;
	cin >> t;
	while (t--)
		solve();
}


//Я ломаю монитор, быть собой, да сколько можно?
//За мной ангельский саппорт, это братья стали мод
//Худи, худи наизнанку, антистиль, мой антипод
//А что не так с этим мобом? Я включаю beast мод
//Она новая валюта, но ты полный банкрот
#include <iostream>
#include <locale>
#include <string>
#include <iomanip>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <vector>
#include <stdlib.h>
#include <clocale>
#include <algorithm>
#include <iterator>
#include <list>
#include <set>
#include <cstddef>
#include <cstdio>
#include <stdio.h>
#include <map>
#include <cmath>
#include <functional>
#include <stack>
#include <type_traits>
#include <queue>
#include <memory>
#include <memory.h>
#include <time.h>
#include <ctime>
#pragma warning(disable:4996)
#pragma optimize("", on)
using namespace std;
#define elizeyyy__ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define pf push_front
#define irt insert
#define ll long long
#define int long long
#define ull unsigned long long
#define str string
#define vi vector<int>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define msi multiset<int>
#define msll multiset<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define left false
#define right true
vector <tuple<int, int, int, int, int, int>> ans;
int matrix[113][113] = {};
void proc(int i, int j)
{
	if (matrix[i][j] + matrix[i+1][j] == 0)
		return;
	if (matrix[i][j] + matrix[i+1][j] == 2)
	{
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i + 2, j + 1);
		matrix[i][j] ^= 1;
		matrix[i][j+1] ^= 1;
		matrix[i+1][j] ^= 1;
		return;
	}
	if (matrix[i][j] == 1)
	{
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i + 2, j + 2);
		matrix[i][j] ^= 1;
		matrix[i][j+1] ^= 1;
		matrix[i + 1][j+1] ^= 1;
		return;
	}
	else
	{
		ans.emplace_back(i + 2, j + 1, i + 2, j + 2, i + 1, j + 2);
		matrix[i+1][j] ^= 1;
		matrix[i][j + 1] ^= 1;
		matrix[i + 1][j+1] ^= 1;
		return;
	}
}
void finproc(int i, int j)
{
	if (matrix[i][j] + matrix[i+1][j] == 0)
		return;
	if (matrix[i][j] + matrix[i+1][j] == 2)
	{
		ans.emplace_back(i + 1, j + 1, i+1, j, i+2, j);
		matrix[i][j] ^= 1;
		matrix[i][j - 1] ^= 1;
		matrix[i + 1][j-1] ^= 1;
		ans.emplace_back(i+2, j + 1, i+2, j, i + 1, j);
		matrix[i+1][j] ^= 1;
		matrix[i+1][j-1] ^= 1;
		matrix[i][j -1] ^= 1;
		return;
	}
	if (matrix[i][j] == 1)
	{
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i+1, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i][j - 1] ^= 1;
		ans.emplace_back(i + 1, j + 1, i+1 , j, i+2, j);
		matrix[i][j] ^= 1;
		matrix[i][j - 1] ^= 1;
		matrix[i + 1][j - 1] ^= 1;
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i + 2, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i + 1][j - 1] ^= 1;
		return;
	}
	else
	{
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i +2, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i + 1][j - 1] ^= 1;
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i+1, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i][j - 1] ^= 1;
		ans.emplace_back(i + 1, j, i + 2, j, i+2, j + 1);
		matrix[i][j-1] ^= 1;
		matrix[i+1][j - 1] ^= 1;
		matrix[i + 1][j] ^= 1;
		return;
	}
}
void solve()
{
	memset(matrix, 0, sizeof(matrix));
	ans.clear();
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	{
		str h;
		cin >> h;
		for (int j = 0; j < h.size(); ++j)
		{
			if (h[j] == '1')
				matrix[i][j] = 1;
		}
	}
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < m-1; j++)
		{
			proc(i, j);
		}
		finproc(i, m-1);
	}
	if (n % 2 == 1)
	{
		for (int j = 0; j < m - 1; j++)
		{
			proc(n-2, j);
		}
		finproc(n-2, m - 1);
	}
	cout << ans.size() << "\n";
	for (int i = 0; i < ans.size(); ++i)
	{
			cout << get<0>(ans[i]) << " " << get<1>(ans[i]) << " " << get<2>(ans[i]) << " " << get<3>(ans[i]) << " " << get<4>(ans[i]) << " " << get<5>(ans[i]);
			cout << "\n";
	}
}
signed main()
{
	elizeyyy__;
	int t;
	cin >> t;
	while (t--)
		solve();
}


//Я ломаю монитор, быть собой, да сколько можно?
//За мной ангельский саппорт, это братья стали мод
//Худи, худи наизнанку, антистиль, мой антипод
//А что не так с этим мобом? Я включаю beast мод
//Она новая валюта, но ты полный банкрот
#include <iostream>
#include <locale>
#include <string>
#include <iomanip>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <vector>
#include <stdlib.h>
#include <clocale>
#include <algorithm>
#include <iterator>
#include <list>
#include <set>
#include <cstddef>
#include <cstdio>
#include <stdio.h>
#include <map>
#include <cmath>
#include <functional>
#include <stack>
#include <type_traits>
#include <queue>
#include <memory>
#include <memory.h>
#include <time.h>
#include <ctime>
#pragma warning(disable:4996)
#pragma optimize("", on)
using namespace std;
#define elizeyyy__ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define pf push_front
#define irt insert
#define ll long long
#define int long long
#define ull unsigned long long
#define str string
#define vi vector<int>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define msi multiset<int>
#define msll multiset<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define left false
#define right true
vector <tuple<int, int, int, int, int, int>> ans;
int matrix[113][113] = {};
void proc(int i, int j)
{
	if (matrix[i][j] + matrix[i][j + 1] == 0)
		return;
	if (matrix[i][j] + matrix[i][j + 1] == 2)
	{
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i + 2, j + 1);
		matrix[i][j] ^= 1;
		matrix[i][j+1] ^= 1;
		matrix[i+1][j] ^= 1;
		return;
	}
	if (matrix[i][j] == 1)
	{
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i + 2, j + 2);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i + 1][j+1] ^= 1;
		return;
	}
	else
	{
		ans.emplace_back(i + 1, j + 2, i + 2, j + 1, i + 2, j + 2);
		matrix[i][j+1] ^= 1;
		matrix[i + 1][j] ^= 1;
		matrix[i + 1][j + 1] ^= 1;
		return;
	}
}
void finproc(int i, int j)
{
	if (matrix[i][j] + matrix[i][j + 1] == 0)
		return;
	if (matrix[i][j] + matrix[i][j + 1] == 2)
	{
		ans.emplace_back(i + 1, j + 1, i, j + 1, i, j + 2);
		ans.emplace_back(i, j + 1, i, j + 2, i + 1, j + 2);
		return;
	}
	if (matrix[i][j] == 1)
	{
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i, j + 1);
		ans.emplace_back(i + 1, j + 1, i , j + 1, i, j + 2);
		ans.emplace_back(i, j + 2, i + 1, j + 2, i+1, j + 1);
		return;
	}
	else
	{
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i, j + 2);
		ans.emplace_back(i + 1, j + 2, i, j + 1, i, j + 2);
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i, j + 1);
		return;
	}
}
void solve()
{
	memset(matrix, 0, sizeof(matrix));
	ans.clear();
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	{
		str h;
		cin >> h;
		for (int j = 0; j < h.size(); ++j)
		{
			if (h[j] == '1')
				matrix[i][j] = 1;
		}
	}
	for (int i = 0; i < n - 1; ++i)
	{
		for (int j = 0; j < m-1; j += 2)
		{
			proc(j, i);
		}
		finproc(m - 1, i);
	}
	finproc(m - 1, n - 1);
	cout << ans.size() << "\n";
	for (int i = 0; i < ans.size(); ++i)
	{
			cout << get<0>(ans[i]) << " " << get<1>(ans[i]) << " " << get<2>(ans[i]) << " " << get<3>(ans[i]) << " " << get<4>(ans[i]) << " " << get<5>(ans[i]);
			cout << "\n";
	}
}
signed main()
{
	elizeyyy__;
	int t;
	cin >> t;
	while (t--)
		solve();
}


//Я ломаю монитор, быть собой, да сколько можно?
//За мной ангельский саппорт, это братья стали мод
//Худи, худи наизнанку, антистиль, мой антипод
//А что не так с этим мобом? Я включаю beast мод
//Она новая валюта, но ты полный банкрот
#include <iostream>
#include <locale>
#include <string>
#include <iomanip>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <vector>
#include <stdlib.h>
#include <clocale>
#include <algorithm>
#include <iterator>
#include <list>
#include <set>
#include <cstddef>
#include <cstdio>
#include <stdio.h>
#include <map>
#include <cmath>
#include <functional>
#include <stack>
#include <type_traits>
#include <queue>
#include <memory>
#include <memory.h>
#include <time.h>
#include <ctime>
#pragma warning(disable:4996)
#pragma optimize("", on)
using namespace std;
#define elizeyyy__ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define pf push_front
#define irt insert
#define ll long long
#define int long long
#define ull unsigned long long
#define str string
#define vi vector<int>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define msi multiset<int>
#define msll multiset<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define left false
#define right true
vector <tuple<int, int, int, int, int, int>> ans;
int matrix[113][113] = {};
void proc(int i, int j)
{
	if (matrix[i][j] + matrix[i][j + 1] == 0)
		return;
	if (matrix[i][j] + matrix[i][j + 1] == 2)
	{
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i + 2, j + 1);
		matrix[i][j] ^= 1;
		matrix[i][j+1] ^= 1;
		matrix[i+1][j] ^= 1;
		return;
	}
	if (matrix[i][j] == 1)
	{
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i + 2, j + 2);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i + 1][j+1] ^= 1;
		return;
	}
	else
	{
		ans.emplace_back(i + 1, j + 2, i + 2, j + 1, i + 2, j + 2);
		matrix[i][j+1] ^= 1;
		matrix[i + 1][j] ^= 1;
		matrix[i + 1][j + 1] ^= 1;
		return;
	}
}
void finproc(int i, int j)
{
	if (matrix[i][j] + matrix[i][j + 1] == 0)
		return;
	if (matrix[i][j] + matrix[i][j + 1] == 2)
	{
		ans.emplace_back(i + 1, j + 1, i, j + 1, i, j + 2);
		ans.emplace_back(i, j + 1, i, j + 2, i + 1, j + 2);
		return;
	}
	if (matrix[i][j] == 1)
	{
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i, j + 1);
		ans.emplace_back(i + 1, j + 1, i , j + 1, i, j + 2);
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i, j + 2);
		return;
	}
	else
	{
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i, j + 2);
		ans.emplace_back(i + 1, j + 2, i, j + 1, i, j + 2);
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i, j + 1);
		return;
	}
}
void solve()
{
	memset(matrix, 0, sizeof(matrix));
	ans.clear();
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	{
		str h;
		cin >> h;
		for (int j = 0; j < h.size(); ++j)
		{
			if (h[j] == '1')
				matrix[i][j] = 1;
		}
	}
	for (int i = 0; i < n - 1; ++i)
	{
		for (int j = 0; j < m-1; j += 2)
		{
			proc(j, i);
		}
		finproc(m - 1, i);
	}
	finproc(m - 1, n - 1);
	cout << ans.size() << "\n";
	for (int i = 0; i < ans.size(); ++i)
	{
			cout << get<0>(ans[i]) << " " << get<1>(ans[i]) << " " << get<2>(ans[i]) << " " << get<3>(ans[i]) << " " << get<4>(ans[i]) << " " << get<5>(ans[i]);
			cout << "\n";
	}
}
signed main()
{
	elizeyyy__;
	int t;
	cin >> t;
	while (t--)
		solve();
}


//Я ломаю монитор, быть собой, да сколько можно?
//За мной ангельский саппорт, это братья стали мод
//Худи, худи наизнанку, антистиль, мой антипод
//А что не так с этим мобом? Я включаю beast мод
//Она новая валюта, но ты полный банкрот
#include <iostream>
#include <locale>
#include <string>
#include <iomanip>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <vector>
#include <stdlib.h>
#include <clocale>
#include <algorithm>
#include <iterator>
#include <list>
#include <set>
#include <cstddef>
#include <cstdio>
#include <stdio.h>
#include <map>
#include <cmath>
#include <functional>
#include <stack>
#include <type_traits>
#include <queue>
#include <memory>
#include <memory.h>
#include <time.h>
#include <ctime>
#pragma warning(disable:4996)
#pragma optimize("", on)
using namespace std;
#define elizeyyy__ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define pf push_front
#define irt insert
#define ll long long
#define int long long
#define ull unsigned long long
#define str string
#define vi vector<int>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define msi multiset<int>
#define msll multiset<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define left false
#define right true
vector <tuple<int, int, int, int, int, int>> ans;
int matrix[113][113] = {};
void proc(int i, int j)
{
	if (matrix[i][j] + matrix[i+1][j] == 0)
		return;
	if (matrix[i][j] + matrix[i+1][j] == 2)
	{
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i + 2, j + 1);
		matrix[i][j] ^= 1;
		matrix[i][j+1] ^= 1;
		matrix[i+1][j] ^= 1;
		return;
	}
	if (matrix[i][j] == 1)
	{
		ans.emplace_back(i + 1, j + 1, i + 1, j + 2, i + 2, j + 2);
		matrix[i][j] ^= 1;
		matrix[i][j+1] ^= 1;
		matrix[i + 1][j+1] ^= 1;
		return;
	}
	else
	{
		ans.emplace_back(i + 2, j + 1, i + 2, j + 2, i + 1, j + 2);
		matrix[i+1][j] ^= 1;
		matrix[i][j + 1] ^= 1;
		matrix[i + 1][j+1] ^= 1;
		return;
	}
}
void finproc(int i, int j)
{
	if (matrix[i][j] + matrix[i+1][j] == 0)
		return;
	if (matrix[i][j] + matrix[i+1][j] == 2)
	{
		ans.emplace_back(i + 1, j + 1, i+1, j, i+2, j);
		matrix[i][j] ^= 1;
		matrix[i][j - 1] ^= 1;
		matrix[i + 1][j-1] ^= 1;
		ans.emplace_back(i+1, j + 1, i+2, j, i + 1, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i][j -1] ^= 1;
		return;
	}
	if (matrix[i][j] == 1)
	{
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i+1, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i][j - 1] ^= 1;
		ans.emplace_back(i + 1, j + 1, i+1 , j, i+2, j);
		matrix[i][j] ^= 1;
		matrix[i][j - 1] ^= 1;
		matrix[i + 1][j - 1] ^= 1;
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i + 2, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i + 1][j - 1] ^= 1;
		return;
	}
	else
	{
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i +2, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i + 1][j - 1] ^= 1;
		ans.emplace_back(i + 1, j + 1, i + 2, j + 1, i+1, j);
		matrix[i][j] ^= 1;
		matrix[i+1][j] ^= 1;
		matrix[i][j - 1] ^= 1;
		ans.emplace_back(i + 1, j, i + 2, j, i+2, j + 1);
		matrix[i][j-1] ^= 1;
		matrix[i+1][j - 1] ^= 1;
		matrix[i + 1][j] ^= 1;
		return;
	}
}
void solve()
{
	memset(matrix, 0, sizeof(matrix));
	ans.clear();
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	{
		str h;
		cin >> h;
		for (int j = 0; j < h.size(); ++j)
		{
			if (h[j] == '1')
				matrix[i][j] = 1;
		}
	}
	for (int i = 0; i < n - 1; i+=2)
	{
		for (int j = 0; j < m-1; j++)
		{
			proc(i, j);
		}
		finproc(i, m-1);
	}
	if (n % 2 == 1)
	{
		for (int j = 0; j < m - 1; j++)
		{
			proc(n-2, j);
		}
		finproc(n-2, m - 1);
	}
	cout << ans.size() << "\n";
	for (int i = 0; i < ans.size(); ++i)
	{
			cout << get<0>(ans[i]) << " " << get<1>(ans[i]) << " " << get<2>(ans[i]) << " " << get<3>(ans[i]) << " " << get<4>(ans[i]) << " " << get<5>(ans[i]);
			cout << "\n";
	}
}
signed main()
{
	elizeyyy__;
	int t;
	cin >> t;
	while (t--)
		solve();
}


#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <cstring>
#include <cmath>
#include <map>
#include <string>
#include <numeric>
#include <functional>
#include <fstream>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INF2 0x3f3f3f3f3f3f3f3fll
#define ERR 1e-10
#define endl "\n"
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

using namespace std;
//#define DEBUG
#ifndef DEBUG
#define in cin
#define out cout
#endif

void print(int x, int y, int mode)
{
	x++; y++;
	if (mode == 1)
	{
		out << x << " " << y << " " << x + 1 << " " << y << " " << x << " " << y + 1 << endl;
	}
	else if (mode == 2)
	{
		out << x << " " << y << " " << x+1 << " " << y+1 << " " << x << " " << y + 1 << endl;
	}
	else if (mode == 3)
	{
		out << x << " " << y << " " << x + 1 << " " << y << " " << x+1 << " " << y + 1 << endl;
	}
	else
	{
		out << x+1 << " " << y+1 << " " << x + 1 << " " << y << " " << x << " " << y + 1 << endl;
	}
}

int main()
{
#ifdef DEBUG
	ifstream in("input.txt");
	ofstream out("output.txt");
#endif

	in.tie(NULL);
	out.tie(NULL);
	ios_base::sync_with_stdio(false);
	int testcase = 1;
	in >> testcase;
	for (int tc = 1; tc <= testcase; tc++)
	{
		int n, m;
		int map[100][100];
		char k;
		int count = 0;
		in >> n >> m;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				in >> k;
				map[i][j] = k - '0';
				if (map[i][j]) count += 1;
			}
		}
		out << count * 3 << endl;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				if (map[i][j]==1)
				{
					if (i < n - 1 && j < m - 1)
					{
						print(i, j, 1);
						print(i, j, 2);
						print(i, j, 3);
					}
					else if (i == n - 1 && j < m-1)
					{
						print(i - 1, j, 1);
						print(i - 1, j, 3);
						print(i - 1, j, 4);
					}
					else if (j == m - 1 && i < n - 1)
					{
						print(i, j - 1, 2);
						print(i, j - 1, 1);
						print(i, j - 1, 4);
					}
					else
					{
						print(i - 1, j - 1, 2);
						print(i - 1, j - 1, 3);
						print(i - 1, j - 1, 4);
					}
				}
			}
		}

	}

	return 0;
}


#include<iostream>

using namespace std;

const int MAXN = 205;

int t, n, m;
int a[MAXN][MAXN];
int ans;

struct point
{
	int x, y;

	point(){}

	point(int x_, int y_)
	{
		x = x_;
		y = y_;
	}
};

point blk[5], wt[5];
int blk1, wt1;

struct ansOption
{
	point p1, p2, p3;

	ansOption(){}

	ansOption(point p1_, point p2_, point p3_)
	{
		p1.x = p1_.x;
		p1.y = p1_.y;
		p2.x = p2_.x;
		p2.y = p2_.y;
		p3.x = p3_.x;
		p3.y = p3_.y;
	}

	void print()
	{
		printf("%d %d %d %d %d %d\n", p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
	}
}ansop[MAXN * MAXN];

void updateAns(point p1, point p2, point p3)
{
	ans++;
	ansop[ans].p1 = p1;
	ansop[ans].p2 = p2;
	ansop[ans].p3 = p3;
}

void updateBoW(point p1)
{
	if (a[p1.x][p1.y])
	{
		blk1++;
		blk[blk1] = p1;
	}
	else
	{
		wt1++;
		wt[wt1] = p1;
	}
}

void black1(point b1, point w1, point w2, point w3)
{
	updateAns(b1, w1, w2);
	updateAns(b1, w1, w3);
	updateAns(b1, w2, w3);
}

void black2(point b1, point b2, point w1, point w2)
{
	updateAns(b1, w1, w2);
	updateAns(b2, w1, w2);
}

void black3(point b1, point b2, point b3, point w1)
{
	updateAns(b1, b2, b3);
}

void black4(point b1, point b2, point b3, point b4)
{
	updateAns(b2, b3, b4);
	updateAns(b1, b2, b3);
	updateAns(b1, b2, b4);
	updateAns(b1, b3, b4);
}

int main()
{
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= n; i++)
		{
			char s[105];
			scanf("%s", s + 1);
			for (int j = 1; j <= m; j++)
			a[i][j] = s[j] - '0';
		}

		ans = 0;

		for (int i = 1; i <= n - 2; i++)
		{
			for (int j = 1; j <= m - 1; j++)
			{
				if (a[i][j] && a[i][j + 1])
				{
					point p1(i, j), p2(i, j + 1), p3(i + 1, j);
					updateAns(p1, p2, p3);
					a[i + 1][j] ^= 1; a[i][j] ^= 1; a[i][j + 1] ^= 1;
				}
				else
				if (!a[i][j] && a[i][j + 1])
				{
					point p1(i, j + 1), p2(i + 1, j), p3(i + 1, j + 1);
					updateAns(p1, p2, p3);
					a[i + 1][j] ^= 1; a[i + 1][j + 1] ^= 1;	a[i][j + 1] ^= 1;
				}
				else
				if (a[i][j] && !a[i][j + 1])
				{
					point p1(i, j), p2(i + 1, j), p3(i + 1, j + 1);
					updateAns(p1, p2, p3);
					a[i + 1][j] ^= 1; a[i + 1][j + 1] ^= 1; a[i][j] ^= 1;
				}
			}
		}

		for (int j = 1; j <= m; j += 2)
		{
			if (j == m) j = m - 1;
			blk1 = 0; wt1 = 0;
			point p1(n - 1, j), p2(n - 1, j + 1), p3(n, j), p4(n, j + 1);
			updateBoW(p1);
			updateBoW(p2);
			updateBoW(p3);
			updateBoW(p4);
			if (blk1 == 1)
				black1(blk[1], wt[1], wt[2], wt[3]);
			else
			if (blk1 == 2)
				black2(blk[1], blk[2], wt[1], wt[2]);
			else
			if (blk1 == 3)
				black3(blk[1], blk[2], blk[3], wt[1]);
			else
			if (blk1 == 4)
				black4(blk[1], blk[2], blk[3], blk[4]);
			a[n - 1][j] = a[n - 1][j + 1] = a[n][j] = a[n][j + 1] = 0;
		}

		printf("%d\n", ans);
		for (int i = 1; i <= ans; i++)
		ansop[i].print();
	}
	return 0;
}


//#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<iomanip>
#include<sstream>
#include<fstream>
#include <math.h>
#include<string>
#include <vector>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<stack>
#include<queue>
#include<deque>
#include <algorithm>
#include<functional>
#include<bitset>
#include<time.h>
#include<random>

using namespace std;

#define ll long long
#define pii pair<int, int>
#define psi pair<string, int>
#define pis pair<int, string>
#define pss pair<string, string>

vector<vector<int>> a;
vector<vector<int>> ans;

void Swap(int i1, int j1, int i2, int j2, int i3, int j3)
{
	a[i1][j1] = (a[i1][j1] + 1) % 2;
	a[i2][j2] = (a[i2][j2] + 1) % 2;
	a[i3][j3] = (a[i3][j3] + 1) % 2;
	ans.push_back(vector<int> {i1, j1, i2, j2, i3, j3});
}

void solve()
{
	int n, m;
	cin >> n >> m;
	string s;
	a.assign(n, vector<int>(m,0));
	ans.clear();
	for (int i = 0; i < n; i++)
	{
		cin >> s;
		for (int j = 0; j < s.size(); j++)
			if (s[j] == '1')
				a[i][j] = 1;
	}
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < m - 1; j++)
			if (a[i][j] == 1)
				Swap(i, j, i + 1, j, i, j + 1);
		if (a[i][m - 1] == 1)
			Swap(i, m - 1, i + 1, m - 1, i + 1, m - 2);
	}
	int i = n - 1;
	for (int j = 0; j < m - 2; j++)
		if (a[i][j] == 1)
		{
			Swap(i, j, i, j + 1, i - 1, j + 1);
			Swap(i - 1, j + 1, i, j + 1, i, j + 2);
		}
	int j = m - 2;
	if (a[i][j] == 1)
		Swap(i, j, i, j + 1, i - 1, j + 1);
	i = n - 1;
	j = m - 1;
	if (a[i][j] == 1 || a[i - 1][j] == 1)
	{
		if (a[i][j] + a[i - 1][j] == 1)
		{
			if (a[i][j] == 1)
				Swap(i, j, i, j - 1, i - 1, j - 1);
			else
				Swap(i - 1, j, i, j - 1, i - 1, j - 1);
			Swap(i, j, i - 1, j, i - 1, j - 1);
			Swap(i, j, i - 1, j, i, j - 1);
		}
		else
		{
			Swap(i, j, i, j - 1, i - 1, j - 1);
			Swap(i, j, i - 1, j, i - 1, j - 1);
		}
	}
	cout << ans.size() << endl;
	for (auto e : ans)
	{
		for (auto q : e)
			cout << q + 1 << " ";
		cout << endl;
	}
}

int main()
{
	//srand(time(0));

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	//ios_base::sync_with_stdio(0);
	//cin.tie(0);
	//cout.tie(0);

	//cout << fixed << setprecision(5);
	int t = 1;
	cin >> t;
	for (int i = 0; i < t; i++)
		solve();
	return 0;
}


//#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<iomanip>
#include<sstream>
#include<fstream>
#include <math.h>
#include<string>
#include <vector>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<stack>
#include<queue>
#include<deque>
#include <algorithm>
#include<functional>
#include<bitset>
#include<time.h>
#include<random>

using namespace std;

#define ll long long
#define pii pair<int, int>
#define psi pair<string, int>
#define pis pair<int, string>
#define pss pair<string, string>

vector<vector<int>> a;
vector<vector<int>> ans;

void Swap(int i1, int j1, int i2, int j2, int i3, int j3)
{
	a[i1][j1] = (a[i1][j1] + 1) % 2;
	a[i2][j2] = (a[i2][j2] + 1) % 2;
	a[i3][j3] = (a[i3][j3] + 1) % 2;
	ans.push_back(vector<int> {i1, j1, i2, j2, i3, j3});
}

void solve()
{
	int n, m;
	cin >> n >> m;
	string s;
	a.assign(n, vector<int>(m,0));
	ans.clear();
	for (int i = 0; i < n; i++)
	{
		cin >> s;
		for (int j = 0; j < s.size(); j++)
			if (s[j] == '1')
				a[i][j] = 1;
	}
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < m - 1; j++)
			if (a[i][j] == 1)
				Swap(i, j, i + 1, j, i, j + 1);
		if (a[i][m - 1] == 1)
			Swap(i, m - 1, i + 1, m - 1, i + 1, m - 2);
	}
	int i = n - 1;
	for (int j = 0; j < m - 2; j++)
		if (a[i][j] == 1)
		{
			Swap(i, j, i, j + 1, i - 1, j + 1);
			Swap(i - 1, j + 1, i, j + 1, i, j + 2);
		}
	int j = m - 2;
	if (a[i][j] == 1)
		Swap(i, j, i, j + 1, i - 1, j + 1);
	i = n - 1;
	j = m - 1;
	if (a[i][j] == 1 || a[i - 1][j] == 1)
	{
		if (a[i][j] + a[i - 1][j] == 1)
		{
			if (a[i][j] == 1)
				Swap(i, j, i, j - 1, i - 1, j - 1);
			else
				Swap(i - 1, j, i, j - 1, i - 1, j - 1);
			Swap(i, j, i - 1, j, i - 1, j - 1);
			Swap(i, j, i - 1, j, i, j - 1);
		}
		else
		{
			Swap(i - 1, j, i, j - 1, i - 1, j - 1);
			Swap(i, j, i, j - 1, i - 1, j - 1);
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (a[i][j] == 1)
				cout << "kra";
		}
	}
	cout << ans.size() << endl;
	for (auto e : ans)
	{
		for (auto q : e)
			cout << q + 1 << " ";
		cout << endl;
	}
}

int main()
{
	//srand(time(0));

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	//ios_base::sync_with_stdio(0);
	//cin.tie(0);
	//cout.tie(0);

	//cout << fixed << setprecision(5);
	int t = 1;
	cin >> t;
	for (int i = 0; i < t; i++)
		solve();
	return 0;
}


//#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<iomanip>
#include<sstream>
#include<fstream>
#include <math.h>
#include<string>
#include <vector>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<stack>
#include<queue>
#include<deque>
#include <algorithm>
#include<functional>
#include<bitset>
#include<time.h>
#include<random>

using namespace std;

#define ll long long
#define pii pair<int, int>
#define psi pair<string, int>
#define pis pair<int, string>
#define pss pair<string, string>

vector<vector<int>> a;
vector<vector<int>> ans;

void Swap(int i1, int j1, int i2, int j2, int i3, int j3)
{
	a[i1][j1] = (a[i1][j1] + 1) % 2;
	a[i2][j2] = (a[i2][j2] + 1) % 2;
	a[i3][j3] = (a[i3][j3] + 1) % 2;
	ans.push_back(vector<int> {i1, j1, i2, j2, i3, j3});
}

void solve()
{
	int n, m;
	cin >> n >> m;
	string s;
	a.resize(n, vector<int>(m,0));
	for (int i = 0; i < n; i++)
	{
		cin >> s;
		for (int j = 0; j < s.size(); j++)
			if (s[j] == '1')
				a[i][j] = 1;
	}
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < m - 1; j++)
			if (a[i][j] == 1)
				Swap(i, j, i + 1, j, i, j + 1);
		if (a[i][m - 1] == 1)
			Swap(i, m - 1, i + 1, m - 1, i + 1, m - 2);
	}
	int i = n - 1;
	for (int j = 0; j < m - 2; j++)
		if (a[i][j] == 1)
		{
			Swap(i, j, i, j + 1, i - 1, j + 1);
			Swap(i - 1, j + 1, i, j + 1, i, j + 2);
		}
	int j = m - 2;
	if (a[i][j] == 1)
		Swap(i, j, i, j + 1, i - 1, j + 1);
	i = n - 1;
	j = m - 1;
	if (a[i][j] == 1 || a[i - 1][j] == 1)
	{
		if (a[i][j] + a[i - 1][j] == 1)
		{
			if (a[i][j] == 1)
				Swap(i, j, i, j - 1, i - 1, j - 1);
			else
				Swap(i - 1, j, i, j - 1, i - 1, j - 1);
			Swap(i, j, i - 1, j, i - 1, j - 1);
			Swap(i, j, i - 1, j, i, j - 1);
		}
		else
		{
			Swap(i, j, i, j - 1, i - 1, j - 1);
			Swap(i, j, i - 1, j, i - 1, j - 1);
		}
	}
	cout << ans.size() << endl;
	for (auto e : ans)
	{
		for (auto q : e)
			cout << q + 1 << " ";
		cout << endl;
	}
}

int main()
{
	//srand(time(0));

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	//ios_base::sync_with_stdio(0);
	//cin.tie(0);
	//cout.tie(0);

	//cout << fixed << setprecision(5);
	int t = 1;
	cin >> t;
	for (int i = 0; i < t; i++)
		solve();
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

#define ar array
#define ll long long

const int MAX_N = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;

vector<vector<bool>> grid;
vector<string> output;
int sum;

void swap(int x1, int y1, int x2, int y2, int x3, int y3) {
    sum++;
    grid[x1][y1] = !grid[x1][y1];
    grid[x2][y2] = !grid[x2][y2];
    grid[x3][y3] = !grid[x3][y3];
    output.push_back(to_string(x1 + 1) + " " + to_string(y1 + 1) + " " + to_string(x2 + 1) + " " + to_string(y2 + 1) + " " + to_string(x3 + 1) + " " + to_string(y3 + 1) + "\n");
}

void solve() {
    int n, m, i, j; cin >> n >> m;
    char a;
    sum = 0;
    output.clear();
    grid.clear();
    for (size_t i = 0; i < n; i++)
    {
        vector<bool> row;
        for (size_t j = 0; j < m; j++)
        {
            cin >> a;
            row.push_back((a - 48));
        }
        grid.push_back(row);
    }
    for (size_t i = 0; i < n - 2; i++)
    {
        for (size_t j = 0; j < m; j++)
        {

            if (grid[i][j]) {
                if (j == m - 1) swap(i, j, i + 1, j, i + 1, j - 1);
                else swap(i, j, i + 1, j, i + 1, j + 1);
            }
        }
    }
    i = n - 2;
    for (size_t j = 0; j < m - 2; j++)
    {
        if (grid[i][j] && grid[i + 1][j]) swap(i, j, i + 1, j, i + 1, j + 1);
        else if (grid[i][j]) swap(i, j, i, j + 1, i + 1, j + 1);
        else if (grid[i + 1][j]) swap(i + 1, j, i, j + 1, i + 1, j + 1);
    }
    j = m - 2;
    vector<int> zeros, ones;
    if (grid[i][j]) {
        ones.push_back(i);
        ones.push_back(j);
    }
    else {
        zeros.push_back(i);
        zeros.push_back(j);
    }
    if (grid[i + 1][j]) {
        ones.push_back(i + 1);
        ones.push_back(j);
    }
    else {
        zeros.push_back(i + 1);
        zeros.push_back(j);
    }
    if (grid[i][j + 1]) {
        ones.push_back(i);
        ones.push_back(j + 1);
    }
    else {
        zeros.push_back(i);
        zeros.push_back(j + 1);
    }
    if (grid[i + 1][j + 1]) {
        ones.push_back(i + 1);
        ones.push_back(j + 1);
    }
    else {
        zeros.push_back(i + 1);
        zeros.push_back(j + 1);
    }
    if (ones.size() == 6) {
        swap(ones[0], ones[1], ones[2], ones[3], ones[4], ones[5]);
    }
    else if (ones.size() == 4) {
        swap(zeros[0], zeros[1], zeros[2], zeros[3], ones[0], ones[1]);
        swap(zeros[0], zeros[1], zeros[2], zeros[3], ones[2], ones[3]);
    }
    else if (ones.size() == 2) {
        swap(zeros[0], zeros[1], zeros[2], zeros[3], ones[0], ones[1]);
        swap(zeros[0], zeros[1], zeros[4], zeros[5], ones[0], ones[1]);
        swap(zeros[4], zeros[5], zeros[2], zeros[3], ones[0], ones[1]);
    }
    else if (ones.size() == 8) {
        swap(ones[0], ones[1], ones[2], ones[3], ones[4], ones[5]);
        swap(ones[0], ones[1], ones[2], ones[3], ones[6], ones[7]);
        swap(ones[0], ones[1], ones[6], ones[7], ones[4], ones[5]);
        swap(ones[6], ones[7], ones[2], ones[3], ones[4], ones[5]);
    }
    cout << sum << "\n";
    for (auto i : output) cout << i;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tc; cin >> tc;
    for (int t = 1; t <= tc; t++) {
        solve();
    }
}


#include<iostream>
#include<cmath>
#include<map>
#include<string.h>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<iomanip>
#include<string>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;
const ll mod = 1e9+7;
int main() {
    int t;
    cin >> t;
    while (t--) {
        char ch[105][105];
        int pos[30005];
        int cnt = 0;
        int s = 0;
        int n, m;
        cin >> n >> m;
        for (int i = 1;i <= n;i++) {
            for (int j = 1;j <= m;j++) {
                cin >> ch[i][j];
            }
        }
        for (int i = 1;i <= n;i += 2) {
            if (i == n)i--;
            for (int j = 1;j <= m;j += 2) {
                if (j == m)j--;
                int xx[4], yy[4];
                int left = 0, right = 3;
                int tt = 0;
                for (int x = i;x <= i + 1;x++) {
                    for (int y = j;y <= j + 1;y++) {
                        if (ch[x][y] == '1') {
                            tt++;
                            xx[right] = x;yy[right--] = y;
                            ch[x][y] = '0';
                        }
                        else {
                            xx[left] = x;yy[left++] = y;
                        }
                    }
                }
                //cout << tt << endl;
                if (tt == 0) {
                    continue;
                }
                else if (tt == 1) {
                    pos[++cnt] = xx[3];
                    pos[++cnt] = yy[3];
                    pos[++cnt] = xx[0];
                    pos[++cnt] = yy[0];
                    pos[++cnt] = xx[1];
                    pos[++cnt] = yy[1];
                    pos[++cnt] = xx[0];
                    pos[++cnt] = yy[0];
                    pos[++cnt] = xx[2];
                    pos[++cnt] = yy[2];
                    pos[++cnt] = xx[3];
                    pos[++cnt] = yy[3];
                    pos[++cnt] = xx[1];
                    pos[++cnt] = yy[1];
                    pos[++cnt] = xx[2];
                    pos[++cnt] = yy[2];
                    pos[++cnt] = xx[3];
                    pos[++cnt] = yy[3];
                    s += 3;

                }
                else if (tt == 2) {
                    pos[++cnt] = xx[0];
                    pos[++cnt] = yy[0];
                    pos[++cnt] = xx[1];
                    pos[++cnt] = yy[1];
                    pos[++cnt] = xx[2];
                    pos[++cnt] = yy[2];
                    pos[++cnt] = xx[0];
                    pos[++cnt] = yy[0];
                    pos[++cnt] = xx[1];
                    pos[++cnt] = yy[1];
                    pos[++cnt] = xx[3];
                    pos[++cnt] = yy[3];
                    s += 2;
                }
                else if (tt == 3) {
                    pos[++cnt] = xx[1];
                    pos[++cnt] = yy[1];
                    pos[++cnt] = xx[2];
                    pos[++cnt] = yy[2];
                    pos[++cnt] = xx[3];
                    pos[++cnt] = yy[3];
                    s += 1;
                }
                else if (tt == 4) {
                    pos[++cnt] = xx[0];
                    pos[++cnt] = yy[0];
                    pos[++cnt] = xx[1];
                    pos[++cnt] = yy[1];
                    pos[++cnt] = xx[2];
                    pos[++cnt] = yy[2];
                    pos[++cnt] = xx[0];
                    pos[++cnt] = yy[0];
                    pos[++cnt] = xx[1];
                    pos[++cnt] = yy[1];
                    pos[++cnt] = xx[3];
                    pos[++cnt] = yy[3];
                    pos[++cnt] = xx[0];
                    pos[++cnt] = yy[0];
                    pos[++cnt] = xx[2];
                    pos[++cnt] = yy[2];
                    pos[++cnt] = xx[3];
                    pos[++cnt] = yy[3];
                    pos[++cnt] = xx[1];
                    pos[++cnt] = yy[1];
                    pos[++cnt] = xx[2];
                    pos[++cnt] = yy[2];
                    pos[++cnt] = xx[3];
                    pos[++cnt] = yy[3];
                    s += 4;
                }

            }
        }
        cout << s << endl;
        for (int i = 1;i <= cnt;i++) {
            cout << pos[i] << " ";
            if (i % 6 == 0)cout << endl;
        }
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <stdio.h>
#include <queue>
#include <iomanip>
#include <cmath>
#include <map>
#include <stack>
#include <deque>
using namespace std;

struct str
{
    int x1, y1, x2, y2, x3, y3;
};

vector <vector <int>> v;
vector <str> ans;

bool checker(int i, int j)
{
    vector <pair<int, int>> zeroes;
    vector <pair<int, int>> ones;

    for (int i1 = i; i1 <= i + 1; i1++)
    {
        for (int j1 = j; j1 <= j + 1; j1++)
        {
            if (v[i1][j1] == 1)
            {
                ones.push_back(make_pair(i1, j1));
            }
            else
            {
                zeroes.push_back(make_pair(i1, j1));
            }
        }
    }

    if (zeroes.size() == 4)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void func(int i, int j)
{
    vector <pair<int, int>> zeroes;
    vector <pair<int, int>> ones;

    for (int i1 = i; i1 <= i + 1; i1++)
    {
        for (int j1 = j; j1 <= j + 1; j1++)
        {
            if (v[i1][j1] == 1)
            {
                ones.push_back(make_pair(i1, j1));
            }
            else
            {
                zeroes.push_back(make_pair(i1, j1));
            }
        }
    }

    if (ones.size() == 4)
    {
        ans.push_back({ ones[0].first, ones[0].second, ones[1].first, ones[1].second, zeroes[0].first, zeroes[0].second });

        v[ones[0].first][ones[0].second] = 0;
        v[ones[1].first][ones[1].second] = 0;
        v[zeroes[0].first][zeroes[0].second] = 1;
    }
    else if (ones.size() == 3)
    {
        ans.push_back({ ones[0].first, ones[0].second, ones[1].first, ones[1].second, ones[2].first, ones[2].second });

        v[ones[0].first][ones[0].second] = 0;
        v[ones[1].first][ones[1].second] = 0;
        v[ones[2].first][ones[2].second] = 0;
    }
    else if (ones.size() == 2)
    {
        ans.push_back({ zeroes[0].first, zeroes[0].second, zeroes[1].first, zeroes[1].second, ones[0].first, ones[0].second });

        v[zeroes[0].first][zeroes[0].second] = 1;
        v[zeroes[1].first][zeroes[1].second] = 1;
        v[ones[0].first][ones[0].second] = 0;
    }
    else if (ones.size() == 1)
    {
        ans.push_back({ zeroes[0].first, zeroes[0].second, zeroes[1].first, zeroes[1].second, ones[0].first, ones[0].second });

        v[zeroes[0].first][zeroes[0].second] = 1;
        v[zeroes[1].first][zeroes[1].second] = 1;
        v[ones[0].first][ones[0].second] = 0;
    }
}

void task()
{
    int t;
    cin >> t;

    for (int k = 0; k < t; k++)
    {
        int n, m;
        cin >> n >> m;

        v.resize(n, vector<int>(m));

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                //cin >> v[i][j];
                char x;
                cin >> x;
                if (x == '0')
                {
                    v[i][j] = 0;
                }
                else
                {
                    v[i][j] = 1;
                }
            }
        }

        for (int i = 0; i < n - 1; i++)
        {
            for (int j = 0; j < m - 1; j++)
            {
                vector <pair<int, int>> zeroes;
                vector <pair<int, int>> ones;

                for (int i1 = i; i1 <= i + 1; i1++)
                {
                    for (int j1 = j; j1 <= j + 1; j1++)
                    {
                        if (v[i][j] == 1)
                        {
                            ones.push_back(make_pair(i1, j1));
                        }
                        else
                        {
                            zeroes.push_back(make_pair(i1, j1));
                        }
                    }
                }

                while (!checker(i, j))
                {
                    func(i, j);
                }

            }
        }

        /*for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cout << v[i][j];
            }
            cout << endl;
        }*/
        cout << ans.size() << endl;

        for (int i = 0; i < ans.size(); i++)
        {
            cout << ans[i].x1 + 1 << ' ' << ans[i].y1 + 1 << ' ' << ans[i].x2 + 1 << ' ' << ans[i].y2 + 1 << ' ' << ans[i].x3 + 1 << ' ' << ans[i].y3 + 1 << endl;
        }
        ans.clear();
        v.clear();
    }
}

int main()
{
    task();
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <stdio.h>
#include <queue>
#include <iomanip>
#include <cmath>
#include <map>
#include <stack>
#include <deque>
using namespace std;

struct str
{
    int x1, y1, x2, y2, x3, y3;
};

vector <vector <int>> v;
vector <str> ans;

bool checker(int i, int j)
{
    vector <pair<int, int>> zeroes;
    vector <pair<int, int>> ones;

    for (int i1 = i; i1 <= i + 1; i1++)
    {
        for (int j1 = j; j1 <= j + 1; j1++)
        {
            if (v[i1][j1] == 1)
            {
                ones.push_back(make_pair(i1, j1));
            }
            else
            {
                zeroes.push_back(make_pair(i1, j1));
            }
        }
    }

    if (zeroes.size() == 4)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void func(int i, int j)
{
    vector <pair<int, int>> zeroes;
    vector <pair<int, int>> ones;

    for (int i1 = i; i1 <= i + 1; i1++)
    {
        for (int j1 = j; j1 <= j + 1; j1++)
        {
            if (v[i1][j1] == 1)
            {
                ones.push_back(make_pair(i1, j1));
            }
            else
            {
                zeroes.push_back(make_pair(i1, j1));
            }
        }
    }

    if (ones.size() == 4)
    {
        ans.push_back({ ones[0].first, ones[0].second, ones[1].first, ones[1].second, ones[2].first, ones[2].second });

        v[ones[0].first][ones[0].second] = 0;
        v[ones[1].first][ones[1].second] = 0;
        v[ones[2].first][ones[2].second] = 1;
    }
    else if (ones.size() == 3)
    {
        ans.push_back({ ones[0].first, ones[0].second, ones[1].first, ones[1].second, ones[2].first, ones[2].second });

        v[ones[0].first][ones[0].second] = 0;
        v[ones[1].first][ones[1].second] = 0;
        v[ones[2].first][ones[2].second] = 0;
    }
    else if (ones.size() == 2)
    {
        ans.push_back({ zeroes[0].first, zeroes[0].second, zeroes[1].first, zeroes[1].second, ones[0].first, ones[0].second });

        v[zeroes[0].first][zeroes[0].second] = 1;
        v[zeroes[1].first][zeroes[1].second] = 1;
        v[ones[0].first][ones[0].second] = 0;
    }
    else if (ones.size() == 1)
    {
        ans.push_back({ zeroes[0].first, zeroes[0].second, zeroes[1].first, zeroes[1].second, ones[0].first, ones[0].second });

        v[zeroes[0].first][zeroes[0].second] = 1;
        v[zeroes[1].first][zeroes[1].second] = 1;
        v[ones[0].first][ones[0].second] = 0;
    }
}

void task()
{
    int t;
    cin >> t;

    for (int k = 0; k < t; k++)
    {
        int n, m;
        cin >> n >> m;

        v.resize(n, vector<int>(m));

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                char x;
                cin >> x;
                if (x == '0')
                {
                    v[i][j] = 0;
                }
                else
                {
                    v[i][j] = 1;
                }
            }
        }

        for (int i = 0; i < n - 1; i++)
        {
            for (int j = 0; j < m - 1; j++)
            {
                vector <pair<int, int>> zeroes;
                vector <pair<int, int>> ones;

                for (int i1 = i; i1 <= i + 1; i1++)
                {
                    for (int j1 = j; j1 <= j + 1; j1++)
                    {
                        if (v[i1][j1] == 1)
                        {
                            ones.push_back(make_pair(i1, j1));
                        }
                        else
                        {
                            zeroes.push_back(make_pair(i1, j1));
                        }
                    }
                }

                while (!checker(i, j))
                {
                    func(i, j);
                }
            }
        }

        /*for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cout << v[i][j];
            }
            cout << endl;
        }*/
        cout << ans.size() << endl;

        for (int i = 0; i < ans.size(); i++)
        {
            cout << ans[i].x1 + 1 << ' ' << ans[i].y1 + 1 << ' ' << ans[i].x2 + 1 << ' ' << ans[i].y2 + 1 << ' ' << ans[i].x3 + 1 << ' ' << ans[i].y3 + 1 << endl;
        }
        ans.clear();
        v.clear();
    }
}

int main()
{
    task();
    return 0;
}