// Arup Guha
// 10/5/2015
// Solution to 2003 UCF HS Contest Problem: The Array Reloaded

// Note: The posted output has errors. I have fixed the errors here. They are as follows: there should be no space
//       after the last item on each line of the grid because the output format says space separated. Also, a grid of
//       size 1 row by 0 columns should print one blank line instead of 0 blank lines according to the output spec.

import java.util.*;

public class reloaded {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int r = stdin.nextInt();
		int c = stdin.nextInt();
		int n = stdin.nextInt();
		int loop = 1;

		// Process all cases.
		while (r !=0 || c != 0 || n != 0) {

			// Set up data storage.
			char[][] grid = new char[r][c];
			for (int i=0; i<r; i++)
				Arrays.fill(grid[i], '0');
			operation[] list = new operation[n];

			// Read in all the operations, again in an annoying order.
			for (int i=0; i<n; i++) {
				int sx = stdin.nextInt();
				int sy = stdin.nextInt();
				int dx = stdin.nextInt();
				int dy = stdin.nextInt();
				int t = stdin.nextInt();
				char[][] let = new char[dx][dy];
				for (int j=0; j<dx; j++)
					for (int k=0; k<dy; k++)
						let[j][k] = stdin.next().charAt(0);
				list[i] = new operation(sx, sy, dx, dy, t, let);
			}


			// Sort it so we apply these in the right order.
			Arrays.sort(list);

			// Now perform the applications.
			for (int i=0; i<n; i++)
				list[i].apply(grid);

			// Output result in appropriate format.
			System.out.println("Set "+loop+":");
			for (int i=0; i<r; i++) {
				for (int j=0; j<c; j++) {
					System.out.print(grid[i][j]);
					if (j < c-1) System.out.print(" ");
				}
				System.out.println();
			}
			System.out.println();

			// Get next case.
			r = stdin.nextInt();
			c = stdin.nextInt();
			n = stdin.nextInt();
			loop++;
		}
	}
}

class operation implements Comparable<operation> {

	public int sx;
	public int sy;
	public int dx;
	public int dy;
	public int time;
	public char[][] sub;

	public operation(int myx, int myy, int myrows, int mycols, int mytime, char[][] read) {
		sx = myx;
		sy = myy;
		dx = myrows;
		dy = mycols;
		time = mytime;
		sub = read;
	}

	// Sorts by time.
	public int compareTo(operation other) {
		return this.time - other.time;
	}

	// Applies this operation to grid.
	public void apply(char[][] grid) {
		for (int i=sx; i<sx+dx; i++)
			for (int j=sy; j<sy+dy; j++)
				grid[i][j] = sub[i-sx][j-sy];
	}
}