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

## 문제

Farmer John decided to start his own cruise ship line! He has but one ship, but is hoping for big growth. He recently acquired a map of the area of ocean where his cruise ship will operate. It looks something like the diagram below, with height H (3 <= H <= 1000) and width W (3 <= W <= 1000).

       ...................
...................
.....A.............
.....A..x..........
..x..A.....AAAA....
.....A.....A..A....
.....AAAAAAAA.A....
........A.....A....
.xx...AAA...x.A....
......A............
...AAAAAAAAAAAAA...
...................

In this map, '.' denotes water; 'A' is an element of the main island; and 'x' are other islands.

Farmer John has decided his cruise ship will loop around the main island. However, due to trade restrictions, the path his ship takes is NOT allowed to go around any OTHER islands. For instance, the following path of length 50 is not allowed because it encloses the island denoted by 'x'.

       ...................
....+--+...........
....|A.|...........
....|A.|x.+-----+..
..x.|A.+--+AAAA.|..
....|A.....A..A.|..
....|AAAAAAAA.A.|..
....|...A.....A.|..
.xx.|.AAA...x.A.|..    <--- route circumnavigates 'x' -- illegal!
..+-+.A.........|..
..|AAAAAAAAAAAAA|..
..+-------------+..

Given a map, help Farmer John determine the shortest path his cruise ship can take to go around the main island without going around any other islands.

Two cells are considered connected if they lie vertically or horizontally across from one another (not diagonally). It is guaranteed that the main island is connected and that a solution exists.

Note that FJ's path may visit the same square more than once, for instance there are three squares that are visited more than once in FJ's optimal path (of length 62) for the example:

       ...................
....+--+...........
....|A.|...........
....|A.|x.+----+...
..x.|A.+--+AAAA|...
....|A.....A..A|...
....|AAAAAAAA.A|...
....|...A..+-+A|...
.xx.|.AAA..|x|A|...
..+-+.A....+-+-++..
..|AAAAAAAAAAAAA|..
..+-------------+..

The above diagram is somewhat unclear because of the path overlapping itself.  Drawn in two stages, FJ's optimal path is:

       ...................            ...................
...................            ....+--+...........
.....A.............            ....|A.|...........
.....A..x..........            ....|A.|x.+----+...
..x..A.....AAAA....            ..x.|A.+--+AAAA|...
.....A.....A..A....  and then  ....|A.....A..A|...
.....AAAAAAAA.A....            ....|AAAAAAAA.A|...
....V...A..+>.A....            ....V...A...>+A|...
.xx.|.AAA..|x.A....            .xx...AAA...x|A|...
..+-+.A....+----+..            .....A.......+-+...
..|AAAAAAAAAAAAA|..            ...AAAAAAAAAAAAA...
..+-------------+..            ...................

## 입력

• Line 1: Two space-separated integers: H and W
• Lines 2..H+1: Line i+1 contains contains W characters that are the elements of map row i (all '.' or 'x' or 'A')

## 출력

• Line 1: The minimum length of a path that Farmer John's cruise ship can take

## 예제 입력 1

12 19
...................
...................
.....A.............
.....A..x..........
..x..A.....AAAA....
.....A.....A..A....
.....AAAAAAAA.A....
........A.....A....
.xx...AAA...x.A....
......A............
...AAAAAAAAAAAAA...
...................


## 예제 출력 1

62