#include<bits/stdc++.h>
using namespace std;
long long T,id,n,m,c,f,vc,vf,cnt[1005][1005],ans[1005][1005],cntf;
char a[1005][1005];
int main(){
cin>>T>>id;
while(T--){
vc=0,vf=0;
cin>>n>>m>>c>>f;
memset(cnt,0,sizeof(cnt));
memset(ans,0,sizeof(ans));
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(a[i][j]=='0'){
int k=j+1;
while(a[i][k]=='0') k++,cnt[i][j]++;
} for(int i=3;i<=n;i++) for(int j=1;j<=m-1;j++) if(cnt[i][j]!=0) for(int l=i-2;l>=1;l--){
if(a[l+1][j]=='1') break;
ans[i][j]+=(cnt[l][j]*cnt[i][j])%998244353;
} for(int i=3;i<=n;i++) for(int j=1;j<=m-1;j++) if(ans[i][j]!=0) vc+=ans[i][j],vc%=998244353;
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++) cout<<cnt[i][j]<<" ";
// cout<<endl;
// } cout<<endl;
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++) cout<<ans[i][j]<<" ";
// cout<<endl;
// }
for(int i=3;i<=n-1;i++) for(int j=1;j<=m-1;j++) if(ans[i][j]!=0){
int l=i+1,cntf=0;
while(a[l][j]=='0') cntf++,l++;
ans[i][j]*=cntf,vf+=ans[i][j],vf%=998244353;
}
cout<<(c*vc)%998244353<<" "<<(f*vf)%998244353<<endl;
// cout<<endl;
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++) cout<<ans[i][j]<<" ";
// cout<<endl;
// }
} return 0;
}
/*
1 0
5 4 1 1
1100
0001
0000
0101
0001
*/
Subtask #0 最后两个点TLE
Subtask #1 前四个点AC
求调!!!