eolymp
bolt
Try our new interface for solving problems
Problems

Rook in the maze

Rook in the maze

\textit{Rook} - it's a chess piece, which in one move can move any number of squares horizontally or vertically. However, it can not "jump" over its stand on the path. Vasja recently built on a chessboard kind of labyrinth - in some cells of the board, he put the pawn (the most "weak" chess pieces). Now he wants to know, for a minimum number of moves the Rook can get from one cell to another. He ponders this question for several days, but can not find the answer. So he turned for help to you. Write a program that finds the answer to the problem Vasja. \InputFile The first line contains two positive integers: \textbf{n} and \textbf{m} (\textbf{1} ≤ \textbf{n}, \textbf{m} ≤ \textbf{500}) - the size of the maze. Each of the following \textbf{n} rows contain \textbf{m} characters. \textbf{j}-th symbol of the \textbf{i}-th of these lines corresponds to a cell with coordinates (\textbf{i}, \textbf{j}). It is equal to "\textbf{.}" (dot) if the cell is empty, \textbf{P}, if busy pawn, \textbf{S}, if the starting cell for the rook, \textbf{F}, if this is the ultimate cage. \OutputFile In the output file output the minimum number of moves required by the rook to penetrate the cells of the initial to the final. If the final cell is unattainable from the start - output \textbf{-1}.
Time limit 1 second
Memory limit 64 MiB
Input example #1
4 4
F.PS
.PP.
.PP.
....
Output example #1
3