시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
2 초 512 MB 0 0 0 0.000%

문제

The Sultan Shoal Regatta, one of the most challenging sailing events in Asia, is held annually in the waters south of Singapore. Anthony, an aspiring young local sailor, named his boat Invincible. He has her trimmed such that she can go in three directions (points of sail), relative to the wind, with varying speed:

Allowed point of sail Explanation
Close haul Going at an angle of 45o into the wind
Beam reach Going at an angle of 90o perpendicular to the wind
Broad reach Going at an angle of 135o compared to the wind

He can do this going either with the wind coming from the left (port tack) or from the right (starboard tack), which leads to six possible directions, as depicted in the following diagram.

Note that no sailing boat can go directly into the wind for a longer period (No-Go Zone), and that Anthony’s sailing skills do not allow him to go directly away from the wind (Running).

Anthony uses his GPS (Global Positioning System) device for navigation and enters waypoints in a grid in shape of a square. With the winds on the day of the regatta, it takes him 10 minutes from a waypoint to the diagonally neighboring waypoint into the wind (going close haul), 9 minutes from a waypoint to the neighboring waypoint perpendicular to the wind (going beam reach), and 15 minutes from a waypoint to the diagonally neighboring waypoint away from the wind (going broad reach).

He instructs the crew to allow a change of direction at each waypoint. At each waypoint, the Invincible can change on the same tack (port tack or starboard tack) from close haul to beam reach, from beam reach to close haul, from beam reach to broad reach and from broad reach to beam reach, all without any loss of speed. Anthony’s sailing skills do not allow him to change from broad reach on starboard tack to broad reach on port tack or vice versa (jibing). However, he can change from close haul on port tack to close haul on starboard tack, and vice versa, using a maneuver called tacking, in which he loses 12 minutes, due to the loss of speed from pointing the boat temporarily into the No-Go Zone.

The waters south of Singapore have many parked container ships and tankers. Anthony marks those waypoints that he cannot safely reach due to a parked vessel in his GPS device. The following chart contains an example where Anthony has marked the start of the regatta (S), the finish (F), and the unreachable waypoints (indicated with X). At the begining, Anthony is allowed to point the Invincible in any of the allowed points of sail and and reaches the corresponding speed at the start of the regatta. The wind always blows from top to bottom.

입력

Your program must read from the standard input the following data. The first line specifies the length n of the square-shaped grid, where 2 ≤ n ≤ 150. Each of the following n lines contains n characters, each of which characterizes the corresponding cell of the grid. The start is marked with with ‘S’, the finish with ‘F’, the unreachable waypoints with ‘X’ and all other waypoints with ‘W’.

출력

Your program must write one line to the standard output, containing the smallest number of minutes in which Anthony can take the Invincible from the start to sure victory at the finish of the Sultan Shoal Regatta. If there is no way for Anthony to reach the finish, given his sailing skills and the parked vessels, the output line contains the number -1 instead.

예제 입력 1

9
WWWWSWXWW
WWWWWWWWW
WWWWWWWWW
WWWWWWXWW
WWWWWWWWW
WWWWWWWWW
FWWWWWWWW
WWWWWWWWW
WWWWWWWWW

예제 출력 1

268

One of the routes that allows Anthony to go from start to finish in 268 minutes is indicated below.

In this figure, each section between waypoints is marked with the number of minutes to traverse it. In addition, the two tacks require 12 minutes each, as indicated by the large numbers on the right and left.