#include<bits/stdc++.h>

using namespace std;

int main()
{
    //freopen("000.txt","r",stdin);
    int n,p,q,i,j,k,l=0;
    string s;
    cin>>n>>p>>q>>s;

    for(i=0;i<=n;i++)
    {
        if((n-i*p)%q==0)
            break;
    }
    if(i>n || n-i*p<0)
    {
        cout<<-1;
        return 0;
    }
    cout<<i+(n-i*p)/q<<endl;
    l=0;
    for(j=0;j<i;j++)
    {
        for(k=0;k<p;k++)
            cout<<s[l++];
        cout<<endl;
    }
    for(;l<n;)
    {
        for(k=0;k<q;k++)
            cout<<s[l++];
        cout<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,p,q,i,j,k,l;
    string s;
    cin>>n>>p>>q>>s;

    for(i=0;i<=n;i++)
    {
        if((n-i*p)%q==0)
            break;
    }

    if(i>n || n-i*p<0)
    {
        cout<<-1;
        return 0;
    }

    cout<<i+(n-i)/q<<endl;
    l=0;
    for(j=0;j<i;j++)
    {
        for(k=0;k<p;k++)
            cout<<s[l++];
        cout<<endl;
    }
    for(;l<n;)
    {
        for(k=0;k<q;k++)
            cout<<s[l++];
        cout<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,p,q,i,j,k,l;
    string s;
    cin>>n>>p>>q>>s;

    for(i=0;i<=n;i++)
    {
        if((n-i*p)%q==0)
            break;
    }

    if(i>n || n-i*p<0)
    {
        cout<<-1;
        return 0;
    }

    cout<<i+(n-i)/q<<endl;

    l=0;
    for(j=0;j<i;j++)
    {
        for(k=0;k<p;k++)
            cout<<s[l++];
        cout<<endl;
    }
    for(;l<n;)
    {
        for(k=0;k<q;k++)
            cout<<s[l++];
        cout<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    //freopen("000.txt","r",stdin);
    int n,p,q,i,j,k,l;
    string s;
    cin>>n>>p>>q>>s;

    for(i=0;i<=n;i++)
    {
        if((n-i*p)%q==0)
            break;
    }
    if(i>n || n-i*p<0)
    {
        cout<<-1;
        return 0;
    }
    cout<<i+(n-i*p)/q<<endl;
    l=0;
    for(j=0;j<i;j++)
    {
        for(k=0;k<p;k++)
            cout<<s[l++];
        cout<<endl;
    }
    for(;l<n;)
    {
        for(k=0;k<q;k++)
            cout<<s[l++];
        cout<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,p,q,i,j,k,l;
    string s;
    cin>>n>>p>>q>>s;

    for(i=0;i<=n;i++)
    {
        if((n-i*p)%q==0)
            break;
    }

    if(i>n || n-i*p<0)
    {
        cout<<-1;
        return 0;
    }
    cout<<i+(n-i)/q<<endl;
    l=0;
    for(j=0;j<i;j++)
    {
        for(k=0;k<p;k++)
            cout<<s[l++];
        cout<<endl;
    }
    for(;l<n;)
    {
        for(k=0;k<q;k++)
            cout<<s[l++];
        cout<<endl;
    }

    return 0;
}


#include <iostream>
using namespace std;
int main(){
	string inp ;
	int n, p, q;
	cin >> n >> p >> q;
	cin >> inp;
	if(n == (p+q)){
		cout << 2 << endl << inp.substr(0, p) << endl << inp.substr(p,q);
	}else if(n % p == 0){
		cout << n/p << endl;
		for(int i = 0 ; i < inp.length() ; i+= p){
			cout << inp.substr(i, p) << endl;
		}
	}else if(n % q == 0){
		cout << n/q << endl;
		for(int i = 0 ; i < inp.length() ; i+= q){
			cout  << inp.substr(i, q) << endl;
		}
	}else if(n % p % q == 0){
		cout << (n/p) + ((n%p)/q) << endl;
		for(int i = 0 ; i < (n/p)*p ; i+= p){
			cout  << inp.substr(i, p) << endl;
		}
		for(int i = (n/p)*p ; i < inp.length() ; i+=q){
			cout  << inp.substr(i, q) << endl;
		}
	}
	else{
		bool flag = false;
		int count =  0;
		int tamp;
		tamp = n;
		for(int i = 0 ; i < inp.length() ; i++){
			tamp -= p;
			count++;
			if(tamp % q == 0){
				cout << count + (tamp/q) << endl;
				for(int i = 0 ; i < p*count ; i+= p){
					cout  << inp.substr(i, p) << endl;
				}
				for(int i = p*count ; i < inp.length() ; i+=q){
					cout  << inp.substr(i, q) << endl;
				}
				flag = true;
				break;
			}
		}
		if(!flag) cout << -1;
	}
}


#include <iostream>
using namespace std;
int main(){
	string inp ;
	int n, p, q;
	cin >> n >> p >> q;
	cin >> inp;
	if(n == (p+q)){
		cout << inp.substr(0, p) << endl << inp.substr(p,q);
	}else if(n % p == 0){
		for(int i = 0 ; i < inp.length() ; i+= p){
			cout << inp.substr(i, p) << endl;
		}
	}else if(n % q == 0){
		for(int i = 0 ; i < inp.length() ; i+= q){
			cout << inp.substr(i, q) << endl;
		}
	}else cout << -1 ;
}


#include <iostream>
using namespace std;
int main(){
	string inp ;
	int n, p, q;
	cin >> n >> p >> q;
	cin >> inp;
	if(n == (p+q)){
		cout << 2 << endl << inp.substr(0, p) << endl << inp.substr(p,q);
	}else if(n % p == 0){
		cout << n/p << endl;
		for(int i = 0 ; i < inp.length() ; i+= p){
			cout << inp.substr(i, p) << endl;
		}
	}else if(n % q == 0){
		cout << n/q << endl;
		for(int i = 0 ; i < inp.length() ; i+= q){
			cout  << inp.substr(i, q) << endl;
		}
	}else cout << -1 ;
}


#include <iostream>
using namespace std;
int main(){
	string inp ;
	int n, p, q;
	cin >> n >> p >> q;
	cin >> inp;
	if(n == (p+q)){
		cout << 2 << endl << inp.substr(0, p) << endl << inp.substr(p,q);
	}else if(n % p == 0){
		cout << n/p << endl;
		for(int i = 0 ; i < inp.length() ; i+= p){
			cout << inp.substr(i, p) << endl;
		}
	}else if(n % q == 0){
		cout << n/q << endl;
		for(int i = 0 ; i < inp.length() ; i+= q){
			cout  << inp.substr(i, q) << endl;
		}
	}else if(n % p % q == 0){
		int tamp;
		cout << (n/p) + ((n%p)/q) << endl;
		for(int i = 0 ; i < (n/p)*p ; i+= p){
			cout  << inp.substr(i, p) << endl;
		}
		for(int i = (n/p)*p ; i < inp.length() ; i+=q){
			cout  << inp.substr(i, q) << endl;
		}
	}
	else cout << -1 ;
}


#include <iostream>
using namespace std;
int main(){
	string inp ;
	int n, p, q;
	cin >> n >> p >> q;
	cin >> inp;
	if(n == (p+q)){
		cout << 2 << endl << inp.substr(0, p) << endl << inp.substr(p,q);
	}else if(n % p == 0){
		cout << n/p << endl;
		for(int i = 0 ; i < inp.length() ; i+= p){
			cout << inp.substr(i, p) << endl;
		}
	}else if(n % q == 0){
		cout << n/q << endl;
		for(int i = 0 ; i < inp.length() ; i+= q){
			cout  << inp.substr(i, q) << endl;
		}
	}else if(n % p % q == 0){
		cout << (n/p) + ((n%p)/q) << endl;
		for(int i = 0 ; i < (n/p)*p ; i+= p){
			cout  << inp.substr(i, p) << endl;
		}
		for(int i = (n/p)*p ; i < inp.length() ; i+=q){
			cout  << inp.substr(i, q) << endl;
		}
	}
	else{
		bool flag = false;
		int count =  0;
		int tamp;
		tamp = n;
		for(int i = 0 ; i < inp.length() ; i++){
			tamp -= p;
			count++;
			if(tamp % q == 0 && tamp >= q){
				cout << count + (tamp/q) << endl;
				for(int i = 0 ; i < p*count ; i+= p){
					cout  << inp.substr(i, p) << endl;
				}
				for(int i = p*count ; i < inp.length() ; i+=q){
					cout  << inp.substr(i, q) << endl;
				}
				flag = true;
				break;
			}
		}
		if(!flag) cout << -1;
	}
}


#include <iostream>
#include <string>
#include <cstdio>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <sstream>
#include <utility>
#include <cmath>
#include <bitset>
#include <cstring>
#include <utility>
#include <set>
using namespace std;
typedef long long ll;
struct num{
    int x;
    int y;
    bool f;
};
num good(int x,int p,int q) {
    num n1;
    int sum = 0;
    for(int i = 1; sum < x; i++) {

        for(int j = 1; sum < x; j++) {
            sum = i*p + q*j;
            if(sum == x) {
                n1.x = i;
                n1.y = j;
                n1.f = true;
                return n1;
            }
        }
        sum = 0;
    }
    n1.x = 0;
    n1.y = 0;
    n1.f = false;
    return n1;
}

int main() {
    int N, p, q;
    string s;
    cin >> N >> p >> q;
    cin >> s;
    num n = good(N,p,q);
    cout << n.x << n.y << endl;
    if (p+q == N) {
        cout << N / 2 << endl;
        while(s.size()>0) {
            for(int j = 0; j < p; j++) {
                cout << s[j];
            }
            s.erase(0,p);
            cout << endl;
            for(int j = 0; j < q; j++) {
                cout << s[j];
            }
            s.erase(0,q);
            cout << endl;
        }

    }

    else if( N % p == 0) {
        cout << N/p << endl;
        while(s.size()>0) {
            for(int j = 0; j < p; j++) {
                cout << s[j];
            }
            s.erase(0,p);
            cout << endl;
        }
    }
    else if( N % q == 0) {
        cout << N/q << endl;
        while(s.size()>0){
            for(int j = 0; j < q; j++) {
                cout << s[j];
            }
            s.erase(0,q);
            cout << endl;
        }
    }
    else if (p+q < N && n.f) {
        cout << n.x + n.y<< endl;
        while(s.size()>0) {
            for(int v = 0; v < n.x; v++) {
                for(int j = 0; j < p; j++) {
                    cout << s[j];
                }
                s.erase(0,p);
                cout << endl;
            }
            for(int v = 0; v < n.y; v++) {
                for(int j = 0; j < q; j++) {
                    cout << s[j];
                }
                s.erase(0,q);
                cout << endl;
            }
        }

    }
    else cout <<-1 << endl;

}


#include <iostream>
#include <string>
#include <cstdio>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <sstream>
#include <utility>
#include <cmath>
#include <bitset>
#include <cstring>
#include <utility>
#include <set>
typedef long long ll;

using namespace std;

int main() {
    int N, p, q;
    string s;
    cin >> N >> p >> q;
    cin >> s;
    if (p+q == N) {
        cout << N / 2 << endl;
        while(s.size()>0) {
            for(int j = 0; j < p; j++) {
                cout << s[j];
            }
            s.erase(0,p);
            cout << endl;
            for(int j = 0; j < q; j++) {
                cout << s[j];
            }
            s.erase(0,q);
            cout << endl;
        }

    }
    else if( N % p == 0) {
        cout << N/p << endl;
        while(s.size()>0) {
            for(int j = 0; j < p; j++) {
                cout << s[j];
            }
            s.erase(0,p);
            cout << endl;
        }
    }
    else if( N % q == 0) {
        cout << N/q << endl;
        while(s.size()>0){
            for(int j = 0; j < q; j++) {
                cout << s[j];
            }
            s.erase(0,q);
            cout << endl;
        }
    }
    else cout <<-1 << endl;

}


#include <iostream>
#include <string>
#include <cstdio>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <sstream>
#include <utility>
#include <cmath>
#include <bitset>
#include <cstring>
#include <utility>
#include <set>
using namespace std;
typedef long long ll;
struct num{
    int x;
    int y;
    bool f;
};
num good(int x,int p,int q) {
    num n1;
    int sum = 0;
    for(int i = 1; sum < x; i++) {

        for(int j = 1; sum < x; j++) {
            sum = i*p + q*j;
            if(sum == x) {
                n1.x = i;
                n1.y = j;
                n1.f = true;
                return n1;
            }
        }
        sum = 0;
    }
    n1.x = 0;
    n1.y = 0;
    n1.f = false;
    return n1;
}

int main() {
    int N, p, q;
    string s;
    cin >> N >> p >> q;
    cin >> s;
    num n = good(N,p,q);
    if (p+q == N) {
        cout << N / 2 << endl;
        while(s.size()>0) {
            for(int j = 0; j < p; j++) {
                cout << s[j];
            }
            s.erase(0,p);
            cout << endl;
            for(int j = 0; j < q; j++) {
                cout << s[j];
            }
            s.erase(0,q);
            cout << endl;
        }

    }

    else if( N % p == 0) {
        cout << N/p << endl;
        while(s.size()>0) {
            for(int j = 0; j < p; j++) {
                cout << s[j];
            }
            s.erase(0,p);
            cout << endl;
        }
    }
    else if( N % q == 0) {
        cout << N/q << endl;
        while(s.size()>0){
            for(int j = 0; j < q; j++) {
                cout << s[j];
            }
            s.erase(0,q);
            cout << endl;
        }
    }
    else if (p+q < N && n.f) {
        cout << n.x + n.y<< endl;
        while(s.size()>0) {
            for(int v = 0; v < n.x; v++) {
                for(int j = 0; j < p; j++) {
                    cout << s[j];
                }
                s.erase(0,p);
                cout << endl;
            }
            for(int v = 0; v < n.y; v++) {
                for(int j = 0; j < q; j++) {
                    cout << s[j];
                }
                s.erase(0,q);
                cout << endl;
            }
        }

    }
    else cout <<-1 << endl;

}


#include <iostream>
#include <string>
#include <cstdio>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <sstream>
#include <utility>
#include <cmath>
#include <bitset>
#include <cstring>
#include <utility>
#include <set>
using namespace std;
typedef long long ll;
struct num{
    int x;
    int y;
    bool f;
};
num good(int x,int p,int q) {
    num n1;
    int sum = 0;
    for(int i = 1; i*p < x; i++) {
        for(int j = 1; sum < x; j++) {
            sum = i*p + q*j;
            if(sum == x) {
                n1.x = i;
                n1.y = j;
                n1.f = true;
                return n1;
            }
        }
        sum = 0;
    }
    n1.x = 0;
    n1.y = 0;
    n1.f = false;
    return n1;
}

int main() {
    int N, p, q;
    string s;
    cin >> N >> p >> q;
    cin >> s;
    num n = good(N,p,q);
    if (p+q == N) {
        cout << N / 2 << endl;
        while(s.size()>0) {
            for(int j = 0; j < p; j++) {
                cout << s[j];
            }
            s.erase(0,p);
            cout << endl;
            for(int j = 0; j < q; j++) {
                cout << s[j];
            }
            s.erase(0,q);
            cout << endl;
        }

    }

    else if( N % p == 0) {
        cout << N/p << endl;
        while(s.size()>0) {
            for(int j = 0; j < p; j++) {
                cout << s[j];
            }
            s.erase(0,p);
            cout << endl;
        }
    }
    else if( N % q == 0) {
        cout << N/q << endl;
        while(s.size()>0){
            for(int j = 0; j < q; j++) {
                cout << s[j];
            }
            s.erase(0,q);
            cout << endl;
        }
    }
    else if (p+q < N && n.f) {
        cout << n.x + n.y<< endl;
        while(s.size()>0) {
            for(int v = 0; v < n.x; v++) {
                for(int j = 0; j < p; j++) {
                    cout << s[j];
                }
                s.erase(0,p);
                cout << endl;
            }
            for(int v = 0; v < n.y; v++) {
                for(int j = 0; j < q; j++) {
                    cout << s[j];
                }
                s.erase(0,q);
                cout << endl;
            }
        }

    }
    else cout <<-1 << endl;

}


#include <iostream>
#include <string>
#include <cstdio>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <sstream>
#include <utility>
#include <cmath>
#include <bitset>
#include <cstring>
#include <utility>
#include <set>
typedef long long ll;

using namespace std;

int main() {
    int N, p, q;
    string s;
    cin >> N >> p >> q;
    cin >> s;
    if (p+q == N) {
        cout << N / 2 << endl;
        while(s.size()>0) {
            for(int j = 0; j < p; j++) {
                cout << s[j];
            }
            s.erase(0,p);
            cout << endl;
            for(int j = 0; j < q; j++) {
                cout << s[j];
            }
            s.erase(0,q);
            cout << endl;
        }

    }

    else if( N % p == 0) {
        cout << N/p << endl;
        while(s.size()>0) {
            for(int j = 0; j < p; j++) {
                cout << s[j];
            }
            s.erase(0,p);
            cout << endl;
        }
    }
    else if( N % q == 0) {
        cout << N/q << endl;
        while(s.size()>0){
            for(int j = 0; j < q; j++) {
                cout << s[j];
            }
            s.erase(0,q);
            cout << endl;
        }
    }
    else if (p+q < N) {
        int m = N / p;
        int b = (N%p) / q;
        cout << m+b<< endl;
        while(s.size()>0) {
            for(int v = 0; v < m; v++) {
                for(int j = 0; j < p; j++) {
                    cout << s[j];
                }
                s.erase(0,p);
                cout << endl;
            }
            for(int v = 0; v < b; v++) {
                for(int j = 0; j < q; j++) {
                    cout << s[j];
                }
                s.erase(0,q);
                cout << endl;
            }
        }

    }
    else cout <<-1 << endl;

}


#include <iostream>
#include <string>
#include <cstdio>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <sstream>
#include <utility>
#include <cmath>
#include <bitset>
#include <cstring>
#include <utility>
#include <set>
using namespace std;
typedef long long ll;
struct num{
    int x;
    int y;
    bool f;
};
num good(int x,int p,int q) {
    num n1;
    int sum = 0;
    for(int i = 1; i*p < x; i++) {
        for(int j = 1; sum < x; j++) {
            sum = i*p + q*j;
            if(sum == x) {
                n1.x = i;
                n1.y = j;
                n1.f = true;
                return n1;
            }
        }
        sum = 0;
    }
    n1.x = 0;
    n1.y = 0;
    n1.f = false;
    return n1;
}

int main() {
    int N, p, q;
    string s;
    cin >> N >> p >> q;
    cin >> s;
    num n = good(N,p,q);
    if( N % p == 0) {
        cout << N/p << endl;
        while(s.size()>0) {
            for(int j = 0; j < p; j++) {
                cout << s[j];
            }
            s.erase(0,p);
            cout << endl;
        }
    }
    else if( N % q == 0) {
        cout << N/q << endl;
        while(s.size()>0){
            for(int j = 0; j < q; j++) {
                cout << s[j];
            }
            s.erase(0,q);
            cout << endl;
        }
    }
    else if (p+q <= N && n.f) {
        cout << n.x + n.y << endl;
        while(s.size()>0) {
            for(int v = 0; v < n.x; v++) {
                for(int j = 0; j < p; j++) {
                    cout << s[j];
                }
                s.erase(0,p);
                cout << endl;
            }
            for(int v = 0; v < n.y; v++) {
                for(int j = 0; j < q; j++) {
                    cout << s[j];
                }
                s.erase(0,q);
                cout << endl;
            }
        }

    }
    else cout <<-1 << endl;

}


#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main(){
	int n,x,y;
	string s;
	scanf("%d%d%d",&n,&x,&y);
	cin>>s;
	if(x<y)swap(x,y);
	int p=n%x;
	for(int k=0;k<x;k++){
		if(x==0&&y*k!=p)continue;
		if((y*k)%x==p){
			int now=0;
			printf("%d\n",k+(n-y*k)/x);
			for(int t=0;t<k;t++){
				cout<<s.substr(now,y)<<endl;
				now+=y;
			}
			for(;now<s.length();now+=x)
			cout<<s.substr(now,x)<<endl;
			return 0;
		}
	}
	puts("-1");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main(){
	int n,x,y;
	string s;
	cin>>s;
	scanf("%d%d%d",&n,&x,&y);
	if(x<y)swap(x,y);
	int p=n%x;
	for(int k=0;k<x;k++)
		if((y*k)%x==p){
			int now=0;
			printf("%d\n",k+(n-y*k)/x);
			while(k--){
				cout<<s.substr(now,y)<<endl;
				now+=y;
			}
			for(;now<s.length();now+=x)
			cout<<s.substr(now,x)<<Endl;
			return 0;
		}
	puts("-1");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main(){
	int n,x,y;
	string s;
	scanf("%d%d%d",&n,&x,&y);
	cin>>s;
	if(x<y)swap(x,y);
	if(x){
		int p=n%x;
		for(int k=0;k<x;k++){
			if((y*k)%x==p){
				int now=0;
				printf("%d\n",k+(n-y*k)/x);
				for(int t=0;t<k;t++){
					cout<<s.substr(now,y)<<endl;
					now+=y;
				}
				for(;now<s.length();now+=x)
				cout<<s.substr(now,x)<<endl;
				return 0;
			}
		}
	}
	puts("-1");
	return 0;
}


def func(x1,y1,x2,y2):
    global mat,k
    ones,z=[],[]
    if mat[x1][y1]==1:
        ones.append((x1+1,y1+1))
    else:
        z.append((x1+1,y1+1))
    if mat[x2][y1]==1:
        ones.append((x2+1,y1+1))
    else:
        z.append((x2+1,y1+1))
    if mat[x1][y2]==1:
        ones.append((x1+1,y2+1))
    else:
        z.append((x1+1,y2+1))
    if mat[x2][y2]==1:
        ones.append((x2+1,y2+1))
    else:
        z.append((x2+1,y2+1))
    while len(ones)!=0:
        if len(ones)==4:
            k+=1
            t=[]
            for x,y in ones[:-1]:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            out.append(t)
        elif len(ones)==3:
            k+=1
            t=[]
            for x,y in ones:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            out.append(t)
        elif len(ones)==2:
            k+=1
            t=[]
            for x,y in ones[:-1]:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            for x,y in z:
                mat[x-1][y-1]=1
                t.append(x)
                t.append(y)
            out.append(t)
        else:
            k+=1
            t=[]
            for x,y in ones:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            for x,y in z[:-1]:
                mat[x-1][y-1]=1
                t.append(x)
                t.append(y)
            out.append(t)
        ones,z=[],[]
        if mat[x1][y1]==1:
            ones.append((x1+1,y1+1))
        else:
            z.append((x1+1,y1+1))
        if mat[x2][y1]==1:
            ones.append((x2+1,y1+1))
        else:
            z.append((x2+1,y1+1))
        if mat[x1][y2]==1:
            ones.append((x1+1,y2+1))
        else:
            z.append((x1+1,y2+1))
        if mat[x2][y2]==1:
            ones.append((x2+1,y2+1))
        else:
            z.append((x2+1,y2+1))

for _ in range(int(input())):
    n,m=map(int,input().split())
    mat=[]
    for i in range(n):
        mat.append(list(map(int,list(input()))))
    k=0
    out=[]
    i=0
    if n%2:
        j=0
        if m%2:
            func(n-2,m-2,n-1,m-1)
        while j<m-1:
            if mat[n-1][j]==1:
                k+=1
                out.append([n,j+1,n-1,j,n-1,j+1])
            j+=1
    while i<n-1:
        j=0
        if m%2 and mat[i][m-1]==1:
            mat[i][m-1]=0
            k+=1
            out.append([i+1,m,i+1,m-1,i+2,m-1])
        while j<m-1:
            func(i,j,i+1,j+1)
            j+=2
        i+=2
    print(k)
    for i in out:
        print(*i)


def flip(x,y):
    global mat
    if mat[x][y]:
        mat[x][y]=0
    else:
        mat[x][y]=1
def func(x1,y1,x2,y2):
    global mat,k
    ones,z=[],[]
    if mat[x1][y1]==1:
        ones.append((x1+1,y1+1))
    else:
        z.append((x1+1,y1+1))
    if mat[x2][y1]==1:
        ones.append((x2+1,y1+1))
    else:
        z.append((x2+1,y1+1))
    if mat[x1][y2]==1:
        ones.append((x1+1,y2+1))
    else:
        z.append((x1+1,y2+1))
    if mat[x2][y2]==1:
        ones.append((x2+1,y2+1))
    else:
        z.append((x2+1,y2+1))
    while len(ones)!=0:
        if len(ones)==4:
            k+=1
            t=[]
            for x,y in ones[:-1]:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            out.append(t)
        elif len(ones)==3:
            k+=1
            t=[]
            for x,y in ones:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            out.append(t)
        elif len(ones)==2:
            k+=1
            t=[]
            for x,y in ones[:-1]:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            for x,y in z:
                mat[x-1][y-1]=1
                t.append(x)
                t.append(y)
            out.append(t)
        else:
            k+=1
            t=[]
            for x,y in ones:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            for x,y in z[:-1]:
                mat[x-1][y-1]=1
                t.append(x)
                t.append(y)
            out.append(t)
        ones,z=[],[]
        if mat[x1][y1]==1:
            ones.append((x1+1,y1+1))
        else:
            z.append((x1+1,y1+1))
        if mat[x2][y1]==1:
            ones.append((x2+1,y1+1))
        else:
            z.append((x2+1,y1+1))
        if mat[x1][y2]==1:
            ones.append((x1+1,y2+1))
        else:
            z.append((x1+1,y2+1))
        if mat[x2][y2]==1:
            ones.append((x2+1,y2+1))
        else:
            z.append((x2+1,y2+1))

for _ in range(int(input())):
    n,m=map(int,input().split())
    mat=[]
    for i in range(n):
        mat.append(list(map(int,list(input()))))
    k=0
    out=[]
    i=0
    if n%2:
        j=0
        if m%2:
            func(n-2,m-2,n-1,m-1)
        while j<m-1:
            if mat[n-1][j]==1:
                flip(n-1,j)
                flip(n-2,j)
                flip(n-2,j+1)
                k+=1
                out.append([n,j+1,n-1,j+1,n-1,j+2])
            j+=1
    while i<n-1:
        j=0
        if m%2:
            if mat[i][m-1]:
                flip(i,m-1)
                flip(i,m-2)
                flip(i+1,m-2)
                k+=1
                out.append([i+1,m,i+1,m-1,i+2,m-1])
            if mat[i+1][m-1]:
                flip(i+1,m-1)
                flip(i,m-2)
                flip(i+1,m-2)
                k+=1
                out.append([i+2,m,i+1,m-1,i+2,m-1])

        while j<m-1:
            func(i,j,i+1,j+1)
            j+=2
        i+=2
    print(k)
    for i in out:
        print(*i)


def func(x1,y1,x2,y2):
    global mat,k
    ones,z=[],[]
    if mat[x1][y1]==1:
        ones.append((x1+1,y1+1))
    else:
        z.append((x1+1,y1+1))
    if mat[x2][y1]==1:
        ones.append((x2+1,y1+1))
    else:
        z.append((x2+1,y1+1))
    if mat[x1][y2]==1:
        ones.append((x1+1,y2+1))
    else:
        z.append((x1+1,y2+1))
    if mat[x2][y2]==1:
        ones.append((x2+1,y2+1))
    else:
        z.append((x2+1,y2+1))
    while len(ones)!=0:
        if len(ones)==4:
            k+=1
            t=[]
            for x,y in ones[:-1]:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            out.append(t)
        elif len(ones)==3:
            k+=1
            t=[]
            for x,y in ones:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            out.append(t)
        elif len(ones)==2:
            k+=1
            t=[]
            for x,y in ones[:-1]:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            for x,y in z:
                mat[x-1][y-1]=1
                t.append(x)
                t.append(y)
            out.append(t)
        else:
            k+=1
            t=[]
            for x,y in ones:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            for x,y in z[:-1]:
                mat[x-1][y-1]=1
                t.append(x)
                t.append(y)
            out.append(t)
        ones,z=[],[]
        if mat[x1][y1]==1:
            ones.append((x1+1,y1+1))
        else:
            z.append((x1+1,y1+1))
        if mat[x2][y1]==1:
            ones.append((x2+1,y1+1))
        else:
            z.append((x2+1,y1+1))
        if mat[x1][y2]==1:
            ones.append((x1+1,y2+1))
        else:
            z.append((x1+1,y2+1))
        if mat[x2][y2]==1:
            ones.append((x2+1,y2+1))
        else:
            z.append((x2+1,y2+1))

for _ in range(int(input())):
    n,m=map(int,input().split())
    mat=[]
    for i in range(n):
        mat.append(list(map(int,list(input()))))
    k=0
    out=[]
    i=0
    if n%2:
        j=0
        if m%2:
            func(n-2,m-2,n-1,m-1)
        while j<m-1:
            func(n-2,j,n-1,j+1)
            j+=2
    while i<n-1:
        j=0
        if m%2:
            func(i,m-2,i+1,m-1)
        while j<m-1:
            func(i,j,i+1,j+1)
            j+=2
        i+=2
    print(k)
    for i in out:
        print(*i)


def flip(x,y):
    global mat
    if mat[x][y]:
        mat[x][y]=0
    else:
        mat[x][y]=1
def func(x1,y1,x2,y2):
    global mat,k
    ones,z=[],[]
    if mat[x1][y1]==1:
        ones.append((x1+1,y1+1))
    else:
        z.append((x1+1,y1+1))
    if mat[x2][y1]==1:
        ones.append((x2+1,y1+1))
    else:
        z.append((x2+1,y1+1))
    if mat[x1][y2]==1:
        ones.append((x1+1,y2+1))
    else:
        z.append((x1+1,y2+1))
    if mat[x2][y2]==1:
        ones.append((x2+1,y2+1))
    else:
        z.append((x2+1,y2+1))
    while len(ones)!=0:
        if len(ones)==4:
            k+=1
            t=[]
            for x,y in ones[:-1]:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            out.append(t)
        elif len(ones)==3:
            k+=1
            t=[]
            for x,y in ones:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            out.append(t)
        elif len(ones)==2:
            k+=1
            t=[]
            for x,y in ones[:-1]:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            for x,y in z:
                mat[x-1][y-1]=1
                t.append(x)
                t.append(y)
            out.append(t)
        else:
            k+=1
            t=[]
            for x,y in ones:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            for x,y in z[:-1]:
                mat[x-1][y-1]=1
                t.append(x)
                t.append(y)
            out.append(t)
        ones,z=[],[]
        if mat[x1][y1]==1:
            ones.append((x1+1,y1+1))
        else:
            z.append((x1+1,y1+1))
        if mat[x2][y1]==1:
            ones.append((x2+1,y1+1))
        else:
            z.append((x2+1,y1+1))
        if mat[x1][y2]==1:
            ones.append((x1+1,y2+1))
        else:
            z.append((x1+1,y2+1))
        if mat[x2][y2]==1:
            ones.append((x2+1,y2+1))
        else:
            z.append((x2+1,y2+1))

for _ in range(int(input())):
    n,m=map(int,input().split())
    mat=[]
    for i in range(n):
        mat.append(list(map(int,list(input()))))
    k=0
    out=[]
    i=0
    if n%2:
        j=0
        if m%2:
            func(n-2,m-2,n-1,m-1)
        while j<m-1:
            if mat[n-1][j]==1:
                flip(n-1,j)
                flip(n-2,j)
                flip(n-2,j+1)
                k+=1
                out.append([n,j+1,n-1,j+1,n-1,j+2])
            if mat[n-1][j+1]==1:
                flip(n-1,j+1)
                flip(n-2,j)
                flip(n-2,j+1)
                k+=1
                out.append([n,j+2,n-1,j+1,n-1,j+2])
            j+=2
    while i<n-1:
        j=0
        if m%2:
            if mat[i][m-1]:
                flip(i,m-1)
                flip(i,m-2)
                flip(i+1,m-2)
                k+=1
                out.append([i+1,m,i+1,m-1,i+2,m-1])
            if mat[i+1][m-1]:
                flip(i+1,m-1)
                flip(i,m-2)
                flip(i+1,m-2)
                k+=1
                out.append([i+2,m,i+1,m-1,i+2,m-1])

        while j<m-1:
            func(i,j,i+1,j+1)
            j+=2
        i+=2
    print(k)
    for i in out:
        print(*i)


def func(x1,y1,x2,y2):
    global mat,k
    ones,z=[],[]
    if mat[x1][y1]==1:
        ones.append((x1+1,y1+1))
    else:
        z.append((x1+1,y1+1))
    if mat[x2][y1]==1:
        ones.append((x2+1,y1+1))
    else:
        z.append((x2+1,y1+1))
    if mat[x1][y2]==1:
        ones.append((x1+1,y2+1))
    else:
        z.append((x1+1,y2+1))
    if mat[x2][y2]==1:
        ones.append((x2+1,y2+1))
    else:
        z.append((x2+1,y2+1))
    while len(ones)!=0:
        if len(ones)==4:
            k+=1
            t=[]
            for x,y in ones[:-1]:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            out.append(t)
        elif len(ones)==3:
            k+=1
            t=[]
            for x,y in ones:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            out.append(t)
        elif len(ones)==2:
            k+=1
            t=[]
            for x,y in ones[:-1]:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            for x,y in z:
                mat[x-1][y-1]=1
                t.append(x)
                t.append(y)
            out.append(t)
        else:
            k+=1
            t=[]
            for x,y in ones:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            for x,y in z[:-1]:
                mat[x-1][y-1]=1
                t.append(x)
                t.append(y)
            out.append(t)
        ones,z=[],[]
        if mat[x1][y1]==1:
            ones.append((x1+1,y1+1))
        else:
            z.append((x1+1,y1+1))
        if mat[x2][y1]==1:
            ones.append((x2+1,y1+1))
        else:
            z.append((x2+1,y1+1))
        if mat[x1][y2]==1:
            ones.append((x1+1,y2+1))
        else:
            z.append((x1+1,y2+1))
        if mat[x2][y2]==1:
            ones.append((x2+1,y2+1))
        else:
            z.append((x2+1,y2+1))

for _ in range(int(input())):
    n,m=map(int,input().split())
    mat=[]
    for i in range(n):
        mat.append(list(map(int,list(input()))))
    k=0
    out=[]
    i=0
    if n%2:
        j=0
        if m%2:
            func(n-2,m-2,n-1,m-1)
        while j<m-1:
            if mat[n-1][j]==1:
                k+=1
                out.append([n,j+1,n-1,j+2,n-1,j+1])
            j+=1
    while i<n-1:
        j=0
        if m%2 and mat[i][m-1]==1:
            mat[i][m-1]=0
            k+=1
            out.append([i+1,m,i+1,m-1,i+2,m-1])
        while j<m-1:
            func(i,j,i+1,j+1)
            j+=2
        i+=2
    print(k)
    for i in out:
        print(*i)


def flip(x,y):
    global mat
    if mat[x][y]:
        mat[x][y]=0
    else:
        mat[x][y]=1
def func(x1,y1,x2,y2):
    global mat,k
    ones,z=[],[]
    if mat[x1][y1]==1:
        ones.append((x1+1,y1+1))
    else:
        z.append((x1+1,y1+1))
    if mat[x2][y1]==1:
        ones.append((x2+1,y1+1))
    else:
        z.append((x2+1,y1+1))
    if mat[x1][y2]==1:
        ones.append((x1+1,y2+1))
    else:
        z.append((x1+1,y2+1))
    if mat[x2][y2]==1:
        ones.append((x2+1,y2+1))
    else:
        z.append((x2+1,y2+1))
    while len(ones)!=0:
        if len(ones)==4:
            k+=1
            t=[]
            for x,y in ones[:-1]:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            out.append(t)
        elif len(ones)==3:
            k+=1
            t=[]
            for x,y in ones:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            out.append(t)
        elif len(ones)==2:
            k+=1
            t=[]
            for x,y in ones[:-1]:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            for x,y in z:
                mat[x-1][y-1]=1
                t.append(x)
                t.append(y)
            out.append(t)
        else:
            k+=1
            t=[]
            for x,y in ones:
                mat[x-1][y-1]=0
                t.append(x)
                t.append(y)
            for x,y in z[:-1]:
                mat[x-1][y-1]=1
                t.append(x)
                t.append(y)
            out.append(t)
        ones,z=[],[]
        if mat[x1][y1]==1:
            ones.append((x1+1,y1+1))
        else:
            z.append((x1+1,y1+1))
        if mat[x2][y1]==1:
            ones.append((x2+1,y1+1))
        else:
            z.append((x2+1,y1+1))
        if mat[x1][y2]==1:
            ones.append((x1+1,y2+1))
        else:
            z.append((x1+1,y2+1))
        if mat[x2][y2]==1:
            ones.append((x2+1,y2+1))
        else:
            z.append((x2+1,y2+1))

for _ in range(int(input())):
    n,m=map(int,input().split())
    mat=[]
    for i in range(n):
        mat.append(list(map(int,list(input()))))
    k=0
    out=[]
    i=0
    if n%2:
        j=0
        if m%2:
            func(n-2,m-2,n-1,m-1)
        while j<m-1:
            if mat[n-1][j]==1:
                flip(n-1,j)
                flip(n-2,j)
                flip(n-2,j+1)
                k+=1
                out.append([n,j+1,n-1,j+1,n-1,j+2])
            j+=1
    while i<n-1:
        j=0
        if m%2 and mat[i][m-1]==1:
            flip(i,m-1)
            flip(i,m-2)
            flip(i+1,m-2)
            k+=1
            out.append([i+1,m,i+1,m-1,i+2,m-1])
        while j<m-1:
            func(i,j,i+1,j+1)
            j+=2
        i+=2
    print(k)
    for i in out:
        print(*i)


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        x-=1
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:

        aux.append(x+2)
        aux.append(y+1)
        if s[x+1][y]=='1':
            s[x+1][y]='0'
        else:
            s[x+1][y]='1'

    if c<=1:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'

    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        y-=1
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
        if s[x][y+1]=='1':
            s[x][y+1]='0'
        else:
            s[x][y+1]='1'
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    x=n-2
    y=m-2
    q=solve(x,y)
    while q!=1:
        q=solve(x,y)
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            if(j+1==m ) and i+1==n:
                continue
            if(j+1==m ):
                clean1(i,j-1)
            if(i+1==n ):

                clean2(i-1,j)

    for i in range(0,n-1,2):
        for j in range(0,m-1,2):
            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        x-=1
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        s[x][y]='0'
    if c<=1:
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        y-=1
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        s[x][y]='0'
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    for i in range(0,n,2):
        for j in range(0,m,2):
            if(i+1==n ):
                clean2(i-1,j)
            if(j+1==m ):
                clean1(i,j-1)

    for i in range(0,n,2):
        for j in range(0,m,2):
            if(i+1==n ):
                continue
            if(j+1==m ):
                continue

            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        x-=1
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
    if c<=1:
        aux.append(x+1)
        aux.append(y+1)
        s[x][y]='0'

    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        y-=1
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        s[x][y]='0'
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):

            if(j+1==m ):
                clean1(i,j-1)
            if(i+1==n ):
                clean2(i-1,j)
    for i in range(0,n,2):
        for j in range(0,m,2):
            if(i+1==n ):
                continue
            if(j+1==m ):
                continue

            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        x-=1
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+2)
        aux.append(y+1)
        if s[x+1][y]=='1':
            s[x+1][y]='0'
        else:
            s[x+1][y]='1'

    if c<=1:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'

    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        y-=1
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
        if s[x][y+1]=='1':
            s[x][y+1]='0'
        else:
            s[x][y+1]='1'
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            if(j+1==m ) and i+1==n:
                continue
            if(j+1==m ):

                clean1(i,j-1)
            if(i+1==n ):

                clean2(i-1,j)

    for i in range(0,n-1,2):
        for j in range(0,m-1,2):
            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        x-=1
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:

        aux.append(x+2)
        aux.append(y+1)
        if s[x+1][y]=='1':
            s[x+1][y]='0'
        else:
            s[x+1][y]='1'

    if c<=1:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'

    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        y-=1
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
        if s[x][y+1]=='1':
            s[x][y+1]='0'
        else:
            s[x][y+1]='1'
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            if(j+1==m ) and i+1==n:
                continue
            if(j+1==m ):
                clean1(i,j-1)
            if(i+1==n ):

                clean2(i-1,j)
    x=n-2
    y=n-2
    q=solve(x,y)
    while q!=1:
        q=solve(x,y)

    for i in range(0,n-1,2):
        for j in range(0,m-1,2):
            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    for i in range(0,n-1,2):
        for j in range(0,m-1,2):
            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        return 0
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:

        aux.append(x+2)
        aux.append(y+1)
        if s[x+1][y]=='1':
            s[x+1][y]='0'
        else:
            s[x+1][y]='1'

    if c<=1:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'

    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        return 0
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
        if s[x][y+1]=='1':
            s[x][y+1]='0'
        else:
            s[x][y+1]='1'
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    x=n-2
    y=m-2
    q=solve(x,y)
    while q!=1:
        q=solve(x,y)

    x=n-2
    y=m-2
    if m%2!=0:
        for i in range(0,n,2):
            clean1(i,m-2)

    if n%2!=0:
        for i in range(0,m,2):
            clean2(n-2,i)

    for i in range(0,n-1,2):
        for j in range(0,m-1,2):
            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        return 0
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:

        aux.append(x+2)
        aux.append(y+1)
        if s[x+1][y]=='1':
            s[x+1][y]='0'
        else:
            s[x+1][y]='1'

    if c<=1:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'

    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        return 0
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
        if s[x][y+1]=='1':
            s[x][y+1]='0'
        else:
            s[x][y+1]='1'
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    x=n-2
    y=m-2
    if n%2!=0 and m%2!=0:
        q=solve(x,y)
        while q!=1:
            q=solve(x,y)

    x=n-2
    y=m-2
    if m%2!=0:
        for i in range(0,n,2):
            clean1(i,m-2)

    if n%2!=0:
        for i in range(0,m,2):
            clean2(n-2,i)

    for i in range(0,n-1,2):
        for j in range(0,m-1,2):
            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        x-=1
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:

        aux.append(x+2)
        aux.append(y+1)
        if s[x+1][y]=='1':
            s[x+1][y]='0'
        else:
            s[x+1][y]='1'

    if c<=1:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'

    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        y-=1
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
        if s[x][y+1]=='1':
            s[x][y+1]='0'
        else:
            s[x][y+1]='1'
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            if(j+1==m ) and i+1==n:
                continue
            if(j+1==m ):
                clean1(i,j-1)
            if(i+1==n ):

                clean2(i-1,j)
    x=n-2
    y=m-2
    q=solve(x,y)
    while q!=1:
        q=solve(x,y)

    for i in range(0,n-1,2):
        for j in range(0,m-1,2):
            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        x-=1
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
    if c<=1:
        aux.append(x+1)
        aux.append(y+1)
        s[x][y]='0'

    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        y-=1
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        s[x][y]='0'
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            if(i+1==n ):
                clean2(i-1,j)
            if(j+1==m ):
                clean1(i,j-1)
    for i in range(0,n,2):
        for j in range(0,m,2):
            if(i+1==n ):
                continue
            if(j+1==m ):
                continue

            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        x-=1
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+2)
        aux.append(y+1)
        if s[x+1][y]=='1':
            s[x+1][y]='0'
        else:
            s[x+1][y]='1'

    if c<=1:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'

    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        y-=1
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'
        s[x][y]='0'
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
        if s[x][y+1]=='1':
            s[x][y+1]='0'
        else:
            s[x][y+1]='1'
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            if(j+1==m ) and i+1==n:
                continue
            if(j+1==m ):
                clean1(i,j-1)
            if(i+1==n ):
                clean2(i-1,j)

    for i in range(0,n-1,2):
        for j in range(0,m-1,2):
            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        x-=1
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+2)
        aux.append(y+1)
        if s[x+1][y]=='1':
            s[x+1][y]='0'
        else:
            s[x+1][y]='1'

    if c<=1:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'

    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        y-=1
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'
        s[x][y]='0'
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
        if s[x][y+1]=='1':
            s[x][y+1]='0'
        else:
            s[x][y+1]='1'
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    for i in range(n-1,-1,-1):
        for j in range(m-1,-1,-1):
            if(j+1==m ) and i+1==n:
                continue
            if(j+1==m ):
                clean1(i,j-1)
            if(i+1==n ):
                clean2(i-1,j)

    for i in range(0,n-1,2):
        for j in range(0,m-1,2):
            print(i,j)
            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        x-=1
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
    if c<=1:
        aux.append(x+2)
        aux.append(y+1)
    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        y-=1
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    for i in range(0,n,2):
        for j in range(0,m,2):
            if(i+1==n ):
                clean2(i-1,j)
            if(j+1==m ):
                clean1(i,j-1)

    for i in range(0,n,2):
        for j in range(0,m,2):
            if(i+1==n ):
                continue
            if(j+1==m ):
                continue

            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))


from sys import stdin,stdout
"""
n=int(stdin.readline().strip())
n,m=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
"""

def solve(x,y):
    c=0
    for i in range(x,x+2):
        for j in range(y,y+2):
            if s[i][j]=='1':
                c+=1
    if c==0:
        return 1
    if c==4 or c==3:
        aux=[]
        z=0
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and z<=2:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    z+=1
        ans.append(aux.copy())
        return 0
    if c==1 or c==2:
        y1=0
        z=0
        aux=[]
        for i in range(x,x+2):
            for j in range(y,y+2):
                if s[i][j]=='1' and y1==0:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='0'
                    y1+=1
                    continue
                if s[i][j]=='0' and z<=1:
                    aux.append(i+1)
                    aux.append(j+1)
                    s[i][j]='1'
                    z+=1
                    continue
        ans.append(aux.copy())
        return 0

def clean1(x,y):
    if x+1==n:
        x-=1
    c=0
    aux=[]
    if(s[x][y+1]=='1'):
        aux.append(x+1)
        aux.append(y+2)
        s[x][y+1]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:

        aux.append(x+2)
        aux.append(y+1)
        if s[x+1][y]=='1':
            s[x+1][y]='0'
        else:
            s[x+1][y]='1'

    if c<=1:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'

    ans.append(aux.copy())
def clean2(x,y):
    if y+1==m:
        y-=1
    c=0
    aux=[]
    if(s[x+1][y]=='1'):
        aux.append(x+2)
        aux.append(y+1)
        s[x+1][y]='0'
        c+=1
    if(s[x+1][y+1]=='1'):
        aux.append(x+2)
        aux.append(y+2)
        s[x+1][y+1]='0'
        c+=1
    if c==0:
        return
    if c<=2:
        aux.append(x+1)
        aux.append(y+1)
        if s[x][y]=='1':
            s[x][y]='0'
        else:
            s[x][y]='1'
    if c<=1:
        aux.append(x+1)
        aux.append(y+2)
        if s[x][y+1]=='1':
            s[x][y+1]='0'
        else:
            s[x][y+1]='1'
    ans.append(aux.copy())

T=int(stdin.readline().strip())
for caso in range(T):
    n,m=map(int,stdin.readline().strip().split())
    s=[list(stdin.readline().strip()) for i in range(n)]
    ans=[]
    x=n-2
    y=m-2
    if m%2!=0:
        for i in range(0,n,2):
            clean1(i,m-2)
    if n%2!=0:
        for i in range(0,m,2):
            clean2(n-2,i)
    x=n-2
    y=m-2
    q=solve(x,y)
    while q!=1:
        q=solve(x,y)

    for i in range(0,n-1,2):
        for j in range(0,m-1,2):
            x=solve(i,j)
            while x!=1:
                x=solve(i,j)

    stdout.write("%d\n" % len(ans))
    for i in ans:
        stdout.write("%d %d %d %d %d %d\n" % (i[0],i[1],i[2],i[3],i[4],i[5]))