#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
long long t,id,k[1005][1005],a[1005][1005],sum[1005][1005],pi[1005][1005],ansc,ansf;
char g;
int main() {
cin>>t>>id;
while(t--) {
long long n,m,c,f;
ansc=0,ansf=0;
scanf("%lld%lld%lld%lld",&n,&m,&c,&f);
memset(k,0,sizeof(k));
memset(sum,0,sizeof(sum));
memset(pi,0,sizeof(pi)); //后续跟着几个0
memset(a,1,sizeof(a));
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
cin>>g;
if(g=='0') {
a[i][j]=0;
} else {
a[i][j]=1;
}
}
}
for(int i=n-1; i>=1; i--) {
for(int j=1; j<=n; j++) {
if(a[i+1][j]==0) {
pi[i][j]=pi[i+1][j]+1;
}
}
}
for(int i=1; i<=n; i++) {
for(int j=m-1; j>=1; j--) {
if(a[i][j+1]==0&&a[i][j]==0) {
k[i][j]=k[i][j+1]+1;
k[i][j]%=mod;
}
if(a[i-1][j]==1||a[i][j]==1) {
sum[i][j]=k[i][j];
sum[i][j]%=mod;
} else {
sum[i][j]=k[i][j]+sum[i-1][j];
sum[i][j]%=mod;
}
if(a[i][j]==0&&a[i-1][j]==0&&a[i-2][j]==0) {
ansc+=(sum[i-2][j]*k[i][j])%mod ;
ansc%=mod;
ansf+=((sum[i-2][j]*k[i][j])%mod)*pi[i][j]);
ansf%=mod;
}
}
}
cout<<(ansc*c)%mod<<" "<<(ansf*f)%mod<<endl;
}
return 0;
}