#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e3+10,mod=998244353;
int n,m,C,F;
char s[N];
int  a[N][N],ls[N][N],rs[N][N];
void solve()
{
    memset(a,0,sizeof(a));
    memset(ls,0,sizeof(ls));
    memset(rs,0,sizeof(rs));
    scanf("%lld%lld%lld%lld",&n,&m,&C,&F);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s+1);
        for(int j=m;j>=1;j--)
        {
            a[i][j]=(s[j]-'0')^1;
            if(a[i][j])ls[i][j]=ls[i][j+1]+1;
            else ls[i][j]=0;
        }
    }
    for(int j=1;j<=m;j++)
    {
        for(int i=n;i>=1;i--)
            if(a[i][j])rs[i][j]=rs[i+1][j]+1;
            else rs[i][j]=0;
    }
    int ans=0;
    for(int j=1;j<=n;j++)
    {
        deque<int>q;int sum=0;
        for(int i=n;i>=1;i--)
        {
            if(i+2<=n)q.push_front(i+2),sum+=max(0ll,ls[i+2][j]-1);
            while(!q.empty()&&(i+rs[i][j]-1<q.back()))
				sum-=max(0ll,ls[q.back()][j]-1),q.pop_back();
            ans=(ans+sum*max(0ll,ls[i][j]-1))%mod;
        }
    }
    printf("%lld ",C*(ans%mod+mod)%mod);ans=0;
    for(int j=1;j<=n;j++)
    {
        deque<int>q;int sum=0;
        for(int i=n;i>=1;i--)
        {
            if(i+2<=n)q.push_front(i+2),sum=(sum+max(0ll,ls[i+2][j]-1)*max(0ll,rs[i+2][j]-1))%mod;
            while(!q.empty()&&(i+rs[i][j]-1<q.back()))
				sum=(sum-max(0ll,ls[q.back()][j]-1)*max(0ll,rs[q.back()][j]-1))%mod,q.pop_back();
            ans=(ans+sum*max(0ll,ls[i][j]-1))%mod;
        }
    }
    printf("%lld\n",F*(ans%mod+mod)%mod);
}
signed main()
{
    freopen("plant.in","r",stdin);
    freopen("plant.out","w",stdout);
    int _,id;scanf("%lld%lld",&_,&id);
    while(_--)solve();
    return 0;
}