#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define init(x) memset (x,0,sizeof (x))
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1e3 + 5;
const int MOD = 998244353;
inline int read ();
queue <int> q;
int t,id,n,m,f,c,x[MAX][MAX],y[MAX][MAX],sum[MAX];//the extending number of x/y
ll ansc,ansf;
char s[MAX][MAX];
int main ()
{
	freopen ("plant.in","r",stdin);
	freopen ("plant.out","w",stdout);
	t = read ();id = read ();
	while (t--)
	{
		n = read ();m = read ();c = read ();f = read ();
		ansc = ansf = 0;
		for (int i = 1;i <= n;++i)
			for (int j = 1;j <= m;++j) x[i][j] = y[i][j] = 0;
		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] == '1')
				{
					while (!q.empty ()) x[i][q.front ()] = j - q.front () - 1,q.pop ();
				}
				else q.push (j);
			}
			while (!q.empty ()) x[i][q.front ()] = m - q.front (),q.pop ();
		}
		for (int j = 1;j <= m;++j)
		{
			for (int i = 1;i <= n;++i)
			{
				if (s[i][j] == '1')
				{
					while (!q.empty ()) y[q.front ()][j] = i - q.front () - 1,q.pop ();
				}
				else q.push (i);
			}
			while (!q.empty ()) y[q.front ()][j] = n - q.front (),q.pop ();
		}
		for (int j = 1;j <= m;++j)//y0
		{
			for (int i = 1;i <= n;++i)//x1
				sum[i] = sum[i - 1] + x[i][j],sum[i] %= MOD;	
			for (int i = 1;i <= n;++i) 
				if (y[i][j] > 1) ansc += 1ll * x[i][j] * (sum[i + y[i][j]] - sum[i + 1] + MOD) % MOD,ansc %= MOD;
		}	
		printf ("%lld ",ansc * c);
		if (!f) {printf ("0\n");continue;}
		for (int j = 1;j <= m;++j)//y0
		{
			for (int i = 1;i <= n;++i)//x1
			{
				for (int k = 1;k <= n;++k) sum[k] = 0;
				for (int k = 2;k <= y[i][j];++k) sum[i + k] = sum[i + k - 1] + 1ll * x[i][j] * x[i + k][j],sum[i + k] %= MOD;
				for (int k = 3;k <= y[i][j];++k)//x3
					ansf += sum[i + k - 1] - sum[i + 1] + MOD,ansf %= MOD;
			}
		}		
		printf ("%lld\n",ansf * f);
	}
	return 0;
}
inline int read ()
{
	int s = 0,f = 1;
	char ch = getchar ();
	while ((ch < '0' || ch > '9') && ch != EOF)
	{
		if (ch == '-') f = -1;
		ch = getchar ();
	}
	while (ch >= '0' && ch <= '9')
	{
		s = s * 10 + ch - '0';
		ch = getchar ();
	}
	return f * s;
}

