#include <bits/stdc++.h>
using namespace std;
long long Mod=998244353;
int T,ceshiid;
int n,m,c,f;
string s;
int tuk[1005][1005];
int heng[1005][1005],shu[1005][1005];
int stheng[1605][1605][11];//1<<9=512,108MB
int stheng2[1605][1605][11];//1<<9=512,108MB
long long totC=0,totF=0;
void chushiST(){
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			stheng[i][j][0]=heng[i][j];
		}
	}
	for(int i=1;i<=m;i++){//ST
		for(int j=1;j<=9;j++){
			for(int u=1;u<=n;u++){
				stheng[u][i][j]=stheng[u][i][j-1]+stheng[u+(1<<(j-1))][i][j-1];
			}
		}
	}
}
void chushiST2(){
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			stheng2[i][j][0]=heng[i][j]*(n-i+1)%Mod;
		}
	}
	for(int i=1;i<=m;i++){//ST
		for(int j=1;j<=9;j++){
			for(int u=1;u<=n;u++){
				stheng2[u][i][j]=(stheng2[u][i][j-1]+stheng2[u+(1<<(j-1))][i][j-1])%Mod;
				
			}
		}
	}
}
long long query(int xm,int upn,int downn){
	int delta=downn-upn+1;
	int kk=9;
	long long anssum=0;
	while(delta>0&&kk>=0){
		if((delta&(1<<kk))){
			delta-=(1<<kk);
			anssum+=stheng[upn][xm][kk];
			upn+=(1<<kk);
		}
		kk--;
	}
	return anssum;
}
long long query2(int xm,int upn,int downn){
	int delta=downn-upn+1;
	int kk=9;
	long long anssum=0;
	while(delta>0&&kk>=0){
		if((delta&(1<<kk))){
			delta-=(1<<kk);
			anssum+=stheng2[upn][xm][kk];
			upn+=(1<<kk);
		}
		kk--;
	}
	return anssum;
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&ceshiid);
	while(T--){
		totC=0;
		totF=0;
		memset(tuk,0,sizeof(tuk));
		memset(heng,0,sizeof(heng));
		memset(shu,0,sizeof(shu));
		memset(stheng,0,sizeof(stheng));
		memset(stheng2,0,sizeof(stheng2));
		scanf("%d%d%d%d",&n,&m,&c,&f);
		for(int i=1;i<=n;i++){
			char ch=getchar();
			while(ch>'1'||ch<'0')ch=getchar();
		//	cin>>s;
			for(int j=1;j<=m;j++){
				tuk[i][j]=ch-'0';
				ch=getchar();
			}
		}
		for(int i=1;i<=n;i++){
			if(tuk[i][m]==0)heng[i][m]=1;
			else heng[i][m]=0;
		}
		for(int j=m-1;j>=1;j--){
			for(int i=1;i<=n;i++){
				if(tuk[i][j]==1){
					heng[i][j]=0;
				}else{
					heng[i][j]=heng[i][j+1]+1;
				}
			}
		}
		chushiST();
		chushiST2();
		for(int j=1;j<=m;j++){
			if(tuk[n][j]==1)shu[n][j]=0;
			else shu[n][j]=1;
		}
		for(int i=n-1;i>=1;i--){
			for(int j=1;j<=m;j++){
				if(tuk[i][j]==1){
					shu[i][j]=0;
				}else{
					shu[i][j]=1+shu[i+1][j];
				}
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m-1;j++){
				if(shu[i][j]>=3){
					totC=(totC+heng[i][j+1]*(query(j+1,i+2,i+shu[i][j]-1))%Mod)%Mod;
				}
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m-1;j++){
				if(shu[i][j]>=4){
					totF=(totF+heng[i][j+1]*(query2(j+1,i+2,i+shu[i][j]-1)-query(j+1,i+2,i+shu[i][j]-1)%Mod*(n+1-(i+shu[i][j]-1))%Mod)+Mod)%Mod;
				}
			}
		}
		printf("%lld %lld\n",totC*c%Mod,totF*f%Mod);
	}
}
