#include<bits/stdc++.h>
#define int long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int maxn=1024,mod=998244353;
int T,id,n,m,c,f,cntl[maxn][maxn],suml[maxn][maxn],cntf[maxn][maxn],ansc,ansf;
string s[maxn];
void init()
{
	For(j,1,m)
	{
		int cnt=0;
		Rep(i,n,1)
		{
			if(s[i][j]=='0') cnt++;
			else cnt=0;
			cntf[i][j]=cnt;
		}
	}
	For(i,1,n)
	{
		int cnt=0;
		Rep(j,m,1)
		{
			if(s[i][j]=='0') cnt++;
			else cnt=0;
			cntl[i][j]=cnt;
		}
	}
	For(j,1,m)
		For(i,1,n)
			if(s[i][j]=='0')
				suml[i][j]=suml[i-1][j]+cntl[i][j]-1;
/*
	For(i,1,n)
	{
		For(j,1,m) cout<<cntl[i][j]<<' ';
		cout<<endl;
	}
	cout<<endl;
	For(i,1,n)
	{
		For(j,1,m) cout<<suml[i][j]<<' ';
		cout<<endl;
	}
	cout<<endl;
	For(i,1,n)
	{
		For(j,1,m) cout<<cntf[i][j]<<' ';
		cout<<endl;
	}*/
}
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	cin>>T>>id;
	while(T--)
	{
		cin>>n>>m>>c>>f;
		For(i,1,n)
		{
			cin>>s[i];
			s[i]=' '+s[i];
		}
		if(n<=2||m<=1)
		{
			cout<<0<<' '<<0<<endl;
			continue;
		}
		init();
		ansc=ansf=0;
		For(j,1,m)
		{
			For(i,1,n)
			{
				if(i>=3&&s[i][j]=='0'&&s[i-1][j]=='0')
				{
					int deta=(cntl[i][j]-1)*suml[i-2][j]%mod;
					ansc=(ansc+deta)%mod;
					ansf=(ansf+deta*(cntf[i][j]-1)%mod)%mod;
				}
			}
		}
		cout<<c*ansc<<' '<<f*ansf<<endl;
	}
	return 0;
}