405055: GYM101755 H Safe Path

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

H. Safe Pathtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You play a new RPG. The world map in it is represented by a grid of n × m cells. Any playing character staying in some cell can move from this cell in four directions — to the cells to the left, right, forward and back, but not leaving the world map.

Monsters live in some cells. If at some moment of time you are in the cell which is reachable by some monster in d steps or less, he immediately runs to you and kills you.

You have to get alive from one cell of game field to another. Determine whether it is possible and if yes, find the minimal number of steps required to do it.

Input

The first line contains three non-negative integers n, m and d (2 ≤ n·m ≤ 200000, 0 ≤ d ≤ 200000) — the size of the map and the maximal distance at which monsters are dangerous.

Each of the next n lines contains m characters. These characters can be equal to «.», «M», «S» and «F», which denote empty cell, cell with monster, start cell and finish cell, correspondingly. Start and finish cells are empty and are presented in the input exactly once.

Output

If it is possible to get alive from start cell to finish cell, output minimal number of steps required to do it. Otherwise, output «-1».

ExamplesInput
5 7 1
S.M...M
.......
.......
M...M..
......F
Output
12
Input
7 6 2
S.....
...M..
......
.....M
......
M.....
.....F
Output
11
Input
7 6 2
S.....
...M..
......
......
.....M
M.....
.....F
Output
-1
Input
4 4 2
M...
.S..
....
...F
Output
-1
Note

Please note that monsters can run and kill you on start cell and on finish cell as well.

加入题单

算法标签: