#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

#define N 1005
#define ll long long
const ll mod=998244353;
int T,id;
int n,m,a[N][N];
ll C,F;
ll ans,d[N][N],s[N][N];
string str;
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
    scanf("%d%d",&T,&id);
    while (T--){
    	scanf("%d%d%lld%lld",&n,&m,&C,&F);
    	for (int i=1 ; i<=n ; i++){
    		cin>>str;
    		for (int j=1 ; j<=m ; j++)
    		    a[i][j]=str[j-1]-'0';
		}
    	for (int i=1 ; i<=n ; i++)
    	    for (int j=m ; j>=1 ; j--){
    	    	if (a[i][j]==1) d[i][j]=0;
    	    	else d[i][j]=d[i][j+1]+1;
			}
		for (int j=1 ; j<=m ; j++)
		    for (int i=n ; i>=1 ; i--){
		    	s[i][j]=0;
		    	if (a[i][j]==1) s[i][j]=0;
		    	else s[i][j]=(s[i+1][j]+d[i][j]-1)%mod;
			}
		ans=0;
		for (int i=1 ; i<=n-2 ; i++)
		    for (int j=1 ; j<m ; j++){
		    	if (a[i][j] || a[i+1][j] || a[i+2][j]) continue;
		    	ans=(ans+(d[i][j]-1)*s[i+2][j]%mod)%mod;
		    }
	    printf("%lld ",C*ans%mod);
	    // C-
	    for (int j=1 ; j<=m ; j++){
	    	ll tmp=0;
		    for (int i=n ; i>=1 ; i--){
		    	s[i][j]=0;
		    	if (a[i][j]==1) s[i][j]=0,tmp=0;
		    	else tmp++,s[i][j]=(s[i+1][j]+(tmp-1)*(d[i][j]-1)%mod)%mod;
			}
		}
		ans=0;
		for (int i=1 ; i<=n-3 ; i++)
		    for (int j=1 ; j<m ; j++){
		    	if (a[i][j] || a[i+1][j] || a[i+2][j]) continue;
		    	ans=(ans+(d[i][j]-1)*s[i+2][j]%mod)%mod;
		    }
		printf("%lld\n",F*ans%mod);
		// F-
	}
	return 0;
}