#include<bits/stdc++.h>
using namespace std;

//#define int long long

template<class T>
inline bool read(T &x){
	x=0;char ch;bool f=0;
	for(ch=getchar();!isdigit(ch);ch=getchar()){
		if(ch==EOF)return 0;
		if(ch=='-')f=1;
	}
	for (;isdigit(ch);ch=getchar()){
		x=(x<<1)+(x<<3)+(ch^48);
	}
	if(f)x=-x;
	return 1;
} 

template<class T>
void write(T x){
	if(x<0){
		putchar('-');
		write(-x);
		return ;
	}
	if(x>9)write(x/10);
	putchar(x%10|48);
}

template<class T>
inline void writeln(T x){
	write(x);putchar('\n');
}

template<class T>
inline void writespace(T x){
	write(x);putchar(' ');
}

int T,id,ansc,ansf;
int n,m,c,f;
long long ri[1010][1010],up[1010][1010],dn[1010][1010];
long long pre[1010][1010];
int a[1010][1010];
string s;

const long long mod=998244353;
inline long long add(long long a,long long b){return (a+b)%mod;}
inline long long mul(long long a,long long b){return (a*b)%mod;}


signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	cin>>T>>id;
	while(T--){
		read(n);read(m);read(c);read(f);
		ansc=ansf=0;
		memset(up,0,sizeof(up));
		memset(ri,0,sizeof(ri));
		memset(dn,0,sizeof(dn));
		memset(pre,0,sizeof(pre));
		for(register int i=1;i<=n;++i){
			cin>>s;
			for(register int j=1;j<=m;++j){
				a[i][j]=s[j-1]^48;
			}
		}
		for(register int i=1;i<=n;++i){
			for(register int j=m;j>=1;--j){
				if(a[i][j]==0)ri[i][j]=ri[i][j+1]+1;
			}
		}
		for(register int i=1;i<=n;++i){
			for(register int j=1;j<=m;++j){
				if(a[i][j]==0)up[i][j]=up[i-1][j]+1;
			}
		}
		for(register int i=n;i>=1;--i){
			for(register int j=1;j<=m;++j){
				if(a[i][j]==0)dn[i][j]=dn[i+1][j]+1;
			}
		}
		for(register int j=1;j<=m;++j){
			for(register int i=1;i<=n;++i){
				if(a[i][j]==0)pre[i][j]=pre[i-1][j]+ri[i][j]-1;
			}
		}
		for(register int i=n;i>=1;--i){
			for(register int j=1;j<=m;++j){
				if(a[i][j]==0){
					if(up[i][j]>=2){
						ansc=add(ansc,mul(ri[i][j]-1,pre[i-2][j]-pre[i-up[i][j]][j]));
						if(dn[i][j]>=2){
							ansf=add(ansf,mul(dn[i][j]-1,mul(ri[i][j]-1,pre[i-2][j]-pre[i-up[i][j]][j])));
						}
					}
					
				}
			}
		}
		writespace(mul(ansc,c));writeln(mul(ansf,f));
	}
	return 0;
}