#include<bits/stdc++.h>
#define LL long long
#define reg register
using namespace std;
const LL N = 1e3 + 10;
const LL mod = 998244353;
inline LL read();
inline void write(LL x);
LL T,n,m,c,f,id,dis[N][N];
LL Vc,Vf;
//输入处理 
inline void input(){
	Vc = 0,Vf = 0;
	memset(dis,0,sizeof(dis));
	n = read(),m = read(),c = read(),f = read();
	string s;//输入
	for(reg int i = 1;i <= n;i++){
		std::cin>>s;
		for(reg int j = 1;j <= m;j++){
			dis[i][j] = s[j - 1] ^ '0';
		}
	}
	//调试 
	/* 
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= m;j++){
			std::cout<<dis[i][j]<<" ";
		}
		std::cout<<endl;
	} 
	*/
}

//竖向二分查找最大K 
inline LL shufind(LL x1,LL j){
	LL s = x1 - 1;
	LL l = x1 - 1,r = n + 1;
	while(l < r - 1){
		LL mid = l + r >> 1;
		LL val1 = dis[mid][j] - dis[mid][j - 1];
		LL val2 = dis[s][j] - dis[s][j - 1];
		if(val1 == val2) l = mid;
		else r = mid;
	}
	return l;
}

//横向二分查找最大
inline LL henfind(LL y1,LL i){
	LL s = y1 - 1;
	LL l = y1 - 1,r = m + 1;
	while(l < r - 1){
		LL mid = l + r >> 1;
		LL val1 = dis[i][mid] - dis[i - 1][mid];
		LL val2 = dis[i][s] - dis[i - 1][s];
		if(val1 == val2) l = mid;
		else r = mid;
	}
	return l;
} 

//计算答案 
inline void deal(){
	//起点x1的位置 
	for(reg int i = 1;i <= n - 2;i++){
		for(reg int j = 1;j <= m;j++){
			//判断该点是否为起点
			if(dis[i][j] - dis[i][j - 1] - dis[i - 1][j] + dis[i - 1][j - 1]) continue;
			if(henfind(j,i) - j < 1) continue;
			LL temp = henfind(j,i) - j;
			LL end = shufind(i,j);
			for(int k = i + 2;k <= end;k++){
				if(henfind(j,k) - j < 1) continue;
				LL demp = henfind(j,k) - j;
				Vc += demp * temp;
				Vc %= mod;
				if(k < end) {
					Vf += demp * temp * (end - k);
					Vf %= mod;
				}
			}
			//终点x2的位置
			
		}
	}
}

//前缀和方便计算 
inline void ass(){
	for(reg int i = 1;i <= n;i++){
		for(reg int j = 1;j <= m;j++){
			dis[i][j] += dis[i - 1][j] + dis[i][j -1] - dis[i - 1][j - 1];
		}
	}
		//调试 
	/* 
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= m;j++){
			std::cout<<dis[i][j]<<" ";
		}
		std::cout<<endl;
	} 
	*/
}

int main(){
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	T = read(),id = read();
	while(T--){
		input();
		ass();
		deal();
		LL ans1 = Vc * c % mod;
		std::cout<<ans1;
		putchar(' ');
		LL ans2 = Vf * f % mod;
		std::cout<<ans2;
		putchar(10);
	}
	return 0;
}


inline LL read(){
	LL n = 0;char ch = getchar();
	while( ch < '0' || ch > '9') ch = getchar();
	while(ch >= '0' && ch <= '9'){
		n = (n << 3) + (n << 1) + (ch ^ 48);
		ch = getchar();
	}
	return n;
}

