//Private Eye, dancing with the wind.
#include<bits/stdc++.h>
using namespace std;

#define MOD 998244353
#define MAXN 1005
char s[MAXN][MAXN];
int sum[MAXN][MAXN],qwq[MAXN][MAXN],sum2[MAXN][MAXN],sum3[MAXN][MAXN];

int main()
{
 	freopen("plant.in","r",stdin);
 	freopen("plant.out","w",stdout);
 	int T,id;
 	scanf("%d%d",&T,&id);
	while(T--)
	{
		int n,m,c,f;
		scanf("%d%d%d%d",&n,&m,&c,&f);
		for(int i=1;i<=n;i++)
			scanf("%s",s[i]+1);
		for(int i=1;i<=n;i++)
		{
			sum[i][m+1]=0;
			for(int j=m;j>=1;j--)
				if(s[i][j]=='1') sum[i][j]=0;
				else sum[i][j]=sum[i][j+1]+1;
		}
		for(int j=1;j<=m;j++)
		{
			sum2[n+1][j]=sum3[n+1][j]=qwq[n+1][j]=0;
			for(int i=n;i>=1;i--)
			{
				if(s[i][j]=='1') sum2[i][j]=sum3[i][j]=qwq[i][j]=0;
				else
				{
					sum2[i][j]=sum2[i+1][j]+sum[i][j]-1;
					qwq[i][j]=qwq[i+1][j]+1;
					sum3[i][j]=(1ll*sum3[i+1][j]+1ll*(sum[i][j]-1)*(qwq[i][j]-1))%MOD;
				}
			}
		}
		int ansc=0,ansf=0;
		for(int i=1;i<=n-2;i++)
			for(int j=1;j<=m;j++)
			{
				if(s[i][j]=='1') continue;
				if(s[i+1][j]=='1') continue;
				if(s[i+2][j]=='1') continue;
				(ansc+=1ll*(sum[i][j]-1)*(sum2[i+2][j])%MOD)%=MOD;
				(ansf+=1ll*(sum[i][j]-1)*(sum3[i+2][j])%MOD)%=MOD;
			}
		printf("%d %d\n",ansc*c,ansf*f);
	}
}