#include<bits/stdc++.h>
using namespace std;
const int N=1e3+5,P=998244353;
#define int long long
bool st[N][N];char s[N];
int dep[N][N],len[N][N],vis[N][N],n,m,t,id,c,f;
typedef pair<int,int> pii;vector<pii> dui;
void work(){
	scanf("%lld%lld%lld%lld",&n,&m,&c,&f);int ansc=0,ansf=0;
	memset(st,1,sizeof(st));memset(dep,0,sizeof dep);memset(len,0,sizeof len);
	for(int i=1;i<=n;i++){
		scanf("%s",s+1);
		for(int j=1;j<=m;j++){
			st[i][j]=s[j]-'0';
		}
	}
	for(int i=n;i>=1;i--){
		for(int j=m;j>=1;j--){
			if(!st[i][j]) dep[i][j]=dep[i+1][j]+1,len[i][j]=len[i][j+1]+1;
		}
	}
	for(int j=1;j<=m;j++){
		int i=0,res=0;
		while(i<=n){
			while(!st[i][j]&&i<=n){
				if(len[i][j]>=2) break;
				i++;
			}
			if(len[i][j]>=2){
				int ji=i;
				int res=0;i++;
				while(!st[i][j]&&i<=n){
					if(i>=ji+2) res+=len[i-2][j]-1;
					ansc=(ansc+res*(len[i][j]-1)%P)%P;
					if(!st[i+1][j]) ansf=(ansf+res*(len[i][j]-1)%P*(ji+dep[ji][j]-1-i))%P;
					i++;
				}
			}
			i++;
		}
	}
	ansc=ansc*c%P;ansf=ansf*f%P;
	printf("%lld %lld\n",ansc,ansf);
	return;
}
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	scanf("%lld%lld",&t,&id);
	while(t--){
		work();
	}
	return 0;
}