#include<bits/stdc++.h>
using namespace std;
int T,id;
int n,m,c,f;
int a[1010][1010];
string s;
const int N=998244353;
long long ans1=0,ans2=0;
int checkx(int x,int y){
	int ansx=0;
	for(int i=x+1;i<=n;i++){
		if(a[i][y]==0)ansx++;
		else break;
	}	
	return ansx;
}
int checky(int x,int y){
	int ansy=0;
	for(int i=y+1;i<=m;i++){
		if(a[x][i]==0)ansy++;
		else break;
	}
	return ansy;
}
bool vis1[1010][1010];
void dfs1(int x,int y){
	if(x>=n||y>=m||vis1[x][y])return;
	vis1[x][y]=true;
	if(a[x][y]==1){
		
		dfs1(x+1,y);
		dfs1(x,y+1);
		return ;
	}
	int v=checkx(x,y);
	int kk=checky(x,y);
	int sum=0;
	for(int i=x+2;i<=v+x;i++){	
		sum=(sum+checky(i,y))%N;
	}		
	ans1=(ans1+(sum*kk)%N)%N;
	dfs1(x+1,y);
	dfs1(x,y+1);
}
bool vis2[1010][1010];
void dfs2(int x,int y){
	if(x>=n||y>=m||vis2[x][y])return;
	vis2[x][y]=true;
	if(a[x][y]==1){
		dfs2(x+1,y);
		dfs2(x,y+1);
		return ;
	}
	int v=checkx(x,y);
	int kk=checky(x,y);
	int sum=0;
	for(int i=x+2;i<=v+x;i++){
		
		sum=(sum+checky(i,y)*checkx(i,y))%N;
	}
	ans2=(ans2+(sum*kk)%N)%N;
	dfs2(x+1,y);
	dfs2(x,y+1);
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ios::sync_with_stdio(false);
	cin>>T>>id;
	while(T--){
		memset(vis1,false,sizeof(vis1));
		memset(vis2,false,sizeof(vis2));
		cin>>n>>m>>c>>f;
		if(c==0&&f==0){
			cout<<0<<" "<<0<<endl;
			return 0;
		}
		for(int i=1;i<=n;i++){
			cin>>s;
			for(int j=1;j<=m;j++){
				a[i][j]=s[j-1]-'0';
			}
		}
		ans1=0;
		ans2=0;
		dfs1(1,1);
		dfs2(1,1);
		cout<<ans1*c<<" "<<ans2*f<<endl;
	}
	return 0;
}