#include<bits/stdc++.h>
using namespace std;
int t, id, n, m, a[1005][1005], h[1005][1005], z[1005][1005], mod = 998244353;
int main () {
scanf("%d%d", &t, &id);
while(t--) {
int c, f, ansc = 0, ansf = 0;
scanf("%d%d%d%d", &n, &m, &c, &f);
if(c==0 && f==0) {printf("0"); continue;}
for(int i = 1;i <= n;++i)
for(int j = 1;j <= m;++j)
scanf("%1d", &a[i][j]);
for(int i = 1;i <= n;++i) {
int pos = 1, now = 1;
while(now <= m) {
if(a[i][now] == 1) {
for(int j = now-1;j >= pos;--j) h[i][j] = now-j;
pos = now+1;
}
++now;
}
for(int j = now;j >= pos;--j) h[i][j] = now-j;
}
for(int i = 1;i <= m;++i) {
int pos = 1, now = 1;
while(now <= n) {
if(a[now][i] == 1) {
for(int j = now-1;j >= pos;--j) z[j][i] = now-j;
pos = now+1;
}
++now;
}
for(int j = now;j >= pos;--j) z[j][i] = now-j;
}
for(int i = 1;i <= n;++i)
for(int j = 1;j <= m;++j) {
if(h[i][j]<2 || z[i][j]<2 || a[i+1][j]) continue;
for(int k = i+2;a[k][j]==0 && k<=n;++k) {
if(h[k][j]>=2) ansc = (ansc + (h[i][j]-1)*(h[k][j]-1)) % mod;
if(z[k][j]>=2) ansf = (ansf + (h[i][j]-1)*(h[k][j]-1)*(z[k][j]-1)) % mod;
}
}
printf("%d %d\n", ansc*c, ansf*f);
}
return 0;
}