#include <bits/stdc++.h>
#define int long long
#define L(i,j,k) for(int (i)=(j);(i)<=(k);(i)++)
#define R(i,j,k) for(int (i)=(j);(i)>=(k);(i)--)
#define dg if(false)
using namespace std;
const int N=1010,mod=998244353;
string s[N];
bool a[N][N],C,F;
int ca,fa;
int n,m,id,ri[N][N],dw[N][N],sum[N][N],sum_[N][N];
void work(){
  cin>>n>>m>>C>>F;L(i,1,n) cin>>s[i];
  ca=0,fa=0;
  dg cout<<n<<' '<<m<<' '<<C<<' '<<F<<endl;
  L(i,1,n) L(j,1,m){
    a[i][j]=s[i][j-1]=='1';
  }
  dg L(i,1,n) L(j,1,m)
    cout<<a[i][j]<<" \n"[j==m];
  dg cout<<endl;
  L(i,1,n) a[i][m+1]=1;
  L(i,1,n){
    L(j,1,m){
      if(a[i][j]) continue;
      int l=j,r=j;
      for(;r<=m;r++){
        if(a[i][r]) {
          j=--r;
          break;
        }if(r==m){
          j=m;break;
        }
      }int len=r-l+1;
      // cout<<i<<' '<<l<<' '<<r<<endl;
      L(k,l,r) ri[i][k]=len--;
    }
  }
  dg L(i,1,n) L(j,1,m)
    cout<<ri[i][j]<<" \n"[j==m];
  dg cout<<endl;

  L(i,1,m){
    L(j,1,n){
      if(a[j][i]) continue;
      int l=j,r=j;
      for(;r<=n;r++){
        if(a[r][i]) {
          j=--r;
          break;
        }if(r==n){
          j=n;break;
        }
      }int len=r-l+1;
      L(k,l,r) dw[k][i]=len--;
    }
  }
  dg L(i,1,n) L(j,1,m)
    cout<<dw[i][j]<<" \n"[j==m];
  dg cout<<endl;
  L(i,1,m) L(j,1,n){
    if(a[j][i]) sum[j][i]=sum[j-1][i];
    else if(ri[j][i]==1){
      sum[j][i]=sum[j-1][i];
    }else sum[j][i]=sum[j-1][i]+ri[j][i]-1;
  }

  dg L(i,1,n) L(j,1,m)
    cout<<sum[i][j]<<" \n"[j==m];
  dg cout<<endl;

  L(i,1,m) L(j,1,n){
    if(a[j][i]) sum_[j][i]=sum_[j-1][i];
    else if(dw[j][i]==1||ri[j][i]==1){
      sum_[j][i]=sum_[j-1][i];
    }else sum_[j][i]=(sum_[j-1][i]+(ri[j][i]-1)*(dw[j][i]-1)%mod)%mod;
  }

  dg L(i,1,n) L(j,1,m)
    cout<<sum_[i][j]<<" \n"[j==m];
  dg cout<<endl;

  L(i,1,n-2) L(j,1,m){
    if(ri[i][j]<=1) continue;
    (ca+=(ri[i][j]-1)*(sum[i+dw[i][j]-1][j]-sum[i+1][j])%mod)%=mod;
    dg cout<<j+dw[i][j]-1<<endl;
    dg cout<<i<<' '<<j<<' '<<ca<<endl;
  }
  dg cout<<ca<<endl;

  L(i,1,n-3) L(j,1,m){
    if(ri[i][j]<=1) continue;
    (fa+=(ri[i][j]-1)*(sum_[i+dw[i][j]-1][j]-sum_[i+1][j])%mod)%=mod;
    // dg cout<<j+dw[i][j]-1<<endl;
    // dg cout<<i<<' '<<j<<' '<<ca<<endl;
  }
  dg cout<<fa<<endl;
  cout<<ca*C<<' '<<fa*F<<endl;
}signed main(){
	freopen("plant.in","r",stdin);
  freopen("plant.out","w",stdout);
  ios::sync_with_stdio(false);cin.tie(nullptr);
  int t;cin>>t>>id;
  L(i,1,t) work();
  return 0;
}