#include<bits/stdc++.h>
#define rint register int
#define fu(i,a,b,d,c) for(rint i=a;i<=b&&c;i+=d)
#define fd(i,a,b,d,c) for(rint i=a;i>=b&&c;i-=d)
using namespace std;
inline int read(){
	char c=0,f=1;int num=0;
	while(c<'0'||c>'9')((c=getchar())=='-')&&(f=-f);
	while(c>='0'&&c<='9')num=(num<<1)+(num<<3)+(c^48),c=getchar();
	return num*f;
}
const int MAXN = 1050, LJC = 998244353;
int T,n,m,c,f;
char str[MAXN][MAXN];
int stre[MAXN][MAXN];
int solveC(){
	int sum=0,ans=0;
	fu(y0,1,m,1,1){
		sum=0;
		fu(x,1,n,1,1){
			if(str[x][y0]=='1'){sum=0;continue;}
			else{
				if(sum==0){
					(sum+=stre[x][y0]-1)%=LJC;continue;
				}
				else{
					(ans+=1ll*(sum-stre[x-1][y0]+1)*(stre[x][y0]-1)%LJC)%=LJC;
					(sum+=stre[x][y0]-1)%=LJC;
				}
			}
		}
	}
	return (ans+LJC)%LJC;
}
int solveF(){
	int sum=0,ans=0,cs=0;
	fu(y0,1,m,1,1){
		sum=0,cs=0;
		fu(x,1,n,1,1){
			if(str[x][y0]=='1'){sum=cs=0;continue;}
			else{
				if(sum==0){
					(sum+=stre[x][y0]-1)%=LJC;continue;
				}
				else{
					(ans+=cs)%=LJC;
					(cs+=1ll*(sum-stre[x-1][y0]+1)*(stre[x][y0]-1)%LJC)%=LJC;
					(sum+=stre[x][y0]-1)%=LJC;
				}
			}
		}
	}
	return (ans+LJC)%LJC;
}
signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read(),read();
	while(T--){
		scanf("%d %d %d %d\n",&n,&m,&c,&f);
		fu(i,1,n,1,1)scanf("%s\n",str[i]+1);
		fu(i,1,n,1,1){
			fd(j,m,1,1,1){
				if(str[i][j]=='1')stre[i][j]=0;
				else stre[i][j]=stre[i][j+1]+1;
			}
		}
		printf("%lld %lld\n",1ll*c*solveC()%LJC,1ll*f*solveF()%LJC);
	}
}
