The input is a sequence of datasets. A dataset is formatted as follows:
w d
c11 . . . cw1
.
.
.
c1d . . . cwd
v1v2v3v4v5v6
The first line is a pair of positive integers w and d separated by a space. The next d lines are w-character-long strings c
11 ... c
w1,... , c
1d ... c
wd with no spaces. Each character c
ij is one of the letters r, g, b, c, m, y, w and k, which stands for red, green, blue, cyan, magenta, yellow, white and black respectively, or a sign #. Each of r, g, b, c, m, y and # occurs once and only once in a dataset. The last line is a six-character-long string v
1v
2v
3v
4v
5v
6 which is a permutation of “rgbcmy”.
The integers w and d denote the width (the length from the east end to the west end) and the depth (the length from the north end to the south end) of a bed. The unit is the length of a side of a square. You can assume that neither w nor d is greater than 30.
Each character c
ij shows the color of a square in the bed. The characters c
11, c
w1, c
1d and c
wd correspond to the north-west corner, the north-east corner, the south-west corner and the south-east corner of the bed respectively. If c
ij is a letter, it indicates the color of the corresponding square. If c
ij is a #, the corresponding square is colored white and is the initial position of the cube.
The string v
1v
2v
3v
4v
5v
6 shows the order of colors of squares to visit. The cube should visit the squares colored v
1, v
2, v
3, v
4, v
5 and v
6 in this order.
The end of the input is indicated by a line containing two zeros separated by a space.