#include<bits/stdc++.h>
#define up(i,x,y) for (int (i)=(x);(i)<=(y);(i)++)
#define down(i,x,y) for (int (i)=(x);(i)>=(y);(i)--)
#define ll long long
#define elif else if
using namespace std;
int read(){
	int x=0;
	char c=getchar();
	bool f=0;
	while (c<'0' or c>'9') {
		if (c=='-') f=1;
		c=getchar();
	}
	while (c>='0' and c<='9'){
		x=x*10+c-'0';
		c=getchar();
	}
	return (f?-x:x);
}
const int mod=998244353;
int n,m,t[1024][1024];
int dp[1024][1024];
ll vc,vf,c,f;
char ln[1005];
void solve(){
	n=read(),m=read(),c=read(),f=read();
	if (c==0 and f==0) {
		printf("0 0\n");
		return;
	}
	vc=vf=0;
	int a=m+1,b=n+1;
	up(i,0,n+1) t[i][0]=t[i][a]=dp[i][0]=dp[i][a]=0;
	up(i,0,m+1) t[0][i]=t[b][i]=dp[0][i]=dp[b][i]=0;
	up(i,1,n){
		scanf("%s",ln);
		up(j,1,m)
			t[i][j]=ln[j-1]-'0';
	}
	up(i,1,n){
		down(j,m,1){
			if (t[i][j]){
				dp[i][j]=0;
			}
			else{
				ll tmp=0;
				dp[i][j]=dp[i][j+1]+1;
				if (dp[i][j]>=2 and dp[i-1][j]){
					down(k,i-2,1){
						if (dp[k][j]) tmp=(tmp+(dp[i][j]-1)*(dp[k][j]-1))%mod;
						else break;
					}
				}
				if (tmp){
					bool flg=0;
					vc=(vc+tmp)%mod;
					up(k,i+1,n){
						if (t[k][j]){
							vf=(vf+(k-i-1)*(tmp))%mod;
							flg=1;
							break;
						}
					}
					if (not flg){
						vf=(vf+(n-i)*(tmp))%mod;
					}
				}
			}
		}
	}
	printf("%lld %lld\n",c*vc%mod,f*vf%mod);
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int t=read(),id=read();
	while(t--){
		solve();
	}
	fclose(stdin);
	fclose(stdout);
}