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


const int N = 1005;
const int mod = 998244353;

int n,m;
int c,f;
int id;

char s[N][N];

long long hang[N][N];
long long lie[N][N];

long long ans1 , ans2;

void solve()
{
	scanf("%d%d%d%d",&n,&m,&c,&f);
	for ( int i = 1 ; i <= n ; i++ ) scanf("%s",s[i]+1);
	
	for ( int i = 1 ; i <= n ; i++ ) for ( int j = 1 ; j <= m ; j++ ) if ( s[i][j] == '0' ) hang[i][j] = 1;
	for ( int i = 1 ; i <= n ; i++ ) for ( int j = m ; j >= 1 ; j-- ) if ( s[i][j] == '0' ) hang[i][j] += hang[i][j+1];
	
	for ( int j = 1 ; j <= m ; j++ ) for ( int i = 1 ; i <= n ; i++ ) if ( s[i][j] == '0' ) lie[i][j] = 1;
	for ( int j = 1 ; j <= m ; j++ ) for ( int i = n ; i >= 1 ; i-- ) if ( s[i][j] == '0' ) lie[i][j] += lie[i+1][j];
	
	for ( int j = 1 ; j <= m ; j++ )
	{
		long long sum = 0;
		for ( int i = 1 ; i <= n ; i++ )
		{
			if ( hang[i][j] > 1 )
			{
				if ( hang[i-1][j] > 1 ) ans1 = (ans1+(hang[i][j]-1)*(sum-(hang[i-1][j]-1))%mod)%mod;
				else ans1 = (ans1+(hang[i][j]-1)*sum%mod)%mod;
				sum += hang[i][j]-1;
			}
			if ( s[i][j] == '1' ) sum = 0;
		}
	}
	for ( int j = 1 ; j <= m ; j++ )
	{
		long long sum = 0;
		for ( int i = 1 ; i <= n ; i++ )
		{
			if ( hang[i][j] > 1 )
			{
				long long w;
				if ( hang[i-1][j] > 1 ) w = (hang[i][j]-1)*(sum-(hang[i-1][j]-1))%mod;
				else w = (hang[i][j]-1)*sum%mod;
				ans2 = (ans2+w*(lie[i][j]-1)%mod)%mod; 
				sum += hang[i][j]-1;
			}
			if ( s[i][j] == '1' ) sum = 0;
		}
	}
	
	printf("%lld %lld",ans1*c,ans2*f);
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T;
	scanf("%d%d",&T,&id);
	while ( T-- ) solve();
	return 0;
}