#include<bits/stdc++.h>
using namespace std;
int n, m, c, k, p[501], x1[501], y3[501], x2[501], y2[501], xt[501], yt[501], nm[501][501];
struct num{
int v, s;
}ans[501][501];
int main(){
cin >> n >> m >> c >> k;
for(int i=1; i<=k; i++){
cin >> x1[i] >> y3[i] >> x2[i] >> y2[i] >> p[i] >> xt[i] >> yt[i];
}
for(int i=1; i<=c; i++){
for(int j=1; j<=c; j++){
cin >> nm[i][j];
}
}
for(int i=1; i<=k; i++){
for(int a=x1[i]; a<=x2[i]; a++){
for(int b=y3[i]; b<=y2[i]; b++){
if(ans[a][b].v<p[i]){
ans[a][b].v=p[i];
ans[a][b].s=nm[xt[i]+(a-x1[i])][yt[i]+(b-y3[i])];
}
}
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(ans[i][j].s==0) ans[i][j].s=-1;
cout << ans[i][j].s << ' ';
}
cout << endl;
}
return 0;
}