#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] = 0;
    }
    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;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
inline LL read()
{
    LL x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

const int N=1e2+10;
const int M=3e4+10;

int Map[N][N],x1[M],yy1[M],x2[M],y2[M],x3[M],y3[M];
int n,m,tot;

void Init()
{
    tot=0;
    memset(Map,0,sizeof(Map));
    memset(x1,0,sizeof(x1));
    memset(x2,0,sizeof(x2));
    memset(x3,0,sizeof(x3));
    memset(yy1,0,sizeof(yy1));
    memset(y2,0,sizeof(y2));
    memset(y3,0,sizeof(y3));
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        Init();
        n=read();
        m=read();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
                scanf("%1d",&Map[i][j]);
        }

        for(int i=1;i<n;i++)
        {
            for(int j=1;j<m;j++)
            {
                int a1,b1,a2,b2,a3,b3,a4,b4,z1,z2,z3,z4;
                a1=i;
                a2=i;
                a3=i+1;
                a4=i+1;
                b1=j;
                b2=j+1;
                b3=j;
                b4=j+1;
                z1=Map[a1][b1];
                z2=Map[a2][b2];
                z3=Map[a3][b3];
                z4=Map[a4][b4];
                if(z1==0&&z2==0&&z3==0&&z4==0)
                        continue;

                //1 �� 1
                if(z1==1&&z2==0&&z3==0&&z4==0)
                {
                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a3;
                    y3[tot]=b3;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;
                }

                if(z1==0&&z2==1&&z3==0&&z4==0)
                {
                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a2;
                    yy1[tot]=b2;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a3;
                    y3[tot]=b3;
                }

                if(z1==0&&z2==0&&z3==1&&z4==0)
                {
                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a2;
                    yy1[tot]=b2;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a3;
                    y3[tot]=b3;
                }

                if(z1==0&&z2==0&&z3==0&&z4==1)
                {
                    tot++;
                    x1[tot]=a2;
                    yy1[tot]=b2;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;
                }

                //2�� 1
                if(z1==1&&z2==1&&z3==0&&z4==0)
                {
                    tot++;
                    x1[tot]=a2;
                    yy1[tot]=b2;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;
                }
                if(z1==0&&z2==0&&z3==1&&z4==1)
                {
                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a3;
                    y3[tot]=b3;
                }
                if(z1==1&&z2==0&&z3==1&&z4==0)
                {
                    tot++;
                    x1[tot]=a2;
                    yy1[tot]=b2;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a4;
                    y3[tot]=b4;
                }
                if(z1==0&&z2==1&&z3==0&&z4==1)
                {
                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a3;
                    y3[tot]=b3;
                }
                if(z1==1&&z2==0&&z3==0&&z4==1)
                {
                    tot++;
                    x1[tot]=a2;
                    yy1[tot]=b2;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a3;
                    y3[tot]=b3;
                }
                if(z1==0&&z2==1&&z3==1&&z4==0)
                {
                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a4;
                    y3[tot]=b4;
                }
                //3��1
                if(z1==1&&z2==1&&z3==1&&z4==0)
                {
                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a3;
                    y3[tot]=b3;
                }
                if(z1==1&&z2==1&&z3==0&&z4==1)
                {
                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a4;
                    y3[tot]=b4;
                }
                if(z1==1&&z2==0&&z3==1&&z4==1)
                {
                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;
                }
                if(z1==0&&z2==1&&z3==1&&z4==1)
                {
                    tot++;
                    x1[tot]=a2;
                    yy1[tot]=b2;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;
                }
                //4��1
                if(z1==1&&z2==1&&z3==1&&z4==1)
                {
                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a3;
                    y3[tot]=b3;

                    tot++;
                    x1[tot]=a2;
                    yy1[tot]=b2;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a3;
                    y2[tot]=b3;
                    x3[tot]=a4;
                    y3[tot]=b4;

                    tot++;
                    x1[tot]=a1;
                    yy1[tot]=b1;
                    x2[tot]=a2;
                    y2[tot]=b2;
                    x3[tot]=a4;
                    y3[tot]=b4;
                }
                Map[a1][b1]=0;
                Map[a2][b2]=0;
                Map[a3][b3]=0;
                Map[a4][b4]=0;
            }
        }

        printf("%d\n",tot);
        for(int i=1;i<=tot;i++)
        {
            printf("%d %d %d %d %d %d\n",x1[i],yy1[i],x2[i],y2[i],x3[i],y3[i]);
        }
    }
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std ;
int t , n , m ;
char s[110][110] ;

int main(){
	cin >> t ;
	while(t--){
		scanf("%d%d" , &n , &m) ;
		for(int i = 0 ; i < n ; i++)	scanf("%s" , s[i]) ;
		int num = 0 ;
		for(int i = 0 ; i < n ; i++){
			for(int j = 0 ; j < m ; j++){
				if(s[i][j] == '1')	num++ ;
			}
		}
		num *= 3 ;
		printf("%d\n" , num) ;
		for(int i = 0 ; i < n ; i++){
			for(int j = 0 ; j < m ; j++){
				if(s[i][j] == '1'){
					int ki = 1 , kj = 1 ;
					if(i == n - 1)	ki = -1 ;
					if(j == m - 1)	kj = -1 ;
					printf("%d %d %d %d %d %d\n" , i+1, j+1, i+1+ki, j+1, i+1, j+1+kj) ;
					printf("%d %d %d %d %d %d\n" , i+1, j+1, i+1+ki, j+1+kj, i+1, j+1+kj) ;
					printf("%d %d %d %d %d %d\n" , i+1, j+1, i+1+ki, j+1, i+1+ki, j+1+kj) ;
				}
			}
		}
	}
	return 0 ;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

using namespace std;
typedef long long ll;

ll T,n,m;
char str[110][110];
int main()
{
	scanf("%lld",&T);
	while(T--)
	{
		scanf("%lld%lld",&n,&m);
		for(ll i=0;i<n;i++)
			scanf("%s",str[i]);
		ll times=0;
		for(ll i=0;i<n;i++)
			for(ll j=0;j<m;j++)
				if(str[i][j]=='1')
					times++;
		printf("%lld\n",times*3);
		for(ll i=0;i<n;i++)
		{
			for(ll j=0;j<m;j++)
			{
				if(str[i][j]=='1')
				{
					if(i!=n-1 && j!=m-1)
					{
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1+1,j+1,i+1,j+1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1+1,j+1,i+1+1,j+1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j+1+1,i+1+1,j+1+1);
					}
					else if(i==0 && j==m-1)
					{
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j-1+1,i+1+1,j+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1+1,j+1,i+1+1,j-1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j-1+1,i+1+1,j-1+1);
					}
					else if(i==n-1 && j==0)
					{
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i-1+1,j+1,i+1,j+1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i-1+1,j+1,i-1+1,j+1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j+1+1,i+1,j+1+1);
					}
					else
					{
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j-1+1,i-1+1,j+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j-1+1,i-1+1,j-1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i-1+1,j+1,i-1+1,j-1+1);
					}
				}

			}
		}
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

using namespace std;
typedef long long ll;

ll T,n,m;
char str[110][110];
int main()
{
	scanf("%lld",&T);
	while(T--)
	{
		scanf("%lld%lld",&n,&m);
		for(ll i=0;i<n;i++)
			scanf("%s",str[i]);
		ll times=0;
		for(ll i=0;i<n;i++)
			for(ll j=0;j<m;j++)
				if(str[i][j]=='1')
					times++;
		printf("%lld\n",times*3);
		for(ll i=0;i<n;i++)
		{
			for(ll j=0;j<m;j++)
			{
				if(str[i][j]=='1')
				{
					if(i!=n-1 && j!=m-1)
					{
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1+1,j+1,i+1,j+1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1+1,j+1,i+1+1,j+1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j+1+1,i+1+1,j+1+1);
					}
					else if(i==0 && j==m-1)
					{
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j-1+1,i+1+1,j+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1+1,j+1,i+1+1,j-1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j-1+1,i+1+1,j-1+1);
					}
					else if(i==n-1 && j==0)
					{
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i-1+1,j+1,i+1,j+1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i-1+1,j+1,i-1+1,j+1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i-1+1,j+1+1,i+1,j+1+1);
					}
					else
					{
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j-1+1,i-1+1,j+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j-1+1,i-1+1,j-1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i-1+1,j+1,i-1+1,j-1+1);
					}
				}

			}
		}
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

using namespace std;
typedef long long ll;

ll T,n,m;
char str[110][110];
int main()
{
	scanf("%lld",&T);
	while(T--)
	{
		scanf("%lld%lld",&n,&m);
		for(ll i=0;i<n;i++)
			scanf("%s",str[i]);
		ll times=0;
		for(ll i=0;i<n;i++)
			for(ll j=0;j<m;j++)
				if(str[i][j]=='1')
					times++;
		printf("%lld\n",times*3);
		for(ll i=0;i<n;i++)
		{
			for(ll j=0;j<m;j++)
			{
				if(str[i][j]=='1')
				{
					printf("%lld %lld %lld %lld %lld %lld\n",i,j,i+1,j,i,j+1);
					printf("%lld %lld %lld %lld %lld %lld\n",i,j,i+1,j,i+1,j+1);
					printf("%lld %lld %lld %lld %lld %lld\n",i,j,i,j+1,i+1,j+1);
				}
			}
		}
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

using namespace std;
typedef long long ll;

ll T,n,m;
char str[110][110];
int main()
{
	scanf("%lld",&T);
	while(T--)
	{
		scanf("%lld%lld",&n,&m);
		for(ll i=0;i<n;i++)
			scanf("%s",str[i]);
		ll times=0;
		for(ll i=0;i<n;i++)
			for(ll j=0;j<m;j++)
				if(str[i][j]=='1')
					times++;
		printf("%lld\n",times*3);
		for(ll i=0;i<n;i++)
		{
			for(ll j=0;j<m;j++)
			{
				if(str[i][j]=='1')
				{
					if(i!=n-1 && j!=m-1)
					{
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1+1,j+1,i+1,j+1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1+1,j+1,i+1+1,j+1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j+1+1,i+1+1,j+1+1);
					}
					else if(i==0 && j==m-1)
					{
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j-1+1,i+1+1,j+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1+1,j+1,i+1+1,j-1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j-1+1,i+1+1,j-1+1);
					}
					else if(i==n-1 && j==0)
					{
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i-1+1,j+1,i+1,j+1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i-1+1,j+1,i-1+1,j+1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j+1+1,i+1,j+1);
					}
					else
					{
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j-1+1,i-1+1,j+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i+1,j-1+1,i-1+1,j-1+1);
						printf("%lld %lld %lld %lld %lld %lld\n",i+1,j+1,i-1+1,j+1,i-1+1,j-1+1);
					}
				}

			}
		}
	}

	return 0;
}


t=int(input())

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

                else:
                    B.append ([i,j,i+1,j+1,i,j])
                    B.append([i+1,j,i+1,j+1,i,j+1])
                    B.append ([i+1,j,i,j,i,j+1])
    print(u)
    for j in range(u):
        print(*B[j])


t=int(input())

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

                else:
                    B.append ([i,j,i+1,j+1,i,j])
                    B.append([i+1,j,i+1,j+1,i,j+1])
                    B.append ([i+1,j,i,j,i,j+1])
    print(u)
    for j in range(u):
        print(*B[j])


t=int(input())

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


t=int(input())

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


import math

def selectTiles(mepas, x, y):
    ans = []
    if mepas[x][y] == 1:
        ans.append(x + 1)
        ans.append(y + 1)
        mepas[x][y] = 0 if mepas[x][y] else 1
    if mepas[x][y+1] == 1 or len(ans) < 2:
        ans.append(x + 1)
        ans.append(y + 2)
        mepas[x][y+1] = 0 if mepas[x][y+1] else 1
    if mepas[x+1][y] == 1 or len(ans) < 4:
        ans.append(x + 2)
        ans.append(y + 1)
        mepas[x+1][y] = 0 if mepas[x+1][y] else 1
    if len(ans) < 6:
        ans.append(x + 2)
        ans.append(y + 2)
        mepas[x+1][y+1] = 0 if mepas[x+1][y+1] else 1
    return mepas, tuple(ans)

def selectTiles2(mepas, x, y):
    ans = []
    if mepas[x][y] == 1:
        ans.append(x + 1)
        ans.append(y + 1)
        mepas[x][y] = 0 if mepas[x][y] else 1
    if mepas[x+1][y] == 1 or len(ans) < 2:
        ans.append(x + 2)
        ans.append(y + 1)
        mepas[x+1][y] = 0 if mepas[x+1][y] else 1
    if mepas[x][y+1] == 1 or len(ans) < 4:
        ans.append(x + 1)
        ans.append(y + 2)
        mepas[x][y+1] = 0 if mepas[x][y+1] else 1
    if len(ans) < 6:
        ans.append(x + 2)
        ans.append(y + 2)
        mepas[x+1][y+1] = 0 if mepas[x+1][y+1] else 1
    return mepas, tuple(ans)

def countOnes(mepas, x, y):
    m = 0
    m += mepas[x][y]
    m += mepas[x+1][y]
    m += mepas[x][y+1]
    m += mepas[x+1][y+1]
    return m

def finishingTouch(mepas, x, y):
    ans = []
    c = countOnes(mepas, x, y)
    if c == 1:
        mepas, a1 = oneLeft(mepas, x, y)
        mepas, a2 = twoLeft(mepas, x, y)
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a1)
        ans.append(a2)
        ans.append(a3)
    elif c == 2:
        mepas, a2 = twoLeft(mepas, x, y)
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a2)
        ans.append(a3)
    elif c == 3:
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a3)

    return mepas, ans

def oneLeft(mepas, x, y):
    ones = 1
    zeros = 2
    ans = []
    for xx in range(2):
        for yy in range(2):
            if mepas[x+xx][y+yy] == 1 and ones > 0:
                ones -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 0
            elif mepas[x+xx][y+yy] == 0 and zeros > 0:
                zeros -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 1
    return mepas, tuple(ans)

def twoLeft(mepas, x, y):
    ones = 1
    zeros = 2
    ans = []
    for xx in range(2):
        for yy in range(2):
            if mepas[x+xx][y+yy] == 1 and ones > 0:
                ones -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 0
            elif mepas[x+xx][y+yy] == 0 and zeros > 0:
                zeros -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 1
    return mepas, tuple(ans)

T = int(input())
for t in range(T):
    X, Y = map(int, input().split())
    mepas = []
    ans = []
    for x in range(X):
        y = [int(c) for c in input()]
        mepas.append(y)
    # print(*mepas, sep="\n")

    if X % 2 == 1:
        x = 0
        for y in range(Y-1):
            if mepas[x][y] == 1 or (y == Y-2 and mepas[x][y+1] == 1):
                mepas, temporary = selectTiles(mepas, x, y)
                ans.append(temporary)

    if Y % 2 == 1:
        y = 0
        for x in range(X-1):
            if mepas[x][y] == 1 or (x == X - 2 and mepas[x+1][y] == 1):
                mepas, temporary = selectTiles2(mepas, x, y)
                ans.append(temporary)

    for x in range(0, X-1, 2):
        x += X % 2
        for y in range(0, Y-1, 2):
            y += Y % 2
            # if x < X-2:
            #     if mepas[x][y] == 1 or (y == Y-2 and mepas[x][y+1] == 1):
            #         mepas, temporary = selectTiles(mepas, x, y)
            #         ans.append(temporary)
            # else:
            #     if mepas[x][y] == 1 or mepas[x+1][y] == 1:
            #         mepas, temporary = selectTiles2(mepas, x, y)
            #         ans.append(temporary)
            mepas, temporary = finishingTouch(mepas, x, y)
            ans = ans + temporary

    print(len(ans))
    for a in ans:
        print(*a)
    # print(*mepas, sep="\n")


import math

def selectTiles(mepas, x, y):
    ans = []
    if mepas[x][y] == 1:
        ans.append(x + 1)
        ans.append(y + 1)
        mepas[x][y] = 0 if mepas[x][y] else 1
    if mepas[x][y+1] == 1 or len(ans) < 2:
        ans.append(x + 1)
        ans.append(y + 2)
        mepas[x][y+1] = 0 if mepas[x][y+1] else 1
    if mepas[x+1][y] == 1 or len(ans) < 4:
        ans.append(x + 2)
        ans.append(y + 1)
        mepas[x+1][y] = 0 if mepas[x+1][y] else 1
    if len(ans) < 6:
        ans.append(x + 2)
        ans.append(y + 2)
        mepas[x+1][y+1] = 0 if mepas[x+1][y+1] else 1
    return mepas, tuple(ans)

def selectTiles2(mepas, x, y):
    ans = []
    if mepas[x][y] == 1:
        ans.append(x + 1)
        ans.append(y + 1)
        mepas[x][y] = 0 if mepas[x][y] else 1
    if mepas[x+1][y] == 1 or len(ans) < 2:
        ans.append(x + 2)
        ans.append(y + 1)
        mepas[x+1][y] = 0 if mepas[x+1][y] else 1
    if mepas[x][y+1] == 1 or len(ans) < 4:
        ans.append(x + 1)
        ans.append(y + 2)
        mepas[x][y+1] = 0 if mepas[x][y+1] else 1
    if len(ans) < 6:
        ans.append(x + 2)
        ans.append(y + 2)
        mepas[x+1][y+1] = 0 if mepas[x+1][y+1] else 1
    return mepas, tuple(ans)

def countOnes(mepas, x, y):
    m = 0
    m += mepas[x][y]
    m += mepas[x+1][y]
    m += mepas[x][y+1]
    m += mepas[x+1][y+1]
    return m

def finishingTouch(mepas, x, y):
    ans = []
    c = countOnes(mepas, x, y)
    if c == 1:
        mepas, a1 = oneLeft(mepas, x, y)
        mepas, a2 = twoLeft(mepas, x, y)
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a1)
        ans.append(a2)
        ans.append(a3)
    elif c == 2:
        mepas, a2 = twoLeft(mepas, x, y)
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a2)
        ans.append(a3)
    elif c == 3:
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a3)
    else:
        mepas, a0 = selectTiles(mepas, x, y)
        mepas, a1 = oneLeft(mepas, x, y)
        mepas, a2 = twoLeft(mepas, x, y)
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a0)
        ans.append(a1)
        ans.append(a2)
        ans.append(a3)

    return mepas, ans

def oneLeft(mepas, x, y):
    ones = 1
    zeros = 2
    ans = []
    for xx in range(2):
        for yy in range(2):
            if mepas[x+xx][y+yy] == 1 and ones > 0:
                ones -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 0
            elif mepas[x+xx][y+yy] == 0 and zeros > 0:
                zeros -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 1
    return mepas, tuple(ans)

def twoLeft(mepas, x, y):
    ones = 1
    zeros = 2
    ans = []
    for xx in range(2):
        for yy in range(2):
            if mepas[x+xx][y+yy] == 1 and ones > 0:
                ones -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 0
            elif mepas[x+xx][y+yy] == 0 and zeros > 0:
                zeros -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 1
    return mepas, tuple(ans)

T = int(input())
for t in range(T):
    X, Y = map(int, input().split())
    mepas = []
    ans = []
    for x in range(X):
        y = [int(c) for c in input()]
        mepas.append(y)
    # print(*mepas, sep="\n")

    if X % 2 == 1:
        x = 0
        for y in range(Y-1):
            if mepas[x][y] == 1 or (y == Y-2 and mepas[x][y+1] == 1):
                mepas, temporary = selectTiles(mepas, x, y)
                ans.append(temporary)

    if Y % 2 == 1:
        y = 0
        for x in range(X-1):
            if mepas[x][y] == 1 or (x == X - 2 and mepas[x+1][y] == 1):
                mepas, temporary = selectTiles2(mepas, x, y)
                ans.append(temporary)

    for x in range(0, X-1, 2):
        x += X % 2
        for y in range(0, Y-1, 2):
            y += Y % 2
            # if x < X-2:
            #     if mepas[x][y] == 1 or (y == Y-2 and mepas[x][y+1] == 1):
            #         mepas, temporary = selectTiles(mepas, x, y)
            #         ans.append(temporary)
            # else:
            #     if mepas[x][y] == 1 or mepas[x+1][y] == 1:
            #         mepas, temporary = selectTiles2(mepas, x, y)
            #         ans.append(temporary)
            mepas, temporary = finishingTouch(mepas, x, y)
            ans = ans + temporary

    print(len(ans))
    for a in ans:
        print(*a)
    print(*mepas, sep="\n")


import math

def selectTiles(mepas, x, y):
    ans = []
    if mepas[x][y] == 1:
        ans.append(x + 1)
        ans.append(y + 1)
        mepas[x][y] = 0 if mepas[x][y] else 1
    if mepas[x][y+1] == 1 or len(ans) < 2:
        ans.append(x + 1)
        ans.append(y + 2)
        mepas[x][y+1] = 0 if mepas[x][y+1] else 1
    if mepas[x+1][y] == 1 or len(ans) < 4:
        ans.append(x + 2)
        ans.append(y + 1)
        mepas[x+1][y] = 0 if mepas[x+1][y] else 1
    if len(ans) < 6:
        ans.append(x + 2)
        ans.append(y + 2)
        mepas[x+1][y+1] = 0 if mepas[x+1][y+1] else 1
    return mepas, tuple(ans)

def selectTiles2(mepas, x, y):
    ans = []
    if mepas[x][y] == 1:
        ans.append(x + 1)
        ans.append(y + 1)
        mepas[x][y] = 0 if mepas[x][y] else 1
    if mepas[x+1][y] == 1 or len(ans) < 2:
        ans.append(x + 2)
        ans.append(y + 1)
        mepas[x+1][y] = 0 if mepas[x+1][y] else 1
    if mepas[x][y+1] == 1 or len(ans) < 4:
        ans.append(x + 1)
        ans.append(y + 2)
        mepas[x][y+1] = 0 if mepas[x][y+1] else 1
    if len(ans) < 6:
        ans.append(x + 2)
        ans.append(y + 2)
        mepas[x+1][y+1] = 0 if mepas[x+1][y+1] else 1
    return mepas, tuple(ans)

def countOnes(mepas, x, y):
    m = 0
    m += mepas[x][y]
    m += mepas[x+1][y]
    m += mepas[x][y+1]
    m += mepas[x+1][y+1]
    return m

def finishingTouch(mepas, x, y):
    ans = []
    c = countOnes(mepas, x, y)
    if c == 1:
        mepas, a1 = oneLeft(mepas, x, y)
        mepas, a2 = twoLeft(mepas, x, y)
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a1)
        ans.append(a2)
        ans.append(a3)
    elif c == 2:
        mepas, a2 = twoLeft(mepas, x, y)
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a2)
        ans.append(a3)
    elif c == 3:
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a3)
    else:
        mepas, a0 = selectTiles(mepas, x, y)
        mepas, a1 = oneLeft(mepas, x, y)
        mepas, a2 = twoLeft(mepas, x, y)
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a0)
        ans.append(a1)
        ans.append(a2)
        ans.append(a3)

    return mepas, ans

def oneLeft(mepas, x, y):
    ones = 1
    zeros = 2
    ans = []
    for xx in range(2):
        for yy in range(2):
            if mepas[x+xx][y+yy] == 1 and ones > 0:
                ones -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 0
            elif mepas[x+xx][y+yy] == 0 and zeros > 0:
                zeros -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 1
    return mepas, tuple(ans)

def twoLeft(mepas, x, y):
    ones = 1
    zeros = 2
    ans = []
    for xx in range(2):
        for yy in range(2):
            if mepas[x+xx][y+yy] == 1 and ones > 0:
                ones -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 0
            elif mepas[x+xx][y+yy] == 0 and zeros > 0:
                zeros -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 1
    return mepas, tuple(ans)

T = int(input())
for t in range(T):
    X, Y = map(int, input().split())
    mepas = []
    ans = []
    for x in range(X):
        y = [int(c) for c in input()]
        mepas.append(y)
    # print(*mepas, sep="\n")

    if X % 2 == 1:
        x = 0
        for y in range(Y-1):
            if mepas[x][y] == 1 or (y == Y-2 and mepas[x][y+1] == 1):
                mepas, temporary = selectTiles(mepas, x, y)
                ans.append(temporary)

    if Y % 2 == 1:
        y = 0
        for x in range(X-1):
            if mepas[x][y] == 1 or (x == X - 2 and mepas[x+1][y] == 1):
                mepas, temporary = selectTiles2(mepas, x, y)
                ans.append(temporary)

    for x in range(0, X-1, 2):
        x += X % 2
        for y in range(0, Y-1, 2):
            y += Y % 2
            # if x < X-2:
            #     if mepas[x][y] == 1 or (y == Y-2 and mepas[x][y+1] == 1):
            #         mepas, temporary = selectTiles(mepas, x, y)
            #         ans.append(temporary)
            # else:
            #     if mepas[x][y] == 1 or mepas[x+1][y] == 1:
            #         mepas, temporary = selectTiles2(mepas, x, y)
            #         ans.append(temporary)
            mepas, temporary = finishingTouch(mepas, x, y)
            ans = ans + temporary

    print(len(ans))
    for a in ans:
        print(*a)
    # print(*mepas, sep="\n")


import math

def selectTiles(mepas, x, y):
    ans = []
    if mepas[x][y] == 1:
        ans.append(x + 1)
        ans.append(y + 1)
        mepas[x][y] = 0 if mepas[x][y] else 1
    if mepas[x][y+1] == 1 or len(ans) < 2:
        ans.append(x + 1)
        ans.append(y + 2)
        mepas[x][y+1] = 0 if mepas[x][y+1] else 1
    if mepas[x+1][y] == 1 or len(ans) < 4:
        ans.append(x + 2)
        ans.append(y + 1)
        mepas[x+1][y] = 0 if mepas[x+1][y] else 1
    if len(ans) < 6:
        ans.append(x + 2)
        ans.append(y + 2)
        mepas[x+1][y+1] = 0 if mepas[x+1][y+1] else 1
    return mepas, tuple(ans)

def selectTiles2(mepas, x, y):
    ans = []
    if mepas[x][y] == 1:
        ans.append(x + 1)
        ans.append(y + 1)
        mepas[x][y] = 0 if mepas[x][y] else 1
    if mepas[x+1][y] == 1 or len(ans) < 2:
        ans.append(x + 2)
        ans.append(y + 1)
        mepas[x+1][y] = 0 if mepas[x+1][y] else 1
    if mepas[x][y+1] == 1 or len(ans) < 4:
        ans.append(x + 1)
        ans.append(y + 2)
        mepas[x][y+1] = 0 if mepas[x][y+1] else 1
    if len(ans) < 6:
        ans.append(x + 2)
        ans.append(y + 2)
        mepas[x+1][y+1] = 0 if mepas[x+1][y+1] else 1
    return mepas, tuple(ans)

def countOnes(mepas, x, y):
    m = 0
    m += mepas[x][y]
    m += mepas[x+1][y]
    m += mepas[x][y+1]
    m += mepas[x+1][y+1]
    return m

def finishingTouch(mepas, x, y):
    ans = []
    c = countOnes(mepas, x, y)
    if c == 1:
        mepas, a1 = oneLeft(mepas, x, y)
        mepas, a2 = twoLeft(mepas, x, y)
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a1)
        ans.append(a2)
        ans.append(a3)
    elif c == 2:
        mepas, a2 = twoLeft(mepas, x, y)
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a2)
        ans.append(a3)
    elif c == 3:
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a3)
    elif c == 4:
        mepas, a0 = selectTiles(mepas, x, y)
        mepas, a1 = oneLeft(mepas, x, y)
        mepas, a2 = twoLeft(mepas, x, y)
        mepas, a3 = selectTiles(mepas, x, y)
        ans.append(a0)
        ans.append(a1)
        ans.append(a2)
        ans.append(a3)

    return mepas, ans

def oneLeft(mepas, x, y):
    ones = 1
    zeros = 2
    ans = []
    for xx in range(2):
        for yy in range(2):
            if mepas[x+xx][y+yy] == 1 and ones > 0:
                ones -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 0
            elif mepas[x+xx][y+yy] == 0 and zeros > 0:
                zeros -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 1
    return mepas, tuple(ans)

def twoLeft(mepas, x, y):
    ones = 1
    zeros = 2
    ans = []
    for xx in range(2):
        for yy in range(2):
            if mepas[x+xx][y+yy] == 1 and ones > 0:
                ones -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 0
            elif mepas[x+xx][y+yy] == 0 and zeros > 0:
                zeros -= 1
                ans.append(x + xx + 1)
                ans.append(y + yy + 1)
                mepas[x+xx][y+yy] = 1
    return mepas, tuple(ans)

T = int(input())
for t in range(T):
    X, Y = map(int, input().split())
    mepas = []
    ANS = []
    for x in range(X):
        y = [int(c) for c in input()]
        mepas.append(y)
    # print(*mepas, sep="\n")

    if X % 2 == 1:
        x = 0
        for y in range(Y-1):
            if mepas[x][y] == 1 or (y == Y-2 and mepas[x][y+1] == 1):
                mepas, temporary = selectTiles(mepas, x, y)
                ANS.append(temporary)

    if Y % 2 == 1:
        y = 0
        for x in range(X-1):
            if mepas[x][y] == 1 or (x == X - 2 and mepas[x+1][y] == 1):
                mepas, temporary = selectTiles2(mepas, x, y)
                ANS.append(temporary)

    for x in range(0, X-1, 2):
        x += X % 2
        for y in range(0, Y-1, 2):
            y += Y % 2
            # if x < X-2:
            #     if mepas[x][y] == 1 or (y == Y-2 and mepas[x][y+1] == 1):
            #         mepas, temporary = selectTiles(mepas, x, y)
            #         ans.append(temporary)
            # else:
            #     if mepas[x][y] == 1 or mepas[x+1][y] == 1:
            #         mepas, temporary = selectTiles2(mepas, x, y)
            #         ans.append(temporary)
            mepas, temporary = finishingTouch(mepas, x, y)
            ANS = ANS + temporary

    print(len(ANS))
    for a in ANS:
        print(*a)
    # print(*mepas, sep="\n")


# map(int, input().split())
def main():
    n, m = map(int, input().split())
    a = [[] for i in range(n)]
    for i in range(n):
        a[i] = list(input())
    x1 = []
    x2 = []
    x3 = []
    y1 = []
    y2 = []
    y3 = []
    if a[0][m - 1] == '1':
        y1.append(0)
        x1.append(m - 1)
        y2.append(0)
        x2.append(m - 2)
        y3.append(1)
        x3.append(m - 1)

        y1.append(0)
        x1.append(m - 1)
        y2.append(0)
        x2.append(m - 2)
        y3.append(1)
        x3.append(m - 2)

        y1.append(0)
        x1.append(m - 1)
        y2.append(1)
        x2.append(m - 2)
        y3.append(1)
        x3.append(m - 1)
    if a[n - 1][0] == '1':
        y1.append(n - 1)
        x1.append(0)
        y2.append(0)
        x2.append(n - 2)
        x3.append(1)
        y3.append(n - 1)

        y1.append(n - 1)
        x1.append(0)
        y2.append(0)
        x2.append(n - 2)
        x3.append(1)
        y3.append(n - 2)

        y1.append(n - 1)
        x1.append(0)
        y2.append(1)
        x2.append(n - 2)
        x3.append(1)
        y3.append(n - 1)

    for i in range(n):
        for j in range(m):
            if (i == 0 and j == m - 1) or (i == n - 1 and j == 0):
                continue
            if a[i][j] == '0':
                continue
            if i == n - 1 or j == m - 1:
                y1.append(i)
                x1.append(j)
                y2.append(i - 1)
                x2.append(j)
                x3.append(i)
                y3.append(j - 1)

                y1.append(i)
                x1.append(j)
                y2.append(i - 1)
                x2.append(j)
                x3.append(i - 1)
                y3.append(j - 1)

                y1.append(i)
                x1.append(j)
                y2.append(i)
                x2.append(j - 1)
                x3.append(i - 1)
                y3.append(j - 1)
                continue
            y1.append(i)
            x1.append(j)
            y2.append(i + 1)
            x2.append(j)
            x3.append(i)
            y3.append(j + 1)

            y1.append(i)
            x1.append(j)
            y2.append(i + 1)
            x2.append(j)
            x3.append(i + 1)
            y3.append(j + 1)

            y1.append(i)
            x1.append(j)
            y2.append(i)
            x2.append(j + 1)
            x3.append(i + 1)
            y3.append(j + 1)
    print(len(x1))
    for i in range(len(x1)):
        print(x1[i] + 1, y1[i] + 1, x2[i] + 1, y2[i] + 1, x3[i] + 1, y3[i] + 1,)

rw = int(input())
for wewq in range(rw):
    main()


# map(int, input().split())
def main():
    n, m = map(int, input().split())
    a = [[] for i in range(n)]
    for i in range(n):
        a[i] = list(input())
    x1 = []
    x2 = []
    x3 = []
    y1 = []
    y2 = []
    y3 = []
    if a[0][m - 1] == '1':
        y1.append(0)
        x1.append(m - 1)
        y2.append(0)
        x2.append(m - 2)
        y3.append(1)
        x3.append(m - 1)

        y1.append(0)
        x1.append(m - 1)
        y2.append(0)
        x2.append(m - 2)
        y3.append(1)
        x3.append(m - 2)

        y1.append(0)
        x1.append(m - 1)
        y2.append(1)
        x2.append(m - 2)
        y3.append(1)
        x3.append(m - 1)
    if a[n - 1][0] == '1':
        y1.append(n - 1)
        x1.append(0)
        x2.append(0)
        y2.append(n - 2)
        x3.append(1)
        y3.append(n - 1)

        y1.append(n - 1)
        x1.append(0)
        x2.append(0)
        y2.append(n - 2)
        x3.append(1)
        y3.append(n - 2)

        y1.append(n - 1)
        x1.append(0)
        x2.append(1)
        y2.append(n - 2)
        x3.append(1)
        y3.append(n - 1)

    for i in range(n):
        for j in range(m):
            if (i == 0 and j == m - 1) or (i == n - 1 and j == 0):
                continue
            if a[i][j] == '0':
                continue
            if i == n - 1 or j == m - 1:
                y1.append(i)
                x1.append(j)
                y2.append(i - 1)
                x2.append(j)
                y3.append(i)
                x3.append(j - 1)

                y1.append(i)
                x1.append(j)
                y2.append(i - 1)
                x2.append(j)
                y3.append(i - 1)
                x3.append(j - 1)

                y1.append(i)
                x1.append(j)
                y2.append(i)
                x2.append(j - 1)
                y3.append(i - 1)
                x3.append(j - 1)
                continue
            y1.append(i)
            x1.append(j)
            y2.append(i + 1)
            x2.append(j)
            y3.append(i)
            x3.append(j + 1)

            y1.append(i)
            x1.append(j)
            y2.append(i + 1)
            x2.append(j)
            y3.append(i + 1)
            x3.append(j + 1)

            y1.append(i)
            x1.append(j)
            y2.append(i)
            x2.append(j + 1)
            y3.append(i + 1)
            x3.append(j + 1)
    print(len(x1))
    for i in range(len(x1)):
        print(y1[i] + 1, x1[i] + 1, y2[i] + 1, x2[i] + 1, y3[i] + 1, x3[i] + 1)

rw = int(input())
for wewq in range(rw):
    main()


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

                else:
                    if lst[i][j]=='0':
                        pass
                    else:
                        ans.append([i,j,i-1,j,i-1,j+1])
                        ans.append([i,j+1,i-1,j,i-1,j+1])
                        lst[i][j]='0'

                        if lst[i][j+1]=='1':
                            lst[i][j+1]='0'
                        else:
                            lst[i][j+1]='1'

            else:
                if j==k-1:
                    if lst[i][j]=='0':
                        pass
                    else:
                        if lst[i][j-1]=='0':
                            ans.append([i,j,i+1,j,i+1,j-1])
                            lst[i][j]='0'
                            if lst[i+1][j]=='1':
                                lst[i+1][j]='0'
                            else:
                                lst[i+1][j]='1'
                            if lst[i+1][j-1]=='1':
                                lst[i+1][j-1]='0'
                            else:
                                lst[i+1][j-1]='1'

                else:
                    if lst[i][j]=='0':
                        pass
                    else:
                        if lst[i][j+1]=='0':
                            ans.append([i,j,i+1,j,i+1,j+1])
                            lst[i][j]='0'
                            if lst[i+1][j]=='1':
                                lst[i+1][j]='0'
                            else:
                                lst[i+1][j]='1'
                            if lst[i+1][j+1]=='1':
                                lst[i+1][j+1]='0'
                            else:
                                lst[i+1][j+1]='1'
                        else:
                            ans.append([i,j,i,j+1,i+1,j+1])

                            lst[i][j]='0'
                            if lst[i][j+1]=='1':
                                lst[i][j+1]='0'
                            else:
                                lst[i][j+1]='1'
                            if lst[i+1][j+1]=='1':
                                lst[i+1][j+1]='0'
                            else:
                                lst[i+1][j+1]='1'
    print(len(ans))
    for i in ans:
        for j in i:
            print(j+1,end=" ")
        print()


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

                else:
                    if lst[i][j]=='0':
                        pass
                    else:
                        ans.append([i,j,i-1,j,i-1,j+1])
                        ans.append([i,j+1,i-1,j,i-1,j+1])
                        lst[i][j]='0'

                        if lst[i][j+1]=='1':
                            lst[i][j+1]='0'
                        else:
                            lst[i][j+1]='1'

            else:
                if j==k-1:
                    if lst[i][j]=='0':
                        pass
                    else:
                        if lst[i][j-1]=='0':
                            ans.append([i,j,i+1,j,i+1,j-1])
                            lst[i][j]='0'
                            if lst[i+1][j]=='1':
                                lst[i+1][j]='0'
                            else:
                                lst[i+1][j]='1'
                            if lst[i+1][j-1]=='1':
                                lst[i+1][j-1]='0'
                            else:
                                lst[i+1][j-1]='1'

                else:
                    if lst[i][j]=='0':
                        pass
                    else:
                        if lst[i][j+1]=='0':
                            ans.append([i,j,i+1,j,i+1,j+1])
                            lst[i][j]='0'
                            if lst[i+1][j]=='1':
                                lst[i+1][j]='0'
                            else:
                                lst[i+1][j]='1'
                            if lst[i+1][j+1]=='1':
                                lst[i+1][j+1]='0'
                            else:
                                lst[i+1][j+1]='1'
                        else:
                            ans.append([i,j,i,j+1,i+1,j+1])
                            lst[i][j]='0'
                            lst[i][j+1]='1'
                            lst[i+1][j+1]='1'

                            lst[i][j]='1'
                            if lst[i][j+1]=='1':
                                lst[i][j+1]='0'
                            else:
                                lst[i][j+1]='1'
                            if lst[i+1][j+1]=='1':
                                lst[i+1][j+1]='0'
                            else:
                                lst[i+1][j+1]='1'
    print(len(ans))
    for i in ans:
        for j in i:
            print(j+1,end=" ")
        print()


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

for _ in  range(int(input())):

    n,m=get_ints()
    c=[]
    ans=[]

    for i in range(n):
        b=input()
        c.append(b)
   ## print(c)
    for i in range(n-1):
        for j in range(m-1):
            ##print(i,j)
            zeros=[]
            ones=[]
            arr=[]
            if c[i][j]=='1':
                ones.append([i+1,j+1])
            if c[i][j]=='0':
                zeros.append([i+1,j+1])
            if c[i][j+1]=='1':
                ones.append([i+1,j+2])
            if c[i][j+1]=='0':
                zeros.append([i+1,j+2])
            if c[i+1][j]=='1':
                ones.append([i+2,j+1])
            if c[i+1][j]=='0':
                zeros.append([i+2,j+1])
            if c[i+1][j+1]=='1':
                ones.append([i+2,j+2])
            if c[i+1][j+1]=='0':
                zeros.append([i+2,j+2])

            if len(ones)==0:
                continue
            if  len(ones)==2:

                arr=[zeros[0][0],zeros[0][1],zeros[1][0],zeros[1][1],ones[0][0],ones[0][1]]
                ans.append(arr)
                arr = [zeros[0][0], zeros[0][1], zeros[1][0], zeros[1][1], ones[1][0], ones[1][1]]
                ans.append(arr)

            if len(ones)==3:
                    arr=[ones[0][0],ones[0][1],ones[1][0],ones[1][1],zeros[0][0],zeros[0][1]]
                    ans.append(arr)
                    arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], ones[2][0], ones[2][1]]
                    ans.append(arr)
                    arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], zeros[0][0], zeros[0][1]]
                    ans.append(arr)

            if len(ones)==1:
                arr=[zeros[0][0],zeros[0][1],zeros[1][0],zeros[1][1],ones[0][0],ones[0][1]]
                ans.append(arr)
                arr=[zeros[0][0],zeros[0][1],zeros[2][0],zeros[2][1],ones[0][0],ones[0][1]]
                ans.append(arr)
                arr=[zeros[1][0],zeros[1][1],zeros[2][0],zeros[2][1],ones[0][0],ones[0][1]]
                ans.append(arr)

            if len(ones)==4:
                arr=[ones[0][0],ones[0][1],ones[1][0],ones[1][1],ones[2][0],ones[2][1]]
                ans.append(arr)
                arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], ones[3][0], ones[3][1]]
                ans.append(arr)
                arr = [ones[0][0], ones[0][1], ones[2][0], ones[2][1], ones[3][0], ones[3][1]]
                ans.append(arr)
                arr = [ones[1][0], ones[1][1], ones[2][0], ones[2][1], ones[3][0], ones[3][1]]
                ans.append(arr)

    print(len(ans))
    for i in range(len(ans)):
        print(*ans[i])


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

for _ in  range(int(input())):

    n,m=get_ints()
    c=[]
    ans=[]

    for i in range(n):
        b=input()
        c.append(b)
   ## print(c)
    for i in range(n-1):
        for j in range(m-1):
            ##print(i,j)
            zeros=[]
            ones=[]
            arr=[]
            if c[i][j]=='1':
                ones.append([i+1,j+1])
            if c[i][j]=='0':
                zeros.append([i+1,j+1])
            if c[i][j+1]=='1':
                ones.append([i+1,j+2])
            if c[i][j+1]=='0':
                zeros.append([i+1,j+2])
            if c[i+1][j]=='1':
                ones.append([i+2,j+1])
            if c[i+1][j]=='0':
                zeros.append([i+2,j+1])
            if c[i+1][j+1]=='1':
                ones.append([i+2,j+2])
            if c[i+1][j+1]=='0':
                zeros.append([i+2,j+2])

            if len(ones)==0:
                continue
            if  len(ones)==2:

                arr=[zeros[0][0],zeros[0][1],zeros[1][0],zeros[1][1],ones[0][0],ones[0][1]]
                ans.append(arr)
                arr = [zeros[0][0], zeros[0][1], zeros[1][0], zeros[1][1], ones[1][0], ones[1][1]]
                ans.append(arr)

            if len(ones)==3:
                    arr=[ones[0][0],ones[0][1],ones[1][0],ones[1][1],zeros[0][0],zeros[0][1]]
                    ans.append(arr)
                    arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], ones[2][0], ones[2][1]]
                    ans.append(arr)
                    arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], zeros[0][0], zeros[0][1]]
                    ans.append(arr)

            if len(ones)==1:
                arr=[zeros[0][0],zeros[0][1],zeros[1][0],zeros[1][1],ones[0][0],ones[0][1]]
                ans.append(arr)
                arr=[zeros[0][0],zeros[0][1],zeros[2][0],zeros[2][1],ones[0][0],ones[0][1]]
                ans.append(arr)
                arr=[zeros[1][0],zeros[1][1],zeros[2][0],zeros[2][1],ones[0][0],ones[0][1]]

            if len(ones)==4:
                arr=[ones[0][0],ones[0][1],ones[1][0],ones[1][1],ones[2][0],ones[2][1]]
                ans.append(arr)
                arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], ones[3][0], ones[3][1]]
                ans.append(arr)
                arr = [ones[0][0], ones[0][1], ones[2][0], ones[2][1], ones[3][0], ones[3][1]]
                ans.append(arr)
                arr = [ones[1][0], ones[1][1], ones[2][0], ones[2][1], ones[3][0], ones[3][1]]
                ans.append(arr)

    print(len(ans))
    for i in range(len(ans)):
        print(*ans[i])


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

for _ in  range(int(input())):

    n,m=get_ints()
    c=[]
    ans=[]

    for i in range(n):
        b=input()
        b=[str(x) for x in b]
        c.append(b)
   ## print(c)
    for i in range(n-1):
        for j in range(m-1):
            ##print(i,j)
            zeros=[]
            ones=[]
            arr=[]
            if c[i][j]=='1':
                ones.append([i+1,j+1])
            if c[i][j]=='0':
                zeros.append([i+1,j+1])
            if c[i][j+1]=='1':
                ones.append([i+1,j+2])
            if c[i][j+1]=='0':
                zeros.append([i+1,j+2])
            if c[i+1][j]=='1':
                ones.append([i+2,j+1])
            if c[i+1][j]=='0':
                zeros.append([i+2,j+1])
            if c[i+1][j+1]=='1':
                ones.append([i+2,j+2])
            if c[i+1][j+1]=='0':
                zeros.append([i+2,j+2])

            if len(ones)==0:
                continue
            if  len(ones)==2:

                arr=[zeros[0][0],zeros[0][1],zeros[1][0],zeros[1][1],ones[0][0],ones[0][1]]
                ans.append(arr)
                arr = [zeros[0][0], zeros[0][1], zeros[1][0], zeros[1][1], ones[1][0], ones[1][1]]
                ans.append(arr)

            if len(ones)==3:
                    arr=[ones[0][0],ones[0][1],ones[1][0],ones[1][1],zeros[0][0],zeros[0][1]]
                    ans.append(arr)
                    arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], ones[2][0], ones[2][1]]
                    ans.append(arr)
                    arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], zeros[0][0], zeros[0][1]]
                    ans.append(arr)

            if len(ones)==1:
                arr=[zeros[0][0],zeros[0][1],zeros[1][0],zeros[1][1],ones[0][0],ones[0][1]]
                ans.append(arr)
                arr=[zeros[0][0],zeros[0][1],zeros[2][0],zeros[2][1],ones[0][0],ones[0][1]]
                ans.append(arr)
                arr=[zeros[1][0],zeros[1][1],zeros[2][0],zeros[2][1],ones[0][0],ones[0][1]]
                ans.append(arr)

            if len(ones)==4:
                arr=[ones[0][0],ones[0][1],ones[1][0],ones[1][1],ones[2][0],ones[2][1]]
                ans.append(arr)
                arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], ones[3][0], ones[3][1]]
                ans.append(arr)
                arr = [ones[0][0], ones[0][1], ones[2][0], ones[2][1], ones[3][0], ones[3][1]]
                ans.append(arr)
                arr = [ones[1][0], ones[1][1], ones[2][0], ones[2][1], ones[3][0], ones[3][1]]
                ans.append(arr)
            c[i][j],c[i+1][j],c[i][j+1],c[i+1][j+1]='0','0','0','0'
    print(len(ans))
    for i in range(len(ans)):
        print(*ans[i])


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

for _ in  range(int(input())):

    n,m=get_ints()
    c=[]
    ans=[]

    for i in range(n):
        b=input()
        c.append(b)
   ## print(c)
    for i in range(n-1):
        for j in range(m-1):
            ##print(i,j)
            zeros=[]
            ones=[]
            arr=[]
            if c[i][j]=='1':
                ones.append([i+1,j+1])
            if c[i][j]=='0':
                zeros.append([i+1,j+1])
            if c[i][j+1]=='1':
                ones.append([i+1,j+2])
            if c[i][j+1]=='0':
                zeros.append([i+1,j+2])
            if c[i+1][j]=='1':
                ones.append([i+2,j+1])
            if c[i+1][j]=='0':
                zeros.append([i+2,j+1])
            if c[i+1][j+1]=='1':
                ones.append([i+2,j+2])
            if c[i+1][j+1]=='0':
                zeros.append([i+2,j+2])

            if len(ones)==0:
                continue
            if  len(ones)==2:

                arr=[zeros[0][0],zeros[0][1],zeros[1][0],zeros[1][1],ones[0][0],ones[0][1]]
                ans.append(arr)
                arr = [zeros[0][0], zeros[0][1], zeros[1][0], zeros[1][1], ones[1][0], ones[1][1]]
                ans.append(arr)

            if len(ones)==3:
                    arr=[ones[0][0],ones[0][1],ones[1][0],ones[1][1],zeros[0][0],zeros[0][1]]
                    ans.append(arr)
                    arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], ones[2][0], ones[2][1]]
                    ans.append(arr)
                    arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], zeros[0][0], zeros[0][1]]
                    ans.append(arr)

            if len(ones)==1:
                arr=[zeros[0][0],zeros[0][1],zeros[1][0],zeros[1][1],ones[0][0],ones[0][1]]
                ans.append(arr)
                arr=[zeros[0][0],zeros[0][1],zeros[2][0],zeros[2][1],ones[0][0],ones[0][1]]
                ans.append(arr)
                arr=[zeros[1][0],zeros[1][1],zeros[2][0],zeros[2][1],ones[0][0],ones[0][1]]

            if len(ones)==4:
                arr=[ones[0][0],ones[0][1],ones[1][0],ones[1][1],ones[2][0],ones[2][1]]
                ans.append(arr)
                arr = [ones[0][0], ones[0][1], ones[1][0], ones[1][1], ones[3][0], ones[3][1]]
                ans.append(arr)
                arr = [ones[0][0], ones[0][1], ones[2][0], ones[2][1], ones[3][0], ones[3][1]]
                ans.append(arr)
                arr = [ones[1][0], ones[1][1], ones[2][0], ones[2][1], ones[3][0], ones[3][1]]
                ans.append(arr)

    for i in range(len(ans)):
        print(*ans[i])


t=int(input())
while t>0:
    t-=1
    n,m=[int(x) for x in input().split()]
    arr=[]
    for i in range(n):
        s=input()
        a=[]
        for j in range(m):
            a.append(s[j])
        arr.append(a)
    ans=[]
    result=0
    for row in range(n-1):
        for col in range(m-1):
            if arr[row][col]=="1":
                i=row+1;j=col+1
                ans.append([i,j,i,j+1,i+1,j])
                ans.append([i,j,i+1,j,i+1,j+1])
                ans.append([i,j,i,j+1,i+1,j+1])
                result+=3
    for row in range(n-1):
        j=m
        if arr[row][m-1]=="1":
            i=row+1
            ans.append([i,j,i,j-1,i+1,j-1])
            ans.append([i,j-1,i,j,i+1,j])
            ans.append([i,j,i+1,j,i+1,j-1])
            result+=3
    for col in range(m-1):
        if arr[n-1][col]=="1":
            i=n;j=col+1
            ans.append([i,j,i-1,j,i,j+1])
            ans.append([i,j,i,j+1,i-1,j+1])
            ans.append([i,j,i-1,j,i-1,j+1])
            result+=3
    if arr[n-1][m-1]=="1":
        i=n
        j=m
        ans.append([i,j,i-1,j-1,i,j-1])
        ans.append([i,j,i,j-1,i-1,j])
        ans.append([i,j,i-1,j,i-1,j-1])
        result+=3
    print(result)
    for i in range(result):
        print(*ans[i])


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


import math
t = int(input())

for case in range(t):
    n , m = input().split()
    n, m = int(n), int(m)
    mat = []
    for i in range(n):
        l = [int(i) for i in input()]
        mat.append(l)

    '''
    oper = 0
    for i in range(0, n-1):
        for j in range(0,m-1):
            if mat[i][j]==1 and mat[i+1][j]==1 and mat[i][j+1]==1 and mat[i+1][j+1]==0:
                mat[i][j]=mat[i+1][j]=mat[i][j+1]='T'
                oper+=1
            elif mat[i][j]==1 and mat[i+1][j]==1 and mat[i+1][j+1]==1 and mat[i][j+1]==0:
                mat[i][j]=mat[i+1][j]=mat[i+1][j+1]='T'
                oper+=1
            elif mat[i+1][j]==1 and mat[i][j+1]==1 and mat[i+1][j+1]==1 and mat[i][j]==0:
                mat[i+1][j]=mat[i][j+1]=mat[i+1][j+1]='T'
                oper+=1
            elif mat[i][j+1]==1 and mat[i][j]==1 and mat[i+1][j+1]==1 and mat[i+1][j]==0:
                mat[i][j+1]=mat[i][j]=mat[i+1][j+1]='T'
                oper+=1

    count1 = 0

    for i in range(0, n):
        for j in range(0,m):
            if mat[i][j]==1:
                count1+=1
    if count1==0:
        print(oper)
        for i in range(0, n-1):
            for j in range(0,m-1):
                if mat[i][j]=='T' and mat[i+1][j]=='T' and mat[i][j+1]=='T' and mat[i+1][j+1]==0:
                    print(i+1,j+1,i+1+1,j+1,i+1,j+1+1)
                elif mat[i][j]=='T' and mat[i+1][j]=='T' and mat[i+1][j+1]=='T' and mat[i][j+1]==0:
                    print(i+1,j+1,i+1+1,j+1,i+1+1,j+1+1)
                elif mat[i+1][j]=='T' and mat[i][j+1]=='T' and mat[i+1][j+1]=='T' and mat[i][j]==0:
                    print(i+1+1,j+1,i+1,j+1+1,i+1+1,j+1+1)
                elif mat[i][j+1]=='T' and mat[i][j]=='T' and mat[i+1][j+1]=='T' and mat[i+1][j]==0:
                    print(i+1,j+1+1,i+1,j+1,i+1+1,j+1+1)

    print(mat)
    print()
    '''
    for i in range(0, n):
        for j in range(0,m):
            if 0<=j<=m-2 and 0<=i<=n-2:
                if mat[i][j]==1:
                    mat[i][j]^=1
                    mat[i][j+1]^=1
                    mat[i+1][j]^=1
                    print(i+1,j+1,i+1,j+1+1,i+1+1, j+1)

                    mat[i][j]^=1
                    mat[i+1][j+1]^=1
                    mat[i+1][j+1]^=1
                    print(i+1,j+1,i+1+1,j+1,i+1+1, j+1+1)

                    mat[i][j]^=1
                    mat[i][j+1]^=1
                    mat[i+1][j+1]^=1
                    print(i+1,j+1,i+1,j+1+1,i+1+1, j+1+1)

            elif i==m-1 and j==n-1:
                if mat[i][j]==1:
                    mat[i][j]^=1
                    mat[i-1][j]^=1
                    mat[i][j-1]^=1
                    print(i+1,j+1,i-1+1,j+1,i+1, j-1+1)

                    mat[i][j]^=1
                    mat[i-1][j]^=1
                    mat[i-1][j-1]^=1
                    print(i+1,j+1,i-1+1,j+1,i-1+1, j-1+1)

                    mat[i][j]^=1
                    mat[i][j-1]^=1
                    mat[i-1][j-1]^=1
                    print(i+1,j+1,i+1,j-1+1,i-1+1, j-1+1)

            elif i==n-1:
                if mat[i][j]==1:
                    mat[i][j]^=1
                    mat[i-1][j]^=1
                    mat[i][j+1]^=1
                    print(i+1,j+1,i-1+1,j+1,i+1, j+1+1)

                    mat[i][j]^=1
                    mat[i-1][j]^=1
                    mat[i-1][j+1]^=1
                    print(i+1,j+1,i-1+1,j+1,i-1+1, j+1+1)

                    mat[i][j]^=1
                    mat[i][j+1]^=1
                    mat[i-1][j+1]^=1
                    print(i+1,j+1,i+1,j+1+1,i-1+1, j+1+1)
            elif j==m-1:
                if mat[i][j]==1:
                    mat[i][j]^=1
                    mat[i+1][j]^=1
                    mat[i][j-1]^=1
                    print(i+1,j+1,i+1+1,j+1,i+1, j-1+1)

                    mat[i][j]^=1
                    mat[i][j-1]^=1
                    mat[i+1][j-1]^=1
                    print(i+1,j+1,i+1,j-1+1,i+1+1, j+1)

                    mat[i][j]^=1
                    mat[i+1][j]^=1
                    mat[i+1][j-1]^=1
                    print(i+1,j+1,i+1+1,j+1,i+1+1, j-1+1)


//dont try to understand my code i really dont understand it
#include<iostream>
#include<fstream>
#include<cmath>
#include<string>
#include<set>
#include <map>
#include<vector>
#include <queue>
#include <stack>
#include<algorithm>
using namespace std;
typedef long long ll;
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define pb push_back
#define mp make_pair
#define mt make_tuple
typedef pair<int, int> ii;
const double PI = acos(-1.0);
const ll INF = 1000000000000030200;
//const ll F = LLONG_MIN;
const int A = 1234567;
typedef long long ll;
// write what you need here : ...

char out[1000002];
bool vis[1000002];
int mx = 0;

int main()
{
	for (int i = 0;i <= 1000002;i++)
	{
		out[i] = 'a';
	}
	int a;
	cin >> a;
	while (a--)
	{
		string s;
		int k;
		cin >>  s >> k;
		while (k--)
		{
			int temp;

			cin >>  temp;
			temp--;
			//cout << "as" << endl;
			//cout << "k " << k << endl;
			for (int i = temp , d = 0; d <  s.length(); i++, d++)
			{
				//cout << "sad" << endl;
				if (!vis[i])
				{
					out[i] = s[d];
					vis[i] = true;
					if (mx < i)
						mx = i;
				}
			}
		}
	}

	for (int i = 0;i <= mx;i++)
	{
			cout << out[i];
	}

	return 0;
}


//dont try to understand my code i really dont understand it
#include<iostream>
#include<fstream>
#include<cmath>
#include<string>
#include<set>
#include <map>
#include<vector>
#include <queue>
#include <stack>
#include<algorithm>
using namespace std;
typedef long long ll;
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define pb push_back
#define mp make_pair
#define mt make_tuple
typedef pair<int, int> ii;
const double PI = acos(-1.0);
const ll INF = 1000000000000030200;
//const ll F = LLONG_MIN;
const int A = 1234567;
typedef long long ll;
// write what you need here : ...

char out[1000022];
bool vis[1000022];
ll mx = 0;

int main()
{
	for (int i = 0;i <= 1000002;i++)
	{
		out[i] = 'a';
	}
	int a;
	cin >> a;
	while (a--)
	{
		string s;
		int k;
		cin >>  s >> k;
		while (k--)
		{
			ll temp;

			cin >>  temp;
			temp--;
			//cout << "as" << endl;
			//cout << "k " << k << endl;
			if (mx < temp + s.size())
					mx = temp + s.size;
			for (ll i = temp , d = 0; d <  s.size(); i++, d++)
			{
				//cout << "sad" << endl;
				if (!vis[i])
				{
					out[i] = s[d];
					vis[i] = true;
				}

			}
		}
	}

	for (int i = 0;i < mx;i++)
	{
			cout << out[i];
	}

	return 0;
}


//dont try to understand my code i really dont understand it
#include<iostream>
#include<fstream>
#include<cmath>
#include<string>
#include<set>
#include <map>
#include<vector>
#include <queue>
#include <stack>
#include<algorithm>
using namespace std;
typedef long long ll;
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define pb push_back
#define mp make_pair
#define mt make_tuple
typedef pair<int, int> ii;
const double PI = acos(-1.0);
const ll INF = 1000000000000030200;
//const ll F = LLONG_MIN;
const int A = 1234567;
typedef long long ll;
// write what you need here : ...

char out[2000022];

ll mx = 0;

int main()
{
	memset(out, 'a', sizeof(out));
	int a;
	cin >> a;
	while (a--)
	{
		string s;
		int k;
		cin >>  s >> k;
		while (k--)
		{
			ll temp;

			cin >>  temp;
			temp--;
			//cout << "as" << endl;
			//cout << "k " << k << endl;
			if (mx < temp + s.size())
					mx = temp + s.size();
			for (ll i = temp , d = 0; d <  s.size(); i++, d++)
			{
				//cout << "sad" << endl;
					out[i] = s[d];
			}
		}
	}

	for (int i = 0;i < mx;i++)
	{
			cout << out[i];
	}

	return 0;
}


//dont try to understand my code i really dont understand it
#include <iostream>
#include <stdio.h>
#include <string>
#include <memory.h>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <time.h>
#include <assert.h>
#include <cmath>
#include <stack>
#include <string.h>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long ll;
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define pb push_back
#define mp make_pair
#define mt make_tuple
typedef pair<int, int> ii;
const double PI = acos(-1.0);
const ll INF = 1000000000000030200;
//const ll F = LLONG_MIN;
const int A = 1234567;
typedef long long ll;
// write what you need here : ...

char out[2000022];

ll mx = 0;

int main()
{
	memset(out, 'a', sizeof(out));
	int a;
	cin >> a;
	while (a--)
	{
		string s;
		int k;
		cin >>  s >> k;
		while (k--)
		{
			ll temp;

			cin >>  temp;
			temp--;
			//cout << "as" << endl;
			//cout << "k " << k << endl;
			if (mx < temp + s.size())
					mx = temp + s.size();
			for (ll i = temp , d = 0; d <  s.size(); i++, d++)
			{
				//cout << "sad" << endl;
					out[i] = s[d];
			}
		}
	}

	for (int i = 0;i < mx;i++)
	{
			cout << out[i];
	}

	return 0;
}


//dont try to understand my code i really dont understand it
#include<iostream>
#include<fstream>
#include<cmath>
#include<string>
#include<set>
#include <map>
#include<vector>
#include <queue>
#include <stack>
#include<algorithm>
using namespace std;
typedef long long ll;
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define pb push_back
#define mp make_pair
#define mt make_tuple
typedef pair<int, int> ii;
const double PI = acos(-1.0);
const ll INF = 1000000000000030200;
//const ll F = LLONG_MIN;
const int A = 1234567;
typedef long long ll;
// write what you need here : ...

char out[2000002];
set<int>s;
set<int>::iterator it;
int main()
{
	for (int i = 0;i < 2000002;i++)
		s.insert(i);
	int a;
	cin >> a;
	string s1;
	int k, temp;
	int mx = 0;
	for (int i = 0;i < a;i++)
	{
		cin >> s1 >> k;
		while (k--)
		{
			cin >> temp;
			temp--;
			int len = s1.length();
			mx = max(mx, temp + len);
			it = s.lower_bound(temp);
			while (it  != s.end() && *it < temp + len)
			{
				out[*it] = s1[*it - temp];
				s.erase(it++);
			}

		}

	}

	for (int i = 0;i < mx;i++)
		if (out[i] != NULL)
			cout << out[i];
		else
			cout << 'a';
	return 0;
}


//dont try to understand my code i really dont understand it
#include<iostream>
#include<fstream>
#include<cmath>
#include<string>
#include<set>
#include <map>
#include<vector>
#include <queue>
#include <stack>
#include<algorithm>
using namespace std;
typedef long long ll;
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define pb push_back
#define mp make_pair
#define mt make_tuple
typedef pair<int, int> ii;
const double PI = acos(-1.0);
const ll INF = 1000000000000030200;
//const ll F = LLONG_MIN;
const int A = 1234567;
typedef long long ll;
// write what you need here : ...

char out[1000022];
bool vis[1000022];
ll mx = 0;

int main()
{
	for (int i = 0;i <= 1000002;i++)
	{
		out[i] = 'a';
	}
	int a;
	cin >> a;
	while (a--)
	{
		string s;
		int k;
		cin >>  s >> k;
		while (k--)
		{
			ll temp;

			cin >>  temp;
			temp--;
			//cout << "as" << endl;
			//cout << "k " << k << endl;
			if (mx < temp + s.size())
					mx = temp + s.size();
			for (ll i = temp , d = 0; d <  s.size(); i++, d++)
			{
				//cout << "sad" << endl;
					out[i] = s[d];
			}
		}
	}

	for (int i = 0;i < mx;i++)
	{
			cout << out[i];
	}

	return 0;
}


//dont try to understand my code i really dont understand it
#include<iostream>
#include<fstream>
#include<cmath>
#include<string>
#include<set>
#include <map>
#include<vector>
#include <queue>
#include <stack>
#include<algorithm>
using namespace std;
typedef long long ll;
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define pb push_back
#define mp make_pair
#define mt make_tuple
typedef pair<int, int> ii;
const double PI = acos(-1.0);
const ll INF = 1000000000000030200;
//const ll F = LLONG_MIN;
const int A = 1234567;
typedef long long ll;
// write what you need here : ...

char out[1000002];
bool vis[1000002];
int mx = 0;

int main()
{
	for (int i = 0;i <= 1000002;i++)
	{
		out[i] = 'a';
	}
	int a;
	cin >> a;
	while (a--)
	{
		string s;
		int k;
		cin >>  s >> k;
		while (k--)
		{
			int temp;

			cin >>  temp;
			temp--;
			//cout << "as" << endl;
			//cout << "k " << k << endl;
			for (int i = temp , d = 0; i < temp + s.length(); i++, d++)
			{
				//cout << "sad" << endl;
				if (!vis[i])
				{
					out[i] = s[d];
					vis[i] = true;
					if (mx < i)
						mx = i;
				}
			}
		}
	}

	for (int i = 0;i <= mx;i++)
	{
			cout << out[i];
	}

	return 0;
}


//dont try to understand my code i really dont understand it
#include<iostream>
#include<fstream>
#include<cmath>
#include<string>
#include<set>
#include <map>
#include<vector>
#include <queue>
#include <stack>
#include<algorithm>
using namespace std;
typedef long long ll;
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define pb push_back
#define mp make_pair
#define mt make_tuple
typedef pair<int, int> ii;
const double PI = acos(-1.0);
const ll INF = 1000000000000030200;
//const ll F = LLONG_MIN;
const int A = 1234567;
typedef long long ll;
// write what you need here : ...

char out[2000002];
set<int>s;
set<int>::iterator it;
int main()
{
	for (int i = 0;i < 2000002;i++)
		s.insert(i);
	int a;
	cin >> a;
	string s1;
	int k, temp;
	int mx = 0;
	for (int i = 0;i < a;i++)
	{
		cin >> s1 >> k;
		while (k--)
		{
			cin >> temp;
			temp--;
			int len = s1.length();
			mx = max(mx, temp);
			it = s.lower_bound(temp);
			while (it  != s.end() && *it < temp + len)
			{
				out[*it] = s1[*it - temp];
				s.erase(it++);
			}

		}

	}

	for (int i = 0;i < mx;i++)
		if (!out[i])
			out[i] = 'a';
	printf("%s\n", out);
	return 0;
}


//dont try to understand my code i really dont understand it
#include<iostream>
#include<fstream>
#include<cmath>
#include<string>
#include<set>
#include <map>
#include<vector>
#include <queue>
#include <stack>
#include<algorithm>
using namespace std;
typedef long long ll;
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define pb push_back
#define mp make_pair
#define mt make_tuple
typedef pair<int, int> ii;
const double PI = acos(-1.0);
const ll INF = 1000000000000030200;
//const ll F = LLONG_MIN;
const int A = 1234567;
typedef long long ll;
// write what you need here : ...

char out[1000002];
bool vis[1000002];
int mx = 0;

int main()
{
	memset(out, 'a', sizeof(out));
	int a;
	cin >> a;
	while (a--)
	{
		string s;
		int k;
		cin >>  s >> k;
		while (k--)
		{
			int temp;

			cin >>  temp;
			temp--;
			//cout << "as" << endl;
			//cout << "k " << k << endl;
			for (int i = temp , d = 0; i < temp + s.length(); i++, d++)
			{
				//cout << "sad" << endl;
				if (!vis[i])
				{
					out[i] = s[d];
					vis[i] = true;
					if (mx < i)
						mx = i;
				}
			}
		}
	}

	for (int i = 0;i <= mx;i++)
	{
		cout << out[i];
	}

	return 0;
}


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

#define BG begin()
#define ED end()
#define st first
#define nd second
#define PB push_back
#define PF push_front
#define FOR(i,a,b) for (long long i=a;i<b;i++)
#define FORE(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (long long i=a;i>=b; i--)
#define TR(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define ri(n)({\
    int neg=0;\
    n=0;\
    char ch;\
    for(ch=getchar(); ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') neg=1-neg;\
    n=ch-48;\
    for(ch=getchar(); ch>='0' && ch<='9'; ch=getchar()) n=(n<<3)+(n<<1)+ch-48;\
    if (neg) n=-n;\
})

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> II;
typedef pair<ll,ll> LL;
typedef pair<int,II> iII;
typedef pair<II,int> IIi;
const ll INF=1000000000+7;
const double esp=1e-13;
const ld PI = acos(-1.0);

ll nhan(ll x, ll y, ll m){
    ll ans=0;
    while (y){
        if (y%2) ans=(ans+x)%m;
        x=x*2%m;
        y/=2;
    }
    return ans;
}

ll power(ll a, ll n, ll m){
    ull ans=1;
    while (n){
        if (n%2) ans=ans*a%m;
        a=a*a%m;
        n/=2;
    }
    return ans;
}

bool check(ll n){
    if (n==2) return true;
    if (n<2 || n%2==0) return false;
    ll p[3]={3,5,7};
    ll a,d;
    int r,s;
    d=n-1;
    s=0;
    while (d%2==0){
        d/=2;
        s++;
    }
    FORE(i,0,2){
        if (n==p[i]) return true;
        if (n%p[i]==0) return false;
        a=power(p[i],d,n);
        if (a!=1){
            for (r=0; r<s && a!=n-1; r++) a=a*a%n;
            if (r==s) return false;
        }
    }
    return true;
}

ll phi(ll n){
    ll ans=n;
    ll n2=(ll)sqrt(n);
    FORE(i,2,n2)
    if (n%i==0){
        while (n%i==0) n/=i;
        ans-=ans/i;
    }
    if (n>1) ans-=ans/n;
    return ans;
}

const int maxn = 2000000+10;

int test,n,pos,len;
int ans[maxn];
int k[maxn];
int pre[maxn];
int ne[maxn];
string s[maxn];
vector<int> a[maxn];
int b[maxn];

void solve(int x, int y){
    ans[x] = b[0];
    int u = ne[x];
 //   debug(x);
    while (u <= y){
      //  cout << u << "    ";
        ans[u] = b[ u - x ];
        ne[ pre[u] ] = ne[u];
        pre[ ne[u] ] = pre[u];
        u = ne[u];
    }
  //  cout << "\n";
}

int main(){
 //   freopen("codeforces.inp", "r", stdin);
 //   freopen(".out", "w", stdout);
    scanf("%d", &test);
    int n = 0;
    FORE(i,1,test){
   //     cin >> s[i] >> k[i];
        cin >> s[i];
        scanf("%d", &k[i]);
        len = s[i].length();
        FORE(j,1,k[i]){
            //cin >> pos;
            scanf("%d", &pos);
            a[i].PB(pos);
            n = max(n, pos + len - 1);
        }
    }
    FORE(i,1,n){
        pre[i] = i - 1;
        ne[i] = i + 1;
    }
    FORE(i,1,test){
        len = s[i].length();
        FOR(j,0,len) b[j] = s[i][j];
        FOR(j,0,k[i]){
            pos = a[i][j];
            solve(pos, pos + len - 1);
        }
    }
    string res = "";
    FORE(i,1,n){
     //   if (ans[i] == 0) cout << "a";
     //   else cout << (char) ans[i];
        if (ans[i] == 0) res+='a';
        else res+=(char)ans[i];
    }
    cout << res;
}


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

#define BG begin()
#define ED end()
#define st first
#define nd second
#define PB push_back
#define PF push_front
#define FOR(i,a,b) for (long long i=a;i<b;i++)
#define FORE(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (long long i=a;i>=b; i--)
#define TR(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define ri(n)({\
    int neg=0;\
    n=0;\
    char ch;\
    for(ch=getchar(); ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') neg=1-neg;\
    n=ch-48;\
    for(ch=getchar(); ch>='0' && ch<='9'; ch=getchar()) n=(n<<3)+(n<<1)+ch-48;\
    if (neg) n=-n;\
})

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> II;
typedef pair<ll,ll> LL;
typedef pair<int,II> iII;
typedef pair<II,int> IIi;
const ll INF=1000000000+7;
const double esp=1e-13;
const ld PI = acos(-1.0);

ll nhan(ll x, ll y, ll m){
    ll ans=0;
    while (y){
        if (y%2) ans=(ans+x)%m;
        x=x*2%m;
        y/=2;
    }
    return ans;
}

ll power(ll a, ll n, ll m){
    ull ans=1;
    while (n){
        if (n%2) ans=ans*a%m;
        a=a*a%m;
        n/=2;
    }
    return ans;
}

bool check(ll n){
    if (n==2) return true;
    if (n<2 || n%2==0) return false;
    ll p[3]={3,5,7};
    ll a,d;
    int r,s;
    d=n-1;
    s=0;
    while (d%2==0){
        d/=2;
        s++;
    }
    FORE(i,0,2){
        if (n==p[i]) return true;
        if (n%p[i]==0) return false;
        a=power(p[i],d,n);
        if (a!=1){
            for (r=0; r<s && a!=n-1; r++) a=a*a%n;
            if (r==s) return false;
        }
    }
    return true;
}

ll phi(ll n){
    ll ans=n;
    ll n2=(ll)sqrt(n);
    FORE(i,2,n2)
    if (n%i==0){
        while (n%i==0) n/=i;
        ans-=ans/i;
    }
    if (n>1) ans-=ans/n;
    return ans;
}

const int maxn = 2000000+10;

int test,n,pos;
int ans[maxn];
int vt[maxn];
int k[maxn];
int pre[maxn];
int ne[maxn];
string s[maxn];
vector<int> a[maxn];

void solve(int x, int y, int index){
   // cout << x << " " << y << "\n";
    ans[x] = index;
    vt[x] = 1;
    int u = ne[x];
    while (u <= y){
        ans[u] = index;
        vt[u] = u - x + 1;
        ne[ pre[u] ] = ne[u];
        pre[ ne[u] ] = pre[u];
        u = ne[u];
    }
}

int main(){
 //   freopen("codeforces.inp", "r", stdin);
 //   freopen(".out", "w", stdout);
    cin >> test;
    int n = 0;
    FORE(i,1,test){
        cin >> s[i] >> k[i];
        int len = s[i].length();
        FORE(j,1,k[i]){
            cin >> pos;
            a[i].PB(pos);
            n = max(n, pos + len - 1);
        }
    }
    FORE(i,1,n){
        pre[i] = i - 1;
        ne[i] = i + 1;
    }
    FORE(i,1,test){
        int len = s[i].length();
        FOR(j,0,k[i]){
            pos = a[i][j];
            solve(pos, pos + len - 1, i);
        }
    }
    FORE(i,1,n){
        if (ans[i] == 0) cout << "a";
        else{
          //  string s2 = s[ ans[i] ];
            cout <<  s[ ans[i] ][vt[i]-1];
        }
    }
}


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

#define BG begin()
#define ED end()
#define st first
#define nd second
#define PB push_back
#define PF push_front
#define FOR(i,a,b) for (long long i=a;i<b;i++)
#define FORE(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (long long i=a;i>=b; i--)
#define TR(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define ri(n)({\
    int neg=0;\
    n=0;\
    char ch;\
    for(ch=getchar(); ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') neg=1-neg;\
    n=ch-48;\
    for(ch=getchar(); ch>='0' && ch<='9'; ch=getchar()) n=(n<<3)+(n<<1)+ch-48;\
    if (neg) n=-n;\
})

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> II;
typedef pair<ll,ll> LL;
typedef pair<int,II> iII;
typedef pair<II,int> IIi;
const ll INF=1000000000+7;
const double esp=1e-13;
const ld PI = acos(-1.0);

ll nhan(ll x, ll y, ll m){
    ll ans=0;
    while (y){
        if (y%2) ans=(ans+x)%m;
        x=x*2%m;
        y/=2;
    }
    return ans;
}

ll power(ll a, ll n, ll m){
    ull ans=1;
    while (n){
        if (n%2) ans=ans*a%m;
        a=a*a%m;
        n/=2;
    }
    return ans;
}

bool check(ll n){
    if (n==2) return true;
    if (n<2 || n%2==0) return false;
    ll p[3]={3,5,7};
    ll a,d;
    int r,s;
    d=n-1;
    s=0;
    while (d%2==0){
        d/=2;
        s++;
    }
    FORE(i,0,2){
        if (n==p[i]) return true;
        if (n%p[i]==0) return false;
        a=power(p[i],d,n);
        if (a!=1){
            for (r=0; r<s && a!=n-1; r++) a=a*a%n;
            if (r==s) return false;
        }
    }
    return true;
}

ll phi(ll n){
    ll ans=n;
    ll n2=(ll)sqrt(n);
    FORE(i,2,n2)
    if (n%i==0){
        while (n%i==0) n/=i;
        ans-=ans/i;
    }
    if (n>1) ans-=ans/n;
    return ans;
}

const int maxn = 1000000+10;

int test,n,pos;
int ans[maxn];
int vt[maxn];
int k[maxn];
int pre[maxn];
int ne[maxn];
string s[maxn];
vector<int> a[maxn];

void solve(int x, int y, int index){
   // cout << x << " " << y << "\n";
    ans[x] = index;
    vt[x] = 1;
    int u = ne[x];
    while (u <= y){
        ans[u] = index;
        vt[u] = u - x + 1;
        ne[ pre[u] ] = ne[u];
        pre[ ne[u] ] = pre[u];
        u = ne[u];
    }
}

int main(){
 //   freopen("codeforces.inp", "r", stdin);
 //   freopen(".out", "w", stdout);
    cin >> test;
    int n = 0;
    FORE(i,1,test){
        cin >> s[i] >> k[i];
        int len = s[i].length();
        FORE(j,1,k[i]){
            cin >> pos;
            a[i].PB(pos);
            n = max(n, pos + len - 1);
        }
    }
    FORE(i,1,n){
        pre[i] = i - 1;
        ne[i] = i + 1;
    }
    FORE(i,1,test){
        int len = s[i].length();
        FOR(j,0,k[i]){
            pos = a[i][j];
            solve(pos, pos + len - 1, i);
        }
    }
    FORE(i,1,n){
        if (ans[i] == 0) cout << "a";
        else{
            cout <<  s[ ans[ vt[i]-1 ] ];
        }
    }
}


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

#define BG begin()
#define ED end()
#define st first
#define nd second
#define PB push_back
#define PF push_front
#define FOR(i,a,b) for (long long i=a;i<b;i++)
#define FORE(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (long long i=a;i>=b; i--)
#define TR(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define ri(n)({\
    int neg=0;\
    n=0;\
    char ch;\
    for(ch=getchar(); ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') neg=1-neg;\
    n=ch-48;\
    for(ch=getchar(); ch>='0' && ch<='9'; ch=getchar()) n=(n<<3)+(n<<1)+ch-48;\
    if (neg) n=-n;\
})

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> II;
typedef pair<ll,ll> LL;
typedef pair<int,II> iII;
typedef pair<II,int> IIi;
const ll INF=1000000000+7;
const double esp=1e-13;
const ld PI = acos(-1.0);

ll nhan(ll x, ll y, ll m){
    ll ans=0;
    while (y){
        if (y%2) ans=(ans+x)%m;
        x=x*2%m;
        y/=2;
    }
    return ans;
}

ll power(ll a, ll n, ll m){
    ull ans=1;
    while (n){
        if (n%2) ans=ans*a%m;
        a=a*a%m;
        n/=2;
    }
    return ans;
}

bool check(ll n){
    if (n==2) return true;
    if (n<2 || n%2==0) return false;
    ll p[3]={3,5,7};
    ll a,d;
    int r,s;
    d=n-1;
    s=0;
    while (d%2==0){
        d/=2;
        s++;
    }
    FORE(i,0,2){
        if (n==p[i]) return true;
        if (n%p[i]==0) return false;
        a=power(p[i],d,n);
        if (a!=1){
            for (r=0; r<s && a!=n-1; r++) a=a*a%n;
            if (r==s) return false;
        }
    }
    return true;
}

ll phi(ll n){
    ll ans=n;
    ll n2=(ll)sqrt(n);
    FORE(i,2,n2)
    if (n%i==0){
        while (n%i==0) n/=i;
        ans-=ans/i;
    }
    if (n>1) ans-=ans/n;
    return ans;
}

set<int> ss;
int test,n,k,pos;
int ans[1000000+10];
int vt[1000000+10];
string s[1000000+10];

void solve(int x, int y, int index){
   // cout << x << " " << y << "\n";
    set<int>::iterator it;
    while (true){
        it = ss.lower_bound(x);
        if (it!=ss.ED){
            if ( x <= *it && *it <=y ){
                ans[ *it ] = index;
                vt[*it] = (*it) - x + 1;
                ss.erase(it);
            }
            else break;
        }
        else break;
    }
}

int main(){
 //   freopen("codeforces.inp", "r", stdin);
 //   freopen(".out", "w", stdout);
    ios_base::sync_with_stdio (false);
    cin.tie();
    cin >> test;
    int n = 0;
    FORE(i,1,1000000) ss.insert(i);
    FORE(i,1,test){
        cin >> s[i] >> k;
        int len = s[i].length();
        FORE(j,1,k){
            cin >> pos;
            n = max(n, pos + len - 1);
            solve(pos, pos + len - 1, i);
        }
    }
  //  cout << n << "\n";
  //  FORE(i,1,n) cout << ans[i] << " ";
  //  FORE(i,1,n) cout << vt[i] << " ";
    FORE(i,1,n){
        if (ans[i] == 0) cout << "a";
        else{
            string s2 = s[ ans[i] ];
            cout << s2[vt[i]-1];
        }
    }
}


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

#define BG begin()
#define ED end()
#define st first
#define nd second
#define PB push_back
#define PF push_front
#define FOR(i,a,b) for (long long i=a;i<b;i++)
#define FORE(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (long long i=a;i>=b; i--)
#define TR(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define ri(n)({\
    int neg=0;\
    n=0;\
    char ch;\
    for(ch=getchar(); ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') neg=1-neg;\
    n=ch-48;\
    for(ch=getchar(); ch>='0' && ch<='9'; ch=getchar()) n=(n<<3)+(n<<1)+ch-48;\
    if (neg) n=-n;\
})

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> II;
typedef pair<ll,ll> LL;
typedef pair<int,II> iII;
typedef pair<II,int> IIi;
const ll INF=1000000000+7;
const double esp=1e-13;
const ld PI = acos(-1.0);

ll nhan(ll x, ll y, ll m){
    ll ans=0;
    while (y){
        if (y%2) ans=(ans+x)%m;
        x=x*2%m;
        y/=2;
    }
    return ans;
}

ll power(ll a, ll n, ll m){
    ull ans=1;
    while (n){
        if (n%2) ans=ans*a%m;
        a=a*a%m;
        n/=2;
    }
    return ans;
}

bool check(ll n){
    if (n==2) return true;
    if (n<2 || n%2==0) return false;
    ll p[3]={3,5,7};
    ll a,d;
    int r,s;
    d=n-1;
    s=0;
    while (d%2==0){
        d/=2;
        s++;
    }
    FORE(i,0,2){
        if (n==p[i]) return true;
        if (n%p[i]==0) return false;
        a=power(p[i],d,n);
        if (a!=1){
            for (r=0; r<s && a!=n-1; r++) a=a*a%n;
            if (r==s) return false;
        }
    }
    return true;
}

ll phi(ll n){
    ll ans=n;
    ll n2=(ll)sqrt(n);
    FORE(i,2,n2)
    if (n%i==0){
        while (n%i==0) n/=i;
        ans-=ans/i;
    }
    if (n>1) ans-=ans/n;
    return ans;
}

const int maxn = 1500000+10;

int test,n,pos,len;
int ans[maxn];
int k[maxn];
int pre[maxn];
int ne[maxn];
string s[maxn];
vector<int> a[maxn];
int b[maxn];

void solve(int x, int y, string s){
    FOR(i,0,len) b[i] = s[i];
   // cout << x << " " << y << "\n";
    ans[x] = s[0];
    int u = ne[x];
    while (u <= y){
        ans[u] = b[ u - x ];
        ne[ pre[u] ] = ne[u];
        pre[ ne[u] ] = pre[u];
        u = ne[u];
    }
}

int main(){
 //   freopen("codeforces.inp", "r", stdin);
 //   freopen(".out", "w", stdout);
    ios_base::sync_with_stdio (false);
    cin.tie();
    cin >> test;
    int n = 0;
    FORE(i,1,test){
        cin >> s[i] >> k[i];
        len = s[i].length();
        FORE(j,1,k[i]){
            cin >> pos;
            a[i].PB(pos);
            n = max(n, pos + len - 1);
        }
    }
    FORE(i,1,n){
        pre[i] = i - 1;
        ne[i] = i + 1;
    }
    FORE(i,1,test){
        len = s[i].length();
        FOR(j,0,k[i]){
            pos = a[i][j];
            solve(pos, pos + len - 1, s[i]);
        }
    }
    FORE(i,1,n){
        if (ans[i] == 0) cout << "a";
        else cout << (char) ans[i];
    }
}