#include<bits/stdc++.h>
using namespace std ;

const int N = 1e3 + 2 ;
int T , id , n , m ;
int c , f , k , j , result_c , result_f ; 
unsigned long long answer_c , answer_f , x ,  y , value1 , value2 ;
string u ;
bool st[N][N] ;

int line_1(int a , int b )
{
	k = 0 ;
	while(st[a+k+1][b] == true)
	{
		k ++ ;
	}
	return k ;
}

int column_1(int a , int b)
{
	j = 0 ;
	while(st[a][b+j+1] == true )
	{
		j ++ ;
	}
	return j ;
}

void deal()
{
	answer_c = value1 = 0 ;
	answer_f = value2 = 0 ;
	for (int i = 1 ; i < m ; i ++ )
	{
		for(int j = 1 ; j < n-1 ; j ++ )
		{
			if (st[i][j] == true)	
			{	
				if (line_1(i,j) >= 1 )
				{
					if (column_1(i,j) >= 2 )
					{
						x= line_1(i,j) ;
						y = column_1(i,j) ;
//						cout<<i<<" "<<j<<" "<<x<<" "<<y<<" EEE "<<endl ;
						value1 = 0 ;
						for (int g = 2 ; g <= y ;g ++ )
						{
							value1 += line_1(i,j+g) ;
//							cout<<g<<" "<<value1<<" "<<line_1(i,j+g)<<" "<<column_1(i,j+g)<<" QQQ "<<endl ;
						}
						answer_c += x * value1 ;
//						cout<<x<<" "<<value1<<" "<<answer_c<<" TTT "<<endl ;
						if (column_1(i,j) >= 3)
						{
							value2 = 0 ;
							for (int g = 2 ; g <= y ; g ++ )
							{
								value2 += line_1(i,j+g) * column_1(i,j+g) ;
//								cout<<g<<" "<<value2<<" "<<line_1(i,j+g)<<" "<<column_1(i,j+g)<<" WWW "<<endl ;
							}
							answer_f += x * value2 ;
//							cout<<x<<" "<<value2<<" "<<answer_f<<" YYY "<<endl ;
						}
					}
				}
			}
		}
	}
	result_c = answer_c % 998244353 ;
	result_f = answer_f % 998244353 ;
}

int main()
{

	freopen("plant.in","r",stdin) ;
	freopen("plant.out","w",stdout) ;

	cin>>T>>id ;
//	cout<<T<<" "<<id<<endl ;
	for(int i = 1 ; i <= T ; i ++ )
	{
		cin>>n>>m>>c>>f ;
//		cout<<n<<" "<<m<<" "<<c<<" "<<f<<endl ; 
		for (int j = 1 ; j <= n ; j ++ )
		{
			cin>>u ;
			for(int i = 0 ; i < m ; i ++ )
			{
				if (u[i] == '0')
				{
					st[i+1][j] = true ;
				}
				else 
				{
					st[i+1][j] = false ;
				}
//				cout<<st[i+1][j]<<" "<<i+j<<" III "<<endl ;
			}
		}
		deal() ;
		cout<<c * result_c<<" "<<f * result_f<<endl ;
	}
	return 0 ;
}