import java.util.Scanner;
public class Main {
private static String blank = "\\x20";
private static String newLine = "\\x0A";
private static String x = "\\x";
private static String reset = "\\x1B\\x5B\\x30\\x6D";
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// System.out.println("\033[38;2;255;0;0mHello\033[0m");
// System.out.println("\033[48;2;255;255;255mWorld\033[0m");
// 1 1
// 1 1
//#010203
// 2 2
// 1 2
//#111111
//#0
//#000000
//#111
int m = sc.nextInt();
int n = sc.nextInt();
int p = sc.nextInt();
int q = sc.nextInt();
RGB[][] colors = new RGB[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
String s = sc.next();
colors[i][j] = format(s);
}
}
RGB[][] temps = new RGB[n / q][m / p];
for(int i = 0; i < n; i += q) {
for(int j = 0; j < m; j += p){
int i_ = i / q;
int j_ = j / p;
temps[i_][j_] = new RGB();
for(int k = i; k < i + q; k++){
for(int t = j; t < j + p; t++){
temps[i_][j_].increase(colors[k][t]);
}
}
temps[i_][j_].average(p * q);
}
}
RGB prev = null;
for(int i = 0; i < n / q; i++){
for(int j = 0; j < m / p; j++){
int count = j;
while((count < m / p - 1) && temps[i][j].equals(temps[i][j + 1])) count++;
colorOutput(temps[i][j]);
j = count;
while(count-- != -1){
System.out.print(blank);
}
prev = temps[i][j];
}
if(!prev.isDefault()){
colorOutput(new RGB());
}
System.out.print(newLine);
}
}
private static RGB format(String color){
RGB result = new RGB();
if(color.length() == 2){
result.r = getInteger(color.charAt(1)) * 16 + getInteger(color.charAt(1));
result.g = result.r;
result.b = result.r;
}else if(color.length() == 4){
result.r = getInteger(color.charAt(1)) * 16 + getInteger(color.charAt(1));
result.g = getInteger(color.charAt(2)) * 16 + getInteger(color.charAt(2));
result.b = getInteger(color.charAt(3)) * 16 + getInteger(color.charAt(3));
}else{
result.r = getInteger(color.charAt(1)) * 16 + getInteger(color.charAt(2));
result.g = getInteger(color.charAt(3)) * 16 + getInteger(color.charAt(4));
result.b = getInteger(color.charAt(5)) * 16 + getInteger(color.charAt(6));
}
return result;
}
private static int getInteger(char s){
if(s >= '0' && s <= '9'){
return s - '0';
}else{
return s - 'a' + 10;
}
}
// System.out.println("\033[38;2;255;0;0mHello\033[0m");
// System.out.println("\033[48;2;255;255;255mWorld\033[0m");
private static void colorOutput(RGB rgb){
if(rgb.isDefault()){
System.out.print(reset);
}else{
StringBuilder result = new StringBuilder();
result.append("\\x1B\\x5B\\x34\\x38\\x3B\\x32\\x3B");
String r = String.valueOf(rgb.r);
for(int i = 0; i < r.length(); i++){
result.append(x);
result.append(Integer.toHexString((int)r.charAt(i)).toUpperCase());
}
result.append("\\x3B");
String g = String.valueOf(rgb.g);
for(int i = 0; i < g.length(); i++){
result.append(x);
result.append(Integer.toHexString((int)g.charAt(i)).toUpperCase());
}
result.append("\\x3B");
String b = String.valueOf(rgb.b);
for(int i = 0; i < b.length(); i++){
result.append(x);
result.append(Integer.toHexString((int)b.charAt(i)).toUpperCase());
}
result.append("\\x6D");
System.out.print(result.toString());
}
}
}
class RGB{
int r = 0;
int g = 0;
int b = 0;
public RGB(){
}
public void increase(RGB rgb){
this.r += rgb.r;
this.g += rgb.g;
this.b += rgb.b;
}
public void average(int avg){
this.r /= avg;
this.g /= avg;
this.b /= avg;
}
@Override
public boolean equals(Object obj) {
RGB rgb = (RGB)obj;
if(r == rgb.r && g == rgb.g && b == rgb.b){
return true;
}else{
return false;
}
}
public boolean isDefault(){
return r == 0 && g == 0 && b == 0;
}
}