#include <bits/stdc++.h>
#define LL long long
using namespace std;
int const N=1010,mod=998244353;
string s;
int a[N][N],b[N][N],c[N][N],n,m,tc,tf,T,id;
LL ansc,ansf;
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>T>>id;
	while(T--)
	{
		memset(a,0,sizeof(a));
		memset(c,0,sizeof(c));
		ansc=0;ansf=0;
		cin>>n>>m>>tc>>tf;
		for(int i=1;i<=n;i++)
		{
			cin>>s;
			int x=0;
			for(int j=n;j>=1;j--)
			{
				a[i][j]=x;
				if(s[j-1]=='0') c[i][j]=0,x++;
				else c[i][j]=1,x=0,a[i][j]=-1;
			}
		}
		for(int j=1;j<=m;j++)
		{
			int x=0;
			for(int i=n;i>=1;i--)
			{
				b[i][j]=x;
				if(c[i][j]==0) x++;
				else x=0,b[i][j]=-1;
			}
		}
		for(int j=1;j<=m;j++)//Çóc 
		{
			int x=1,as=0,flag=1;
			while(x<=n){
				if(a[x][j]!=-1) as+=a[x][j];
				else {
				
					for(int i=flag;i<=x-2;i++)
					{
						ansc=(ansc+a[i][j]*(as-a[i][j]-a[i+1][j]))%mod;
						as-=a[i][j];
					}
					flag=x+1;
					as=0;
				}
				x++;
			}
			for(int i=flag;i<=x-2;i++)
			{
				ansc=(ansc+a[i][j]*(as-a[i][j]-a[i+1][j]))%mod;
				as-=a[i][j];
			}
		}
		for(int j=1;j<=m;j++){//Çóf 
			int x=1,as=0,flag=1;
			while(x<=n){
				if(a[x][j]!=-1) as+=a[x][j]*b[x][j];
				else {
					for(int i=flag;i<=x-2;i++){
						ansf=(ansf+a[i][j]*(as-a[i][j]*b[i][j]-a[i+1][j]*b[i+1][j]))%mod;
						as-=a[i][j]*b[i][j];
					}
					flag=x+1;
					as=0;
				}
				x++;
			}
			for(int i=flag;i<=x-2;i++){
				ansf=(ansf+a[i][j]*(as-a[i][j]*b[i][j]-a[i+1][j]*b[i+1][j]))%mod;
				as-=a[i][j]*b[i][j];
			}
		}
		cout<<(ansc*tc)%mod<<" "<<(ansf*tf)%mod<<"\n";
	}
	return 0;
}
