#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
	char c=getchar();int x=0,f=1;
	for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}

#define mod 998244353
struct modint{
	int x;
	modint(int o=0){x=o;}
	modint &operator +=(modint o){return x=(x+o.x>=mod?x+o.x-mod:x+o.x),*this;}
	modint &operator -=(modint o){return x=(x-o.x<0?x-o.x+mod:x-o.x),*this;}
	modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
};

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 300005
#define inf 0x3f3f3f3f
#define ull unsigned long long

int T,O;

int n,m;
modint c,f;
modint res1,res2;
char s[1005][1005];
int r[1005][1005],d[1005][1005];

void work()
{
	n=read(),m=read(),c=read(),f=read();
	For(i,1,n){
		scanf("%s",s[i]+1);
	}
	For(i,0,n+1)For(j,0,m+1)r[i][j]=d[i][j]=0;
	Rep(i,n,1)
		Rep(j,m,1){
			if(s[i][j]=='0'){
				r[i][j]=r[i][j+1]+1;
				d[i][j]=d[i+1][j]+1;
			}
		}
	res1=res2=0;
	For(j,1,m){
		modint sum=0;
		For(x,3,n){
			int y=x-2;
			if(r[y][j]>=2) sum+=r[y][j]-1;
			if(s[x-1][j]=='1') sum=0;
			if(r[x][j]>=2){
				res1+=sum*(r[x][j]-1);
				res2+=sum*(r[x][j]-1)*(d[x][j]-1);
			}
		}
	}
	res1*=c;
	res2*=f;
	cout<<res1.x<<" "<<res2.x<<"\n";
}

signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T=read(),O=read();
	while(T--)work();
	return 0;
}
