Grasshopper
We are at a funfair, where an array of trampolines, named "The Grasshopper Labyrinth", catches our attention. As the gure below shows, all of them are labelled with non-negative integers:
People are inside, jumping from one trampoline to another, trying to reach the trampoline in the northwest corner, where the exit to the attraction is located. If you reach the exit fast enough, you may win a prize. However, in order to be eligible for the prize, you must abide by the following rule: after leaping from a trampoline labelled with z, you need to get to another one z trampolines away, in the same row or column.
Therefore, your problem is to find the shortest path from any trampoline to the way out, measured by the number of leaps needed. Since the length of the jump from any trampoline is given, it is sucient to label each trampoline with the direction of the best jump from it.
For a given starting position, a path is considered shorter than another if it requires a smaller number of jumps; in case of a tie, the path whose rst step gets you northmost in the array is to be preferred; and in case of a tie, the one getting you westmost.
Instead of these symbols, we are using the plain text ones: the appropriate cardinal point ('N', 'S', 'E' or 'W') for the arrows, 'X' for the trampolines without possible escape, and the asterisk '*' for the special trampoline at the upper left position, which represents the exit.
Input
Several cases are given in a single test file. The first line in each test case contains a pair of integers between 1 and 50, separated by a single space; the first is the number of rows and the second the number of columns in the matrix. Then, the entries in the matrix follow, line by line, each element being a non-negative integer, again separated by single whitespace characters. A 0×0 matrix will denote the end of the test cases, and hence should not be analyzed.
Output
The expected output of each data case is a character matrix. Each element is one of the allowed charset, "N", "S", "E", "W", "X" or "*", as described above. The output for each case is followed by a blank line.