#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10,mod=998244353;
int T,id,n,m,c,f,a[N][N],rf[N][N],tmp[N],df[N],sumc,sumf,ansc,ansf;
void getc(int&x){
	while(!isdigit(x=getchar()));
	x-='0';
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d%d",&T,&id);
	while(T--){
		scanf("%d%d%d%d",&n,&m,&c,&f),ansc=ansf=0;
		for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)getc(a[i][j]);
		for(int j=m;j>=1;--j){
			sumc=sumf=0;
			for(int i=n;i>=1;--i){
				if(a[i][j]){
					rf[i][j]=df[i]=0;
					sumf=sumc=0;
					continue;
				}
				rf[i][j]=(j==m?1:rf[i][j+1]+1);
				df[i]=(i==n?1:df[i+1]+1);
				ansc=(ansc+1ll*sumc*(rf[i][j]-1))%mod;
				ansf=(ansf+1ll*sumf*(rf[i][j]-1))%mod;
				if(i<n&&!a[i+1][j]){
					sumc=(sumc+rf[i+1][j]-1)%mod;
					sumf=(sumf+1ll*(df[i+1]-1)*(rf[i+1][j]-1))%mod;
				}
			}
		}
		printf("%d %d\n",ansc*c,ansf*f);
	}
	return 0;
}
