#include<bits/stdc++.h>
#define LL long long
#define int long long
using namespace std;
inline LL read(){
	LL res=0,fl=1;
	char ch=getchar();
	while(!(ch>='0' && ch<='9')){
		if(ch=='-')fl=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9')res=res*10+ch-'0',ch=getchar();
	return res*fl;
}
inline LL max(LL a,LL b){
	return a>b?a:b;
}
const LL N=1e3+5,mod=998244353;
LL n,m,c,f,a[N][N],b[N][N];
LL ansc,ansf;
char s[N];
signed main(){
//	freopen("plant2.in","r",stdin);
//	freopen("plant2.out","w",stdout);
	int T,id;
	T=read(),id=read();
	while(T--){
		n=read(),m=read(),c=read(),f=read();
		for(int i=1;i<=n;i++){
			scanf("%s",s+1);
			for(int j=1;j<=m;j++){
				a[i][j]=s[j]-'0';
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=m;j>=1;j--){
				if(a[i][j]==1)b[i][j]=0;
				else b[i][j]=b[i][j+1]+1;
			}
		}
		for(int j=1;j<=m;j++){
			for(int x=1;x<=n;x++){
				for(int y=x+2;y<=n;y++){
					bool fl=1;
					for(int t=x;t<=y;t++){
						if(a[t][j]==1){
							fl=0;
							break;
						}
					}
					int cntt=0;
					for(int t=y+1;t<=n;t++){
						if(a[t][j]==1){
							break;
						}		
						cntt++;				
					}
					if(!fl)continue;
					ansc+=max(0,b[x][j]-1)*max(0,b[y][j]-1)%mod;
					ansf+=max(0,b[x][j]-1)%mod*max(0,b[y][j]-1)*cntt%mod;
				}
			}
		}
		cout<<ansc*c%mod<<' '<<ansf*f%mod<<'\n';
	}
	return 0;
}