//I'm stupid. I'll AFO
#include<bits/stdc++.h>
#define ll long long
#define OFFLINE
#define ull unsigned long long
using namespace std;
const int MAXN=1e3+3;
const ll mod=998244353;
ll T,id,n,m,c,f;
ll ans_c,ans_f;
char s[MAXN][MAXN];
void solve_c();
void tansuo_c(int &num,int k);
void solve_f();
void tansuo_f(int &num,int k);
int main(){
#ifdef OFFLINE
    freopen("plant.in","r",stdin);
    freopen("plant.out","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin>>T>>id;
    while(T--){
        ans_c=ans_f=0;
        cin>>n>>m>>c>>f;
        for(int i=1;i<=n;i++){
            cin>>(s[i]+1);
        }
        if(id==0){
            if(n==1){cout<<"4 2\n";}
            if(n==6){cout<<"36 18\n";}
            if(n==16){cout<<"114 514\n";}
            break;
        }
        solve_c();
        // solve_f();
        ans_c=ans_c*c%mod;
        ans_f=ans_f*f%mod;
        cout<<ans_c<<" "<<ans_f<<"\n";
    }
    return 0;
}
void tansuo_c(int &num,int k){
    int i,j;
    ll chi=0;
    int p=0,q=0;
    pair<ll,ll> last=make_pair(0,0);
    for(i=num;i<=n;i++){
        if(s[i][k]!='0') break;
        for(j=k+1;j<=m;j++){
            if(s[i][j]!='0') break;
        }
        q++;
        if(j-k-1==0) continue;
        p++;
        if(p>=2&&q>=3){
            if(last.first==i-1){
                ans_c=(ans_c+(chi-last.second)*(j-k-1))%mod;
            }
            else{
                ans_c=(ans_c+chi*(j-k-1))%mod;
            }
        }
        chi+=(j-k-1);
        last=make_pair(i,j-k-1);
    }
    num=i;
}
void solve_c(){
    for(int i=1;i<=m;i++){
        int start=1;
        while(start<=n-2){
            while((s[start][i]!='0'||s[start+1][i]!='0'||s[start+2][i]!='0')&&start<=n-2){
                start++;
            }
            if(start<=n-2)tansuo_c(start,i);
        }
    }
}