#include<cstdio>
#include<cstring>
#define db double
#define pc putchar
#define gc getchar
#define ll long long
#define Tp template<typename _T>
#define Me(a,b) memset(a,b,sizeof(a))
Tp _T mabs(_T x){ return x>0?x:-x; }
Tp _T mmin(_T x,_T y){ return x<y?x:y; }
Tp _T mmax(_T x,_T y){ return x<y?y:x; }
Tp void mswap(_T &x,_T &y){ _T t=x; x=y; y=t; return; }
Tp void print(_T x){ if(x<0) pc('-'),x=-x; if(x>9) print(x/10); pc(x%10+48); return; }
#define maxn 1039
#define maxm
#define MOD 998244353
using namespace std;
#define Type int
Type read(){
	Type sum=0; char c=gc(); int flag=0;
	while(c!='-'&&(c<'0'||c>'9')) c=gc();
	if(c=='-') flag=1,c=gc();
	while('0'<=c&&c<='9') sum=(sum<<1)+(sum<<3)+(c^48),c=gc();
	if(flag) return -sum; else return sum;
}
int id,n,m,c,f; char s[maxn]; ll ansc,ansf,tmp;
int a[maxn][maxn],ri[maxn][maxn],dn[maxn][maxn];
void work(){
	n=read(); m=read(); c=read(); f=read(); int i,j;
	for(i=1;i<=n;i++){
		scanf("%s",s+1);
		for(j=1;j<=m;j++) a[i][j]=(s[j]=='0');
	}
	for(i=1;i<=(n+1);i++) for(j=1;j<=(m+1);j++) ri[i][j]=dn[i][j]=0;//memset
	for(i=1;i<=n;i++) for(j=m;j>=1;j--) if(a[i][j]) ri[i][j]=ri[i][j+1]+1; else ri[i][j]=0;
	for(i=n;i>=1;i--) for(j=1;j<=m;j++) if(a[i][j]) dn[i][j]=dn[i+1][j]+1; else dn[i][j]=0;
	for(j=1;j<=m;j++){
		tmp=0;
		for(i=1;i<=n;i++){
			if(!a[i][j]) tmp=0;
			if(i>2) if(a[i][j]&&a[i-1][j]&&a[i-2][j]) tmp+=ri[i-2][j]-1;
			ansc+=1ll*tmp*(ri[i][j]-1ll)%MOD; ansc%=MOD;
			ansf+=1ll*tmp*(ri[i][j]-1ll)%MOD*(dn[i][j]-1); ansf%=MOD;
		}
	} print(ansc*c%MOD),pc(' '),print(ansf*f%MOD),pc('\n'); return;
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T=read(); id=read(); while(T--) work(); return 0;
}
//biu#2019miss
//slol@2022
//A well beginning is half of succes, but not the whole succes.