#include <bits/stdc++.h>
// plant3.out 114 514?????
//why is not 1919 810???
//JU RUO YI HUO .(DOGE
using namespace std;
typedef long long ll;
const ll mod=998244353,N=1010;
int t,id,n,m,c,f;
bool v[N][N]={0};
struct node{
	int x,y;
}num[N][N];
ll _c,_f;string s;
bool check(int x,int y){
	if(!v[x][y]&&!v[x+1][y]&&!v[x][y+1])return true;
	else return false;
}
int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>t>>id;
	if(id==1){
		while(t--){
			cin>>n>>m>>c>>f;
			cout<<0<<" "<<0<<endl;
		}
		return 0;
	}
	while(t--){
		cin>>m>>n>>c>>f;
		if(m<3||n<2){
			cout<<0<<" "<<0<<endl;
			continue;
		}
		for(int j=1;j<=m;++j){
			cin>>s;for(int i=1;i<=n;++i){
				if(s[i-1]=='0')v[i][j]=0;else v[i][j]=1;
			}
		}
		for(int j=1;j<=m;++j)
			for(int i=1;i<=n;++i){
				int x=i,y=j;
				while(!v[x][y+1]&&y<m)num[i][j].y+=1,y+=1;y=j;
				while(!v[x+1][y]&&x<n)num[i][j].x+=1,x+=1;
			}
		ll ans=0;
		for(int j=1;j<=m-2;++j){
			for(int i=1;i<=n-1;++i){
				if(check(i,j)){
					int x=i,y=j+2;int t0=num[i][j].x;
					while(!v[x][y]&&y<=m){
						_c+=t0*num[x][y].x;
						if(!v[x][y+1])_f+=t0*num[x][y].x*num[x][y].y;
						y+=1;
					}y=j+2;
				}
			}_c%=mod;_f%=mod;
		}
		cout<<_c*c<<" "<<_f*f<<endl;
	}
	return 0;
}

