#include<bits/stdc++.h>
#define ll long long
#define int ll
#define For(i,a,b) for(int i=a;i<=b;++i)
const int mod=998244353;
using namespace std;
int n,m,c,f;
int q[1005][1005];
int tagx[1005][1005],topx[1005];
int tagy[1005][1005],topy[1005];
int calm(int x,int y){
	return (x%mod)*(y%mod)%mod;	
}
int ansC,ansF;
void countC(int k,int l,int r){
	For(i,l,r-2){
		For(j,i+2,r){
			ansC+=calm(q[i][k+1],q[j][k+1]);
		}
	}
}
void countF(int k,int l,int r){
	For(i,l,r-3){
		For(j,i+2,r-1){
			ansF+=calm(q[i][k+1],q[j][k+1])*(r-j);
		}
	}
}
signed main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T,id;
	cin>>T>>id;
	while(T--){
		ansC=0;
		ansF=0;
		memset(q,0,sizeof(q));
		memset(tagx,0,sizeof(tagx));
		memset(topx,0,sizeof(topx));
		memset(tagy,0,sizeof(tagy));
		memset(topy,0,sizeof(topy));
		scanf("%d%d%d%d",&n,&m,&c,&f);
		
		For(i,1,n){
			string a;
			cin>>a;
			For(j,0,m-1){
				q[i][j+1]=a[j]-'0';
			}	
		}
		For(i,1,n){
			For(j,1,m){
				if(q[i][j]==1){
					tagx[i][++topx[i]]=j;
				}
			}
		}
		For(i,1,m){
			For(j,1,n){
				if(q[j][i]==1){
					tagy[i][++topy[i]]=j;
				}
			}
		}
		For(i,1,n){
			int top=1;
			For(j,1,m){
				if(j==tagx[i][top]){
					q[i][j]=0;
					continue;
				}
				while(j>tagx[i][top] and top<=topx[i]){
					top++;
				}
				if(top>topx[i]){
					q[i][j]=m-j+1;
					continue;
				}
				q[i][j]=tagx[i][top]-j;
			}
		}
		For(i,1,m){
			int top=1;
			For(j,1,n){
				if(j==tagy[i][top]){
					continue;
				}
				while(j>=tagy[i][top] and top<=topy[i]){
					top++;
				}
				if(top>topy[i]){
					int l=j,r=n;
					if(r-l<2) continue;
					countC(i,l,r);
					countF(i,l,r);
					j=n;
					continue;
				}
				int l=j,r=tagy[i][top]-1;
				if(r-l<2) continue;
				countC(i,l,r);
				countF(i,l,r);
				j=tagy[i][top];
			}
		}
		cout<<(ansC*c)%mod<<" "<<(ansF*f)%mod<<endl;
		
	}
		
}
