#include<bits/stdc++.h>
#define elif else if
#define pc putchar(' ')
#define pn puts("")
#define rep(i,l,r) for(int i=(l),j=(r);i<=j;++i)
#define per(i,r,l) for(int i=(r),j=(l);i>=j;--i)
#define int long long
#define N 1005
#define A 0x3f3f3f3f
#define P 998244353
using namespace std;
int read(){int ret=0,b=1,c=getchar();while(!isdigit(c))b=c=='-'?-1:1,c=getchar();while(isdigit(c))ret=ret*10+c-'0',c=getchar();return ret*b;}
void wr(int x){if(x<0)putchar('-'),x=-x;if(x>9)wr(x/10);putchar(x%10+'0');return;}
void wrc(int x){wr(x),pc;return;}
void wrn(int x){wr(x),pn;return;}
int Min(int x,int y){return x<=y?x:y;}
int Max(int x,int y){return x>=y?x:y;}
void Swap(int &x,int &y){x^=y,y^=x,x^=y;return;}
int n,m,ansc,ansf,c,f,s[N][N],u[N][N],d[N][N],r[N][N],a[N][N];
char C[N][N];
void init()
{
	memset(s,0,sizeof(s));
	ansc=ansf=0;
	return;
}
int query(int x,int y,int z)
{
	if(x>y)
		return 0;
	return (s[y][z]-s[x-1][z]+P)%P;
}
void add(int &x,int y)
{
	x+=y;
	if(x>=P)
		x-=P;
	return;
}
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int x,y,z,T=read(),id;id=read();
	while(T--)
	{
		n=read(),m=read(),c=read(),f=read(),init();
		rep(i,1,n)
			scanf("%s",C[i]+1);
		rep(i,1,n)
			rep(t,1,m)
				a[i][t]=C[i][t]-'0',u[i][t]=d[i][t]=i,r[i][t]=t;
		rep(i,1,n)
			per(t,m-1,1)
				if(!a[i][t+1])
					r[i][t]=r[i][t+1];
		rep(t,1,m)
			per(i,n-1,1)
				if(!a[i+1][t])
					d[i][t]=d[i+1][t];
		rep(t,1,m)
			rep(i,2,n)
				if(!a[i-1][t])
					u[i][t]=u[i-1][t];
		rep(t,1,m)
			rep(i,1,n)
				s[i][t]=s[i-1][t]+(r[i][t]-t);
		rep(i,1,n)
			rep(t,1,m)
				add(ansc,query(u[i][t],i-2,t)*(r[i][t]-t)%P),add(ansf,query(u[i][t],i-2,t)*(r[i][t]-t)%P*(d[i][t]-i)%P);
		wrc(ansc*c%P),wrn(ansf*f%P);
	}
	fclose(stdin),fclose(stdout);return 0;
}
