#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const int MAXN=1010;
const int mod=998244353;

inline int read(int t=0,bool f=1,char c=0){
    while(!isdigit(c=getchar())) f=c^45;
    while(isdigit(c)) t=(t<<1)+(t<<3)+(c^48) , c=getchar();
    return f?t:-t;
}

char s[MAXN][MAXN];
ll to[MAXN][MAXN];


int main(){

    freopen("plant.in","r",stdin);
    freopen("plant.out","w",stdout);

    ios::sync_with_stdio(0);

    int T,id;
    cin>>T>>id;
    while(T--){
        int n,m,c,f;
        cin>>n>>m>>c>>f;
        for(int i=1;i<=n;++i){
            cin>>(s[i]+1);
        }  
        memset(to,0,sizeof(to));
        for(int i=1;i<=n;++i){
            for(int j=m;j>=1;--j){
                if(s[i][j]=='1'){
                    to[i][j]=0;
                }
                else{
                    to[i][j]=to[i][j+1]+1;
                }
            }
        }
        ll res_c=0;
        ll res_f=0;
        for(int j=1;j<=m;++j){
            int cnt=0;
            ll tmp=0;
            ll sum=0;
            for(int i=1;i<=n;++i){
                if(s[i][j]=='0'){
                    ll now=to[i][j]-1;
                    ll last=to[i-1][j]-1;
                    if(now>0){
                        if(cnt==0){
                            cnt++;
                            sum=now;
                        }
                        else{
                            if(last>0){
                                tmp=(tmp+(sum-last+mod)%mod*now%mod)%mod;
                            }
                            else
                                tmp=(tmp+sum*now%mod)%mod;
                            sum=(sum+now)%mod;
                        }
                    }
                }
                else{
                // cout<<j<<" "<<tmp<<endl;
                    res_c=(res_c+tmp)%mod;
                    cnt=0;
                    tmp=0;
                    sum=0;
                }
                // cout<<i<<" "<<j<<" "<<tmp<<" "<<sum<<" "<<cnt<<endl;
            }
            // cout<<j<<" "<<tmp<<" "<<cnt<<endl;
            res_c=(res_c+tmp)%mod;
            cnt=0;
            tmp=0;
            sum=0;
            for(int i=1;i<=n;++i){
                if(s[i][j]=='0'){
                    // cout<<i<<" "<<j<<" "<<tmp<<endl;
                    res_f=(res_f+tmp)%mod;
                    ll now=to[i][j]-1;
                    ll last=to[i-1][j]-1;
                    if(now>0){
                        if(cnt==0){
                            cnt++;
                            sum=now;
                        }
                        else{
                            if(last>0){
                                tmp=(tmp+(sum-last+mod)%mod*now%mod)%mod;
                            }
                            else
                                tmp=(tmp+sum*now%mod)%mod;
                            sum=(sum+now)%mod;
                        }
                    }
                }
                else{
                    tmp=0;
                    cnt=0;
                    sum=0;
                }
            }

        
        }
        cout<<c*res_c%mod<<" ";
        cout<<f*res_f%mod<<endl;


    }



    return 0;
}