#include<bits/stdc++.h>
using namespace std;
const int p=998244353;
int n,m,c,f,r[1005][1005],T,id;
char s[1005][1005];
int main() {
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	cin>>T>>id;
	while(T--) {
		cin>>n>>m>>c>>f;
		long long ansc=0,ansf=0;
		memset(r,0,sizeof(r));
		for(int i=1; i<=n; i++) {
			cin>>s[i];
			for(int j=m; j>=1; j--)
				if(s[i][j-1]=='0') {
					if(j==m||s[i][j]=='1')
						r[i][j]=j;
					else r[i][j]=r[i][j+1];
				}
		}
		for(int j=1; j<=m; j++) {
			long long sum=0,tmp=0,sum2=0;
			for(int i=1; i<=n; i++) {
				if(s[i][j-1]=='0') {
					sum+=r[i][j]-j;
					sum2+=(r[i][j]-j)*(r[i][j]-j);
					if(i>1&&s[i-1][j-1]=='0')
						tmp+=(r[i][j]-j)*(r[i-1][j]-j);
				} else {
					ansc=(ansc+(sum*sum-sum2)/2-tmp)%p;
					sum=0;
					sum2=0;
					tmp=0;
				}
			}
			ansc=(ansc+(sum*sum-sum2)/2-tmp)%p;
			sum=0;
			sum2=0;
			tmp=0;
		}
		for(int j=1;j<=m;j++){
			long long sum=0,lst=0,cnt=0;//lst stands for 'F' ending in row j
			for(int i=1;i<=n;i++){
				if(s[i][j-1]=='0'){
					cnt++;
					if(cnt>=3)
						lst+=(r[i-1][j]-j)*(sum-r[i-1][j]+j-r[i-2][j]+j);
					sum+=r[i][j]-j;
					ansf=(ansf+lst)%p;
				}
				else {
					cnt=0;
					lst=0;
					sum=0;
				}
			}
		}
		printf("%lld %lld\n",c*ansc,f*ansf);
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}