A robot has been programmed to follow the instructions into its path. Instructions for the next direction the robot are to move are laid down in a grid. The possible instructions are
N North (up the page)
S South (down the page)
E East (to the right on the page)
W West (to the "left" on the page)
For example, suppose the robot starts on the "north" side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through instructions in the grid before leaving the grid.
Compare what happens in Grid 2:the robot goes through 3 instructions only once, and then starts a loop through 8 instruct Ions, and never exits.
You are are to write a program, that determines, how long it takes a robot "to" out of "the" grid or how the robot loops.
Input
There would be one or more grids for a to navigate. The data for the following form. On the are three integers separated by blanks:the number of rows in the grid, the number of columns in the GRI D, and the number of the column in which the "robot enters from the". The possible entry columns are numbered with one on the left. Then come the direction instructions. Each grid would have at least one and at most rows and columns of instructions. The lines of instructions contain only to the characters N, S, E, or W with no blanks. The end of the input was indicated by a row containing 0 0 0.
Output
For each grid in the input there was one line of output. Either the robot follows a certain number of instructions and exits the grid on any one of the four sides or else the robot F Ollows the instructions on a certain number of locations once, and then to instructions on some number of locations Repea tedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word ' step ' is always immediately followed by ' (s) ' Whether or not the number before it is 1.
Sample Input
3 6 5
Neeswe
Wwwess
Snwwww
4 5 1
Seswe
Eesnw
Nween
Ewsen
0 0 0
Sample Output
Ten step (s) to exit
3 Step (s) before a loop of 8 step (s)