#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
#define mod 998244353
int down[maxn][maxn],ri[maxn][maxn],a[maxn][maxn];
template<typename T>inline void read(T &x){register int f=1;x=0;register char c=getchar();while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0',c=getchar();}x*=f;}
signed main(void)
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	register int T,id;read(T),read(id);
	while(T--)
	{
		register int n,m,c,f;read(n),read(m),read(c),read(f);
		string s;
		for(register int i=1;i<=n;i++)
		{
			getline(cin,s);
			for(register int j=0;j<m;j++)
				a[i][j+1]=s[j]-'0';
		}
		for(register int j=1;j<=m;j++) a[0][j]=1;
		for(register int j=1;j<=m;j++)
			for(register int i=1;i<=n;i++)
				if(!a[i][j])
				{
					if(!a[i-1][j])
						down[i][j]=down[i-1][j];
					else
					{
						register int k=i;
						for(;k<=n;k++)
							if(a[k][j])
								break;
						down[i][j]=k-1;
					}
				}
		for(register int i=1;i<=n;i++) a[i][0]=1;
		for(register int i=1;i<=n;i++)
			for(register int j=1;j<=m;j++)
				if(!a[i][j])
				{
					if(!a[i][j-1])
						ri[i][j]=ri[i][j-1];
					else
					{
						register int k=j;
						for(;k<=m;k++)
							if(a[i][k])
								break;
						ri[i][j]=k-1;
					}
				}
		register int C=0,F=0;
		for(register int j=1;j<=m;j++) a[n+1][j]=1;
		for(register int i=1;i<=n;i++)
		{
			for(register int j=1;j<=m;j++)
			{
				if(a[i][j])
					continue;
				if(down[i][j]-i<2)
					continue;
				if(ri[i][j]==j)
					continue;
				register int up=ri[i][j]-j;
				for(register int k=i+2;k<=down[i][j];k++)
				{
					if(ri[k][j]==j)
						continue;
					register int dn=ri[k][j]-j;
					(C+=up*dn)%=mod;
					for(register int l=k+1;l<=n;l++)
						if(!a[l][j])
							(F+=up*dn)%=mod;
						else
							break;
				}
			}
		}
		printf("%d %d\n",C*c%mod,F*f%mod);
	}
	return 0;
}