#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
ll read()
{
	ll x=0,f=1;
	char ch = getchar();
	while (ch > '9' || ch < '0')
		{
			if (ch == '-')
				f = -1;
			ch = getchar();
		}
	while (ch <= '9' && ch >= '0')
		{
			x = x*10+ch-'0';
			ch = getchar();
		}
	return x*f;
}
void write(ll x)
{
	if (x < 0)
		{
			putchar('-');
			x = -x;
		}
	if (x > 9)
		write(x/10);
	putchar(x%10+'0');
}
const ll mod = 998244353;
ll T,id;
ll n,m,c,f;
char gar[1010][1010];
ll righ[1010][1010],down[1010][1010];
ll ansc,ansf;
int main()
{
	//freopen("plant_ex.in","r",stdin);
	
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	
	//clock_t beginclo = clock();
	
	T = read();
	id = read();
	while (T--)
		{
			n = read();
			m = read();
			c = read();
			f = read();
			for (int i=1;i<=n;++i)
				scanf("%s",gar[i]+1);
			
			for (int i=1;i<=n;++i)
				for (int j=1;j<=m;++j)
					righ[i][j] = down[i][j] = 0;
			
			for (int i=n;i>=1;--i)
				for (int j=m;j>=1;--j)
					if (gar[i][j] == '0')
						{
							righ[i][j-1] = righ[i][j]+1;
							down[i-1][j] = down[i][j]+1;
						}
					else
						{
							righ[i][j] = 0;
							down[i][j] = 0;
						}
			
			/*for (int i=1;i<=n;++i)
				{
					for (int j=1;j<=m;++j)
						cout<<righ[i][j]<<" ";
					cout<<endl;
				}cout<<endl;
			for (int i=1;i<=n;++i)
				{
					for (int j=1;j<=m;++j)
						cout<<down[i][j]<<" ";
					cout<<endl;
				}*/
			
			ansc = ansf = 0;
			
			for (int j=1;j<=m;++j)
				for (int i=1;i<=n;++i)
					if (gar[i][j] == '0')
						{
							if (gar[i+1][j] == '1')
								continue;
							for (int k=i+2;k<=n;++k)
								{
									if (gar[k][j] == '1')
										break;
									ansc += righ[i][j]*righ[k][j];
									ansc %= mod;
									//cout<<i<<" "<<j<<" "<<k<<" "<<ansf<<endl;
									ansf += righ[i][j]*righ[k][j]*down[k][j];
									ansf %= mod;
								}
						}
					
			
			printf("%lld %lld\n",c*ansc,f*ansf);
		}
	
	//cout<<"\ntime:"<<(double)(clock()-beginclo)/CLOCKS_PER_SEC<<"s";
	
	return 0;
}
/*
1 0
4 3 1 1
001
010
000
000

1 0
6 6 1 1
000010
011000
000110
010000
011000
000000

ans: 36 18
*/
