#include<bits/stdc++.h>

using namespace std;

const int N=1005;
const int Mod=998244353;
char a[N][N];
int h[N][N],l[N][N];

void pf()
{
	printf("0 0\n");
}

int main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	
	int t,n,m,c,f,id;
	int i,j;
	
	scanf("%d%d",&t,&id);
	while (t--)
	{
		scanf("%d%d%d%d",&n,&m,&c,&f);
		for (i=0;i<n;i++)
		{
			scanf("%s",&a[i]);
            for (j=0;j<m;j++)
			{
				if (j>0&&a[i][j]=='0') h[i][j]=h[i][j-1]+1;
				else h[i][j]=-1;
				if (j==0&&a[i][j]=='0') h[i][j]=0;
				if (i>0&&a[i][j]=='0') l[i][j]=l[i-1][j]+1;
				else l[i][j]=-1;
				if (i==0&&a[i][j]=='0') l[i][j]=0;
			}
		}
		if (c==0&&f==0)
		{
		    pf();
		    continue;
		}
		
		/*for (i=0;i<n;i++)
		{
			for (j=0;j<m;j++)
			{
				cout << h[i][j] << " ";
			}
			cout << endl;
		}
		cout << endl;
		for (i=0;i<n;i++)
		{
			for (j=0;j<m;j++)
			{
				cout << l[i][j] << " ";
			}
			cout << endl;
		}
		cout << endl;*/
		
		long long vc=0,vf=0;
		int jj,ii,iii,jjj;
		
		if (c!=0)
		{
			for (i=0;i<n-2;i++)
	    	{
	    		for (j=0;j<m-1;j++)
	    		{
	    			if (a[i][j]=='0')
	    			{
		    			for (ii=n-1;ii>=i+1;ii--)
	    				{
	    					if (l[ii][j]-l[i][j]==ii-i) break;
	     				}
	    				//cout << ii << " " << jj << endl;
	    				if (ii==i+1) continue;
	    				for (jj=m-1;jj>=j;jj--)
	    				{
	    					if (h[i][jj]-h[i][j]==jj-j) break;
	    				}
	    				//cout << ii << " " << jj << endl;
	    				if (jj==j) continue;
	    				//cout << ii << " " << jj << endl;
		    			for (iii=ii;iii>i+1;iii--)
	      				{
		    				for (jjj=m-1;jjj>=j;jjj--)
	    			      	{
	    			    		if (h[iii][jjj]-h[iii][j]==jjj-j) break;
	    			    	}
	    			    	if (jjj==j) continue;
	    			    	//cout << ii << " " << jj << " " << iii << " " << jjj << endl;
	    			    	vc=((long long)(jjj-j)*(long long)(jj-j)%Mod+vc%Mod)%Mod;
	    				    vf=(((long long)(jjj-j)*(long long)(jj-j)%Mod)*(ii-iii)%Mod+vf%Mod)%Mod;
						}
	    			}
	    		}
	    	}
	    	cout << (long long)vc*(long long)c%Mod;
	    	cout << " " << (long long)vf*(long long)f%Mod << endl;
		}
		
	}
	
	return 0;
}
