atcoder#ABC253B. [ABC253B] Distance Between Tokens

[ABC253B] Distance Between Tokens

Score : 200200 points

Problem Statement

There is a grid with HH horizontal rows and WW vertical columns, in which two distinct squares have a piece.

The state of the squares is represented by HH strings S1,,SHS_1, \dots, S_H of length WW. Si,j=S_{i, j} = o means that there is a piece in the square at the ii-th row from the top and jj-th column from the left; Si,j=S_{i, j} = - means that the square does not have a piece. Here, Si,jS_{i, j} denotes the jj-th character of the string SiS_i.

Consider repeatedly moving one of the pieces to one of the four adjacent squares. It is not allowed to move the piece outside the grid. How many moves are required at minimum for the piece to reach the square with the other piece?

Constraints

  • 2H,W1002 \leq H, W \leq 100
  • HH and WW are integers.
  • Si(1iH)S_i \, (1 \leq i \leq H) is a string of length WW consisting of o and -.
  • There exist exactly two pairs of integers 1iH,1jW1 \leq i \leq H, 1 \leq j \leq W such that Si,j=S_{i, j} = o.

Input

Input is given from Standard Input in the following format:

HH WW

S1S_1

\vdots

SHS_H

Output

Print the answer.

2 3
--o
o--
3

The piece at the 11-st row from the top and 33-rd column from the left can reach the square with the other piece in 33 moves: down, left, left. Since it is impossible to do so in two or less moves, 33 should be printed.

5 4
-o--
----
----
----
-o--
4