#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e3+10;
const ll mod=998244353;
int t,id;
int n,m,c,f;
int a[N][N],b[N][N];
ll vc,vf;
ll ansc,ansf;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9'){
		if (ch=='-') f=-f;
		ch=getchar(); 
	}
	while (ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
int rd(){
	int x=0,f=1;
	char ch=getchar();
	while (ch<'0'||ch>'9'){
		if (ch=='-') f=-f;
		ch=getchar(); 
	}
	x=ch-'0';
	return x*f;
}
void write(ll x){
	if (x<0) putchar('-'),x=-x;
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
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();
		vc=0,vf=0,ansc=0,ansf=0;
		for (int i=1;i<=n+1;i++){
			for (int j=1;j<=m+1;j++){
				a[i][j]=0;
				b[i][j]=0;
			}
		}
		for (int i=1;i<=n;i++){
			for (int j=1;j<=m;j++){
				a[i][j]=rd();
				if (a[i][j]==0) a[i][j]=1;
				else a[i][j]=0;
			}
		}
		for (int i=n;i>=1;i--){
			for (int j=1;j<=m;j++){
				b[i][j]=a[i][j];
				if (a[i][j]!=0) b[i][j]+=b[i+1][j];
			}
		}
		for (int i=1;i<=n;i++){
			for (int j=m;j>=1;j--){
				if (a[i][j]!=0) a[i][j]+=a[i][j+1];
			}
		}
		for (int j=1;j<=m;j++){
			for (int si=1;si<=n-2;si++){
				if (a[si+2][j]==0){
					si+=2;
					continue;
				}
				if (a[si+1][j]==0){
					si++;
					continue;
				}
				for (int ei=si+2;ei<=n;ei++){
					if (a[ei][j]==0) break;
					vc=a[si][j+1]*a[ei][j+1];
					vc%=mod;
					ansc+=(vc*c%mod);
					ansc%=mod;
					if (f){
						vf=a[si][j+1]*a[ei][j+1]*b[ei+1][j];
						vf%=mod;
						ansf+=(vf*f%mod);
						ansf%=mod;
					}
				}
			}
		}
		write(ansc);
		putchar(' ');
		if (f) write(ansf);
		else write(0);
		putchar('\n');
	}
	//The bloody dusk and The pale dawn.
	fclose(stdin);
	fclose(stdout);
	return 0;
}