#include<bits/stdc++.h>
#define F(i,a,b) for(int i=(a);i<=(b);++i)
#define PF(i,a,b) for(int i=(a);i>=(b);--i)
#define Max(a,b) (((a)<(b)) and (a=b))
#define Min(a,b) (((a)>(b)) and (a=b))
#define ll long long
using namespace std;
bool beginning;
namespace solve {
	bool beginning;
	inline int read();
	const int N=1e3+50,mod=998244353;
	int n,m,T,id,c,f;
	char a[N][N];
	bool b[N][N];
	ll R[N][N],D[N][N],Up[N][N];
	ll s[N][N];
	inline void init() {
		memset(R,0,sizeof(R));
		memset(D,0,sizeof(D));
		memset(Up,0,sizeof(Up));
		memset(s,0,sizeof(s));
		F(i,1,n) {
			PF(j,m,1) {
				if(b[i][j])R[i][j]=R[i][j+1]+1;
				else R[i][j]=0;
			}
		}
		F(j,1,m) {
			PF(i,n,1) {
				if(b[i][j])D[i][j]=D[i+1][j]+1;
				else D[i][j]=0;
			}
		}
		F(i,1,n) {
			F(j,1,m) {
				if(b[i][j])Up[i][j]=Up[i-1][j]+1;
				else Up[i][j]=0;
			}
		}
		F(i,1,n)F(j,1,n) {
			if(R[i][j])--R[i][j];
			if(D[i][j])--D[i][j];
			if(Up[i][j])--Up[i][j];
		}
		F(i,1,n) {
			F(j,1,m) {
				s[i][j]=s[i-1][j]+R[i][j];
			}
		}
	}
	inline void show() {
		puts("show===");
		F(i,1,n) {
			F(j,1,m) {
				cerr<<R[i][j]<<" ";
			}
			cerr<<"\n";
		}
		cerr<<"\n";
		F(i,1,n) {
			F(j,1,m) {
				cerr<<D[i][j]<<" ";
			}
			cerr<<"\n";
		}

		cerr<<"\n";
		F(i,1,n) {
			F(j,1,m) {
				cerr<<Up[i][j]<<" ";
			}
			cerr<<"\n";
		}

		cerr<<"\n";
		F(i,1,n) {
			F(j,1,m) {
				cerr<<s[i][j]<<" ";
			}
			cerr<<"\n";
		}
		cerr<<"\n";
		puts("===");
	}
	inline ll solve1() {
		ll Ans=0;
		F(i,1,n-2) {
			F(j,1,m-1) {
				if(b[i][j]) {
					ll tmp=1ll*R[i][j]*(s[i+D[i][j]][j]-s[i+1][j])%mod;
					Ans+=tmp;
					if(Ans>=mod)Ans-=mod;
//				cerr<<"i="<<i<<" j="<<j<<" Ans="<<Ans-tmp<<"\n";
				}
			}
		}
		return Ans;
	}
	inline ll solve2() {
		ll Ans=0;
		F(i,3,n) {
			F(j,1,m-1) {
				if(b[i][j]) {
					ll tmp=1ll*D[i][j]*R[i][j]*(s[i-2][j]-s[i-Up[i][j]-1][j])%mod;
					Ans+=tmp;
					if(Ans>=mod)Ans-=mod;
//				cerr<<"i="<<i<<" j="<<j<<" Ans="<<Ans-tmp<<"\n";
				}
			}
		}
		return Ans;
	}
	bool ending;
	int main() {
//		cerr<<1.0*(&ending-&beginning)/1024/1024<<"MB\n";
//	freopen("","r",stdin);
//	freopen("","w",stdout);
		T=read(),id=read();
		while(T--) {
			n=read(),m=read(),c=read(),f=read();
			F(i,1,n) {
				scanf("%s",a[i]+1);
				F(j,1,m)b[i][j]=(a[i][j]=='0');
			}
			init();
//		show();
			ll Ansc=solve1();
			ll Ansf=solve2();
			printf("%lld %lld\n",Ansc*c%mod,Ansf*f%mod);
		}
		return 0;
	}
	inline int read() {
		int x=0;
		bool f=1;
		char c=getchar();
		while(!isdigit(c))f=c^45,c=getchar();
		while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
		return f?x:-x;
	}

}
bool ending;
int main() {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	solve::main();
	fclose(stdin);
	fclose(stdout);
	return 0;
}
