#include<bits/stdc++.h>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define REP(u) for(int i=h[u],v;v=e[i].t,i;i=e[i].n)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))(_ch=='-')&&(_f=-1),_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=2e2+1,mod=998244353;
V check(int&x){x-=mod,x+=x>>31&mod;}
V cmax(int&x,int y){if(x-y>>31)x=y;}
V cmin(int&x,int y){if(y-x>>31)x=y;}
int T,n,m,t[N][N];
char a[N][N];
struct node{
	int ax,ay,bx,by,cx,cy;
	V work(){t[ax][ay]^=1,t[bx][by]^=1,t[cx][cy]^=1;}
	V output(){cout<<ax<<' '<<ay<<' '<<bx<<' '<<by<<' '<<cx<<' '<<cy<<'\n';}
};
vector<node>ans;
V input(){
	scanf("%d%d",&n,&m),ans.clear();
	FOR(i,1,n)scanf("%s",a[i]+1);
	FOR(i,1,n)FOR(j,1,m)t[i][j]=a[i][j]-'0';
}
V init(){
	ROF(i,n,3)FOR(j,1,m)if(t[i][j]){
			if(j==1)ans.push_back((node){i,j,i-1,j,i-1,j+1}),ans.back().work();
			else ans.push_back((node){i,j,i-1,j,i-1,j-1}),ans.back().work();
		}
}
V work(){
	ROF(j,m,3){
		if(t[1][j]&&t[2][j])ans.push_back((node){1,j,2,j,1,j-1}),ans.back().work();
		if(t[1][j])ans.push_back((node){1,j,1,j-1,2,j-1}),ans.back().work();
		if(t[2][j])ans.push_back((node){2,j,1,j-1,2,j-1}),ans.back().work();
	}
	int cnt=0;
	FOR(i,1,2)FOR(j,1,2)if(t[i][j]&&!t[i%2+1][j%2+1])
		ans.push_back((node){i,j,i%2+1,j,i,j%2+1}),ans.back().work();
	if(t[1][1]){
		ans.push_back((node){1,1,1,2,2,1}),ans.back().work();
		ans.push_back((node){2,2,1,2,2,1}),ans.back().work();
	}
	if(t[1][2]){
		ans.push_back((node){1,2,1,1,2,2}),ans.back().work();
		ans.push_back((node){2,1,1,1,2,2}),ans.back().work();
	}
	cout<<ans.size()<<'\n';
	for(node v:ans)v.output();
//	FOR(i,1,n)FOR(j,1,m)assert(!t[i][j]);
}
int main(){
//	freopen("test.in","r",stdin);
//	freopen(".out","w",stdout);
	for(scanf("%d",&T);T--;){
		input();
		init();
		work();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define REP(u) for(int i=h[u],v;v=e[i].t,i;i=e[i].n)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))(_ch=='-')&&(_f=-1),_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=2e2+1,mod=998244353;
V check(int&x){x-=mod,x+=x>>31&mod;}
V cmax(int&x,int y){if(x-y>>31)x=y;}
V cmin(int&x,int y){if(y-x>>31)x=y;}
int T,n,m,t[N][N];
char a[N][N];
struct node{
	int ax,ay,bx,by,cx,cy;
	V work(){t[ax][ay]^=1,t[bx][by]^=1,t[cx][cy]^=1;}
	V output(){
//		assert(max(max(ax,bx),cx)-min(min(ax,bx),cx)==1);
//		assert(max(max(ay,by),cy)-min(min(ay,by),cy)==1);
		cout<<ax<<' '<<ay<<' '<<bx<<' '<<by<<' '<<cx<<' '<<cy<<'\n';
	}
};
vector<node>ans;
V input(){
	scanf("%d%d",&n,&m),ans.clear();
	FOR(i,1,n)scanf("%s",a[i]+1);
	FOR(i,1,n)FOR(j,1,m)t[i][j]=a[i][j]-'0';
}
V init(){
	ROF(i,n,3)FOR(j,1,m)if(t[i][j]){
			if(j==1)ans.push_back((node){i,j,i-1,j,i-1,j+1}),ans.back().work();
			else ans.push_back((node){i,j,i-1,j,i-1,j-1}),ans.back().work();
		}
}
V work(){
	ROF(j,m,3){
		if(t[1][j]&&t[2][j])
			ans.push_back((node){1,j,2,j,1,j-1}),ans.back().work();
		if(t[1][j])ans.push_back((node){1,j,1,j-1,2,j-1}),ans.back().work();
		if(t[2][j])ans.push_back((node){2,j,1,j-1,2,j-1}),ans.back().work();
	}
	int cnt=0;
	FOR(i,1,2)FOR(j,1,2)cnt+=t[i][j];
	FOR(i,1,2)FOR(j,1,2)t[i][j]^=cnt&1;
	FOR(i,1,2)FOR(j,1,2)if(t[i][j]){
		ans.push_back((node){i%2+1,j%2+1,i%2+1,j,i,j%2+1}),t[i][j]^=1;
	}
	cout<<ans.size()<<'\n';
	for(node v:ans)v.output();
//	FOR(i,1,n)FOR(j,1,m)if(t[i][j])assert(!t[i][j]);
}
int main(){
//	freopen("test.in","r",stdin);
//	freopen(".out","w",stdout);
	for(scanf("%d",&T);T--;){
		input();
		init();
		work();
	}
	return 0;
}


/*************************************
 * @contest:      Codeforces Round #684 (Div. 1).
 * @author:       hkxadpall.
 * @time:         2020-11-17.
*************************************/
#include <bits/stdc++.h>
using namespace std;
typedef unsigned uint32;
typedef long long int64;
typedef unsigned long long uint64;

#if false /* 需要使用 fread 优化，改此参数为 true */
#define getchar() (i_P1 == i_P2 && (i_P2 = (i_P1 = BUF) + fread(BUF, 1, 1 << 21, stdin), i_P1 == i_P2) ? EOF : *i_P1++)
char BUF[1 << 21], *i_P1 = BUF, *i_P2 = BUF;
#endif
namespace Fastio {
    struct Reader {
        char endch;
        Reader() { endch = '\0'; }
        Reader& operator >> (char &ch) { // ignore character ' ', '\r', '\n', '\t'
            ch = getchar();
            while (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t') ch = getchar();
            return *this;
        }
        Reader& operator >> (char *str) {
            while (((*str = getchar()) == ' ' || *str == '\n' || *str == '\r' || *str == '\t') && *str != EOF);
            while ((*++str = getchar()) != ' ' && *str != '\n' && *str != '\r' && *str != '\t' && *str != EOF);
            *str = '\0';
            return *this;
        }
        template <typename Int>
        Reader& operator >> (Int &d) {
            bool flag = 0;
            endch = getchar();
            while ((!isdigit(endch)) && endch != '-' && endch != EOF) endch = getchar();
            if (endch == '-') flag = 1, endch = getchar();
            d = endch & 15;
            while (isdigit(endch = getchar())) d = (d << 3) + (d << 1) + (endch & 15);
            if (flag) d = -d;
            return *this;
        }
        template <typename T>
        inline T get() {
            T Val;
            (*this) >> Val;
            return Val;
        }
    };

    struct Writer {
        Writer& operator << (const char ch) {
            putchar(ch);
            return *this;
        }
        Writer& operator << (const char *ch) {
            while (*ch) putchar(*(ch++));
            return *this;
        }
        Writer& operator << (char* ss) { return *this << (const char *)ss; }
        template <typename Int>
        Writer& operator << (Int x) {
            static char buffer[33];
            static int top = 0;
            if (!x) {
                putchar('0');
                return *this;
            }
            if (x < 0) putchar('-'), x = -x;
            while (x) {
                buffer[++top] = '0' | (x % 10);
                x /= 10;
            }
            while (top) putchar(buffer[top--]);
            return *this;
        }
    };
}
Fastio::Reader kin;
Fastio::Writer kout;

char ss[107];
int T, n, m, a[107][107];
int x[30007][6], y;

signed main() {
    for (kin >> T; T--;) {
        kin >> n >> m;
        for (int i = 1; i <= n; ++i) {
            kin >> (ss + 1);
            for (int j = 1; j <= m; ++j)
                a[i][j] = (ss[j] & 1);
        }
        for (int j = 1; j <= n; ++j) a[j][m + 1] = 0;
        for (int j = 1; j <= m + 1; ++j) a[n + 1][j] = 0;
        y = 0;
        for (int i = 1; i <= n; i += 2)
            for (int j = 1; j <= m; j += 2) {
                if (i == n && j == m) {
                    if (a[i][j]) {
                        x[++y][0] = i;
                        x[y][1] = j;
                        x[y][2] = i - 1;
                        x[y][3] = j;
                        x[y][4] = i;
                        x[y][5] = j - 1;
                        x[++y][0] = i;
                        x[y][1] = j;
                        x[y][2] = i - 1;
                        x[y][3] = j - 1;
                        x[y][4] = i;
                        x[y][5] = j - 1;
                        x[++y][0] = i;
                        x[y][1] = j;
                        x[y][2] = i - 1;
                        x[y][3] = j - 1;
                        x[y][4] = i - 1;
                        x[y][5] = j;
                    }
                } else if (i == n) {
                    if (a[i][j] ^ a[i][j + 1]) {
                        x[++y][0] = i;
                        x[y][1] = j;
                        x[y][2] = i - 1;
                        x[y][3] = j;
                        x[y][4] = i;
                        x[y][5] = j + 1;
                        x[++y][0] = i;
                        x[y][1] = j;
                        x[y][2] = i - 1;
                        x[y][3] = j + 1;
                        x[y][4] = i;
                        x[y][5] = j + 1;
                    }
                    if (a[i][j]) {
                        x[++y][0] = i;
                        x[y][1] = j;
                        x[y][2] = i - 1;
                        x[y][3] = j;
                        x[y][4] = i - 1;
                        x[y][5] = j + 1;
                    }
                    if (a[i][j + 1]) {
                        x[++y][0] = i;
                        x[y][1] = j + 1;
                        x[y][2] = i - 1;
                        x[y][3] = j;
                        x[y][4] = i - 1;
                        x[y][5] = j + 1;
                    }
                } else if (j == m) {
                    if (a[i][j] ^ a[i + 1][j]) {
                        x[++y][0] = i;
                        x[y][1] = j;
                        x[y][2] = i + 1;
                        x[y][3] = j;
                        x[y][4] = i;
                        x[y][5] = j - 1;
                        x[++y][0] = i;
                        x[y][1] = j;
                        x[y][2] = i + 1;
                        x[y][3] = j;
                        x[y][4] = i + 1;
                        x[y][5] = j - 1;
                    }
                    if (a[i][j]) {
                        x[++y][0] = i;
                        x[y][1] = j;
                        x[y][2] = i;
                        x[y][3] = j - 1;
                        x[y][4] = i + 1;
                        x[y][5] = j - 1;
                    }
                    if (a[i + 1][j]) {
                        x[++y][0] = i + 1;
                        x[y][1] = j;
                        x[y][2] = i;
                        x[y][3] = j - 1;
                        x[y][4] = i + 1;
                        x[y][5] = j - 1;
                    }
                } else {
                    if (a[i][j] ^ a[i + 1][j] ^ a[i][j + 1]) {
                        x[++y][0] = i;
                        x[y][1] = j;
                        x[y][2] = i + 1;
                        x[y][3] = j;
                        x[y][4] = i;
                        x[y][5] = j + 1;
                    }
                    if (a[i + 1][j + 1] ^ a[i + 1][j] ^ a[i][j + 1]) {
                        x[++y][0] = i + 1;
                        x[y][1] = j + 1;
                        x[y][2] = i + 1;
                        x[y][3] = j;
                        x[y][4] = i;
                        x[y][5] = j + 1;
                    }
                    if (a[i + 1][j + 1] ^ a[i + 1][j] ^ a[i][j]) {
                        x[++y][0] = i + 1;
                        x[y][1] = j + 1;
                        x[y][2] = i + 1;
                        x[y][3] = j;
                        x[y][4] = i;
                        x[y][5] = j;
                    }
                    if (a[i + 1][j + 1] ^ a[i][j + 1] ^ a[i][j]) {
                        x[++y][0] = i + 1;
                        x[y][1] = j + 1;
                        x[y][2] = i;
                        x[y][3] = j + 1;
                        x[y][4] = i;
                        x[y][5] = j;
                    }
                }
            }
        kout << y << '\n';
        for (int i = 1; i <= y; ++i)
            kout << x[i][0] << ' ' << x[i][1] << ' ' << x[i][2] << ' '
                 << x[i][3] << ' ' << x[i][4] << ' ' << x[i][5] << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)

using namespace std;

typedef long long LL;

const int INF=0x3f3f3f3f;
const int maxn=105;

string s[maxn];

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n,m;
        scanf("%d%d",&n,&m);
        rep(i,0,n-1)cin>>s[i];
        int ans=0;
        rep(i,0,n-1)rep(j,0,m-1)if(s[i][j]=='1')ans++;
        printf("%d\n",3*ans);
        rep(i,1,n){
            rep(j,1,m){
                if(s[i-1][j-1]=='1'){
                    if(i==1&&j==1){
                        printf("%d %d %d %d %d %d\n",i,j,i,j+1,i+1,j+1);
                        printf("%d %d %d %d %d %d\n",i+1,j,i,j,i+1,j+1);
                        printf("%d %d %d %d %d %d\n",i+1,j,i,j+1,i,j);
                    }
                    else if(i==1){
                        printf("%d %d %d %d %d %d\n",i,j,i,j-1,i+1,j-1);
                        printf("%d %d %d %d %d %d\n",i+1,j,i,j,i+1,j-1);
                        printf("%d %d %d %d %d %d\n",i+1,j,i,j-1,i,j);
                    }
                    else if(j==1){
                        printf("%d %d %d %d %d %d\n",i,j,i,j+1,i-1,j+1);
                        printf("%d %d %d %d %d %d\n",i-1,j,i,j,i-1,j+1);
                        printf("%d %d %d %d %d %d\n",i-1,j,i,j+1,i,j);
                    }
                    else{
                        printf("%d %d %d %d %d %d\n",i,j,i,j-1,i-1,j-1);
                        printf("%d %d %d %d %d %d\n",i-1,j,i,j,i-1,j-1);
                        printf("%d %d %d %d %d %d\n",i-1,j,i,j-1,i,j);
                    }
                }
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)

using namespace std;

typedef long long LL;

const int INF=0x3f3f3f3f;
const int maxn=105;

string s[maxn];

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n,m;
        scanf("%d%d",&n,&m);
        rep(i,0,n-1)cin>>s[i];
        int ans=0;
        rep(i,0,n-1)rep(j,0,m-1)if(s[i][j]=='1')ans++;
        printf("%d\n",ans);
        rep(i,1,n){
            rep(j,1,m){
                if(s[i-1][j-1]=='1'){
                    if(i==1&&j==1){
                        printf("%d %d %d %d %d %d\n",i+1,j,i,j+1,i+1,j+1);
                    }
                    else if(i==1){
                        printf("%d %d %d %d %d %d\n",i+1,j,i,j-1,i+1,j-1);
                    }
                    else if(j==1){
                        printf("%d %d %d %d %d %d\n",i-1,j,i,j+1,i-1,j+1);
                    }
                    else{
                        printf("%d %d %d %d %d %d\n",i-1,j,i,j-1,i-1,j-1);
                    }
                }
            }
        }
    }
    return 0;
}


// acm.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>

using namespace std;

const int N = 100;
const int M = 100;

int t[N][M];
vector<pair<int, int>> ans;

void AddAns(int i, int j)
{
	ans.push_back(pair<int, int>(i + 1, j + 1));
	t[i][j] ^= 0x01;
}

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int n, m;
		cin >> n >> m;
		for (int i = 0; i < n; i++)
		{
			string str;
			cin >> str;
			for (int j = 0; j < m; j++)
			{
				t[i][j] = (str[j] == '1') ? 1 : 0;
			}
		}
		ans.clear();
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				if (t[i][j] == 0)
				{
					continue;
				}
				if (j + 1 < m)
				{
					if (i + 1 < n)
					{
						if (t[i][j+1] == 1)
						{
							AddAns(i, j);
							AddAns(i, j + 1);
							if (t[i+1][j] == 1)
							{
								AddAns(i + 1, j);
							}
							else
							{
								AddAns(i + 1, j + 1);
							}
						}
						else
						{
							AddAns(i, j);
							AddAns(i + 1, j);
							AddAns(i + 1, j + 1);
						}
					}
					else
					{
						if (t[i][j+1] == 1)
						{
							AddAns(i - 1, j);
							AddAns(i - 1, j + 1);
							AddAns(i, j + 1);

							AddAns(i - 1, j);
							AddAns(i - 1, j + 1);
							AddAns(i, j);
						}
						else
						{
							AddAns(i, j);
							AddAns(i, j + 1);
							AddAns(i - 1, j);

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

							AddAns(i, j);
							AddAns(i - 1, j);
							AddAns(i - 1, j + 1);
						}
					}
				}
				else
				{
					if (i + 1 < n)
					{
						AddAns(i, j);
						AddAns(i + 1, j);
						AddAns(i + 1, j - 1);
					}
					else
					{
						AddAns(i, j);
						AddAns(i, j - 1);
						AddAns(i - 1, j);

						AddAns(i, j);
						AddAns(i, j - 1);
						AddAns(i - 1, j - 1);

						AddAns(i, j);
						AddAns(i - 1, j);
						AddAns(i - 1, j - 1);
					}
				}
			}
		}

		cout << ans.size() / 3 << endl;
		for (int i = 0; i < ans.size(); i += 3)
		{
			cout << ans[i].first << " " << ans[i].second;
			cout << " " << ans[i + 1].first << " " << ans[i + 1].second;
			cout << " " << ans[i + 2].first << " " << ans[i + 2].second;
			cout << endl;
		}
	}
	return 0;
}


// acm.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>

using namespace std;

const int N = 100;
const int M = 100;

int t[N][M];
vector<pair<int, int>> ans;

void AddAns(int i, int j)
{
	ans.push_back(pair<int, int>(i + 1, j + 1));
	t[i][j] ^= 0x01;
}

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int n, m;
		cin >> n >> m;
		for (int i = 0; i < n; i++)
		{
			string str;
			cin >> str;
			for (int j = 0; j < m; j++)
			{
				t[i][j] = (str[j] == '1') ? 1 : 0;
			}
		}
		ans.clear();
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				if (t[i][j] == 0)
				{
					continue;
				}
				if (j + 1 < m)
				{
					if (i + 1 < n)
					{
						if (t[i][j+1] == 1)
						{
							AddAns(i, j);
							AddAns(i, j + 1);
							if (t[i+1][j] == 1)
							{
								AddAns(i + 1, j);
							}
							else
							{
								AddAns(i + 1, j + 1);
							}
						}
						else
						{
							AddAns(i, j);
							AddAns(i + 1, j);
							AddAns(i + 1, j + 1);
						}
					}
					else
					{
						if (t[i][j+1] == 1)
						{
							AddAns(i - 1, j);
							AddAns(i - 1, j + 1);
							AddAns(i, j + 1);

							AddAns(i - 1, j);
							AddAns(i - 1, j + 1);
							AddAns(i, j);
						}
						else
						{
							AddAns(i, j);
							AddAns(i, j + 1);
							AddAns(i - 1, j);

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

							AddAns(i, j);
							AddAns(i - 1, j);
							AddAns(i - 1, j + 1);
						}
					}
				}
				else
				{
					if (i + 1 < n)
					{
						AddAns(i, j);
						AddAns(i + 1, j);
						AddAns(i + 1, j - 1);
					}
					else
					{
						AddAns(i, j);
						AddAns(i, j - 1);
						AddAns(i - 1, j);

						AddAns(i, j);
						AddAns(i, j - 1);
						AddAns(i - 1, j - 1);

						AddAns(i, j);
						AddAns(i - 1, j);
						AddAns(i - 1, j - 1);
					}
				}
			}
		}

		cout << ans.size() << endl;
		for (int i = 0; i < ans.size(); i += 3)
		{
			cout << ans[i].first << " " << ans[i].second;
			cout << " " << ans[i + 1].first << " " << ans[i + 1].second;
			cout << " " << ans[i + 2].first << " " << ans[i + 2].second;
			cout << endl;
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int N=200,M=3e4+100;
int T,n,m,ans;
int dx[4]={0,1,0,1},dy[4]={0,0,1,1};
int x1[M],x2[M],x3[M],y1[M],y2[M],y3[M],a[N][N];
string s;
int ano(int x)
{
	return ((x-1)^1)+1;
}
void solve1(int i,int j)
{
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(a[x][y])
		{
			a[x][y]=0,a[ano(x)][y]=1,a[ano(x)][ano(y)]=1,x1[++ans]=x,x2[ans]=ano(x),x3[ans]=ano(x),y1[ans]=y,y2[ans]=y,y3[ans]=ano(y);
			break;
		}
}
void solve2(int i,int j)
{//cout<<"B"<<endl;
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(!a[x][y])
			if(x2[ans])
				a[x][y]=1,x1[++ans]=x,y1[ans]=y;
			else
				a[x][y]=1,x2[ans]=x,y2[ans]=y;
		else
			if(!x3[ans])
				a[x][y]=0,x3[ans]=x,y3[ans]=y;
}
void solve3(int i1,int i2,int j1,int j2)
{//cout<<"C"<<endl;
	a[i1][j1]=a[i2][j2]=0,a[ano(i1)][j1]=1,x1[++ans]=i1,y1[ans]=j1,x2[ans]=i2,y2[ans]=j2,x3[ans]=ano(i1),y3[ans]=j1;
}
void solve4(int i,int j)
{
	int i1=0,i2,j1,j2;
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(a[x][y])
			if(i1)
				i2=x,j2=y;
			else
				i1=x,j1=y;
	if(i1!=ano(i2) || j1!=ano(j2))
		solve2(i,j);
	else
		solve3(i1,i2,j1,j2),solve1(i,j),solve2(i,j);
}
void solve5(int i,int j)
{//cout<<"E"<<endl;
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(!a[x][y])
		{//cout<<x<<" "<<y<<" "<<ano(x)<<" "<<ano(y)<<endl;
			a[ano(x)][y]=a[ano(x)][ano(y)]=a[x][ano(y)]=0,x1[++ans]=ano(x),x2[ans]=ano(x),x3[ans]=x,y1[ans]=y,y2[ans]=ano(y),y3[ans]=ano(y);
			break;
		}
}
void solve6(int x,int y)
{
	a[ano(x)][y]=a[ano(x)][ano(y)]=a[x][ano(y)]=0,x1[++ans]=ano(x),x2[ans]=ano(x),x3[ans]=x,y1[ans]=y,y2[ans]=ano(y),y3[ans]=ano(y);
	solve1(x,y),solve2(x,y);
}
int main()
{
	//freopen("CF1439A1.out","w",stdout);
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);ans=0;
		for(int i=1;i<=n*m;i++)
			x1[i]=x2[i]=x3[i]=y1[i]=y2[i]=y3[i]=0;
		x1[0]=x2[0]=x3[0]=y1[0]=y2[0]=y3[0]=1;
		for(int i=1;i<=n;i++)
		{
			cin>>s;
			for(int j=1;j<=m;j++)
				a[i][j]=s[j-1]-'0';
		}
		if(n&1)
			for(int i=1;i<=m;i++)
				if(a[n][i])
					a[n][i]=0,a[n-1][i]^=1,a[n-1][i==m?i-1:i+1]^=1,x1[++ans]=n,x2[ans]=n-1,x3[ans]=n-1,y1[ans]=i,y2[ans]=i,y3[ans]=i==m?i-1:i+1;
		if(m&1)
			for(int i=1;i<=n;i++)
				if(a[i][m])
					if(i<m)
						a[i][m]=0,a[i][m-1]^=1,a[i==1?i+1:i-1][m-1]^=1,x1[++ans]=i,x2[ans]=i,x3[ans]=i==1?i+1:i-1,y1[ans]=m,y2[ans]=m-1,y3[ans]=m-1;
					else
						a[n][m-1]=0,a[n-1][m]^=1,a[n-1][m-1]^=1,x1[++ans]=n,x2[ans]=n-1,x3[ans]=n-1,y1[ans]=m,y2[ans]=m,y3[ans]=m-1;
		for(int i=1;i<=n;i+=2)
			for(int j=1;j<=m;j+=2)
			{
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==1)
					solve1(i,j);
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==2)
					solve4(i,j);
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==3)
					solve5(i,j);
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==4)
					solve6(i,j);
			}
		printf("%d\n",ans);
		for(int i=1;i<=ans;i++)
			printf("%d %d %d %d %d %d\n",x1[i],y1[i],x2[i],y2[i],x3[i],y3[i]);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int N=200,M=3e4+100;
int T,n,m,ans;
int dx[4]={0,1,0,1},dy[4]={0,0,1,1};
int x1[M],x2[M],x3[M],y1[M],y2[M],y3[M],a[N][N];
string s;
int ano(int x)
{
	return ((x-1)^1)+1;
}
void solve1(int i,int j)
{
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(a[x][y])
		{
			a[x][y]=0,a[ano(x)][y]=1,a[ano(x)][ano(y)]=1,x1[++ans]=x,x2[ans]=ano(x),x3[ans]=ano(x),y1[ans]=y,y2[ans]=y,y3[ans]=ano(y);
			break;
		}
}
void solve2(int i,int j)
{//cout<<"B"<<endl;
	ans++;
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(!a[x][y])
			if(!x1[ans])
				a[x][y]=1,x1[ans]=x,y1[ans]=y;
			else
				a[x][y]=1,x2[ans]=x,y2[ans]=y;
		else
			if(!x3[ans])
				a[x][y]=0,x3[ans]=x,y3[ans]=y;
}
void solve3(int i1,int i2,int j1,int j2)
{//cout<<"C"<<endl;
	a[i1][j1]=a[i2][j2]=0,a[ano(i1)][j1]=1,x1[++ans]=i1,y1[ans]=j1,x2[ans]=i2,y2[ans]=j2,x3[ans]=ano(i1),y3[ans]=j1;
}
void solve4(int i,int j)
{
	int i1=0,i2,j1,j2;
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(a[x][y])
			if(i1)
				i2=x,j2=y;
			else
				i1=x,j1=y;
	if(i1!=ano(i2) || j1!=ano(j2))
		solve2(i,j);
	else
		solve3(i1,i2,j1,j2),solve1(i,j),solve2(i,j);
}
void solve5(int i,int j)
{//cout<<"E"<<endl;
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(!a[x][y])
		{//cout<<x<<" "<<y<<" "<<ano(x)<<" "<<ano(y)<<endl;
			a[ano(x)][y]=a[ano(x)][ano(y)]=a[x][ano(y)]=0,x1[++ans]=ano(x),x2[ans]=ano(x),x3[ans]=x,y1[ans]=y,y2[ans]=ano(y),y3[ans]=ano(y);
			break;
		}
}
void solve6(int x,int y)
{
	a[ano(x)][y]=a[ano(x)][ano(y)]=a[x][ano(y)]=0,x1[++ans]=ano(x),x2[ans]=ano(x),x3[ans]=x,y1[ans]=y,y2[ans]=ano(y),y3[ans]=ano(y);
	solve1(x,y),solve2(x,y),solve5(x,y);
}
int main()
{int cnt=0;
	//freopen("CF1439A1.out","w",stdout);
	scanf("%d",&T);//x1[0]=x2[0]=x3[0]=y1[0]=y2[0]=y3[0]=1;
	while(T--)
	{//cout<<++cnt<<endl;
		scanf("%d%d",&n,&m);ans=0;
		for(int i=1;i<=n*m;i++)
			x1[i]=x2[i]=x3[i]=y1[i]=y2[i]=y3[i]=0;

		for(int i=1;i<=n;i++)
		{
			cin>>s;
			for(int j=1;j<=m;j++)
				a[i][j]=s[j-1]-'0';
		}
		if(n&1)
			for(int i=1;i<=m;i++)
				if(a[n][i])
					a[n][i]=0,a[n-1][i]^=1,a[n-1][i==m?i-1:i+1]^=1,x1[++ans]=n,x2[ans]=n-1,x3[ans]=n-1,y1[ans]=i,y2[ans]=i,y3[ans]=i==m?i-1:i+1;
		if(m&1)
			for(int i=1;i<=n;i++)
				if(a[i][m])
					if(i<m)
						a[i][m]=0,a[i][m-1]^=1,a[i==1?i+1:i-1][m-1]^=1,x1[++ans]=i,x2[ans]=i,x3[ans]=i==1?i+1:i-1,y1[ans]=m,y2[ans]=m-1,y3[ans]=m-1;
					else
						a[n][m-1]=0,a[n-1][m]^=1,a[n-1][m-1]^=1,x1[++ans]=n,x2[ans]=n-1,x3[ans]=n-1,y1[ans]=m,y2[ans]=m,y3[ans]=m-1;
		for(int i=1;i<=n;i+=2)
			for(int j=1;j<=m;j+=2)
			{
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==1)
					solve1(i,j);
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==2)
					solve4(i,j);
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==3)
					solve5(i,j);
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==4)
					solve6(i,j);
			}
		printf("%d\n",ans);
		for(int i=1;i<=ans;i++)
			printf("%d %d %d %d %d %d\n",x1[i],y1[i],x2[i],y2[i],x3[i],y3[i]);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int N=200,M=3e4+100;
int T,n,m,ans;
int dx[4]={0,1,0,1},dy[4]={0,0,1,1};
int x1[M],x2[M],x3[M],y1[M],y2[M],y3[M],a[N][N];
string s;
int ano(int x)
{
	return ((x-1)^1)+1;
}
void solve1(int i,int j)
{
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(a[x][y])
		{
			a[x][y]=0,a[ano(x)][y]=1,a[ano(x)][ano(y)]=1,x1[++ans]=x,x2[ans]=ano(x),x3[ans]=ano(x),y1[ans]=y,y2[ans]=y,y3[ans]=ano(y);
			break;
		}
}
void solve2(int i,int j)
{//cout<<"B"<<endl;
	ans++;
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(!a[x][y])
			if(!x1[ans])
				a[x][y]=1,x1[ans]=x,y1[ans]=y;
			else
				a[x][y]=1,x2[ans]=x,y2[ans]=y;
		else
			if(!x3[ans])
				a[x][y]=0,x3[ans]=x,y3[ans]=y;
}
void solve3(int i1,int i2,int j1,int j2)
{//cout<<"C"<<endl;
	a[i1][j1]=a[i2][j2]=0,a[ano(i1)][j1]=1,x1[++ans]=i1,y1[ans]=j1,x2[ans]=i2,y2[ans]=j2,x3[ans]=ano(i1),y3[ans]=j1;
}
void solve4(int i,int j)
{
	int i1=0,i2,j1,j2;
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(a[x][y])
			if(i1)
				i2=x,j2=y;
			else
				i1=x,j1=y;
	if(i1!=ano(i2) || j1!=ano(j2))
		solve2(i,j);
	else
		solve3(i1,i2,j1,j2),solve1(i,j),solve2(i,j);
}
void solve5(int i,int j)
{//cout<<"E"<<endl;
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(!a[x][y])
		{//cout<<x<<" "<<y<<" "<<ano(x)<<" "<<ano(y)<<endl;
			a[ano(x)][y]=a[ano(x)][ano(y)]=a[x][ano(y)]=0,x1[++ans]=ano(x),x2[ans]=ano(x),x3[ans]=x,y1[ans]=y,y2[ans]=ano(y),y3[ans]=ano(y);
			break;
		}
}
void solve6(int x,int y)
{
	a[ano(x)][y]=a[ano(x)][ano(y)]=a[x][ano(y)]=0,x1[++ans]=ano(x),x2[ans]=ano(x),x3[ans]=x,y1[ans]=y,y2[ans]=ano(y),y3[ans]=ano(y);
	solve1(x,y),solve2(x,y);
}
int main()
{
	//freopen("CF1439A1.out","w",stdout);
	scanf("%d",&T);//x1[0]=x2[0]=x3[0]=y1[0]=y2[0]=y3[0]=1;
	while(T--)
	{
		scanf("%d%d",&n,&m);ans=0;
		for(int i=1;i<=n*m;i++)
			x1[i]=x2[i]=x3[i]=y1[i]=y2[i]=y3[i]=0;

		for(int i=1;i<=n;i++)
		{
			cin>>s;
			for(int j=1;j<=m;j++)
				a[i][j]=s[j-1]-'0';
		}
		if(n&1)
			for(int i=1;i<=m;i++)
				if(a[n][i])
					a[n][i]=0,a[n-1][i]^=1,a[n-1][i==m?i-1:i+1]^=1,x1[++ans]=n,x2[ans]=n-1,x3[ans]=n-1,y1[ans]=i,y2[ans]=i,y3[ans]=i==m?i-1:i+1;
		if(m&1)
			for(int i=1;i<=n;i++)
				if(a[i][m])
					if(i<m)
						a[i][m]=0,a[i][m-1]^=1,a[i==1?i+1:i-1][m-1]^=1,x1[++ans]=i,x2[ans]=i,x3[ans]=i==1?i+1:i-1,y1[ans]=m,y2[ans]=m-1,y3[ans]=m-1;
					else
						a[n][m-1]=0,a[n-1][m]^=1,a[n-1][m-1]^=1,x1[++ans]=n,x2[ans]=n-1,x3[ans]=n-1,y1[ans]=m,y2[ans]=m,y3[ans]=m-1;
		for(int i=1;i<=n;i+=2)
			for(int j=1;j<=m;j+=2)
			{
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==1)
					solve1(i,j);
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==2)
					solve4(i,j);
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==3)
					solve5(i,j);
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==4)
					solve6(i,j);
			}
		printf("%d\n",ans);
		for(int i=1;i<=ans;i++)
			printf("%d %d %d %d %d %d\n",x1[i],y1[i],x2[i],y2[i],x3[i],y3[i]);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int N=200,M=3e4+100;
int T,n,m,ans;
int dx[4]={0,1,0,1},dy[4]={0,0,1,1};
int x1[M],x2[M],x3[M],y1[M],y2[M],y3[M],a[N][N];
string s;
int ano(int x)
{
	return ((x-1)^1)+1;
}
void solve1(int i,int j)
{
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(a[x][y])
		{
			a[x][y]=0,a[ano(x)][y]=1,a[ano(x)][ano(y)]=1,x1[++ans]=x,x2[ans]=ano(x),x3[ans]=ano(x),y1[ans]=y,y2[ans]=y,y3[ans]=ano(y);
			break;
		}
}
void solve2(int i,int j)
{//cout<<"B"<<endl;
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(!a[x][y])
			if(x2[ans])
				a[x][y]=1,x1[++ans]=x,y1[ans]=y;
			else
				a[x][y]=1,x2[ans]=x,y2[ans]=y;
		else
			if(!x3[ans])
				a[x][y]=0,x3[ans]=x,y3[ans]=y;
}
void solve3(int i1,int i2,int j1,int j2)
{//cout<<"C"<<endl;
	a[i1][j1]=a[i2][j2]=0,a[ano(i1)][j1]=1,x1[++ans]=i1,y1[ans]=j1,x2[ans]=i2,y2[ans]=j2,x3[ans]=ano(i1),y3[ans]=j1;
}
void solve4(int i,int j)
{
	int i1=0,i2,j1,j2;
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(a[x][y])
			if(i1)
				i2=x,j2=y;
			else
				i1=x,j1=y;
	if(i1!=ano(i2) || j1!=ano(j2))
		solve2(i,j);
	else
		solve3(i1,i2,j1,j2),solve1(i,j),solve2(i,j);
}
void solve5(int i,int j)
{//cout<<"E"<<endl;
	for(int k=0,x=i+dx[k],y=j+dy[k];k<4;k++,x=i+dx[k],y=j+dy[k])
		if(!a[x][y])
		{//cout<<x<<" "<<y<<" "<<ano(x)<<" "<<ano(y)<<endl;
			a[ano(x)][y]=a[ano(x)][ano(y)]=a[x][ano(y)]=0,x1[++ans]=ano(x),x2[ans]=ano(x),x3[ans]=x,y1[ans]=y,y2[ans]=ano(y),y3[ans]=ano(y);
			break;
		}
}
void solve6(int x,int y)
{
	a[ano(x)][y]=a[ano(x)][ano(y)]=a[x][ano(y)]=0,x1[++ans]=ano(x),x2[ans]=ano(x),x3[ans]=x,y1[ans]=y,y2[ans]=ano(y),y3[ans]=ano(y);
	solve1(x,y),solve2(x,y);
}
int main()
{
	//freopen("CF1439A1.out","w",stdout);
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);ans=0;
		for(int i=1;i<=n*m;i++)
			x1[i]=x2[i]=x3[i]=y1[i]=y2[i]=y3[i]=0;
		for(int i=1;i<=n;i++)
		{
			cin>>s;
			for(int j=1;j<=m;j++)
				a[i][j]=s[j-1]-'0';
		}
		if(n&1)
			for(int i=1;i<=m;i++)
				if(a[n][i])
					a[n][i]=0,a[n-1][i]^=1,a[n-1][i==m?i-1:i+1]^=1,x1[++ans]=n,x2[ans]=n-1,x3[ans]=n-1,y1[ans]=i,y2[ans]=i,y3[ans]=i==m?i-1:i+1;
		if(m&1)
			for(int i=1;i<=n;i++)
				if(a[i][m])
					if(i<m)
						a[i][m]=0,a[i][m-1]^=1,a[i==1?i+1:i-1][m-1]^=1,x1[++ans]=i,x2[ans]=i,x3[ans]=i==1?i+1:i-1,y1[ans]=m,y2[ans]=m-1,y3[ans]=m-1;
					else
						a[n][m-1]=0,a[n-1][m]^=1,a[n-1][m-1]^=1,x1[++ans]=n,x2[ans]=n-1,x3[ans]=n-1,y1[ans]=m,y2[ans]=m,y3[ans]=m-1;
		for(int i=1;i<=n;i+=2)
			for(int j=1;j<=m;j+=2)
			{
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==1)
					solve1(i,j);
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==2)
					solve4(i,j);
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==3)
					solve5(i,j);
				if(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]==4)
					solve6(i,j);
			}
		printf("%d\n",ans);
		for(int i=1;i<=ans;i++)
			printf("%d %d %d %d %d %d\n",x1[i],y1[i],x2[i],y2[i],x3[i],y3[i]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read(){
	register int res=0;
	register char c=getchar(),f=1;
	while(c<48||c>57){if(c=='-')f=0;c=getchar();}
	while(c>=48&&c<=57)res=(res<<3)+(res<<1)+(c&15),c=getchar();
	return f?res:-res;
}
inline void write(int x){
	register char c[21],len=0;
	if(!x)return putchar('0'),void();
	if(x<0)x=-x,putchar('-');
	while(x)c[++len]=x%10,x/=10;
	while(len)putchar(c[len--]+48);
}
#define space(x) write(x),putchar(' ')
#define enter(x) write(x),putchar('\n')

const int N=110;
int n,m;
char a[N][N];
int op[N][6],k,o,A,B,C,D;
inline void rev(int x,int y){
	op[k][o++]=x,op[k][o++]=y,a[x][y]^=1;
}
void sol3(int x,int y){
	A=a[x][y],B=a[x][y+1],C=a[x+1][y],D=a[x+1][y+1];
	++k,o=0;
	if(A)rev(x,y);
	if(B)rev(x,y+1);
	if(C)rev(x+1,y);
	if(D)rev(x+1,y+1);
}
void sol2(int x,int y){
	A=a[x][y],B=a[x][y+1],C=a[x+1][y],D=a[x+1][y+1];
	++k,o=0;
	if(!A)rev(x,y);
	if(!B)rev(x,y+1);
	if(!C)rev(x+1,y);
	if(!D)rev(x+1,y+1);
	if(A)rev(x,y);else
	if(B)rev(x,y+1);else
	if(C)rev(x+1,y);else
	if(D)rev(x+1,y+1);
	sol3(x,y);
}
void sol1(int x,int y){
	A=a[x][y],B=a[x][y+1],C=a[x+1][y],D=a[x+1][y+1];
	++k,o=0;
	if(A)rev(x,y);
	if(B)rev(x,y+1);
	if(C)rev(x+1,y);
	if(D)rev(x+1,y+1);
	if(!A&&o<6)rev(x,y);
	if(!B&&o<6)rev(x,y+1);
	if(!C&&o<6)rev(x+1,y);
	if(!D&&o<6)rev(x+1,y+1);
	sol2(x,y);
}
void sol4(int x,int y){
	++k,o=0;
	rev(x,y),rev(x+1,y),rev(x,y+1);
	sol1(x,y);
}
void sol(int x,int y){
	int t=a[x][y]+a[x][y+1]+a[x+1][y]+a[x+1][y+1];
	if(t==3)sol3(x,y);
	else if(t==2)sol2(x,y);
	else if(t==1)sol1(x,y);
	else if(t==4)sol4(x,y);
}
void solve(){
	n=read(),m=read(),k=0;
	for(int i=1;i<=n;++i)scanf("%s",a[i]+1);
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			a[i][j]-=48;

	for(int i=1;i<=n-2;++i)
		for(int j=1;j<=m-2;++j)if(a[i][j]){
			++k,o=0;
			rev(i,j),rev(i,j+1),rev(i+1,j);
		}
	for(int i=1;i<=n-2;++i)sol(i,m-1);
	for(int i=1;i<=m-1;++i)sol(n-1,i);
	enter(k);
	for(int i=1;i<=k;++i,puts(""))
		for(int j=0;j<6;++j)
			space(op[i][j]);

/*	for(int i=1;i<=n;++i,puts(""))
		for(int j=1;j<=m;++j)
			printf("%d",a[i][j]);*/
}

int main(){
	int T=read();
	while(T--)solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read(){
	register int res=0;
	register char c=getchar(),f=1;
	while(c<48||c>57){if(c=='-')f=0;c=getchar();}
	while(c>=48&&c<=57)res=(res<<3)+(res<<1)+(c&15),c=getchar();
	return f?res:-res;
}
inline void write(int x){
	register char c[21],len=0;
	if(!x)return putchar('0'),void();
	if(x<0)x=-x,putchar('-');
	while(x)c[++len]=x%10,x/=10;
	while(len)putchar(c[len--]+48);
}
#define space(x) write(x),putchar(' ')
#define enter(x) write(x),putchar('\n')

const int N=110;
int n,m;
char a[N][N];
int op[N*N][6],k,o,A,B,C,D;
inline void rev(int x,int y){
	op[k][o++]=x,op[k][o++]=y,a[x][y]^=1;
}
void sol3(int x,int y){
	A=a[x][y],B=a[x][y+1],C=a[x+1][y],D=a[x+1][y+1];
	++k,o=0;
	if(A)rev(x,y);
	if(B)rev(x,y+1);
	if(C)rev(x+1,y);
	if(D)rev(x+1,y+1);
}
void sol2(int x,int y){
	A=a[x][y],B=a[x][y+1],C=a[x+1][y],D=a[x+1][y+1];
	++k,o=0;
	if(!A)rev(x,y);
	if(!B)rev(x,y+1);
	if(!C)rev(x+1,y);
	if(!D)rev(x+1,y+1);
	if(A)rev(x,y);else
	if(B)rev(x,y+1);else
	if(C)rev(x+1,y);else
	if(D)rev(x+1,y+1);
	sol3(x,y);
}
void sol1(int x,int y){
	A=a[x][y],B=a[x][y+1],C=a[x+1][y],D=a[x+1][y+1];
	++k,o=0;
	if(A)rev(x,y);
	if(B)rev(x,y+1);
	if(C)rev(x+1,y);
	if(D)rev(x+1,y+1);
	if(!A&&o<6)rev(x,y);
	if(!B&&o<6)rev(x,y+1);
	if(!C&&o<6)rev(x+1,y);
	if(!D&&o<6)rev(x+1,y+1);
	sol2(x,y);
}
void sol4(int x,int y){
	++k,o=0;
	rev(x,y),rev(x+1,y),rev(x,y+1);
	sol1(x,y);
}
void sol(int x,int y){
	int t=a[x][y]+a[x][y+1]+a[x+1][y]+a[x+1][y+1];
	if(t==3)sol3(x,y);
	else if(t==2)sol2(x,y);
	else if(t==1)sol1(x,y);
	else if(t==4)sol4(x,y);
}
void solve(){
	n=read(),m=read(),k=0;
	for(int i=1;i<=n;++i)scanf("%s",a[i]+1);
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			a[i][j]-=48;

	for(int i=1;i<=n-2;++i)
		for(int j=1;j<=m-2;++j)if(a[i][j]){
			++k,o=0;
			rev(i,j),rev(i,j+1),rev(i+1,j);
		}
	for(int i=1;i<=n-2;++i)sol(i,m-1);
	for(int i=1;i<=m-1;++i)sol(n-1,i);
	enter(k);
	for(int i=1;i<=k;++i,puts(""))
		for(int j=0;j<6;++j)
			space(op[i][j]);

/*	for(int i=1;i<=n;++i,puts(""))
		for(int j=1;j<=m;++j)
			printf("%d",a[i][j]);*/
}

int main(){
	int T=read();
	while(T--)solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read(){
	register int res=0;
	register char c=getchar(),f=1;
	while(c<48||c>57){if(c=='-')f=0;c=getchar();}
	while(c>=48&&c<=57)res=(res<<3)+(res<<1)+(c&15),c=getchar();
	return f?res:-res;
}
inline void write(int x){
	register char c[21],len=0;
	if(!x)return putchar('0'),void();
	if(x<0)x=-x,putchar('-');
	while(x)c[++len]=x%10,x/=10;
	while(len)putchar(c[len--]+48);
}
#define space(x) write(x),putchar(' ')
#define enter(x) write(x),putchar('\n')

const int N=110;
int n,m;
char a[N][N];
int op[N*N][6],k,o,A,B,C,D;
inline void rev(int x,int y){
	op[k][o++]=x,op[k][o++]=y,a[x][y]^=1;
}
void sol3(int x,int y){
	A=a[x][y],B=a[x][y+1],C=a[x+1][y],D=a[x+1][y+1];
	++k,o=0;
	if(A)rev(x,y);
	if(B)rev(x,y+1);
	if(C)rev(x+1,y);
	if(D)rev(x+1,y+1);
}
void sol2(int x,int y){
	A=a[x][y],B=a[x][y+1],C=a[x+1][y],D=a[x+1][y+1];
	++k,o=0;
	if(!A)rev(x,y);
	if(!B)rev(x,y+1);
	if(!C)rev(x+1,y);
	if(!D)rev(x+1,y+1);
	if(A)rev(x,y);else
	if(B)rev(x,y+1);else
	if(C)rev(x+1,y);else
	if(D)rev(x+1,y+1);
	sol3(x,y);
}
void sol1(int x,int y){
	A=a[x][y],B=a[x][y+1],C=a[x+1][y],D=a[x+1][y+1];
	++k,o=0;
	if(A)rev(x,y);
	if(B)rev(x,y+1);
	if(C)rev(x+1,y);
	if(D)rev(x+1,y+1);
	if(!A&&o<6)rev(x,y);
	if(!B&&o<6)rev(x,y+1);
	if(!C&&o<6)rev(x+1,y);
	if(!D&&o<6)rev(x+1,y+1);
	sol2(x,y);
}
void sol4(int x,int y){
	++k,o=0;
	rev(x,y),rev(x+1,y),rev(x,y+1);
	sol1(x,y);
}
void sol(int x,int y){
	int t=a[x][y]+a[x][y+1]+a[x+1][y]+a[x+1][y+1];
	if(t==3)sol3(x,y);
	else if(t==2)sol2(x,y);
	else if(t==1)sol1(x,y);
	else if(t==4)sol4(x,y);
}
void solve(){
	n=read(),m=read(),k=0;
	for(int i=1;i<=n;++i)scanf("%s",a[i]+1);
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			a[i][j]-=48;

	for(int x=1;x<=n-2;++x)
		for(int y=1;y<=m-2;++y)if(a[x][y]){
			A=a[x][y],B=a[x][y+1],C=a[x+1][y],D=a[x+1][y+1];
			++k,o=0;
			if(A)rev(x,y);
			if(B)rev(x,y+1);
			if(C)rev(x+1,y);
			if(D&&o<6)rev(x+1,y+1);
			if(!B&&o<6)rev(x,y+1);
			if(!C&&o<6)rev(x+1,y);
			if(!D&&o<6)rev(x+1,y+1);
		}
	for(int i=1;i<=n-2;i+=2)sol(i,m-1);
	for(int i=1;i<=m-2;i+=2)sol(n-1,i);
	sol(n-1,m-1);
	enter(k);
	for(int i=1;i<=k;++i,puts(""))
		for(int j=0;j<6;++j)
			space(op[i][j]);

/*	for(int i=1;i<=n;++i,puts(""))
		for(int j=1;j<=m;++j)
			printf("%d",a[i][j]);*/
}

int main(){
	int T=read();
	while(T--)solve();
}


#include<bits/stdc++.h>
typedef unsigned long long ull;
typedef long long ll;
template<class T1,class T2,class T3>T1 ksm(T1 a,T2 b,T3 mod){T1 ans=1;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
#ifndef ONLINE_JUDGE
template<class T>T __gcd(T a,T b){ll r;while(b>0){r=a%b;a=b;b=r;}return a;}
#endif
#define inf INT_MAX
const double pi=acos(-1.);
const int def=110;
const int mod=1000000007;
using namespace std;

struct node{
	int x,y;
};
struct node1{
	node n1,n2,n3;
};

vector<node>v[2];
vector<node1>ans;
int a[def][def];

void out(int x,int y,int z)
{	node now1,now2,now3;
	now1=v[x].back();v[x].pop_back();
	//printf("%d %d ",now.x,now.y);
	now2=v[y].back();v[y].pop_back();
	//printf("%d %d ",now.x,now.y);
	now3=v[z].back();v[z].pop_back();
	//printf("%d %d\n",now.x,now.y);
	ans.push_back({now1,now2,now3});
	v[x^1].push_back(now1);
	v[y^1].push_back(now2);
	v[z^1].push_back(now3);
}

int main()
{	int _=1,__=1,n,m;
	#ifndef ONLINE_JUDGE
	freopen("/Users/py/Documents/workspace/C++/try.in","r",stdin);
	freopen("/Users/py/Documents/workspace/C++/try.out","w",stdout);
	#endif
	for(((1)?scanf("%d",&_):EOF);_;_--,__++){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++){
				char c;
				scanf(" %c",&c);
				a[i][j]=c-'0';
			}
		for(int i=1;i<n;i++)
			for(int j=1;j<m;j++){
				v[0].clear();
				v[1].clear();
				for(int x=0;x<=1;x++)
					for(int y=0;y<=1;y++)
						if(a[i+x][j+y])v[1].push_back({i+x,j+y});
						else v[0].push_back({i+x,j+y});
				switch(v[1].size()){
					case 0:break;
					case 4:
						out(1,1,1);
					case 1:
						out(1,0,0);
					case 2:
						out(1,0,0);
					case 3:
						out(1,1,1);
				}
			}
		printf("%lu\n",ans.size());
		for(auto i:ans)
			printf("%d %d %d %d %d %d\n",i.n1.x,i.n1.y,i.n2.x,i.n2.y,i.n3.x,i.n3.y);
	}
	return 0;
}


#include<bits/stdc++.h>
typedef unsigned long long ull;
typedef long long ll;
template<class T1,class T2,class T3>T1 ksm(T1 a,T2 b,T3 mod){T1 ans=1;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
#ifndef ONLINE_JUDGE
template<class T>T __gcd(T a,T b){ll r;while(b>0){r=a%b;a=b;b=r;}return a;}
#endif
#define inf INT_MAX
const double pi=acos(-1.);
const int def=110;
const int mod=1000000007;
using namespace std;

struct node{
	int x,y;
};
struct node1{
	node n1,n2,n3;
};

vector<node>v[2];
vector<node1>ans;
int a[def][def];

void out(int x,int y,int z)
{	node now1,now2,now3;
	now1=v[x].back();v[x].pop_back();
	//printf("%d %d ",now.x,now.y);
	now2=v[y].back();v[y].pop_back();
	//printf("%d %d ",now.x,now.y);
	now3=v[z].back();v[z].pop_back();
	//printf("%d %d\n",now.x,now.y);
	ans.push_back({now1,now2,now3});
	v[x^1].push_back(now1);
	v[y^1].push_back(now2);
	v[z^1].push_back(now3);
}

int main()
{	int _=1,__=1,n,m;
	#ifndef ONLINE_JUDGE
	freopen("/Users/py/Documents/workspace/C++/try.in","r",stdin);
	freopen("/Users/py/Documents/workspace/C++/try.out","w",stdout);
	#endif
	for(((1)?scanf("%d",&_):EOF);_;_--,__++){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++){
				char c;
				scanf(" %c",&c);
				a[i][j]=c-'0';
			}
		ans.clear();
		for(int i=1;i<n;i++)if(i%2||i==n-1)
			for(int j=1;j<m;j++){
				v[0].clear();
				v[1].clear();
				for(int x=0;x<=1;x++)
					for(int y=0;y<=1;y++)
						if(a[i+x][j+y])v[1].push_back({i+x,j+y});
						else v[0].push_back({i+x,j+y});
				switch(v[1].size()){
					case 0:break;
					case 4:
						out(1,1,1);
					case 1:
						out(1,0,0);
					case 2:
						out(1,0,0);
					case 3:
						out(1,1,1);
				}
			}
		printf("%lu\n",ans.size());
		for(auto i:ans)
			printf("%d %d %d %d %d %d\n",i.n1.x,i.n1.y,i.n2.x,i.n2.y,i.n3.x,i.n3.y);
	}
	return 0;
}


#include<bits/stdc++.h>
typedef unsigned long long ull;
typedef long long ll;
template<class T1,class T2,class T3>T1 ksm(T1 a,T2 b,T3 mod){T1 ans=1;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
#ifndef ONLINE_JUDGE
template<class T>T __gcd(T a,T b){ll r;while(b>0){r=a%b;a=b;b=r;}return a;}
#endif
#define inf INT_MAX
const double pi=acos(-1.);
const int def=110;
const int mod=1000000007;
using namespace std;

struct node{
	int x,y;
};
struct node1{
	node n1,n2,n3;
};

vector<node>v[2];
vector<node1>ans;
int a[def][def];

void out(int x,int y,int z)
{	node now1,now2,now3;
	now1=v[x].back();v[x].pop_back();
	//printf("%d %d ",now.x,now.y);
	now2=v[y].back();v[y].pop_back();
	//printf("%d %d ",now.x,now.y);
	now3=v[z].back();v[z].pop_back();
	//printf("%d %d\n",now.x,now.y);
	ans.push_back({now1,now2,now3});
	v[x^1].push_back(now1);
	v[y^1].push_back(now2);
	v[z^1].push_back(now3);
	a[now1.x][now1.y]^=1;
	a[now2.x][now2.y]^=1;
	a[now3.x][now3.y]^=1;
}

int main()
{	int _=1,__=1,n,m;
	#ifndef ONLINE_JUDGE
	freopen("/Users/py/Documents/workspace/C++/try.in","r",stdin);
	freopen("/Users/py/Documents/workspace/C++/try.out","w",stdout);
	#endif
	for(((1)?scanf("%d",&_):EOF);_;_--,__++){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++){
				char c;
				scanf(" %c",&c);
				a[i][j]=c-'0';
			}
		ans.clear();
		for(int i=1;i<n;i++)if(i%2||i==n-1)
			for(int j=1;j<m;j++)if(j%2||j==m-1){
				v[0].clear();
				v[1].clear();
				for(int x=0;x<=1;x++)
					for(int y=0;y<=1;y++)
						if(a[i+x][j+y])v[1].push_back({i+x,j+y});
						else v[0].push_back({i+x,j+y});
				switch(v[1].size()){
					case 0:break;
					case 4:
						out(1,1,1);
					case 1:
						out(1,0,0);
					case 2:
						out(1,0,0);
					case 3:
						out(1,1,1);
				}
			}
		printf("%lu\n",ans.size());
		for(auto i:ans)
			printf("%d %d %d %d %d %d\n",i.n1.x,i.n1.y,i.n2.x,i.n2.y,i.n3.x,i.n3.y);
	}
	return 0;
}


#include<bits/stdc++.h>
typedef unsigned long long ull;
typedef long long ll;
template<class T1,class T2,class T3>T1 ksm(T1 a,T2 b,T3 mod){T1 ans=1;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
#ifndef ONLINE_JUDGE
template<class T>T __gcd(T a,T b){ll r;while(b>0){r=a%b;a=b;b=r;}return a;}
#endif
#define inf INT_MAX
const double pi=acos(-1.);
const int def=110;
const int mod=1000000007;
using namespace std;

struct node{
	int x,y;
};
struct node1{
	node n1,n2,n3;
};

vector<node>v[2];
vector<node1>ans;
int a[def][def];

void out(int x,int y,int z)
{	node now1,now2,now3;
	now1=v[x].back();v[x].pop_back();
	//printf("%d %d ",now.x,now.y);
	now2=v[y].back();v[y].pop_back();
	//printf("%d %d ",now.x,now.y);
	now3=v[z].back();v[z].pop_back();
	//printf("%d %d\n",now.x,now.y);
	ans.push_back({now1,now2,now3});
	v[x^1].push_back(now1);
	v[y^1].push_back(now2);
	v[z^1].push_back(now3);
}

int main()
{	int _=1,__=1,n,m;
	#ifndef ONLINE_JUDGE
	freopen("/Users/py/Documents/workspace/C++/try.in","r",stdin);
	freopen("/Users/py/Documents/workspace/C++/try.out","w",stdout);
	#endif
	for(((1)?scanf("%d",&_):EOF);_;_--,__++){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++){
				char c;
				scanf(" %c",&c);
				a[i][j]=c-'0';
			}
		ans.clear();
		for(int i=1;i<n;i++)
			for(int j=1;j<m;j++){
				v[0].clear();
				v[1].clear();
				for(int x=0;x<=1;x++)
					for(int y=0;y<=1;y++)
						if(a[i+x][j+y])v[1].push_back({i+x,j+y});
						else v[0].push_back({i+x,j+y});
				switch(v[1].size()){
					case 0:break;
					case 4:
						out(1,1,1);
					case 1:
						out(1,0,0);
					case 2:
						out(1,0,0);
					case 3:
						out(1,1,1);
				}
			}
		printf("%lu\n",ans.size());
		for(auto i:ans)
			printf("%d %d %d %d %d %d\n",i.n1.x,i.n1.y,i.n2.x,i.n2.y,i.n3.x,i.n3.y);
	}
	return 0;
}


#include<bits/stdc++.h>
typedef unsigned long long ull;
typedef long long ll;
template<class T1,class T2,class T3>T1 ksm(T1 a,T2 b,T3 mod){T1 ans=1;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
#ifndef ONLINE_JUDGE
template<class T>T __gcd(T a,T b){ll r;while(b>0){r=a%b;a=b;b=r;}return a;}
#endif
#define inf INT_MAX
const double pi=acos(-1.);
const int def=110;
const int mod=1000000007;
using namespace std;

struct node{
	int x,y;
};
struct node1{
	node n1,n2,n3;
};

vector<node>v[2];
vector<node1>ans;
int a[def][def];

void out(int x,int y,int z)
{	node now1,now2,now3;
	now1=v[x].back();v[x].pop_back();
	//printf("%d %d ",now.x,now.y);
	now2=v[y].back();v[y].pop_back();
	//printf("%d %d ",now.x,now.y);
	now3=v[z].back();v[z].pop_back();
	//printf("%d %d\n",now.x,now.y);
	ans.push_back({now1,now2,now3});
	v[x^1].push_back(now1);
	v[y^1].push_back(now2);
	v[z^1].push_back(now3);
}

int main()
{	int _=1,__=1,n,m;
	#ifndef ONLINE_JUDGE
	freopen("/Users/py/Documents/workspace/C++/try.in","r",stdin);
	freopen("/Users/py/Documents/workspace/C++/try.out","w",stdout);
	#endif
	for(((1)?scanf("%d",&_):EOF);_;_--,__++){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++){
				char c;
				scanf(" %c",&c);
				a[i][j]=c-'0';
			}
		ans.clear();
		for(int i=1;i<n;i++)if(i%2||i==n-1)
			for(int j=1;j<m;j++)if(j%2||j==m-1){
				v[0].clear();
				v[1].clear();
				for(int x=0;x<=1;x++)
					for(int y=0;y<=1;y++)
						if(a[i+x][j+y])v[1].push_back({i+x,j+y});
						else v[0].push_back({i+x,j+y});
				switch(v[1].size()){
					case 0:break;
					case 4:
						out(1,1,1);
					case 1:
						out(1,0,0);
					case 2:
						out(1,0,0);
					case 3:
						out(1,1,1);
				}
			}
		printf("%lu\n",ans.size());
		for(auto i:ans)
			printf("%d %d %d %d %d %d\n",i.n1.x,i.n1.y,i.n2.x,i.n2.y,i.n3.x,i.n3.y);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <ctime>
#include <set>
#include <vector>
#include <iomanip>
#include <cassert>
using namespace std;
const int Maxn = 1000 + 10;
char s[Maxn][Maxn];
int X[Maxn * Maxn], Y[Maxn * Maxn];
int read()
{
    int rt = 0, in = 1; char ch = getchar();
    while(ch < '0' || ch > '9') {if(ch == '-') in = -1; ch = getchar();}
    while(ch >= '0' && ch <= '9') {rt = rt * 10 + ch - '0'; ch = getchar();}
    return rt * in;
}

int main()
{
    int T = read();
    while(T--)
    {
        int n = read(), m = read();
        for(int i = 1; i <= n; i++) scanf("%s",s[i]+1);
        X[0] = Y[0] = 0;
        for(int i = 1; i < n; i += 2)
        {
            for(int j = 1; j < m; j++)
            {
                if(j != m-1)
                {
                    int tot = 0;
                    if(s[i][j] == '0' && s[i+1][j] == '0')  continue;
                    if(s[i][j] == '1')  tot++, s[i][j] = '0', X[++X[0]] = i, Y[++Y[0]] = j;
                    if(s[i+1][j] == '1')    tot++, s[i+1][j] = '0', X[++X[0]] = i + 1, Y[++Y[0]] = j;
                    if(tot == 1)
                    {
                        X[++X[0]] = i, Y[++Y[0]] = j+1;
                        X[++X[0]] = i+1, Y[++Y[0]] = j+1;
                        if(s[i][j+1] == '0')    s[i][j+1] = '1';
                        else    s[i][j+1] = '0';
                        if(s[i+1][j+1] == '0')  s[i+1][j+1] = '1';
                        else    s[i+1][j+1] = '0';
                    }
                    if(tot == 2)
                    {
                        X[++X[0]] = i, Y[++Y[0]] = j + 1;
                        if(s[i][j+1] == '0')    s[i][j+1] = '1';
                        else    s[i][j+1] = '0';
                    }
                }
                if(j == m-1)
                {
                    int cnt = 0;
                    if(s[i][j] == '1')          cnt++;
                    if(s[i+1][j] == '1')        cnt++;
                    if(s[i][j+1] == '1')        cnt++;
                    if(s[i+1][j+1] == '1')      cnt++;
                    if(cnt == 0)    continue;
                    if(cnt == 3)
                    {
                        if(s[i][j] == '1')          X[++X[0]] = i, Y[++Y[0]] = j;
                        if(s[i+1][j] == '1')        X[++X[0]] = i + 1, Y[++Y[0]] = j;
                        if(s[i][j+1] == '1')        X[++X[0]] = i, Y[++Y[0]] = j + 1;
                        if(s[i+1][j+1] == '1')      X[++X[0]] = i + 1, Y[++Y[0]] = j + 1;
                        s[i][j] = s[i+1][j] = s[i][j+1] = s[i+1][j+1] = '0';
                    }
                    if(cnt == 2)
                    {
                        bool flag = 0;
                        int XX = 0, YY = 0;
                        if(!flag && s[i][j] == '1') s[i][j] = '0', flag = 1, XX = i, YY = j, X[++X[0]] = i, Y[++Y[0]] = j;
                        if(!flag && s[i+1][j] == '1') s[i+1][j] = '0', flag = 1, XX = i+1, YY = j, X[++X[0]] = i+1, Y[++Y[0]] = j;
                        if(!flag && s[i][j+1] == '1') s[i][j+1] = '0', flag = 1, XX = i, YY = j+1, X[++X[0]] = i, Y[++Y[0]] = j+1;
                        if(!flag && s[i+1][j+1] == '1') s[i+1][j+1] = '0', flag = 1, XX = i+1, YY = j+1, X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                        if(s[i][j] == '0' && !(XX == i && YY == j))  s[i][j] = '1', X[++X[0]] = i, Y[++Y[0]] = j;
                        if(s[i+1][j] == '0' && !(XX == i+1 && YY == j))  s[i+1][j] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j;
                        if(s[i][j+1] == '0' && !(XX == i && YY == j+1))  s[i][j+1] = '1', X[++X[0]] = i, Y[++Y[0]] = j+1;
                        if(s[i+1][j+1] == '0' && !(XX == i+1 && YY == j+1))  s[i+1][j+1] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                        if(s[i][j] == '1')          X[++X[0]] = i, Y[++Y[0]] = j;
                        if(s[i+1][j] == '1')        X[++X[0]] = i + 1, Y[++Y[0]] = j;
                        if(s[i][j+1] == '1')        X[++X[0]] = i, Y[++Y[0]] = j + 1;
                        if(s[i+1][j+1] == '1')      X[++X[0]] = i + 1, Y[++Y[0]] = j + 1;
                        s[i][j] = s[i+1][j] = s[i][j+1] = s[i+1][j+1] = '0';
                    }
                    if(cnt == 1)
                    {
                        int XX = 0, YY = 0;
                        if(s[i][j] == '1')          s[i][j] = '0', XX = i, YY = j, X[++X[0]] = i, Y[++Y[0]] = j;
                        if(s[i+1][j] == '1')        s[i+1][j] = '0', XX = i+1, YY = j, X[++X[0]] = i + 1, Y[++Y[0]] = j;
                        if(s[i][j+1] == '1')        s[i][j+1] = '0', XX = i, YY = j+1, X[++X[0]] = i, Y[++Y[0]] = j + 1;
                        if(s[i+1][j+1] == '1')      s[i+1][j+1] = '0', XX = i+1, YY = j+1, X[++X[0]] = i + 1, Y[++Y[0]] = j + 1;

                        int tot = 0;
                        if(tot <= 1 && s[i][j] == '0' && !(XX == i && YY == j))  tot++, s[i][j] = '1', X[++X[0]] = i, Y[++Y[0]] = j;
                        if(tot <= 1 && s[i+1][j] == '0' && !(XX == i+1 && YY == j))  tot++, s[i+1][j] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j;
                        if(tot <= 1 && s[i][j+1] == '0' && !(XX == i && YY == j+1))  tot++, s[i][j+1] = '1', X[++X[0]] = i, Y[++Y[0]] = j+1;
                        if(tot <= 1 && s[i+1][j+1] == '0' && !(XX == i+1 && YY == j+1))  tot++, s[i+1][j+1] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                        bool flag = 0;
                        XX = 0, YY = 0;
                        if(!flag && s[i][j] == '1') s[i][j] = '0', flag = 1, XX = i, YY = j, X[++X[0]] = i, Y[++Y[0]] = j;
                        if(!flag && s[i+1][j] == '1') s[i+1][j] = '0', flag = 1, XX = i+1, YY = j, X[++X[0]] = i+1, Y[++Y[0]] = j;
                        if(!flag && s[i][j+1] == '1') s[i][j+1] = '0', flag = 1, XX = i, YY = j+1, X[++X[0]] = i, Y[++Y[0]] = j+1;
                        if(!flag && s[i+1][j+1] == '1') s[i+1][j+1] = '0', flag = 1, XX = i+1, YY = j+1, X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                        if(s[i][j] == '0' && !(XX == i && YY == j))  s[i][j] = '1', X[++X[0]] = i, Y[++Y[0]] = j;
                        if(s[i+1][j] == '0' && !(XX == i+1 && YY == j))  s[i+1][j] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j;
                        if(s[i][j+1] == '0' && !(XX == i && YY == j+1))  s[i][j+1] = '1', X[++X[0]] = i, Y[++Y[0]] = j+1;
                        if(s[i+1][j+1] == '0' && !(XX == i+1 && YY == j+1))  s[i+1][j+1] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                        if(s[i][j] == '1')          X[++X[0]] = i, Y[++Y[0]] = j;
                        if(s[i+1][j] == '1')        X[++X[0]] = i + 1, Y[++Y[0]] = j;
                        if(s[i][j+1] == '1')        X[++X[0]] = i, Y[++Y[0]] = j + 1;
                        if(s[i+1][j+1] == '1')      X[++X[0]] = i + 1, Y[++Y[0]] = j + 1;
                        s[i][j] = s[i+1][j] = s[i][j+1] = s[i+1][j+1] = '0';
                    }
                    if(cnt == 4)
                    {
                        if(j != m-1)
                        {
                            X[++X[0]] = i, Y[++Y[0]] = j; s[i][j] = '0';
                            X[++X[0]] = i+1, Y[++Y[0]] = j; s[i+1][j] = '0';
                            X[++X[0]] = i, Y[++Y[0]] = j+1; s[i][j+1] = '0';
                            continue;
                        }
                        if(j == m-1)
                        {
                            X[++X[0]] = i, Y[++Y[0]] = j; s[i][j] = '0';
                            X[++X[0]] = i+1, Y[++Y[0]] = j; s[i+1][j] = '0';
                            X[++X[0]] = i, Y[++Y[0]] = j+1; s[i][j+1] = '0';

                            int XX = 0, YY = 0;
                            if(s[i][j] == '1')          s[i][j] = '0', XX = i, YY = j, X[++X[0]] = i, Y[++Y[0]] = j;
                            if(s[i+1][j] == '1')        s[i+1][j] = '0', XX = i+1, YY = j, X[++X[0]] = i + 1, Y[++Y[0]] = j;
                            if(s[i][j+1] == '1')        s[i][j+1] = '0', XX = i, YY = j+1, X[++X[0]] = i, Y[++Y[0]] = j + 1;
                            if(s[i+1][j+1] == '1')      s[i+1][j+1] = '0', XX = i+1, YY = j+1, X[++X[0]] = i + 1, Y[++Y[0]] = j + 1;

                            int tot = 0;
                            if(tot <= 1 && s[i][j] == '0' && !(XX == i && YY == j))  tot++, s[i][j] = '1', X[++X[0]] = i, Y[++Y[0]] = j;
                            if(tot <= 1 && s[i+1][j] == '0' && !(XX == i+1 && YY == j))  tot++, s[i+1][j] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j;
                            if(tot <= 1 && s[i][j+1] == '0' && !(XX == i && YY == j+1))  tot++, s[i][j+1] = '1', X[++X[0]] = i, Y[++Y[0]] = j+1;
                            if(tot <= 1 && s[i+1][j+1] == '0' && !(XX == i+1 && YY == j+1))  tot++, s[i+1][j+1] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                            bool flag = 0;
                            XX = 0, YY = 0;
                            if(!flag && s[i][j] == '1') s[i][j] = '0', flag = 1, XX = i, YY = j, X[++X[0]] = i, Y[++Y[0]] = j;
                            if(!flag && s[i+1][j] == '1') s[i+1][j] = '0', flag = 1, XX = i+1, YY = j, X[++X[0]] = i+1, Y[++Y[0]] = j;
                            if(!flag && s[i][j+1] == '1') s[i][j+1] = '0', flag = 1, XX = i, YY = j+1, X[++X[0]] = i, Y[++Y[0]] = j+1;
                            if(!flag && s[i+1][j+1] == '1') s[i+1][j+1] = '0', flag = 1, XX = i+1, YY = j+1, X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                            if(s[i][j] == '0' && !(XX == i && YY == j))  s[i][j] = '1', X[++X[0]] = i, Y[++Y[0]] = j;
                            if(s[i+1][j] == '0' && !(XX == i+1 && YY == j))  s[i+1][j] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j;
                            if(s[i][j+1] == '0' && !(XX == i && YY == j+1))  s[i][j+1] = '1', X[++X[0]] = i, Y[++Y[0]] = j+1;
                            if(s[i+1][j+1] == '0' && !(XX == i+1 && YY == j+1))  s[i+1][j+1] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                            if(s[i][j] == '1')          X[++X[0]] = i, Y[++Y[0]] = j;
                            if(s[i+1][j] == '1')        X[++X[0]] = i + 1, Y[++Y[0]] = j;
                            if(s[i][j+1] == '1')        X[++X[0]] = i, Y[++Y[0]] = j + 1;
                            if(s[i+1][j+1] == '1')      X[++X[0]] = i + 1, Y[++Y[0]] = j + 1;
                            s[i][j] = s[i+1][j] = s[i][j+1] = s[i+1][j+1] = '0';
                        }
                    }
                }

            }
        }
        //  for(int i = 1; i <= n; i++)
        //      printf("%s\n", s[i]+1);
        if(n % 2 == 1)
        {
            for(int i = n-1; i <= n-1; i++)
            {
                for(int j = 1; j < m; j++)
                {
                    if(j != m-1)
                    {
                        int tot = 0;
                        if(s[i][j] == '0' && s[i+1][j] == '0')  continue;
                        if(s[i][j] == '1')  tot++, s[i][j] = '0', X[++X[0]] = i, Y[++Y[0]] = j;
                        if(s[i+1][j] == '1')    tot++, s[i+1][j] = '0', X[++X[0]] = i + 1, Y[++Y[0]] = j;
                        // cout << tot<<endl;
                        // system("pause");
                        if(tot == 1)
                        {
                            X[++X[0]] = i, Y[++Y[0]] = j+1;
                            X[++X[0]] = i+1, Y[++Y[0]] = j+1;
                            if(s[i][j+1] == '0')    s[i][j+1] = '1';
                            else    s[i][j+1] = '0';
                            if(s[i+1][j+1] == '0')  s[i+1][j+1] = '1';
                            else    s[i+1][j+1] = '0';
                        }
                        if(tot == 2)
                        {
                            X[++X[0]] = i, Y[++Y[0]] = j + 1;
                            if(s[i][j+1] == '0')    s[i][j+1] = '1';
                            else    s[i][j+1] = '0';
                        }
                    }
                    if(j == m-1)
                    {
                        int cnt = 0;
                        if(s[i][j] == '1')          cnt++;
                        if(s[i+1][j] == '1')        cnt++;
                        if(s[i][j+1] == '1')        cnt++;
                        if(s[i+1][j+1] == '1')      cnt++;
                        if(cnt == 0)    continue;
                        if(cnt == 3)
                        {
                            if(s[i][j] == '1')          X[++X[0]] = i, Y[++Y[0]] = j;
                            if(s[i+1][j] == '1')        X[++X[0]] = i + 1, Y[++Y[0]] = j;
                            if(s[i][j+1] == '1')        X[++X[0]] = i, Y[++Y[0]] = j + 1;
                            if(s[i+1][j+1] == '1')      X[++X[0]] = i + 1, Y[++Y[0]] = j + 1;
                            s[i][j] = s[i+1][j] = s[i][j+1] = s[i+1][j+1] = '0';
                        }
                        if(cnt == 2)
                        {
                            bool flag = 0;
                            int XX = 0, YY = 0;
                            if(!flag && s[i][j] == '1') s[i][j] = '0', flag = 1, XX = i, YY = j, X[++X[0]] = i, Y[++Y[0]] = j;
                            if(!flag && s[i+1][j] == '1') s[i+1][j] = '0', flag = 1, XX = i+1, YY = j, X[++X[0]] = i+1, Y[++Y[0]] = j;
                            if(!flag && s[i][j+1] == '1') s[i][j+1] = '0', flag = 1, XX = i, YY = j+1, X[++X[0]] = i, Y[++Y[0]] = j+1;
                            if(!flag && s[i+1][j+1] == '1') s[i+1][j+1] = '0', flag = 1, XX = i+1, YY = j+1, X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                            if(s[i][j] == '0' && !(XX == i && YY == j))  s[i][j] = '1', X[++X[0]] = i, Y[++Y[0]] = j;
                            if(s[i+1][j] == '0' && !(XX == i+1 && YY == j))  s[i+1][j] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j;
                            if(s[i][j+1] == '0' && !(XX == i && YY == j+1))  s[i][j+1] = '1', X[++X[0]] = i, Y[++Y[0]] = j+1;
                            if(s[i+1][j+1] == '0' && !(XX == i+1 && YY == j+1))  s[i+1][j+1] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                            if(s[i][j] == '1')          X[++X[0]] = i, Y[++Y[0]] = j;
                            if(s[i+1][j] == '1')        X[++X[0]] = i + 1, Y[++Y[0]] = j;
                            if(s[i][j+1] == '1')        X[++X[0]] = i, Y[++Y[0]] = j + 1;
                            if(s[i+1][j+1] == '1')      X[++X[0]] = i + 1, Y[++Y[0]] = j + 1;
                            s[i][j] = s[i+1][j] = s[i][j+1] = s[i+1][j+1] = '0';
                        }
                        if(cnt == 1)
                        {
                            int XX = 0, YY = 0;
                            if(s[i][j] == '1')          s[i][j] = '0', XX = i, YY = j, X[++X[0]] = i, Y[++Y[0]] = j;
                            if(s[i+1][j] == '1')        s[i+1][j] = '0', XX = i+1, YY = j, X[++X[0]] = i + 1, Y[++Y[0]] = j;
                            if(s[i][j+1] == '1')        s[i][j+1] = '0', XX = i, YY = j+1, X[++X[0]] = i, Y[++Y[0]] = j + 1;
                            if(s[i+1][j+1] == '1')      s[i+1][j+1] = '0', XX = i+1, YY = j+1, X[++X[0]] = i + 1, Y[++Y[0]] = j + 1;

                            int tot = 0;
                            if(tot <= 1 && s[i][j] == '0' && !(XX == i && YY == j))  tot++, s[i][j] = '1', X[++X[0]] = i, Y[++Y[0]] = j;
                            if(tot <= 1 && s[i+1][j] == '0' && !(XX == i+1 && YY == j))  tot++, s[i+1][j] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j;
                            if(tot <= 1 && s[i][j+1] == '0' && !(XX == i && YY == j+1))  tot++, s[i][j+1] = '1', X[++X[0]] = i, Y[++Y[0]] = j+1;
                            if(tot <= 1 && s[i+1][j+1] == '0' && !(XX == i+1 && YY == j+1))  tot++, s[i+1][j+1] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                            bool flag = 0;
                            XX = 0, YY = 0;
                            if(!flag && s[i][j] == '1') s[i][j] = '0', flag = 1, XX = i, YY = j, X[++X[0]] = i, Y[++Y[0]] = j;
                            if(!flag && s[i+1][j] == '1') s[i+1][j] = '0', flag = 1, XX = i+1, YY = j, X[++X[0]] = i+1, Y[++Y[0]] = j;
                            if(!flag && s[i][j+1] == '1') s[i][j+1] = '0', flag = 1, XX = i, YY = j+1, X[++X[0]] = i, Y[++Y[0]] = j+1;
                            if(!flag && s[i+1][j+1] == '1') s[i+1][j+1] = '0', flag = 1, XX = i+1, YY = j+1, X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                            if(s[i][j] == '0' && !(XX == i && YY == j))  s[i][j] = '1', X[++X[0]] = i, Y[++Y[0]] = j;
                            if(s[i+1][j] == '0' && !(XX == i+1 && YY == j))  s[i+1][j] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j;
                            if(s[i][j+1] == '0' && !(XX == i && YY == j+1))  s[i][j+1] = '1', X[++X[0]] = i, Y[++Y[0]] = j+1;
                            if(s[i+1][j+1] == '0' && !(XX == i+1 && YY == j+1))  s[i+1][j+1] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                            if(s[i][j] == '1')          X[++X[0]] = i, Y[++Y[0]] = j;
                            if(s[i+1][j] == '1')        X[++X[0]] = i + 1, Y[++Y[0]] = j;
                            if(s[i][j+1] == '1')        X[++X[0]] = i, Y[++Y[0]] = j + 1;
                            if(s[i+1][j+1] == '1')      X[++X[0]] = i + 1, Y[++Y[0]] = j + 1;
                            s[i][j] = s[i+1][j] = s[i][j+1] = s[i+1][j+1] = '0';
                        }
                        if(cnt == 4)
                        {
                            if(j != m-1)
                            {
                                X[++X[0]] = i, Y[++Y[0]] = j; s[i][j] = '0';
                                X[++X[0]] = i+1, Y[++Y[0]] = j; s[i+1][j] = '0';
                                X[++X[0]] = i, Y[++Y[0]] = j+1; s[i][j+1] = '0';
                                continue;
                            }
                            if(j == m-1)
                            {
                                X[++X[0]] = i, Y[++Y[0]] = j; s[i][j] = '0';
                                X[++X[0]] = i+1, Y[++Y[0]] = j; s[i+1][j] = '0';
                                X[++X[0]] = i, Y[++Y[0]] = j+1; s[i][j+1] = '0';

                                int XX = 0, YY = 0;
                                if(s[i][j] == '1')          s[i][j] = '0', XX = i, YY = j, X[++X[0]] = i, Y[++Y[0]] = j;
                                if(s[i+1][j] == '1')        s[i+1][j] = '0', XX = i+1, YY = j, X[++X[0]] = i + 1, Y[++Y[0]] = j;
                                if(s[i][j+1] == '1')        s[i][j+1] = '0', XX = i, YY = j+1, X[++X[0]] = i, Y[++Y[0]] = j + 1;
                                if(s[i+1][j+1] == '1')      s[i+1][j+1] = '0', XX = i+1, YY = j+1, X[++X[0]] = i + 1, Y[++Y[0]] = j + 1;

                                int tot = 0;
                                if(tot <= 1 && s[i][j] == '0' && !(XX == i && YY == j))  tot++, s[i][j] = '1', X[++X[0]] = i, Y[++Y[0]] = j;
                                if(tot <= 1 && s[i+1][j] == '0' && !(XX == i+1 && YY == j))  tot++, s[i+1][j] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j;
                                if(tot <= 1 && s[i][j+1] == '0' && !(XX == i && YY == j+1))  tot++, s[i][j+1] = '1', X[++X[0]] = i, Y[++Y[0]] = j+1;
                                if(tot <= 1 && s[i+1][j+1] == '0' && !(XX == i+1 && YY == j+1))  tot++, s[i+1][j+1] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                                bool flag = 0;
                                XX = 0, YY = 0;
                                if(!flag && s[i][j] == '1') s[i][j] = '0', flag = 1, XX = i, YY = j, X[++X[0]] = i, Y[++Y[0]] = j;
                                if(!flag && s[i+1][j] == '1') s[i+1][j] = '0', flag = 1, XX = i+1, YY = j, X[++X[0]] = i+1, Y[++Y[0]] = j;
                                if(!flag && s[i][j+1] == '1') s[i][j+1] = '0', flag = 1, XX = i, YY = j+1, X[++X[0]] = i, Y[++Y[0]] = j+1;
                                if(!flag && s[i+1][j+1] == '1') s[i+1][j+1] = '0', flag = 1, XX = i+1, YY = j+1, X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                                if(s[i][j] == '0' && !(XX == i && YY == j))  s[i][j] = '1', X[++X[0]] = i, Y[++Y[0]] = j;
                                if(s[i+1][j] == '0' && !(XX == i+1 && YY == j))  s[i+1][j] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j;
                                if(s[i][j+1] == '0' && !(XX == i && YY == j+1))  s[i][j+1] = '1', X[++X[0]] = i, Y[++Y[0]] = j+1;
                                if(s[i+1][j+1] == '0' && !(XX == i+1 && YY == j+1))  s[i+1][j+1] = '1', X[++X[0]] = i+1, Y[++Y[0]] = j+1;

                                if(s[i][j] == '1')          X[++X[0]] = i, Y[++Y[0]] = j;
                                if(s[i+1][j] == '1')        X[++X[0]] = i + 1, Y[++Y[0]] = j;
                                if(s[i][j+1] == '1')        X[++X[0]] = i, Y[++Y[0]] = j + 1;
                                if(s[i+1][j+1] == '1')      X[++X[0]] = i + 1, Y[++Y[0]] = j + 1;
                                s[i][j] = s[i+1][j] = s[i][j+1] = s[i+1][j+1] = '0';
                            }
                        }
                    }
                }
            }
        }
        cout << X[0] / 3 << endl;
        for(int i = 1; i <= X[0]; i += 3)
            printf("%d %d %d %d %d %d\n",X[i],Y[i],X[i+1],Y[i+1],X[i+2],Y[i+2]);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
char mp[1010][1010];
void sp(int i,int j){
    if(mp[i][j]=='1') mp[i][j]='0';
    else mp[i][j]='1';
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int t;cin>>t;
    while(t--){
        int n,m;cin>>n>>m;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>mp[i][j];
            }
        }
        vector<vector<int> > vec;
        for(int i=1;i<=n-2;i++){
            for(int j=1;j<=m;j++){
                if(mp[i][j]=='1'){
                    vector<int> tmp;
                    if(j==1){
                        tmp.push_back(i);
                        tmp.push_back(j);
                        tmp.push_back(i+1);
                        tmp.push_back(j);
                        tmp.push_back(i+1);
                        tmp.push_back(j+1);
                        sp(i,j);
                        sp(i+1,j);
                        sp(i+1,j+1);
                    }
                    else{
                        tmp.push_back(i);
                        tmp.push_back(j);
                        tmp.push_back(i+1);
                        tmp.push_back(j);
                        tmp.push_back(i+1);
                        tmp.push_back(j-1);
                        sp(i,j);
                        sp(i+1,j);
                        sp(i+1,j-1);
                    }
                    vec.push_back(tmp);
                }
            }
        }
        for(int j=1;j<=m-1;j++){
            if(mp[n-1][j]=='0'&&mp[n][j]=='0') continue;
            else if(mp[n-1][j]=='1'&&mp[n][j]=='0'){
                vector<int> tmp;
                tmp.push_back(n-1);
                tmp.push_back(j);
                tmp.push_back(n-1);
                tmp.push_back(j+1);
                tmp.push_back(n);
                tmp.push_back(j+1);
                sp(n-1,j);
                sp(n-1,j+1);
                sp(n,j+1);
                vec.push_back(tmp);
            }
            else if(mp[n-1][j]=='0'&&mp[n][j]=='1'){
                vector<int> tmp;
                tmp.push_back(n);
                tmp.push_back(j);
                tmp.push_back(n-1);
                tmp.push_back(j+1);
                tmp.push_back(n);
                tmp.push_back(j+1);
                sp(n,j);
                sp(n-1,j+1);
                sp(n,j+1);
                vec.push_back(tmp);
            }
            else{
                vector<int> tmp;
                tmp.push_back(n-1);
                tmp.push_back(j);
                tmp.push_back(n);
                tmp.push_back(j);
                tmp.push_back(n);
                tmp.push_back(j+1);
                sp(n-1,j);
                sp(n,j);
                sp(n,j+1);
                vec.push_back(tmp);
            }
        }
        auto f11=[&]{
            vector<int> tmp;
            tmp.push_back(n);
            tmp.push_back(m-1);
            tmp.push_back(n-1);
            tmp.push_back(m);
            tmp.push_back(n);
            tmp.push_back(m);
            sp(n,m-1);
            sp(n-1,m);
            sp(n,m);
            vec.push_back(tmp);
        };
        auto f12=[&]{
            vector<int> tmp;
            tmp.push_back(n);
            tmp.push_back(m-1);
            tmp.push_back(n-1);
            tmp.push_back(m-1);
            tmp.push_back(n);
            tmp.push_back(m);
            sp(n,m-1);
            sp(n-1,m-1);
            sp(n,m);
            vec.push_back(tmp);
        };
        auto f21=[&]{
            vector<int> tmp;
            tmp.push_back(n-1);
            tmp.push_back(m-1);
            tmp.push_back(n-1);
            tmp.push_back(m);
            tmp.push_back(n);
            tmp.push_back(m);
            sp(n-1,m-1);
            sp(n-1,m);
            sp(n,m);
            vec.push_back(tmp);
        };
        auto f22=[&]{
            vector<int> tmp;
            tmp.push_back(n);
            tmp.push_back(m-1);
            tmp.push_back(n-1);
            tmp.push_back(m);
            tmp.push_back(n-1);
            tmp.push_back(m-1);
            sp(n,m-1);
            sp(n-1,m);
            sp(n-1,m-1);
            vec.push_back(tmp);
        };

        // for(int i=1;i<=n;i++){
        //     for(int j=1;j<=m;j++){
        //         cout<<mp[i][j];
        //     }
        //     cout<<endl;
        // }

        if(mp[n-1][m]=='1'&&mp[n][m]=='0'){
            f11();
            f21();
            f22();
        }
        else if(mp[n-1][m]=='0'&&mp[n][m]=='1'){
            f11();
            f21();
            f12();
        }
        else if(mp[n-1][m]=='1'&&mp[n][m]=='1'){
            f12();
            f22();
        }
        cout<<vec.size()<<endl;
        for(auto &it:vec){
            for(auto &it2:it){
                cout<<it2<<" ";
            }
            cout<<endl;
        }

        // for(int i=1;i<=n;i++){
        //     for(int j=1;j<=m;j++){
        //         cout<<mp[i][j];
        //     }
        //     cout<<endl;
        // }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
char mp[1010][1010];
void sp(int i,int j){
    if(mp[i][j]=='1') mp[i][j]='0';
    else mp[i][j]='1';
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int t;cin>>t;
    while(t--){
        int n,m;cin>>n>>m;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>mp[i][j];
            }
        }
        vector<vector<int> > vec;
        for(int i=1;i<=n-2;i++){
            for(int j=1;j<=m;j++){
                if(mp[i][j]=='1'){
                    vector<int> tmp;
                    if(j==1){
                        tmp.push_back(i);
                        tmp.push_back(j);
                        tmp.push_back(i+1);
                        tmp.push_back(j);
                        tmp.push_back(i+1);
                        tmp.push_back(j+1);
                        sp(i,j);
                        sp(i+1,j);
                        sp(i+1,j+1);
                    }
                    else{
                        tmp.push_back(i);
                        tmp.push_back(j);
                        tmp.push_back(i+1);
                        tmp.push_back(j);
                        tmp.push_back(i+1);
                        tmp.push_back(j-1);
                        sp(i,j);
                        sp(i+1,j);
                        sp(i+1,j-1);
                    }
                    vec.push_back(tmp);
                }
            }
        }
        for(int j=1;j<=m-1;j++){
            if(mp[n-1][j]=='0'&&mp[n][j]=='0') continue;
            else if(mp[n-1][j]=='1'&&mp[n][j]=='0'){
                vector<int> tmp;
                tmp.push_back(n-1);
                tmp.push_back(j);
                tmp.push_back(n-1);
                tmp.push_back(j+1);
                tmp.push_back(n);
                tmp.push_back(j+1);
                sp(n-1,j);
                sp(n-1,j+1);
                sp(n,j+1);
                vec.push_back(tmp);
            }
            else if(mp[n-1][j]=='0'&&mp[n][j]=='1'){
                vector<int> tmp;
                tmp.push_back(n);
                tmp.push_back(j);
                tmp.push_back(n-1);
                tmp.push_back(j+1);
                tmp.push_back(n);
                tmp.push_back(j+1);
                sp(n,j);
                sp(n-1,j+1);
                sp(n,j+1);
                vec.push_back(tmp);
            }
            else{
                vector<int> tmp;
                tmp.push_back(n-1);
                tmp.push_back(j);
                tmp.push_back(n);
                tmp.push_back(j);
                tmp.push_back(n);
                tmp.push_back(j+1);
                sp(n-1,j);
                sp(n,j);
                sp(n,j+1);
                vec.push_back(tmp);
            }
        }
        auto f11=[&]{
            vector<int> tmp;
            tmp.push_back(n);
            tmp.push_back(m-1);
            tmp.push_back(n-1);
            tmp.push_back(m);
            tmp.push_back(n);
            tmp.push_back(m);
            sp(n,m-1);
            sp(n-1,m);
            sp(n,m);
            vec.push_back(tmp);
        };
        auto f12=[&]{
            vector<int> tmp;
            tmp.push_back(n);
            tmp.push_back(m-1);
            tmp.push_back(n-1);
            tmp.push_back(m-1);
            tmp.push_back(n);
            tmp.push_back(m);
            sp(n,m-1);
            sp(n-1,m-1);
            sp(n,m);
            vec.push_back(tmp);
        };
        auto f21=[&]{
            vector<int> tmp;
            tmp.push_back(n-1);
            tmp.push_back(m-1);
            tmp.push_back(n-1);
            tmp.push_back(m);
            tmp.push_back(n);
            tmp.push_back(m);
            sp(n-1,m-1);
            sp(n-1,m);
            sp(n,m);
            vec.push_back(tmp);
        };
        auto f22=[&]{
            vector<int> tmp;
            tmp.push_back(n);
            tmp.push_back(m-1);
            tmp.push_back(n-1);
            tmp.push_back(m);
            tmp.push_back(n-1);
            tmp.push_back(m-1);
            sp(n,m-1);
            sp(n-1,m);
            sp(n-1,m-1);
            vec.push_back(tmp);
        };
        if(mp[n-1][m]=='1'&&mp[n][m]=='0'){
            f11();
            f21();
            f22();
        }
        else if(mp[n-1][m]=='0'&&mp[n][m]=='1'){
            f11();
            f21();
            f12();
        }
        else if(mp[n-1][m]=='1'&&mp[n][m]=='1'){
            f11();
            f21();
        }
        cout<<vec.size()<<endl;
        for(auto &it:vec){
            for(auto &it2:it){
                cout<<it2<<" ";
            }
            cout<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
char mp[1010][1010];
void sp(int i,int j){
    if(mp[i][j]=='1') mp[i][j]='0';
    else mp[i][j]='1';
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int t;cin>>t;
    while(t--){
        int n,m;cin>>n>>m;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>mp[i][j];
            }
        }
        vector<vector<int> > vec;
        for(int i=1;i<=n-2;i++){
            for(int j=1;j<=m;j++){
                if(mp[i][j]=='1'){
                    vector<int> tmp;
                    if(i==1){
                        tmp.push_back(i);
                        tmp.push_back(j);
                        tmp.push_back(i+1);
                        tmp.push_back(j);
                        tmp.push_back(i+1);
                        tmp.push_back(j+1);
                        sp(i,j);
                        sp(i+1,j);
                        sp(i+1,j+1);
                    }
                    else{
                        tmp.push_back(i);
                        tmp.push_back(j);
                        tmp.push_back(i+1);
                        tmp.push_back(j);
                        tmp.push_back(i+1);
                        tmp.push_back(j-1);
                        sp(i,j);
                        sp(i+1,j);
                        sp(i+1,j-1);
                    }
                    vec.push_back(tmp);
                }
            }
        }
        for(int j=1;j<=m-1;j++){
            if(mp[n-1][j]=='0'&&mp[n][j]=='0') continue;
            else if(mp[n-1][j]=='1'&&mp[n][j]=='0'){
                vector<int> tmp;
                tmp.push_back(n-1);
                tmp.push_back(j);
                tmp.push_back(n-1);
                tmp.push_back(j+1);
                tmp.push_back(n);
                tmp.push_back(j+1);
                sp(n-1,j);
                sp(n-1,j+1);
                sp(n,j+1);
                vec.push_back(tmp);
            }
            else if(mp[n-1][j]=='0'&&mp[n][j]=='1'){
                vector<int> tmp;
                tmp.push_back(n);
                tmp.push_back(j);
                tmp.push_back(n-1);
                tmp.push_back(j+1);
                tmp.push_back(n);
                tmp.push_back(j+1);
                sp(n,j);
                sp(n-1,j+1);
                sp(n,j+1);
                vec.push_back(tmp);
            }
            else{
                vector<int> tmp;
                tmp.push_back(n-1);
                tmp.push_back(j);
                tmp.push_back(n);
                tmp.push_back(j);
                tmp.push_back(n);
                tmp.push_back(j+1);
                sp(n-1,j);
                sp(n,j);
                sp(n,j+1);
                vec.push_back(tmp);
            }
        }
        auto f11=[&]{
            vector<int> tmp;
            tmp.push_back(n);
            tmp.push_back(m-1);
            tmp.push_back(n-1);
            tmp.push_back(m);
            tmp.push_back(n);
            tmp.push_back(m);
            sp(n,m-1);
            sp(n-1,m);
            sp(n,m);
            vec.push_back(tmp);
        };
        auto f12=[&]{
            vector<int> tmp;
            tmp.push_back(n);
            tmp.push_back(m-1);
            tmp.push_back(n-1);
            tmp.push_back(m-1);
            tmp.push_back(n);
            tmp.push_back(m);
            sp(n,m-1);
            sp(n-1,m-1);
            sp(n,m);
            vec.push_back(tmp);
        };
        auto f21=[&]{
            vector<int> tmp;
            tmp.push_back(n-1);
            tmp.push_back(m-1);
            tmp.push_back(n-1);
            tmp.push_back(m);
            tmp.push_back(n);
            tmp.push_back(m);
            sp(n-1,m-1);
            sp(n-1,m);
            sp(n,m);
            vec.push_back(tmp);
        };
        auto f22=[&]{
            vector<int> tmp;
            tmp.push_back(n);
            tmp.push_back(m-1);
            tmp.push_back(n-1);
            tmp.push_back(m);
            tmp.push_back(n-1);
            tmp.push_back(m-1);
            sp(n,m-1);
            sp(n-1,m);
            sp(n-1,m-1);
            vec.push_back(tmp);
        };
        if(mp[n-1][m]=='1'&&mp[n][m]=='0'){
            f11();
            f21();
            f22();
        }
        else if(mp[n-1][m]=='0'&&mp[n][m]=='1'){
            f11();
            f21();
            f12();
        }
        else if(mp[n-1][m]=='1'&&mp[n][m]=='1'){
            f11();
            f21();
        }
        cout<<vec.size()<<endl;
        for(auto &it:vec){
            for(auto &it2:it){
                cout<<it2<<" ";
            }
            cout<<endl;
        }
    }
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr long long MOD = 998244353;
constexpr int MAX_N = 300000 + 1;
long long fact[MAX_N];
void init_fact() {
    fact[0] = 1;
    for(auto i = 1; i < MAX_N; ++i)
        fact[i] = fact[i-1] * i % MOD;
}

long long pow_mod(long long x, long long y, long long mod) {
    auto ret=1ll;
    while (y) {
            if (y % 2)
                ret = ret * x % mod;
            x =  x * x % mod;
            y /= 2;
        }
    return ret;
}

long long nCr(int n, int r) {
    return fact[n] * pow_mod(fact[n-r], MOD - 2, MOD) % MOD * pow_mod(fact[r], MOD - 2, MOD) % MOD;
}

ll n, A[MAX_N];
void solve(int TestCase) {
    init_fact();
    cin >> n;
    for(auto i = 0; i < 2 * n; ++i) cin >> A[i];
    sort(A, A + 2 * n);

    ll s = 0, p = 0;
    for(auto i = 0; i < 2 * n; ++i) {
        s = (s + A[i]) % MOD;
        if(i < n) p = (p + A[i]) % MOD;
    }

    ll m = (s - p * 2 % MOD + MOD) % MOD;

    ll ret = nCr(2 * n, n);
    ret = ret * m % MOD;
    //ret = ret * pow_mod(2ll, MOD - 2, MOD) % MOD;
    cout << ret << endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    //cin >> t;
    for(auto i = 1; i <= t; ++i) {
        //cout << "Case #"<< i << ": ";
        solve(i);
    }
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int MOD = 998244353;
constexpr int MAX_N = 150000 + 10;
ll n, A[MAX_N * 2];
long long fact[MAX_N];
void init_fact() {
    fact[0] = 1;
    for(auto i = 1; i < MAX_N; ++i)
        fact[i] = fact[i-1] * i % MOD;
}

long long pow_mod(long long x, long long y, long long mod) {
    auto ret=1ll;
    while (y) {
            if (y % 2)
                ret = ret * x % mod;
            x =  x * x % mod;
            y /= 2;
        }
    return ret;
}

long long nCr(int n, int r) {
    return fact[n] * pow_mod(fact[n-r], MOD - 2, MOD) % MOD * pow_mod(fact[r], MOD - 2, MOD) % MOD;
}

void solve(int TestCase) {
    init_fact();

    cin >> n;
    for(auto i = 0; i < 2 * n; ++i) cin >> A[i];

    sort(A, A + 2 * n);

    ll su = 0, small_su = 0;
    for(auto i = 0; i < 2 * n; ++i) {
        su += A[i];
        if(i < n) small_su += A[i];
    }

    ll ret = (su - 2 * small_su) % MOD * nCr(2 * n, n) % MOD;
    cout << ret << endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    //cin >> t;
    for(auto i = 1; i <= t; ++i) {
        //cout << "Case #"<< i << ": ";
        solve(i);
    }
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int MOD = 998244353;
constexpr int MAX_N = 300000 + 10;
ll n, A[MAX_N * 2];
long long fact[MAX_N];
void init_fact() {
    fact[0] = 1;
    for(auto i = 1; i < MAX_N; ++i)
        fact[i] = fact[i-1] * i % MOD;
}

long long pow_mod(long long x, long long y, long long mod) {
    auto ret=1ll;
    while (y) {
            if (y % 2)
                ret = ret * x % mod;
            x =  x * x % mod;
            y /= 2;
        }
    return ret;
}

long long nCr(int n, int r) {
    return fact[n] * pow_mod(fact[n-r], MOD - 2, MOD) % MOD * pow_mod(fact[r], MOD - 2, MOD) % MOD;
}

void solve(int TestCase) {
    init_fact();

    cin >> n;
    for(auto i = 0; i < 2 * n; ++i) cin >> A[i];

    sort(A, A + 2 * n);

    ll s1 = 0, s2 = 0;
    for(auto i = 0; i < n; ++i) s1 += A[i];
    for(auto i = n; i < 2 * n; ++i) s2 += A[i];
    ll p = (s2 - s1) % MOD;

    ll ret = p * nCr(2 * n, n) % MOD;
    cout << ret << endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    //cin >> t;
    for(auto i = 1; i <= t; ++i) {
        //cout << "Case #"<< i << ": ";
        solve(i);
    }
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int MOD = 998244353;
constexpr int MAX_N = 150000 + 10;
ll n, A[MAX_N * 2];
long long fact[MAX_N];
void init_fact() {
    fact[0] = 1;
    for(auto i = 1; i < MAX_N; ++i)
        fact[i] = fact[i-1] * i % MOD;
}

long long pow_mod(long long x, long long y, long long mod) {
    auto ret=1ll;
    while (y) {
            if (y % 2)
                ret = ret * x % mod;
            x =  x * x % mod;
            y /= 2;
        }
    return ret;
}

long long nCr(int n, int r) {
    return fact[n] * pow_mod(fact[n-r], MOD - 2, MOD) % MOD * pow_mod(fact[r], MOD - 2, MOD) % MOD;
}

void solve(int TestCase) {
    init_fact();

    cin >> n;
    for(auto i = 0; i < 2 * n; ++i) cin >> A[i];

    sort(A, A + 2 * n);

    ll su = 0, small_su = 0;
    for(auto i = 0; i < 2 * n; ++i) {
        su += A[i];
        if(i < n) small_su += A[i];
    }
    ll p = (su - 2 * small_su) % MOD;

    ll ret = p * nCr(2 * n, n) % MOD;
    cout << ret << endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    //cin >> t;
    for(auto i = 1; i <= t; ++i) {
        //cout << "Case #"<< i << ": ";
        solve(i);
    }
}


#include<bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define sz(v) (v).size()
// #define MOD 1000000007
#define MOD 998244353
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define ff first
#define ss second
#define INF 1e18
#define sephiroth ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int MAX = 2e5 + 10;
const int MAXN = 1e5 + 10;
// ###################################### Seive ####################################
// vector<ll> primes;
// void seive() {
//     vector<int> v(MAX);
//     for (int i = 2; i * i < MAX; i++) {
//         if (v[i] == 0) {
//             for (int j = i * 2; j < MAX; j += i) {v[j] = 1;}
//         }
//     }
//     primes.pb(2);
//     for (int i = 3; i < MAX; i += 2) if (v[i] == 0) primes.pb(i);
// }
// ####################################### Factorial ##################################
ll fact[350010];
ll inv[350010];
ll in[350010];
void factFunc() {
    int i, j;
    fact[0] = 1;
    for (int i = 1; i <= 300005; i++) {
        fact[i] = (i * fact[i - 1]) % MOD;
    }
    inv[0] = inv[1] = 1;
    ll p = MOD;
    for (int i = 2; i <= 300005; i++) {
        inv[i] = (p - (p / i) * inv[p % i] % p) % p;
    }
    for (int i = 2; i <= 300005; i++) {
        inv[i] = (inv[i] * inv[i - 1]) % MOD;
    }
    in[0] = in[1] = 1;
    for (int i = 2; i <= 300005; i++) {
        in[i] = inv[i] * fact[i - 1];
        in[i] %= MOD;
    }
}
ll help(ll a, ll b) {
    ll an = fact[a];
    an *= inv[b];
    an %= MOD;
    an *= inv[a - b];
    an %= MOD;
    return an;
}
// ###################################### Power ####################################
// ll pw(ll a, ll n) {
//     if (n == 0) return 1;
//     if (n == 1) return a;
//     ll rs = pw(a, n / 2);
//     rs = (1ll * rs % MOD * rs % MOD) % MOD;
//     if (n % 2) rs = (1ll * rs % MOD * a % MOD) % MOD;
//     return rs % MOD;
// }
// ll pw(ll a, ll n) {
//     if (n == 0) return 1.0;
//     if (n == 1) return a;
//     ll rs = pw(a, n / 2);
//     rs = rs * rs;
//     if (n & 1) rs = rs * a;
//     return rs;
// }
// ###################################### Multiply ####################################
ll mul(ll x, ll y) {
    return (x * 1ll * y) % MOD;
}
// ###################################### Add ####################################
ll add(ll x, ll y)
{
    x += y;
    while (x >= MOD) x -= MOD;
    while (x < 0) x += MOD;
    return x;
}
// ###################################### Prime Check ####################################
bool isPrime(ll n) {
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
// ###################################### Djikstra ####################################
// void djikstra(int v) {
//     d = vector<ll> (n + 1, INF);
//     pa = vector<ll> (n + 1, -1);
//     set<pll> s;
//     d[v] = 0;
//     pa[v] = 1;
//     s.insert({0, v});
//     int u;
//     while (!s.empty()) {
//         u = s.begin()->ss;
//         s.erase(s.begin());
//         for (auto p : adj[u]) {
//             if (d[p.ff] > d[u] + p.ss) {
//                 s.erase({d[p.ff], p.ff});
//                 d[p.ff] = d[u] + p.ss;
//                 pa[p.ff] = u;
//                 s.insert({d[p.ff], p.ff});
//             }
//         }
//     }
// }
// ###################################### factors ####################################
// vector<ll> factors(ll x) {
//     vector<ll> rs;
//     for (ll i = 2; i * i <= x; i += 1) {
//         if (x % i == 0) {
//             rs.pb(i);
//             while (x % i == 0) x /= i;
//         }
//     }
//     if (x > 1) rs.pb(x);
//     sort(all(rs));
//     return rs;
// }
int main() {
#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input1.txt", "r", stdin);
    // for printing output from output.txt
    freopen("output1.txt", "w", stdout);
#endif
    sephiroth
    // int tc;
    // cin >> tc;
    // while (tc--) {
    // }
    factFunc();
    ll sm = 0;
    int n;
    cin >> n;
    vector<ll> v(2 * n);
    for (int i = 0; i < 2 * n; i++) cin >> v[i];
    sort(all(v));
    for (int i = 0; i < n; i++) sm -= v[i];
    for (int i = n; i < 2 * n; i++) sm = add(sm, v[i]);

    cout << mul(sm, help(2 * n, n));

}


#include<bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define sz(v) (v).size()
// #define MOD 1000000007
#define MOD 998244353
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define ff first
#define ss second
#define INF 1e18
#define sephiroth ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int MAX = 2e5 + 10;
const int MAXN = 1e5 + 10;
// ###################################### Seive ####################################
// vector<ll> primes;
// void seive() {
//     vector<int> v(MAX);
//     for (int i = 2; i * i < MAX; i++) {
//         if (v[i] == 0) {
//             for (int j = i * 2; j < MAX; j += i) {v[j] = 1;}
//         }
//     }
//     primes.pb(2);
//     for (int i = 3; i < MAX; i += 2) if (v[i] == 0) primes.pb(i);
// }
// ####################################### Factorial ##################################
ll fact[350010];
ll inv[350010];
ll in[350010];
void factFunc() {
    int i, j;
    fact[0] = 1;
    for (int i = 1; i <= 200005; i++) {
        fact[i] = (i * fact[i - 1]) % MOD;
    }
    inv[0] = inv[1] = 1;
    ll p = MOD;
    for (int i = 2; i <= 200005; i++) {
        inv[i] = (p - (p / i) * inv[p % i] % p) % p;
    }
    for (int i = 2; i <= 200005; i++) {
        inv[i] = (inv[i] * inv[i - 1]) % MOD;
    }
    in[0] = in[1] = 1;
    for (int i = 2; i <= 200005; i++) {
        in[i] = inv[i] * fact[i - 1];
        in[i] %= MOD;
    }
}
ll help(ll a, ll b) {
    ll an = fact[a];
    an *= inv[b];
    an %= MOD;
    an *= inv[a - b];
    an %= MOD;
    return an;
}
// ###################################### Power ####################################
// ll pw(ll a, ll n) {
//     if (n == 0) return 1;
//     if (n == 1) return a;
//     ll rs = pw(a, n / 2);
//     rs = (1ll * rs % MOD * rs % MOD) % MOD;
//     if (n % 2) rs = (1ll * rs % MOD * a % MOD) % MOD;
//     return rs % MOD;
// }
// ll pw(ll a, ll n) {
//     if (n == 0) return 1.0;
//     if (n == 1) return a;
//     ll rs = pw(a, n / 2);
//     rs = rs * rs;
//     if (n & 1) rs = rs * a;
//     return rs;
// }
// ###################################### Multiply ####################################
ll mul(ll x, ll y) {
    return (x * 1ll * y) % MOD;
}
// ###################################### Add ####################################
ll add(ll x, ll y)
{
    x += y;
    while (x >= MOD) x -= MOD;
    while (x < 0) x += MOD;
    return x;
}
// ###################################### Prime Check ####################################
bool isPrime(ll n) {
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
// ###################################### Djikstra ####################################
// void djikstra(int v) {
//     d = vector<ll> (n + 1, INF);
//     pa = vector<ll> (n + 1, -1);
//     set<pll> s;
//     d[v] = 0;
//     pa[v] = 1;
//     s.insert({0, v});
//     int u;
//     while (!s.empty()) {
//         u = s.begin()->ss;
//         s.erase(s.begin());
//         for (auto p : adj[u]) {
//             if (d[p.ff] > d[u] + p.ss) {
//                 s.erase({d[p.ff], p.ff});
//                 d[p.ff] = d[u] + p.ss;
//                 pa[p.ff] = u;
//                 s.insert({d[p.ff], p.ff});
//             }
//         }
//     }
// }
// ###################################### factors ####################################
// vector<ll> factors(ll x) {
//     vector<ll> rs;
//     for (ll i = 2; i * i <= x; i += 1) {
//         if (x % i == 0) {
//             rs.pb(i);
//             while (x % i == 0) x /= i;
//         }
//     }
//     if (x > 1) rs.pb(x);
//     sort(all(rs));
//     return rs;
// }
int main() {
#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input1.txt", "r", stdin);
    // for printing output from output.txt
    freopen("output1.txt", "w", stdout);
#endif
    sephiroth
    // int tc;
    // cin >> tc;
    // while (tc--) {
    // }
    factFunc();
    ll sm = 0;
    int n;
    cin >> n;
    vector<ll> v(2 * n);
    for (int i = 0; i < 2 * n; i++) cin >> v[i];
    sort(all(v));
    for (int i = 0; i < n; i++) sm -= v[i];
    for (int i = n; i < 2 * n; i++) sm += v[i];
    sm %= MOD;
    cout << mul(sm, help(2 * n, n));

}