beecrowd | 1376

Brothers

By Jorge Teran Bolivia
Timelimit: 2

In the land of ACM ruled a great King who became obsessed with order. The kingdom had a rectangular form, and the King divided the territory into a grid of small rectangular counties. Before dying, the King distributed the counties among his sons.

However, he was unaware that his children had developed a strange rivalry: the first heir hated the second heir, but not the rest; the second heir hated the third heir, but not the rest, and so on . . . Finally, the last heir hated the first heir, but not the other heirs.

As soon as the King died, the strange rivalry among the King’s sons sparked off a generalized war in the kingdom. Attacks only took place between pairs of adjacent counties (adjacent counties are those that share one vertical or horizontal border). A county X attacked an adjacent county Y whenever the owner of X hated the owner of Y . The attacked county was always conquered by the attacking brother. By a rule of honor all the attacks were carried out simultaneously, and a set of simultaneous attacks was called a battle. After a certain number of battles, the surviving sons made a truce and never battled again. For example, if the King had three sons, named 0, 1 and 2, the figure below shows what happens in the first battle for a given initial land distribution:

You were hired to help an ACM historian determining, given the number of heirs, the initial land distribution and the number of battles, what was the land distribution after all battles.

Input

The input contains several test cases. The first line of a test case contains four integers N, R, C and K, separated by single spaces. N is the number of heirs (2 ≤ N ≤ 100), R and C are the dimensions of the kingdom (2 ≤ R, C ≤ 100), and K is the number of battles (1 ≤ K ≤ 100). Heirs are identified by sequential integers starting from zero (0 is the first heir, 1 is the second heir, ..., N − 1 is the last heir). Each of the next R lines contains C integers Hr,c separated by single spaces, representing initial land distribution: Hr,c is the initial owner of the county in row r and column c (0 ≤ Hr,cN − 1).

The last test case is followed by a line containing four zeroes separated by single spaces.

Output

For each test case, your program must print R lines with C integers each, separated by single spaces in the same format as the input, representing the land distribution after all battles.

Sample Input Sample Output

3 4 4 3
0 1 2 0
1 0 2 0
0 1 2 0
0 1 2 2
4 2 3 4
1 0 3
2 1 2
8 4 2 1
0 7
1 6
2 5
3 4
0 0 0 0

2 2 2 0
2 1 0 1
2 2 2 0
0 2 0 0
1 0 3
2 1 2
7 6
0 5
1 4
2 3