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

//heng cidianyoubianjige0
//shu cidianxianmianjige0
int heng[1005][1005],shu[1005][1005];
char a[1005][1005];
long long T,idd,n,m,c,f,vc,vf;

int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	cin>>T>>idd;
	while(T--){
		vc=vf=0;
		cin>>n>>m>>c>>f;
		for(int i=1 ;i<=n ;i++){
			char aa[1005];
			cin>>aa;
			for(int j=1 ;j<=m ;j++){
				a[i][j]=aa[j-1];
				heng[i][j]=0;
				shu[i][j]=0;
			}
		}
		if(idd==1){
			cout<<0<<' '<<0<<endl;
			continue;
		}
		for(int i=n ;i>=1 ;i--){
			for(int j=m ;j>=1 ;j--){
				if(a[i][j]=='1') continue;
				if(j!=m and a[i][j+1]=='0') heng[i][j]=heng[i][j+1]+1;
				if(i!=n and a[i+1][j]=='0') shu[i][j]=shu[i+1][j]+1;
			}
		}
		/*
		for(int i=1 ;i<=n ;i++){
			for(int j=1 ;j<=m ;j++){
				cout<<a[i][j]<<' ';
			}
			cout<<endl;
		}
		cout<<endl;
		for(int i=1 ;i<=n ;i++){
			for(int j=1 ;j<=m ;j++){
				cout<<heng[i][j]<<' ';
			}
			cout<<endl;
		}
		cout<<endl;
		for(int i=1 ;i<=n ;i++){
			for(int j=1 ;j<=m ;j++){
				cout<<shu[i][j]<<' ';
			}
			cout<<endl;
		}
		cout<<endl;
		*/
		for(int i=1 ;i<=n ;i++){
			for(int j=1 ;j<=m ;j++){
				if(heng[i][j]>=1 and shu[i][j]>=2){
					for(int k=i+2 ;k<=n ;k++){
						if(a[k][j]=='1') break;
						long long xx=heng[i][j]*heng[k][j];
						long long yy=xx*shu[k][j];
						vc=(vc+xx)%998244353;
						vf=(vf+yy)%998244353;
					}
				}
			}
		}
		cout<<vc*c<<' '<<vf*f<<endl;
		memset(a,0,sizeof(a));
	}
	return 0;
}
