#include<bits/stdc++.h>
using namespace std;
const int e=998244353;
int t,id,n,m,c,f,g[1005][1005],l[1005][1005],ansc,ansf;
string s[1005];
int len(int x,int y)
{
	if(l[x][y])return l[x][y];
	int cnt=0;
	while(!g[x][y]&&y<=m)
	{
		y++;
		cnt++;
	}
	return cnt-1;
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%d %d",&t,&id);
	for(int q=1;q<=t;q++)
	{
		scanf("%d %d %d %d",&n,&m,&c,&f);
		ansc=0,ansf=0;
		memset(g,0,sizeof(g));
		for(int i=1;i<=n;i++)
		{
			cin>>s[i];
		}
		if(!c&&!f)
		{
			printf("0 0");
			continue;
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(s[i][j-1]=='1')g[i][j]=1;
			}
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(!g[i][j]&&!g[i+1][j])
				{
					int l=len(i,j);
					if(!l)continue;
					int p=i+2;
					while(!g[p][j]&&p<=n)
					{
						p++;
					}
					int k=i+2;
					while(!g[k][j]&&k<=n)
					{
						int l2=len(k,j);
						k++;
						ansc+=l2*l;
						ansf+=l2*l*(p-k);
						ansc%=e;
						ansf%=e;
					}
				}
			}
		}
		printf("%d %d",ansc*c,ansf*f);
	}
	return 0;
}
