#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define ll long long
#define gc getchar()
const int M=1002;
int n,m,c,f,save[1002][1002],cool[1002][1002];
char a[M][M];
bool b[M][M],up[M][M];
void inti(){
	cin>>n>>m>>c>>f;
	memset(save,0,sizeof(save));
	memset(cool,0,sizeof(cool));
	for(int i=1;i<=n;i++) {
		cin>>a[i];
		for(int j=0;j<m;j++)
			if(a[i][j]=='1') b[i][j+1]=0;
			else b[i][j+1]=1;
	}
	for(int i=n;i>0;i--)
		for(int j=m;j>0;j--){
			if(b[i][j]){
				save[i][j]=save[i][j+1]+1;
				cool[i][j]=cool[i+1][j]+1;
			}	
		}
}
ll ansc,ansf;
void cal(int x,int y){
	ll sumf=0,sumc=0;
	if(save[x][y]>1&&cool[x][y]>2){
		for(int i=2;i<cool[x][y];i++){
			int tmp=save[x+i][y]-1;
			sumc=(ll)(tmp+sumc)%mod;
			sumf=(sumf+(ll)(tmp*(cool[x+i][y]-1)))%mod;	
		}
	ansc=(ansc+sumc*(save[x][y]-1))%mod;
	ansf=(ansf+sumf*(save[x][y]-1))%mod;
	}
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int t,id;
	cin>>t>>id;
	while(t--){
		inti();
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++){
				cal(i,j);
			}
		cout<<ansc*c%mod<<" "<<ansf*f%mod<<endl;				
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}