#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e3+10,p=998244353;
int t,id;
int n,m,c,f;
char s[N];
int a[N][N];
ll hor[N][N],ver[N][N];
ll anc,anf;
void init(){
    for(int i=1;i<=n;i++){
        int fl=1;
        scanf("%s",s);
        for(int j=1;j<=m;j++){
            a[i][j]=s[j-1]-'0';
            if(a[i][j]==1){
                for(int k=fl+1;k<j;k++)
                    hor[i][k]=hor[i][k-1]-1;
                fl=j+1;
            }
            else ++hor[i][fl];
        }
        for(int k=fl+1;k<=m;k++)
            hor[i][k]=hor[i][k-1]-1;
    }
    for(int j=1;j<=m;j++){
        int fl=1;
        for(int i=1;i<=n;i++){
            if(a[i][j]==1){
                for(int k=fl+1;k<i;k++)
                    ver[k][j]=ver[k-1][j]-1;
                fl=i+1;
            }
            else ++ver[fl][j];
        }
        for(int k=fl+1;k<=n;k++)
            ver[k][j]=ver[k-1][j]-1;
    }
}
void CF(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(a[i][j]==1||hor[i][j]<=1||ver[i][j]<=1) continue;

            for(int k=i+2;k<=i+ver[i][j];k++){
                if(a[k][j]==1||hor[k][j]<=1) continue;
                anc+=(hor[i][j]-1)*(hor[k][j]-1)%p;anc%=p;
                if(ver[k][j]>1)
                    anf+=(hor[i][j]-1)*(hor[k][j]-1)*(ver[k][j]-1)%p,anf%=p;
            }
        }
    }
}
int main(){
    freopen("plant.in","r",stdin);
    freopen("plant.out","w",stdout);
    scanf("%d%d",&t,&id);
    while(t--){
        scanf("%d%d%d%d",&n,&m,&c,&f);
        init();
        if(id==1){printf("0\n");continue;}
        CF();
        printf("%lld %lld\n",anc*c,anf*f);
    }
    fclose(stdin);
    fclose(stdout);
	return 0;
}
