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

typedef unsigned long long ull;
const int MOD=998244353,N=1010;
int T,id;
int n,m,c,f;
string s;
bool a[N][N];
int hy[N][N]={},th=0,tz[N][N]={};
ull su=0,ansc=0,ansf=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++){
			cin>>s;
			for(int j=0;j<m;j++){
				a[i][j+1]=s[j]-'0';
			}
			th=0;
			for(int j=m;j>=1;j--){
				if(a[i][j])hy[i][j]=th=0;
				else hy[i][j]=th++;
			}
		}
		for(int j=1;j<=m;j++){
			th=0;
			for(int i=n;i>=1;i--){
				if(a[i][j])tz[i][j]=th=0;
				else tz[i][j]=th++;
			}
		}
		for(int j=1;j<m;j++){
			su=0;
			for(int i=1;i<=n;i++){
				if(a[i][j])su=0;
				else{
					if(i>=3&&(!a[i-1][j]))su+=hy[i-2][j];
					ansc+=su*hy[i][j]%MOD;
					ansf+=su*hy[i][j]%MOD*tz[i][j]%MOD;
					ansc%=MOD;
					ansf%=MOD;
				}
//				printf("%d %d %llu %llu %llu\n",i,j,su,ansc,ansf);
			}
		}
		printf("%llu %llu\n",ansc*c%MOD,ansf*f%MOD);
	}
	return 0;
}
