cswork / OAOpen practice workspace

Wells FargoOA #3

Find Maximum Distance

Estimated MediumJudge-ready

Topics (AI-labelled)Grid SearchShortest PathsBinary Search

You are given a rectangular grid containing S, E, ., and *. A path goes from S to E, moving up, down, left, or right each step without revisiting cells; * is an obstacle but may be passed through. The safety value of a path is the minimum, over the cells on the path, of the Manhattan distance to the nearest obstacle. Find the maximum safety value over all paths.

Input

The first line contains rows cols; then rows lines follow, each a string of length cols. Source range: 2≤rows,cols≤200. This site explicitly requires exactly one S, one E, and at least one *, with every other character being . or *.

Output

Output the maximum guaranteed minimum Manhattan distance.

Example 1

Input:

2 2
S*
*E

Output:

0

Example 2

Input:

3 3
S..
***
..E

Output:

0

Example 3

Input:

3 3
S..
...
**E

Output:

1

Editorial

Loading solution…