#include<bits/stdc++.h>
#define REP(i,first,last) for(int i=first;i<=last;++i)
#define DOW(i,first,last) for(int i=first;i>=last;--i)
#define LL long long
#define CLEAN(a) memset(a,0,sizeof(a))
using namespace std;
const int MAXN=1e3+233;
const int MOD=998244353;
int opt,n,m,c,f;
char s[MAXN][MAXN];
int ll[MAXN][MAXN];
int dd[MAXN][MAXN];
int sll[MAXN][MAXN];
int sll_[MAXN][MAXN];
void Work()
{
	CLEAN(ll);
	CLEAN(sll);
	CLEAN(dd);
	CLEAN(sll_);
	scanf("%d%d%d%d",&n,&m,&c,&f);
	REP(i,1,n)
	{
		REP(j,1,m)
		{
			s[i][j]=0;
			while(s[i][j]!='0'&&s[i][j]!='1')
			{
				scanf("%c",&s[i][j]);	
			}
		}
		s[i][m+1]=0;
	}
	REP(i,1,m+1)
	{
		s[n+1][i]=0;
	}
	REP(i,1,n)
	{
		DOW(j,m,1)
		{
			ll[i][j]=ll[i][j+1]*(s[i][j]=='0')+(s[i][j]=='0');
		}
	}
	DOW(i,n,1)
	{
		REP(j,1,m)
		{
			sll[i][j]=sll[i+1][j]+ll[i][j]-1;
			dd[i][j]=dd[i+1][j]*(s[i][j]=='0')+(s[i][j]=='0');
			sll_[i][j]=(sll_[i+1][j]+1ll*(ll[i][j]-1)*(dd[i][j]-1))%MOD;
		}
	}
	LL answer_c=0;
	LL answer_f=0;
	REP(i,1,n)
	{
		REP(j,1,m)
		{
			if(s[i][j]=='0'&&ll[i][j]>1&&dd[i][j]>2)
			{
				answer_c=(answer_c+1ll*(ll[i][j]-1)*(sll[i+2][j]-sll[i+dd[i][j]][j]))%MOD;
				answer_f=(answer_f+1ll*(ll[i][j]-1)*(1ll*sll_[i+2][j]+MOD-sll_[i+dd[i][j]][j]))%MOD;
			}
		}
	}
	printf("%lld %lld\n",c*answer_c,f*answer_f);
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int t;
	scanf("%d%d",&t,&opt);
	REP(i,1,t)
	{
		Work();
	}
	return 0;
}