#include<bits/stdc++.h>
using namespace std;
const int mod = 998244353, N = 1005;
typedef long long ll;
int n, m, c, f;
char ch[N][N];
bool g[N][N];
ll ansc, ansf;
int col[N][N], row[N][N]; //col -> hengxiang di yi ge keng // row -> zhongxiang di yi ge keng
void work_C()///// Ac tick
{
	//cout<<"work_C\n";
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(g[i][j]) continue;
			if(col[i][j]==j+1) continue;
			ll hang = col[i][j]-j-1;
			ll thang = 0;
			//cout<<i<<" "<<j<<" "<<hang<<"\n";
			for(int k=i+2;k<row[i][j];k++)
			{
				thang = (thang+col[k][j]-j-1)%mod;
			}
			ansc = (ansc%mod+1ll*hang*thang%mod)%mod; 
		}
	}
}
void work_F()// Ac tick
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(g[i][j]) continue;
			if(col[i][j]==j+1) continue;
			ll hang = col[i][j]-j-1;
			ll thang, tlie, tans = 0;
			for(int k=i+2;k<row[i][j]-1;k++)
			{
				if(col[k][j]==j+1)
					continue;
				thang = col[k][j]-j-1;
				tlie = row[k][j]-k-1;
				tans = (tans+tlie*thang%mod)%mod;
			}
			ansf = (ansf%mod+tans*hang%mod)%mod;
		}
	}
}
int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	int T, id;
	scanf("%d%d", &T, &id);
	if(id==1)
	{
		while(T--)
			printf("0 0\n");
		return 0;
	}
	else
	{
		while(T--)
		{
			ansc = 0;
			ansf = 0;
			scanf("%d%d%d%d", &n, &m, &c, &f);
			for(int i=1;i<=n;i++)
				for(int j=1;j<=m;j++)
					cin>>ch[i][j];
			for(int i=1;i<=n;i++)
				for(int j=1;j<=m;j++)
					g[i][j] = ch[i][j]-'0';
	//		for(int i=1;i<=n;i++)
	//		{
	//			for(int j=1;j<=m;j++)
	//				cout<<g[i][j]<<" ";
	//			cout<<"\n";		
	//		}
			for(int i=1;i<=n;i++)
			{
				for(int j=1;j<=m;j++)
				{
					bool flag1 =0, flag2 = 0;
					for(int k=j;k<=m;k++)
					{
						if(g[i][k])
						{
							col[i][j] = k;
							flag1 = 1;
							break;
						}
					}
					if(!flag1)
						col[i][j] = m+1;
					for(int k=i;k<=n;k++)
					{
						if(g[k][j])
						{
							row[i][j] = k;
							flag2 = 1;
							break;
						}
					}
					if(!flag2)
						row[i][j] = n+1;
				}
			}
	//		for(int i=1;i<=n;i++)
	//		{
	//			for(int j=1;j<=m;j++)
	//			{
	//				cout<<i<<" "<<j<<" "<<col[i][j]<<" "<<row[i][j]<<"\n";
	//			}
	//		}
			work_C();
			work_F();
			printf("%lld %lld", ansc*c%mod, ansf*f%mod);
		}		
	}
	return 0;
}

/*
1 0
4 3 1 1
001
010
000
000
*/
