시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
2 초 512 MB 41 14 13 33.333%

문제

Your friend just bought a new programmable robot and has asked for your help. The robot operates in a 2D grid that may contain obstacles. The environment has a known start location and a known goal location. The robot is controlled with a string consisting of commands L, R, U, and D, which respectively instruct the robot to move one square to the left, right, up or down in the grid. The robot will ignore a command (but continue with future commands) if the command would cause it to leave the grid or to run into an obstacle. If the robot were to reach the goal position, it immediately stops its program with success (even if further commands exist).

Your friend has already programmed the robot with a command string, but the problem is that your friend is not a very good programmer, and so it may be that the commands do not lead the robot successfully to the goal. You would like to fix the string so that the robot will reach the goal, but you do not want your friend to feel bad, so you are hoping to make as few changes to the program as are needed. A single change consists either of deleting an arbitrary character of the command string, or inserting an arbitrary command anywhere within the string.

As an example, if we consider Sample Input 1, we see that your friend’s command string of DRRDD does not succeed. The initial D moves the robot one spot down. From there, the R (and the subsequent R) are ignored because of the obstacle to the robot’s right. The subsequent D moves the robot down once again and the final D is ignored. However, by deleting the initial D, we can rely on the command string RRDD which does successfully lead the robot to the goal.

If we consider Sample Input 2, we find that your friend’s original command string LDLDLLDR is flawed. However, if we insert the single command U after the fifth command, the resulting string LDLDLULDR successfully guides the robot to the goal. It starts by moving left-down-left; the next down command is ignored because the robot is not allowed to leave the grid. The subsequent left-up-left completes the path to the goal (and the final DR commands are ignored as the robot stops immediately upon reaching the goal).

With Sample Input 3, your friend’s command string can be corrected with two changes, for example with the new command ULDLDLLDLR (although this is not the only way to use two changes to produce a valid sequence).

입력

The first line of the input contains the two integers H and W that respectively define the height and width of the grid such that 1 ≤ H, W ≤ 50. The next H lines each has W characters, describing the corresponding row of the grid, such that the character ’S’ designates the starting location, ’G’ designates the goal location, ’#’ designates an obstacle, and ’.’ designates an empty location. There will always be precisely one ’S’ and one ’G’ in the grid, and there will always exist an unobstructed path from the start position to the goal position. The final line of the input contains your friend’s original command string consisting of between 1 and 50 characters, inclusive.

출력

Output a single integer indicating the minimum number of changes that are needed to fix the program.

예제 입력 1

3 3
S..
.#.
..G
DRRDD

예제 출력 1

1

예제 입력 2

3 7
.......
.G.#.S.
.......
LDLDLLDR

예제 출력 2

1

예제 입력 3

3 7
.#.....
.G.##S.
.......
LDLDLLDR

예제 출력 3

2

예제 입력 4

2 4
S.#.
#..G
RRUUDDRRUUUU

예제 출력 4

0