#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll MOD=998244353;
const ll ny=499122177;
const ll N=1e3+10;
ll n,m,cc,ff,T,id;
ll ansc,ansf;
ll mp[N][N];
ll xh[N][N],xl[N][N];
inline ll read(){
	char c=getchar(); ll d=0,e=1;
	while (c<'0'||c>'9') {
		if (c=='-') e=-1;
		c=getchar();
	}
	while (c>='0'&&c<='9') {
		d=(d<<3)+(d<<1)+c-'0';
		c=getchar();
	}
	return d*e;
}
inline void Pre_solve(){
	for (int i=1;i<=n;++i){
		for (int j=m;j>=1;--j)
			if (mp[i][j]) xh[i][j]=xh[i][j+1]+1;
			else xh[i][j]=0;
	}
	for (int j=1;j<=m;++j){
		for (int i=n;i>=1;--i)
			if (mp[i][j]) xl[i][j]=xl[i+1][j]+1;
			else xl[i][j]=0;
	}
}
inline void solvec(){
	int i;
	for (int j=1;j<=m;++j){
		i=1;
		ll pfh,zh,ls;
		while (i<=n){
			pfh=0; zh=0; ls=0;
			for (int i2=i;i2<=i+xl[i][j]-1;++i2){
				if (i2!=i+xl[i][j]-1) ls=(ls+(xh[i2][j]-1)*(xh[i2+1][j]-1)%MOD)%MOD;
				pfh=(pfh+(xh[i2][j]-1)*(xh[i2][j]-1)%MOD)%MOD;
				zh=(zh+xh[i2][j]-1)%MOD;
			}
			ansc=(ansc+(zh*zh%MOD-pfh+MOD)%MOD*ny%MOD-ls+MOD)%MOD;
			if (xl[i][j]) i=i+xl[i][j];
			else i=i+1;
		}
	}
}
inline void solvef(){
	int i;
	for (int j=1;j<=m;++j){
		i=1;
		ll zh,ls;
		while (i<=n){
			zh=0; ls=0;
			for (int i2=i;i2<=i+xl[i][j]-1;++i2){
				ansf=(ansf+(xl[i2][j]-1)*(xh[i2][j]-1)%MOD*(zh-ls)%MOD)%MOD;
//				cout<<i2<<" "<<j<<" "<<ansf<<endl;
				zh=(zh+xh[i2][j]-1)%MOD;
				ls=xh[i2][j]-1;
			}
//			ansc=(ansc+(zh*zh%MOD-pfh+MOD)%MOD*ny%MOD-ls+MOD)%MOD;
			if (xl[i][j]) i=i+xl[i][j];
			else i=i+1;
		}
	}
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read(); id=read();
	while (T--){
		ansc=0; ansf=0;
		n=read(); m=read(); cc=read();ff=read();
		char c;
		for (int i=1;i<=n;++i){
			for (int j=1;j<=m;++j){
				c=getchar();
				while (c!='0'&&c!='1') c=getchar();
				mp[i][j]=1-(c-'0');
			}
		}
		Pre_solve();
		if (cc) solvec();
		if (ff) solvef();
		ansc=ansc*cc%MOD;
		ansf=ansf*ff%MOD;
		printf("%lld %lld\n",ansc,ansf);
	}
	return 0;
}
