#include<bits/stdc++.h>
#define int long long
using namespace std;

const int mod=998244353;

int n,m,C,F,a[1005][1005],sh[1005][1005],totc,totf,ansc,ansf,cntf,cntc,T,ID;

string s;

signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	cin>>T>>ID;
	while(T--)
	{
		cin>>n>>m>>C>>F;
		for(int i=1;i<=n;i++)
		{
			cin>>s;
			for(int j=0;j<m;j++)
				a[i][j+1]=s[j]=='1';
		}
		for(int i=1;i<=n;i++)
			for(int j=m;j>=1;j--)
				sh[i][j]=((!a[i][j])?(sh[i][j+1]+1):0);
		for(int j=m-1;j>=1;j--)//right to left
		{
			totc=totf=cntf=cntc=0;
			for(int i=n;i>=1;i--)//down to up
			{
				//calc 'C'
				if(a[i][j])
				{
					totc=0;
					cntc=0;
				}
				else
				{
					cntc++;
					totc+=sh[i][j]-1;
					if(cntc>=3)
						ansc=(ansc+((sh[i][j]-1)*(totc-sh[i][j]-sh[i+1][j]+2)%mod))%mod;
				}
				//calc 'F'
				if(a[i][j])
				{
					totf=0;
					cntf=0;
				}
				else
				{
					cntf++;
					totf+=(sh[i][j]-1)*(cntf-1)%mod;
					if(cntf>=4)
						ansf=(ansf+((sh[i][j]-1)*(totf-(sh[i][j]-1)*(cntf-1)%mod-(sh[i+1][j]-1)*(cntf-2)%mod))%mod)%mod;
				}
			}
		}
		cout<<C*ansc<<" "<<F*ansf<<endl;
	}
}//done at 9.53
//shiroko bless me 100pts