#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
typedef vector<ll> vl;
#define pan cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);

#define phi 1.618

#define ff first
#define ss second
#define mp make_pair
#define line cout<<endl;
#define pb push_back
#define Endl "\n"

#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];

#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define cc ll test;cin>>test;while(test--)

#define all(V) (V).begin(),(V).end()
#define srt(V) sort(all(V))
#define srtGreat(V) sort(all(V),greater<ll>())

ll power(ll x,ll y,ll mod)
{
    ll res=1;

    while(y>0)
    {
        if(y%2==1)
        {
            res*=x;

        }
        y/=2; x*=x; 
    }
    return res;
}

#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>

ll solve()
{
    ll n,m;
    cin>>n>>m;
    vector<vector<char>> v(n,vector<char>(m));
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            cin>>v[i][j];
        }
    }
    vector<vector<ll>> ans;
    for(ll i=n-1;i>=0;i--)
    {
        for(ll j=m-1;j>=0;j--)
        {
            if(i>=1&&j>=1)
            {
                if(v[i][j]=='1'&&v[i][j-1]=='1')
                {
                    ans.push_back({i+1,j+1,i+1,j,i,j+1});
                    v[i][j]='0';
                    v[i][j-1]='0';
                    if(v[i-1][j]=='0')
                        v[i-1][j]='1';
                    else
                        v[i-1][j]='0';

                }
                else if(v[i][j]=='1'&&v[i][j-1]=='0')
                {
                    ans.push_back({(i+1),(j+1),(i),(j+1),(i),(j)});

                    if(v[i-1][j-1]=='0')
                        v[i-1][j-1]='1';
                    else
                        v[i-1][j-1]='0';
                    v[i][j]='0';

                    if(v[i-1][j]=='0')
                        v[i-1][j]='1';
                    else
                        v[i-1][j]='0';
                }
                else if(v[i][j]=='0'&&v[i][j-1]=='1')
                {
                    ans.push_back({(i+1),(j),(i),(j+1),(i),(j)});

                    if(v[i-1][j-1]=='0')
                        v[i-1][j-1]='1';
                    else
                        v[i-1][j-1]='0';
                    v[i][j-1]='0';
                    if(v[i-1][j]=='0')
                        v[i-1][j]='1';
                    else
                        v[i-1][j]='0';

                }
            }
        }
    }

    for(ll i=m-1;i>0;i--)
    {
        if(i>=1)
        {
            if(v[0][i]=='1'&&v[0][i-1]=='1')
            {
                ans.push_back({(1),(i+1),(2),(i+1),(2),(i)});
                ans.pb({1,(i),(2),(i),(2),(i+1)});
                v[0][i]='0';
                v[0][i-1]='0';
            }
            else if(v[0][i]=='1'&&v[0][i-1]=='0')
            {
                ans.pb({1,(i+1),1,(i),2,i});
                ans.pb({1,(i+1),1,(i),2,(i+1)});
                ans.pb({1,(i+1),2,i,2,(i+1)});
                v[0][i]='0';
                v[0][i-1]='0';
            }
            else if(v[0][i]=='0'&&v[0][i-1]=='1')
            {
                ans.pb({1,(i+1),1,(i),2,i});
                ans.pb({1,(i+1),1,i,2,(i+1)});
                ans.pb({1,i,2,i,2,(i+1)});
                v[0][i]='0';
                v[0][i-1]='0';
            }
        }
    }
    ll count=ans.size();
    cout<<count<<Endl;
    for(ll i=0;i<count;i++)
    {
        for(ll j=0;j<6;j++)
        {
            cout<<ans[i][j]<<" ";
        }
        line;
    }

    return 0;
}

int main()
{

    pan;

    cc
    {
        solve();
    }
    return 0;
}
/*
    written by Pankaj Kumar.
    country:-INDIA
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
typedef vector<ll> vl;
#define pan cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
// define values.
// #define mod 1000000007
#define phi 1.618
/*  Abbrevations  */
#define ff first
#define ss second
#define mp make_pair
#define line cout<<endl;
#define pb push_back
#define Endl "\n"
// loops
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
// Some print
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define cc ll test;cin>>test;while(test--)
// sort
#define all(V) (V).begin(),(V).end()
#define srt(V) sort(all(V))
#define srtGreat(V) sort(all(V),greater<ll>())
// function

ll power(ll x,ll y,ll mod)
{
    ll res=1;
    // x=x%mod;
    while(y>0)
    {
        if(y%2==1)
        {
            res*=x;
            // res=res%mod;
        }
        y/=2; x*=x; // x=x%mod;
    }
    return res;
}
// datatype definination
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>

/* ascii value 
A=65,Z=90,a=97,z=122
*/
/*  -----------------------------------------------------------------------------------*/


ll solve()
{
    ll n,m;
    cin>>n>>m;
    vector<vector<char>> v(n,vector<char>(m));
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            cin>>v[i][j];
        }
    }
    vector<vector<ll>> ans;
    for(ll i=n-1;i>=0;i--)
    {
        for(ll j=m-1;j>=0;j--)
        {
            if(i>=1&&j>=1)
            {
                if(v[i][j]=='1'&&v[i][j-1]=='1')
                {
                    ans.push_back({i+1,j+1,i+1,j,i,j+1});
                    v[i][j]='0';
                    v[i][j-1]='0';
                    if(v[i-1][j]=='0')
                        v[i-1][j]='1';
                    else
                        v[i-1][j]='0';
                    // v[i-1][j]^=1
                }
                else if(v[i][j]=='1'&&v[i][j-1]=='0')
                {
                    ans.push_back({(i+1),(j+1),(i),(j+1),(i),(j)});
                    // v[i-1][j-1]^=1;
                    if(v[i-1][j-1]=='0')
                        v[i-1][j-1]='1';
                    else
                        v[i-1][j-1]='0';
                    v[i][j]='0';
                    // v[i-1][j]^=1;
                    if(v[i-1][j]=='0')
                        v[i-1][j]='1';
                    else
                        v[i-1][j]='0';
                }
                else if(v[i][j]=='0'&&v[i][j-1]=='1')
                {
                    ans.push_back({(i+1),(j),(i),(j+1),(i),(j)});
                    // v[i-1][j-1]^=1;
                    if(v[i-1][j-1]=='0')
                        v[i-1][j-1]='1';
                    else
                        v[i-1][j-1]='0';
                    v[i][j-1]='0';
                    if(v[i-1][j]=='0')
                        v[i-1][j]='1';
                    else
                        v[i-1][j]='0';
                    // v[i-1][j]^=1;
                }
            }
        }
    }

    for(ll i=m-1;i>0;i--)
    {
        if(i>=1)
        {
            if(v[0][i]=='1'&&v[0][i-1]=='1')
            {
                ans.push_back({(1),(i+1),(2),(i+1),(2),(i)});
                ans.pb({1,(i),(2),(i),(2),(i+1)});
                v[0][i]='0';
                v[0][i-1]='0';
            }
            else if(v[0][i]=='1'&&v[0][i-1]=='0')
            {
                ans.pb({1,(i+1),1,(i),2,i});
                ans.pb({1,(i+1),1,(i),2,(i+1)});
                ans.pb({1,(i+1),2,i,2,(i+1)});
                v[0][i]='0';
                v[0][i-1]='0';
            }
            else if(v[0][i]=='0'&&v[0][i-1]=='1')
            {
                ans.pb({1,(i+1),1,(i),2,i});
                ans.pb({1,(i+1),1,i,2,(i+1)});
                ans.pb({1,i,2,i,2,(i+1)});
                v[0][i]='0';
                v[0][i-1]='0';
            }
        }
    }
    ll count=ans.size();
    cout<<count<<Endl;
    for(ll i=0;i<count;i++)
    {
        for(ll j=0;j<6;j++)
        {
            cout<<ans[i][j]<<" ";
        }
        line;
    }
    
    // cout<<"matrix is "<<endl;
    // for(ll i=0;i<n;i++)
    // {
    //     for(ll j=0;j<m;j++)
    //     {
    //         cout<<v[i][j]<<" ";
    //     }
    //     line;
    // }
    return 0;
}

int main()
{
    //freopen("input.txt"a, "r", stdin);
    pan;
    // solve();
    cc
    {
        solve();
    }
    return 0;
}

/* stuff you should look before submission 
* int overflow
* special test case (n=0||n=1||n=2)
* don't get stuck on one approach if you get wrong answer
*/