#include<bits/stdc++.h>
using namespace std;
int T,id;
int n,m,c,f,a[1005][1005],b[1005][1005]; //b is prefix sum of a
int farthest[1005][1005];
int c1[1005][1005],c2[1005][1005];
const int MOD = 998244353;
int main(){
	#ifndef LOCAL
	freopen("plant.in","r",stdin);
	freopen("plant.out","w",stdout);
	#endif
	ios::sync_with_stdio(0);
	cin >> T >> id;
	while(T--){
		memset(b,0,sizeof(b));
		cin >> n >> m >> c >> f;
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m;j++){
				char ch;
				cin >> ch;
				a[i][j] = ch-'0';
				b[i][j] = b[i][j-1]+b[i-1][j]-b[i-1][j-1]+a[i][j];
			}
		}
		memset(farthest,0,sizeof(farthest));
		if(id==1){
			cout << "0 0" << endl;
		}else if(id==0||(id>=2&&id<=4)||(id>=7&&id<=11)){
			for(int x = 1;x <= n;x++){
				for(int y = m;y >= 1;y--){
					if(a[x][y]==1) farthest[x][y] = 0;
					else farthest[x][y] = farthest[x][y+1]+1;
				}
			}
			
			/*for(int i = 1;i <= n;i++){
				for(int j = 1;j <= m;j++){
					cout << farthest[i][j] << ' '; 
				}
				cout << endl;
			}*/
			
			long long ccount = 0;
			for(int x1 = 1;x1 <= n;x1++){
				for(int x2 = x1+2;x2 <= n;x2++){
					for(int y0 = 1;y0 <= m;y0++){
						if(b[x2][y0]-b[x2][y0-1]-b[x1-1][y0]+b[x1-1][y0-1]>0) continue;
						int c1 = farthest[x1][y0]-1;
						int c2 = farthest[x2][y0]-1;
						ccount += 1ll*c1*c2;
						ccount = ccount%MOD;
					}
				}
			}
			long long fcount = 0;
			for(int x1 = 1;x1 <= n;x1++){
				for(int x2 = x1+2;x2 <= n;x2++){
					for(int x3 = x2+1;x3 <= n;x3++){
						for(int y0 = 1;y0 <= m;y0++){
							if(b[x3][y0]-b[x3][y0-1]-b[x1-1][y0]+b[x1-1][y0-1]>0) continue;
							int f1 = farthest[x1][y0]-1;
							int f2 = farthest[x2][y0]-1;
							fcount += 1ll*f1*f2;
							fcount = fcount%MOD;
						}
					}
				}
			}
			cout << (ccount*c)%MOD << " " << (fcount*f)%MOD << endl;
		}else if(id==15){ //f==0
			memset(c1,0,sizeof(c1));
			memset(c2,0,sizeof(c2));
			for(int x = 1;x <= n;x++){
				for(int y = m;y >= 1;y--){
					if(a[x][y]==1) farthest[x][y] = 0;
					else farthest[x][y] = farthest[x][y+1]+1;
				}
			}
			long long ccount = 0;
			for(int x1 = 1;x1 <= n;x1++){
				for(int x2 = x1+2;x2 <= n;x2++){
					for(int y0 = 1;y0 <= m;y0++){
						if(b[x2][y0]-b[x2][y0-1]-b[x1-1][y0]+b[x1-1][y0-1]>0) continue;
						int c1 = farthest[x1][y0]-1;
						int c2 = farthest[x2][y0]-1;
						ccount += 1ll*c1*c2;
						ccount = ccount%MOD;
					}
				}
			}
			cout << ccount << " " << 0 << endl;
		}else{
			for(int x = 1;x <= n;x++){
				for(int y = m;y >= 1;y--){
					if(a[x][y]==1) farthest[x][y] = 0;
					else farthest[x][y] = farthest[x][y+1]+1;
				}
			}
			
			/*for(int i = 1;i <= n;i++){
				for(int j = 1;j <= m;j++){
					cout << farthest[i][j] << ' '; 
				}
				cout << endl;
			}*/
			
			long long ccount = 0;
			for(int x1 = 1;x1 <= n;x1++){
				for(int x2 = x1+2;x2 <= n;x2++){
					for(int y0 = 1;y0 <= m;y0++){
						if(b[x2][y0]-b[x2][y0-1]-b[x1-1][y0]+b[x1-1][y0-1]>0) continue;
						int c1 = farthest[x1][y0]-1;
						int c2 = farthest[x2][y0]-1;
						ccount += 1ll*c1*c2;
						ccount = ccount%MOD;
					}
				}
			}
			long long fcount = 0;
			for(int x1 = 1;x1 <= n;x1++){
				for(int x2 = x1+2;x2 <= n;x2++){
					for(int x3 = x2+1;x3 <= n;x3++){
						for(int y0 = 1;y0 <= m;y0++){
							if(b[x3][y0]-b[x3][y0-1]-b[x1-1][y0]+b[x1-1][y0-1]>0) continue;
							int f1 = farthest[x1][y0]-1;
							int f2 = farthest[x2][y0]-1;
							fcount += 1ll*f1*f2;
							fcount = fcount%MOD;
						}
					}
				}
			}
			cout << (ccount*c)%MOD << " " << (fcount*f)%MOD << endl;
		}
	}
	return 0;
}