#include <bits/stdc++.h>
using namespace std;
#define MOD 998244353
#define maxn 1005
int T,ID;
int n,m,cmd,fmd;
int a[maxn][maxn];
int cntc,cntf;
int sumx[maxn][maxn];
struct node{
int val,idx,idy;
};
node sumy[maxn][maxn];
int sumyl[maxn][maxn];
int sumf[maxn][maxn];
void csh(){
cntc=0;cntf=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
a[i][j]=-1;
sumx[i][j]=0;
sumyl[i][j]=0;
sumy[i][j].val=0;
}
}
return;
}
void cshreal(){
for(int i=1;i<=1002;++i){
for(int j=1;j<=1002;++j){
a[i][j]=-1;
}
}
return;
}
int main(){
cin>>T>>ID;
cshreal();
for(int ttt=1;ttt<=T;++ttt){
csh();
cin>>n>>m>>cmd>>fmd;
for(int i=1;i<=n;++i){
string s;
cin>>s;
for(int j=1;j<=m;++j){
if(s[j-1]=='1'){
a[i][j]=1;
}
else{
a[i][j]=0;
}
}
}
for(int i=1;i<=n;++i){
for(int j=m;j>=1;--j){
if(a[i][j+1]==0&&a[i][j]==0){
sumx[i][j]=sumx[i][j+1]+1;
}
}
}
for(int j=1;j<=m;++j){
for(int i=n;i>=1;--i){
if(a[i][j]==0){
if(sumy[i+1][j].val==0){
sumy[i][j].val=sumx[i][j];
sumy[i][j].idx=i;
sumy[i][j].idy=j;
}
else{
sumy[i][j].val=sumy[i+1][j].val+sumx[i][j];
sumy[i][j].idx=sumy[i+1][j].idx;
sumy[i][j].idy=sumy[i+1][j].idy;
}
}
}
}
for(int j=1;j<=m;++j){
for(int i=n;i>=1;--i){
if(a[i][j]==0){
sumyl[i][j]=sumyl[i+1][j]+1;
}
}
}
for(int j=1;j<=m;++j){
for(int i=n;i>=1;i--){
if(a[i][j]==0) sumf[i][j]=sumf[i+1][j]+sumyl[i+1][j]*sumx[i][j];
}
}
/*cout<<"a"<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"sumx"<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<sumx[i][j]<<" ";
}
cout<<endl;
}
cout<<"sumyl"<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<sumyl[i][j]<<" ";
}
cout<<endl;
}
cout<<"sumf"<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<sumf[i][j]<<" ";
}
cout<<endl;
}
cout<<"sumy"<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<sumy[i][j].val<<" "<<sumy[i][j].idx<<" "<<sumy[i][j].idy<<" ";
}
cout<<endl;
}
cout<<endl;*/
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
if(sumx[i][j]!=0){
cntc+=sumx[i][j]*(sumy[i][j].val-sumx[i][j]-sumx[i+1][j]);
cntc%=MOD;
if(sumyl[i][j]>=4){
cntf+=sumx[i][j]*sumf[i+2][j];
cntf%=MOD;
}
}
}
}
cout<<(cntc*cmd)%MOD<<" "<<(cntf*fmd)%MOD<<endl;
}
return 0;
}