Under construction

Still being built. Things may change

Amy, Soup is Salty!

Time limit1sMemory limit512 MB

Summary
Given an N by N kitchen grid with one sink and obstacles, find the minimum time for the sink's sound to visit every empty cell, or print -1 if impossible.
Level

Medium7 of 10

Topics
Simulation, BFS, Implementation
Solved
No attempts yet

Problem

Amy is the daughter-in-law of Farmer John. Farmer John did not think much of Amy, an Australian clean beef cow. Then one day, Farmer John tasted the hay soup Amy had cooked and flew into a rage.

FJ: [ Amy, the soup is salty! ]
Amy: [ That's just water. ]
FJ: [ The water is salty too! ]
Amy: [ A ship sailing far away cannot go calmly without meeting wind and waves. Wind and waves are always the friends of those who move forward. Joy in life is found in hardship. ]

The moment a water drop hanging from the faucet falls toward the sink, the floor plan of the kitchen where Farmer John and Amy stand is given. The kitchen is a square grid of N×NN \times N cells.

  1. Let the cells above, below, right, and left of (r,c)(r, c) be (r+1,c),(r−1,c),(r,c+1),(r,c−1)(r+1, c), (r-1, c), (r, c+1), (r, c-1).
  2. At the start, a sound is produced at the cell with the sink. One second later, the initial sound spreads up, down, left, and right, and then disappears.
  3. A sound at a cell (r,c)(r, c) other than the sink spreads one second later to the three adjacent cells except the one it just came from. For example, if the sound came from (r−1,c)(r-1, c) to (r,c)(r, c), it spreads to (r+1,c),(r,c+1),(r,c−1)(r+1, c), (r, c+1), (r, c-1).
  4. If sounds from two or more different cells arrive at cell (r,c)(r, c) at the same time, the sound does not spread to three adjacent cells. Instead, it moves by inertia. Let the previous cells be (r−dri,c−dci)(r-dr_{i}, c-dc_{i}). From one second later, the sound at (r,c)(r, c) moves with velocity (∑idri,∑idci)(\sum_{i} dr_{i}, \sum_{i} dc_{i}). Note that ∣∑idri∣|\sum_{i} dr_{i}| and ∣∑idci∣|\sum_{i} dc_{i}| can each be natural numbers of 2 or more. However, if (∑idri,∑idci)(\sum_{i} dr_{i}, \sum_{i} dc_{i}) are both 0, the sound disappears.
  5. A sound moved by rule (4) keeps moving in the same direction and at the same speed until it collides with another sound or disappears. It does not spread to two or more adjacent cells.
  6. Sound does not spread outside the grid or onto obstacles. It disappears there.
  7. Sound movement is treated as teleportation, so it can jump over obstacles. Empty cells on the path, other than the destination, are considered unvisited. Other sounds on the path do not affect it.

The eve of the storm. You must determine whether the sound of the water drop falling into the sink can reach every empty cell of the kitchen other than obstacles.

Input

The first line contains the grid size NN.

The next NN lines contain the N×NN \times N floor plan of the kitchen.

Output

Print the minimum time in seconds until the sound has visited every cell other than obstacles at least once. If it is impossible, print -1.

Constraints

  • 1≤N≤111 \le N \le 11
  • Every cell Ar,cA_{r,c} (1≤r,c≤N1 \le r,c \le N) is the sink 'S', an empty cell '.', or an obstacle '@'.
  • There is exactly one 'S'.

Examples4

  1. Example 1

    Input
    1
    S
    
    Expected output
    0
    
  2. Example 2

    Input
    7
    @@@@@@@
    @..S..@
    @.@@@.@
    @.....@
    @@@.@@@
    @.....@
    @@@@@@@
    
    Expected output
    -1
    
  3. Example 3

    Input
    7
    @@@@@@@
    @..S..@
    @.@@@.@
    @.....@
    @@.@@@@
    @.....@
    @@@@@@@
    
    Expected output
    10
    
  4. Example 4

    Input
    3
    .@.
    @@.
    ..S
    
    Expected output
    -1