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 c11 · · · cw1,. . . , c1d · · · cwd with no spaces. Each character cij 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 v1v2v3v4v5v6 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 cij shows the color of a square in the bed. The characters c11, cw1, c1d and cwd correspond to the north-west corner, the north-east corner, the south-west corner and the southeast corner of the bed respectively. If cij is a letter, it indicates the color of the corresponding square. If cij is a #, the corresponding square is colored white and is the initial position of the cube. The string v1v2v3v4v5v6 shows the order of colors of squares to visit. The cube should visit the squares colored v1, v2, v3, v4, v5 and v6 in this order.
The end of the input is indicated by a line containing two zeros separated by a space.