#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
inline LL read(){
LL ret=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-f;for(;isdigit(ch);ch=getchar())ret=(ret<<1)+(ret<<3)+(ch&15);return ret*f;
}
#define maxn 1005
const LL TT =998244353;LL sumf[maxn][maxn];
LL n,m,h[maxn][maxn],s[maxn][maxn],sum[maxn][maxn];bool a[maxn][maxn];
LL C,F;
void init(){C=F=0;
	n=read(),m=read();LL c=read(),f=read();for(LL i=1;i<=n;i++)for(LL j=1;j<=m;j++){char ch=getchar();while(!isdigit(ch))ch=getchar();a[i][j]=1^(ch-'0');}
	for(LL i=1;i<=n;i++)for(LL j=m;j   ;j--)if(a[i][j])h[i][j]=a[i][j+1]?h[i][j+1]:j;
	for(LL i=n;i   ;i--)for(LL j=1;j<=m;j++)if(a[i][j])s[i][j]=a[i+1][j]?s[i+1][j]:i;
	for(LL j=1;j<=m;j++)for(LL i=1;i<=n;i++)sum[i][j]=sum[i-1][j]+h[i][j];
	for(LL j=1;j<=m;j++)for(LL i=1;i<=n;i++)sumf[i][j]=(sumf[i-1][j]+h[i][j]*i)%TT;
	for(LL i=1;i<=n;i++){for(LL j=1;j<=m;j++){
			LL Z=(s[i][j]-i-1)*(j*j-j*h[i][j])+(h[i][j]-j)*(sum[s[i][j]][j]-sum[i+1][j]);
			LL K=(j*((s[i][j]+i+2)*(s[i][j]-i-1)/2)-(s[i][j]-i-1)*(s[i][j]*j)+s[i][j]*(sum[s[i][j]][j]-sum[i+1][j])-(sumf[s[i][j]][j]-sumf[i+1][j]));
			if(s[i][j]>=i+2)C=(C+Z)%TT;if(s[i][j]>=i+3)F=(F+K*(h[i][j]-j))%TT;
//			for(LL now=i+2;now<=s[i][j];now++){
//				C=(C+(h[i][j]-j)*(h[now][j]-j))%TT;
//				F=(F+(h[i][j]-j)*(h[now][j]-j)*(s[i][j]-now))%TT;
//			}
		}
	}printf("%lld %lld\n",(C*c)%TT,(F*f)%TT);
	memset(h,0,sizeof h),memset(s,0,sizeof s);memset(sum,0,sizeof sum);memset(sumf,0,sizeof sumf);
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	for(LL t=read(),id=read();t--;)init();return 0;
} 