#include<bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;++i)
#define For(i,a,b) for(int i=a;i>=b;--i)
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
namespace YWK{
	const ll mod=998244353;
    const int N=1e3+5;
    int n,m,c,f;
    int a[N][N];
    ll ansc, ansf;
    int cntl[N][N], cntr[N][N];
    int sl[N][N], top[N][N];
    void Pre(){
        memset(cntl,0,sizeof(cntl));
        memset(cntr,0,sizeof(cntr));
        memset(sl,0,sizeof(sl));
        memset(top,0,sizeof(top));
        ansc=ansf=0;
        {
            //from right to left
            _for(i,1,n){
                cntl[i][m+1]=0;
                For(j,m,1){
                    if(a[i][j]) cntl[i][j]=0;
                    else cntl[i][j]=cntl[i][j+1]+1;
                }
            }
            _for(i,1,m){
                cntr[n+1][i]=0;
                For(j,n,1){
                    if(a[j][i]) cntr[j][i]=0;
                    else cntr[j][i]=cntr[j+1][i]+1;
                }
            }
        }
        {
            _for(j,1,m) sl[0][j]=0;
            _for(i,1,n) _for(j,1,m) sl[i][j]=sl[i-1][j]+cntl[i][j];
        }
        {
            _for(i,1,m){
                top[0][i]=1;
                _for(j,1,n){
                    if(a[j][i]) top[j][i]=j+1;
                    else top[j][i]=top[j-1][i];
                }
            }
        }
    }
    int ask_s(int l1,int l2,int r){ return sl[l2][r]-sl[l1-1][r]; }
    // void Print(){
    //     _for(i,1,n){
    //         _for(j,1,m) printf("%d ",cntl[i][j]);
    //         puts("");
    //     }
    //     _for(i,1,n){
    //         _for(j,1,m) printf("%d ",cntr[i][j]);
    //         puts("");
    //     }
    // }
    void solve_c(){
        _for(i,1,n){
            _for(j,1,m){
                if(a[i][j] or cntl[i][j]<=1 or cntr[i][j]<=2) continue;
                ansc=(ansc+1LL*(cntl[i][j]-1)*(ask_s(i+2,i+cntr[i][j]-1,j)-(cntr[i][j]-2))%mod)%mod;
            }
        }
    }
    void solve_f(){
        _for(i,1,n){
            _for(j,1,m){
                if(a[i][j] or cntl[i][j]<=1 or i-top[i][j]<=1 or cntr[i][j]<=1) continue;
                ansf=(ansf+1LL*(cntr[i][j]-1)*(cntl[i][j]-1)%mod*(ask_s(top[i][j],i-2,j)-((i-2)-top[i][j]+1))%mod)%mod;
            }
        }
    }
    void init(int id){
        scanf("%d%d%d%d",&n,&m,&c,&f);
        _for(i,1,n) _for(j,1,m) scanf("%1d",&a[i][j]);
        _for(i,1,n) a[i][0]=a[i][m+1]=1; _for(i,1,m) a[0][i]=a[n+1][i]=1;
        Pre();
        solve_c();
        solve_f();
        printf("%lld %lld\n",ansc*1LL*c%mod,ansf*1LL*f%mod);
    }
}
int main(){
    freopen("plant.in","r",stdin);
    freopen("plant.out","w",stdout);
	int T, id; scanf("%d%d",&T,&id);
    while(T--) YWK::init(id);
    return 0;
}