#include<bits/stdc++.h>
#define int long long
#define mod 998244353
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
using namespace std;
void setIn(string s){freopen(s.c_str(),"r",stdin);}
void setOu(string s){freopen(s.c_str(),"w",stdout);}
void setIO(string s){setIn(s+".in");setOu(s+".out");}
typedef pair<int,int> pii;
template<class T>void chkmax(T &a,T b){a=max(a,b);}
template<class T>void chkmin(T &a,T b){a=min(a,b);}
template<class T>T read(T &x)
{
	x=0;T f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-'){f=-1;}c=getchar();}
	while(isdigit(c)){x=x*10+(c-'0');c=getchar();}
	return x=x*f;
}
template<class T>void write(T x)
{
	if(x<0){putchar('-');x=-x;}
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
template<class T>void write(T x,char c){write(x),putchar(c);}
int T,id;
int n,m,C,F;
char a[1005][1005];
int c[1005][1005],f[1005][1005],suf[1005][1005],pre[1005][1005],sum[1005][1005];
// suf[i][j] 表示 i,j 向后到达最远的位置
// pre[i][j] 表示 i,j 向上到达最远的位置
// sum[i][j] suf的前缀和
void work()
{
	memset(c,0,sizeof(c));
	memset(f,0,sizeof(f));
	memset(suf,0,sizeof(suf));
	memset(pre,0x3f,sizeof(pre));
	memset(sum,0,sizeof(sum));

	read(n),read(m),read(C),read(F);
	rep(i,1,n)scanf(" %s",a[i]+1);
	rep(i,1,n)rep(j,1,m)a[i][j]-='0';
	rep(i,1,n)per(j,m,1)
		if(a[i][j])suf[i][j]=0;
		else suf[i][j]=max(suf[i][j+1],j);
	rep(i,1,n)rep(j,1,m)
		if(a[i][j])pre[i][j]=0x3f3f3f3f;
		else pre[i][j]=min(i,pre[i-1][j]);
	rep(i,1,n)
	{
		rep(j,1,m)
			sum[i][j]=(sum[i-1][j]+suf[i][j])%mod;
		rep(j,1,m)
			if(!a[i][j]&&suf[i][j]!=j&&pre[i][j]!=i)
			{
				int p=pre[i][j];
				if(p!=0x3f3f3f3f&&p+1!=i&&i>=3)
				{
					int t=sum[i-2][j]-sum[p-1][j]-(i-2-p+1)*j%mod;
					t=(t%mod+mod)%mod;
					c[i][j]=t*(suf[i][j]-j)%mod;
				}
			}
	}
	memset(sum,0,sizeof(sum));
	rep(i,1,n)rep(j,1,m)
	{
		rep(j,1,m)
			sum[i][j]=(sum[i-1][j]+c[i][j])%mod;
		rep(j,1,m)
			if(!a[i][j]&&pre[i][j]!=i)
			{
				int p=pre[i][j];
				if(p!=0x3f3f3f3f&&p+1!=i&&p+2!=i&&i>=4)
					f[i][j]=((sum[i-1][j]-sum[p-1][j])%mod+mod)%mod;
			}
	}
	int ans1=0,ans2=0;
	rep(i,1,n)rep(j,1,m)ans1=(ans1+c[i][j])%mod;
	rep(i,1,n)rep(j,1,m)ans2=(ans2+f[i][j])%mod;
	write(C*ans1,' '),write(F*ans2,'\n');
}
signed main()
{
	setIO("plant");
	read(T),read(id);
	while(T--)work();
}