#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define par pair<int,int>
#define pb push_back
#define mkp make_pair
using namespace std;
const int M=1005,mod=998244353;
ll RD(){
	ll x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
	return x*f;
}
int n,m,c,f;
ll sc,sf;
char a[M][M];
int L[M][M];
int d[M][M];
void get_(){
	for(int j=1;j<=m;j++)
	{
		ll sum=0;
		int pre=1;
		for(int i=1;i<=n;i++)
		{
			ll x=(L[i][j]-j+mod)%mod;
			if(a[i][j]=='1')
			{
				sum=0;pre=i;continue;
			}
			else
			{
				if(i-pre>1)
				{
					sc=(sc+x*(sum-1ll*(L[i-1][j]-j)+mod)%mod)%mod;
					sf=(sf+x*(sum-1ll*(L[i-1][j]-j)+mod)%mod*d[i][j])%mod;
				}
				sum=(sum+x)%mod;
			}
		}
	}
}

void solve(){
	sc=0,sf=0;
	memset(L,0,sizeof(L));
	memset(d,0,sizeof(d));
	
	n=RD(),m=RD(),c=RD(),f=RD();
	for(int i=1;i<=n;i++)
	{
		scanf("%s",a[i]+1);
		int tmp=m;
		for(int j=m;j;j--)
		{
			if(a[i][j]=='1')tmp=j-1;
			L[i][j]=tmp;
		}
	}
	for(int j=1;j<=m;j++)
	{
		int pre=n;
		for(int i=n;i;i--)
		{
			if(a[i][j]=='1')
			{
				pre=i-1;continue;
			}
			else
			{
				d[i][j]=pre-i;
			}
		}
	}
	
//	for(int i=1;i<=n;i++)
//	{
//		for(int j=1;j<=m;j++)cout<<d[i][j]<<"  ";
//		puts("");
//	}
	
	get_();
	
	printf("%lld %lld\n",1ll*c*sc%mod,1ll*f*sf%mod);
}
int main(){
	
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	
	int T=RD(),id=RD();
	while(T--)solve();
	return 0;
}