#include<bits/stdc++.h>
#define int long long
#define rd(x) x=read()
using namespace std;
const int mod=998244353;
const int N=1005;
int read()
{
    int x=0,f=1;char c=getchar();
    while (c>'9' || c<'0') {if (c=='-') f=-1;c=getchar();}
    while ('0'<=c && c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}
int mulc,mulf,ansc,ansf;
int n,m;
char c[N][N];
int s[N][N],x[N][N],y[N][N],z[N][N];
void init()
{
    for (int i=1;i<=n+2;i++) for (int j=1;j<=m+2;j++) s[i][j]=x[i][j]=y[i][j]=0;
    for (int i=1;i<=n;i++) for (int j=m;j>=1;j--) if (c[i][j]=='0') x[i][j]=x[i][j+1]+1;
    for (int j=1;j<=m;j++) for (int i=n;i>=1;i--) if (c[i][j]=='0') y[i][j]=y[i+1][j]+1;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if (c[i][j]=='0') y[i][j]=(y[i][j]-1)*(x[i][j]-1)%mod;
    int sum=0;
    //for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cout<<y[i][j]<<" \n"[j==m];
    for (int j=1;j<=m;j++)
    {
        sum=0;
        for (int i=n;i>=1;i--)
        {
            if (c[i][j]=='1') sum=0;else 
            {
                ansc=(ansc+(x[i][j]-1)*sum)%mod;
                //printf("i=%lld,j=%lld,sum=%lld,ansc=%lld\n",i,j,sum,ansc);
                if (c[i+1][j]=='0') sum=(sum+max(x[i+1][j]-1,0LL))%mod;
            }
        }
    }
    for (int j=1;j<=m;j++)
    {
        sum=0;
        for (int i=n;i>=1;i--)
        {
            if (c[i][j]=='1') sum=0;else 
            {
                ansf=(ansf+(x[i][j]-1)*sum)%mod;
                //printf("i=%lld,j=%lld,sum=%lld,ansf=%lld\n",i,j,sum,ansf);
                if (c[i+1][j]=='0') sum=(sum+max(y[i+1][j],0LL))%mod;
            }
        }
    }
}
void solve()
{
    rd(n);rd(m);rd(mulc);rd(mulf);ansc=ansf=0;
    for (int i=1;i<=n;i++) scanf("%s",c[i]+1);
    init();
    cout<<ansc*mulc<<" "<<ansf*mulf<<"\n";
}
signed main()
{
    freopen("plant.in","r",stdin);
    freopen("plant.out","w",stdout);
    int Q,id;
    rd(Q);rd(id);
    while (Q--) solve();
    fclose(stdin);
    fclose(stdout);
    return 0;
}
//