#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1005,M=998244353;
int T,id,n,m,c,f;
int ansc,ansf,sc[N],sf[N],ri[N][N],down[N][N];
char a[N][N];
signed 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--){
		ansc=ansf=0;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				ri[i][j]=down[i][j]=0;
		cin>>n>>m>>c>>f;
		for(int i=1;i<=n;i++)
			cin>>a[i]+1;
		for(int i=n;i;i--)
			for(int j=m;j;j--)
				if(a[i][j]=='0'){
					ri[i][j]=ri[i][j+1]+1;
					down[i][j]=down[i+1][j]+1;
				}
		for(int j=1;j<=m;j++){
			for(int i=1;i<=n+2;i++)
				sc[i]=sf[i]=0;
			for(int i=n;i;i--){
				if(a[i][j]=='1') continue;
				if(a[i+1][j]=='0') ansc=(ansc+sc[i+2]*(ri[i][j]-1)%M)%M;
				if(a[i+1][j]=='0') ansf=(ansf+sf[i+2]*(ri[i][j]-1)%M)%M;
				sc[i]=(sc[i+1]+ri[i][j]-1)%M;
				sf[i]=(sf[i+1]+(down[i][j]-1)*(ri[i][j]-1)%M)%M;
			}
		}
		cout<<ansc*c%M<<" "<<ansf*f%M<<"\n";
	}
	return 0;
}
