#include<bits/stdc++.h>
using namespace std;
/*------------------------------fast io begin------------------------------*/
namespace Fread{
	const int SIZE=1<<16;
	char buf[SIZE],*S,*T;
	inline char getchar(){
		if(S==T){
			T=(S=buf)+fread(buf,1,SIZE,stdin);
			if(S==T)return '\n';
		}
		return *S++;
	}
}
namespace Fwrite{
	const int SIZE=1<<16;
	char buf[SIZE],*S=buf,*T=buf+SIZE;
	inline void flush(){
		fwrite(buf,1,S-buf,stdout);
		S=buf;
	}
	inline void putchar(char c){
		*S++=c;
		if(S==T)flush();
	}
	struct GreyCrane{
		~GreyCrane(){flush();}
	}HuiHe;
}
#define getchar Fread::getchar
#define putchar Fwrite::putchar
template<typename T>inline void read(T&x){
	x=0;T f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-f;c=getchar();}
	while(isdigit(c))x=x*10+(c^48),c=getchar();
	x*=f;
}
inline void read(string&s){
	char c=getchar();s="";
	while(c==' '||c=='\n'||c=='\r')c=getchar();
	while(!(c==' '||c=='\n'||c=='\r'))s+=c,c=getchar();
}
template<typename T>inline void write(T x){
	if(x==0)putchar('0');
	else{
		if(x<0)putchar('-'),x=-x;
		static int sta[45];int top=0;
		while(x)sta[++top]=x%10,x/=10;
		while(top)putchar(sta[top--]+48);
	}
}
inline void write(char c){putchar(c);}
/*------------------------------fast io end------------------------------*/
#define all(x) x.begin(),x.end()
#define siz(x) ((int)x.size())
#define ll long long
#define pb push_back
#define fi first
#define se second
#define edl putchar('\n')
#define spc putchar(' ')
const int N=1e3+5;
const long long mod=998244353;
int n,m,a[N][N],len1[N][N],len2[N][N],sta[N],top;
long long k1,k2,f[N][N],ans1,ans2;
string s;
inline void solve(){
	ans1=ans2=0;
	memset(len1,0,sizeof(len1));
	memset(len2,0,sizeof(len2));
	memset(f,0,sizeof(f));
	read(n),read(m),read(k1),read(k2);
	for(int i=1;i<=n;i++){
		read(s);
		for(int j=1;j<=m;j++)a[i][j]=s[j-1]-'0';
	}
	for(int i=1;i<=n;i++){
		top=0;
		for(int j=1;j<=m;j++){
			if(a[i][j]){
				len1[i][j]=0;
				for(int k=1;k<=top;k++)len1[i][sta[k]]=top-k+1;
				top=0;
			}else{
				sta[++top]=j;
			}
		}
		for(int k=1;k<=top;k++)len1[i][sta[k]]=top-k+1;
	}
	for(int j=1;j<=m;j++){
		top=0;
		for(int i=1;i<=n;i++){
			if(a[i][j]){
				len2[i][j]=0;
				for(int k=1;k<=top;k++)len2[sta[k]][j]=top-k+1;
				top=0;
			}else{
				sta[++top]=i;
			}
		}
		for(int k=1;k<=top;k++)len2[sta[k]][j]=top-k+1;
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(a[i][j])continue;
			if(i>2){
				if(!a[i-1][j]){
					ans1=(ans1+f[i-2][j]*(len1[i][j]-1)%mod)%mod;
					if(len2[i][j])
					ans2=(ans2+f[i-2][j]*(len1[i][j]-1)%mod*(len2[i][j]-1)%mod)%mod;
				}
			}
			if(a[i][j])f[i][j]=0;
			else f[i][j]=len1[i][j]-1+f[i-1][j];
		}
	}
	write(ans1*k1%mod),spc,write(ans2*k2%mod),edl;
}
int T,id;
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	read(T),read(id);
	while(T--)solve();
	return 0;
}
