cho.sh
Notes
Loading...

Do Not Go to School!

Time limit

2s

Memory limit

160 MB

Problem

Dohyeon lives in a city represented by an N×M grid. Each cell is either empty or a wall. Dohyeon's cell and the school cell are always empty.

Dohyeon may move from the current cell to an adjacent cell in one of the four cardinal directions. He cannot move into a wall and cannot leave the city.

Jungyu wants to stop Dohyeon from reaching school by changing some empty cells into walls. Existing walls cannot be changed, and neither Dohyeon's cell nor the school cell can be changed into a wall.

Find the minimum number of empty cells that must be changed into walls so that Dohyeon cannot reach the school.

Input

The first line contains the height N and width M of the city. (1 ≤ N, M ≤ 100)

Each of the next N lines describes the city map. An empty cell is written as ., and a wall is written as #. Dohyeon's position is written as K, and the school position is written as H. Exactly one K and one H are given.

Output

Print the minimum number of empty cells that must be changed into walls so that Dohyeon cannot reach school. If it is impossible to stop him no matter how walls are added, print -1.