#include <bits/stdc++.h>

using namespace std;

namespace IO{
    template <typename T> void read(T &x){
        bool f = false;
        char c;
        x = 0;
        while (!isdigit(c = getchar())) if (c == '-') f = true;
        while (isdigit(c)) x = 10 * x + c - '0', c = getchar();
        if (f) x = -x;
    }
    template <typename T, typename... Args> void read(T &x, Args&... args){read(x); read(args...);}
} using namespace IO;

typedef long long LL;

const int M = 1e3 + 10, mod = 998244353;

int T, id;
int n, m, c, f;
int l[M][M], down[M][M];
char s[M][M];

int main(){

	freopen("plant.in", "r", stdin);
	freopen("plant.out", "w", stdout);

    read(T, id);
    
    while (T--){

		read(n, m, c, f);
	    for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
	    
		for (int i = 0; i <= n + 1; i++)
			for (int j = 0; j <= m + 1; j++)
				l[i][j] = down[i][j] = -1;
	
		for (int i = 1; i <= n; i++)
			for (int j = m; j; j--)
				if (s[i][j] == '0') l[i][j] = l[i][j + 1] + 1;
			
		for (int i = n; i; i--)
			for (int j = 1; j <= m; j++)
				if (s[i][j] == '0') down[i][j] = down[i + 1][j] + 1;
			
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
				if (s[i][j] == '1') l[i][j] = down[i][j] = 0;
	
	    LL ansc = 0;
	
		if (c){
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= m; j++){
					if (s[i][j] == '1' or down[i][j] < 2) continue;
					if (s[i + 1][j] == '1' or i + 1 > n) continue;
					for (int k = i + 2; k <= n; k++){
						if (s[k][j] == '1') break;
						ansc = (ansc + l[i][j] * l[k][j] % mod) % mod;
					}
				}
		}
	    printf("%lld ", c * ansc % mod);
	
		LL ansf = 0;
		if (f){
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= m; j++){
					if (s[i][j] == '1' or down[i][j] < 2) continue;
					if (s[i + 1][j] == '1' or i + 1 > n) continue;
					for (int k = i + 2; k <= n; k++){
						if (s[k][j] == '1') break;
						ansf = (ansf + l[i][j] * down[k][j] % mod * l[k][j] % mod) % mod;
					}
				}

		}
		printf("%lld\n", f * ansf % mod);
	
	}
    
    return 0;
}