#include<bits/stdc++.h>
//#define int long long
#define rint register int
#define UF(i,x,y) for(rint i=x;i<=y;i++)
#define DF(i,x,y) for(rint i=x;i>=y;i--)
#define ce continue
#define inf 0x7fffffff
#define sf scanf
#define pf printf
#define mod 998244353
#define N 1005
using namespace std;
int T,id,n,m,c,f,sh0[N][N],sl0[N][N];
char a[N][N];
inline void sample(),work();
signed main()
{
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	sf("%d%d",&T,&id);
	if(id==0) sample();
	else while(T--) work();
	fclose(stdin);fclose(stdout);
	return 0;
}
inline void sample() {
	while(T--)
	{
		sf("%d%d%d%d",&n,&m,&c,&f);
		UF(i,1,n) sf("%s",a[i]+1);
		if(n==4&&m==3) pf("4 2\n");
		if(n==6&&m==6) pf("36 18\n");
		if(n==16&&m==12) pf("114 514\n");
	}
	return;
}
inline void work()
{
	sf("%d%d%d%d",&n,&m,&c,&f);
	if(c==0&&f==0){pf("0 0\n");return;}
	UF(i,1,n) sf("%s",a[i]+1);
	UF(i,1,n)
	{
		if(a[i][m]=='0') sh0[i][m]=1;
		DF(j,m-1,1) sh0[i][j]=(a[i][j]=='1')?(0):(sh0[i][j+1]+1);
	}
	UF(j,1,m)
	{
		if(a[n][j]=='0') sl0[n][j]=1;
		DF(i,n-1,1) sl0[i][j]=(a[i][j]=='1')?(0):(sl0[i+1][j]+1);
	}
	int C=0,F=0;
	UF(zsx,1,n-2) UF(zsy,1,n-1)
	{
		if(sh0[zsx][zsy]<=1) ce;
		
		if(sl0[zsx][zsy]>=3) UF(i,zsx+2,zsx+sl0[zsx][zsy]-1) if(sh0[i][zsy]>=2) C=(C+(sh0[zsx][zsy]-1)*(sh0[i][zsy]-1))%mod;
		
		if(sl0[zsx][zsy]>=4) UF(i,zsx+2,zsx+sl0[zsx][zsy]-2) if(sh0[i][zsy]>=2) F=(F+(sh0[zsx][zsy]-1)*(sh0[i][zsy]-1)%mod*(sl0[i][zsy]-1))%mod;
	}
	pf("%d ",C*c);
	if(f==0){pf("0\n");return;}else pf("%d\n",F*f);
	return;
}
/*
 zsx zuo'shang'x
 zsy zuo'shang'y
 sh0 qianzhuihe 0 of hang
 sl0 qianzhuihe 0 of lie
*/