Under construction

Still being built. Things may change

Sailing

Time limit2sMemory limit512 MB

Summary
Find the quickest sailing route on a grid with six moves, tacking costs, and blocked waypoints.
Level

Medium5 of 10

Topics
Shortest path, Graph
Solved
No attempts yet

Problem

Anthony named his boat the Invincible and sails her in a regatta in the waters south of Singapore. Relative to the wind, the Invincible travels in only three ways.

Point of sailDirection of travelTime to the neighboring waypoint
Close haulDiagonally into the wind at 45 degrees10 minutes
Beam reachPerpendicular to the wind at 90 degrees9 minutes
Broad reachDiagonally away from the wind at 135 degrees15 minutes

The wind always blows from the top of the grid to the bottom. A close haul therefore reaches the neighboring waypoint diagonally above, a beam reach the one to the left or to the right, and a broad reach the one diagonally below.

Each point of sail comes in two versions: the starboard tack, with the wind on the right (the three directions heading left), and the port tack, with the wind on the left (the three directions heading right). That gives six possible directions.

The six sailing directions

No sailing boat can go directly into the wind for a longer period (the no-go zone), and Anthony's sailing skills do not allow him to go directly away from the wind (running).

Anthony enters waypoints into his GPS device as a square grid. At each waypoint he changes direction at most once, then sails on to the next waypoint. Within the same tack he switches 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. He cannot jibe, which means switching from broad reach on one tack to broad reach on the other. He can tack, switching from close haul on one tack to close haul on the other, and that maneuver costs 12 minutes because the boat points into the no-go zone and loses speed.

The waters south of Singapore have many parked container ships and tankers. Anthony marks the waypoints that he cannot safely reach because of a parked vessel. He never enters a marked waypoint, but a leg that cuts diagonally between two marked waypoints is not blocked.

At the start Anthony points the Invincible in any one of the six directions and immediately reaches the matching speed.

Input

The first line has the side length nn of the square grid (2≤n≤1502 \le n \le 150).

Each of the next nn lines has nn characters and describes one row of the grid. The start is S, the finish is F, a waypoint Anthony cannot reach is X, and every other waypoint is W. The grid holds exactly one S and one F.

Output

Print one line with the smallest number of minutes in which Anthony can take the Invincible from the start to the finish. If there is no way for him to reach the finish, print -1 instead.

Examples2

  1. Example 1

    Input
    9
    WWWWSWXWW
    WWWWWWWWW
    WWWWWWWWW
    WWWWWWXWW
    WWWWWWWWW
    WWWWWWWWW
    FWWWWWWWW
    WWWWWWWWW
    WWWWWWWWW
    
    Expected output
    268
    
  2. Example 2

    Input
    3
    WFW
    WWW
    WSW
    
    Expected output
    32