import java.util.Scanner;
public class GraphicalEditor {
static int m,n;
static Object[][] image;
static Scanner scan = new Scanner(System.in);
static Object xyCol;
public static void main(String[] args) {
while(true) {
String command = scan.next();
if(command.equalsIgnoreCase("I")) {
m = scan.nextInt();
n = scan.nextInt();
insert(m,n);
while(true) {
command = scan.next();
if(command.equalsIgnoreCase("C")) clear();
else if(command.equalsIgnoreCase("L")) color();
else if(command.equalsIgnoreCase("S")) { save(); clear();}
else if(command.equalsIgnoreCase("V")) vertical();
else if(command.equalsIgnoreCase("H")) horizon();
else if(command.equalsIgnoreCase("K")) rectang();
else if(command.equalsIgnoreCase("F")) {
int x = scan.nextInt();
int y = scan.nextInt();
String col = scan.next();
xyCol = image[y-1][x-1];
fill(x, y, col);
}
}
}else if(command.equalsIgnoreCase("X")) {
scan.close();
break;
}
}
}
//그림판 생성
public static Object[][] insert(int m, int n) {
image = new Object[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
image[i][j] = 0;
}
}
return image;
}
//그림판 초기화
public static Object[][] clear(){
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
image[i][j] = 0;
}
}
return image;
}
// 점 색칠 칠하기
public static Object[][] color() {
int x = scan.nextInt();
int y = scan.nextInt();
String col = scan.next();
image[y-1][x-1] = col;
return image;
}
// 그림판 출력
public static void save() {
String title = scan.nextLine();
System.out.println(title);
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
System.out.print(image[i][j]);
}
System.out.print("\n");
}
}
// 수직으로 긋기
public static Object[][] vertical() {
int x = scan.nextInt();
int y = scan.nextInt();
int y2 = scan.nextInt();
String col = scan.next();
if(y-y2>0) {
for(int i=0;i<=y-y2;i++) {
image[y-i-1][x-1] = col;
}
}else {
for(int i=0;i<=y2-y;i++) {
image[y+i-1][x-1] = col;
}
}
return image;
}
//수평으로 긋기
public static Object[][] horizon() {
int x = scan.nextInt();
int x1 = scan.nextInt();
int y = scan.nextInt();
String col = scan.next();
if(x-x1>0) {
for(int i=0;i<=x-x1;i++) {
image[y][x-1-i] = col;
}
}else {
for(int i=0;i<=x1-x;i++) {
image[y-1][x-1+i] = col;
}
}
return image;
}
//채우기
public static void fill(int x, int y, String col) {
if(((y-2>=0&&x-1>=0)&&(y-2<n&&x-1<m))&&(xyCol.equals(image[y-2][x-1])||xyCol==image[y-2][x-1])) {
image[y-2][x-1] = col;
fill(x, y-1, col);
}
if(((y>=0&&x-1>=0)&&(y<n&&x-1<m))&&(xyCol.equals(image[y][x-1])||xyCol==image[y][x-1])) {
image[y][x-1] = col;
fill(x, y+1, col);
}
if(((y-1>=0)&&(x>=0)&&(y-1<n&&x<m))&&(xyCol.equals(image[y-1][x])||xyCol==image[y-1][x])) {
image[y-1][x] = col;
fill(x+1, y, col);
}
if(((y-1>=0&&x-2>=0)&&(y-1<n&&x-2<m))&&(xyCol.equals(image[y-1][x-2])||xyCol==image[y-1][x-2])) {
image[y-1][x-2] = col;
fill(x-1, y, col);
}
}
//네모 만들기
public static Object[][] rectang() {
int x = scan.nextInt();
int y = scan.nextInt();
int x1 = scan.nextInt();
int y1 = scan.nextInt();
String col = scan.next();
for(int i=y-1;i<y1-1;i++) {
for(int j=x-1;j<x1-1;j++) {
image[i][j] = col;
}
}
return image;
}
}