#include <bits/stdc++.h>
using namespace std;
#define int long long
#define i64 long long
inline int read(void) {
    int sgn=1,x=0; char c=getchar();
    while(c<'0'||c>'9') { (c=='-'&&(sgn=0)); c=getchar(); }
    while('0'<=c&&c<='9') x=x*10+c-'0',c=getchar();
    return sgn ? x : -x;
}
inline int read1(void) {
    char c=getchar();
    while(c<'0'||c>'9') c=getchar(); 
    return c-'0';
}
void write(int x) {
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10),x%=10;
    putchar(x+'0');
}
int n,m,c,f;
i64 AnsC,AnsF;
int v[1010][1010],R[1010][1010];
inline void calcC(void) {
    for(int u=1; u<m; ++u) {
        i64 Sum=0;
        for(int i=1; i<=n; ++i) {
            if(!v[i][u]) AnsC+=Sum*(R[i][u]-1);
            if(v[i][u]) Sum=0;
            else if(!v[i-1][u]&&i!=1) Sum+=(R[i-1][u]-1); 
            // cout<<i<<' '<<u<<' '<<Sum<<endl;
        }
    }
}
inline void calcF(void) {
    for(int u=1; u<m; ++u) {
        i64 sum=0;
        int mul=0;
        for(int i=n; i>=1; --i) {
            if(i<=n-2&&!v[i][u]) AnsF+=sum*(R[i][u]-1);
            if(v[i][u]) sum=0,mul=0;
            else {
                if(i+2<=n&&!v[i+2][u]&&!v[i+1][u]) {
                    ++mul;
                    sum+=(R[i+1][u]-1)*mul;
                }
                // cout<<i<<' '<<u<<' '<<sum<<endl;

            }
        }
    }
}
inline void work(void) {
    for(int i=0; i<=n+1; ++i)
    for(int u=0; u<=m+1; ++u) R[i][u]=v[i][u]=0;
    AnsC=AnsF=0;
    n=read(); m=read(); c=read(); f=read();
    for(int i=1; i<=n; ++i) {
        for(int u=1; u<=m; ++u) {
            v[i][u]=read1();
        }
    }
    for(int i=1; i<=n; ++i) {
        for(int u=m; u>=1; --u) {
            R[i][u]=R[i][u+1]+1;
            if(v[i][u]) R[i][u]=0;
        }
    }
    // for(int i=1; i<=n; ++i) {
    //     for(int u=1; u<=m; ++u)
    //     write(R[i][u]),putchar(" \n"[u==m]);
    // }
    calcC(); calcF();
    write(c*AnsC); putchar(' '); write(f*AnsF); puts("");
}
signed main() {
    freopen("plant.in","r",stdin);
    freopen("plant.out","w",stdout);
    int T=read(); int id=read();
    while(T--) work();
    return 0;
}