package main import ( "bufio" "bytes" "fmt" "math" "os" "strconv" ) var sc = bufio.NewScanner(os.Stdin) func main() { sc.Split(bufio.ScanWords) w, h := nextInt(), nextInt() cave := make([][]byte, h) first, second := make([][2]int, 0, w*h), make([][2]int, 0, w*h) var sPos [2]int for i := range cave { b := []byte(nextLine()) if j := bytes.IndexByte(b, '.'); j != -1 { sPos = [2]int{j, i} } cave[i] = b } cave[sPos[1]][sPos[0]] = 0 move, q := [][2]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, make([][2]int, 0, 400) q = append(q, sPos) for len(q) > 0 { pos := q[0] q = q[1:] first = append(first, pos) for _, m := range move { nextI, nextJ := pos[0]+m[0], pos[1]+m[1] if cave[nextJ][nextI] != '.' { continue } cave[nextJ][nextI] = 0 q = append(q, [2]int{nextI, nextJ}) } } for j, l := range cave { for i, v := range l { if v != '.' { continue } second = append(second, [2]int{i, j}) } } min := 100 for _, f := range first { for _, s := range second { l := int(math.Abs(float64(f[0]-s[0])) + math.Abs(float64(f[1]-s[1]))) if l < min { min = l } } } fmt.Println(min - 1) } func nextLine() string { sc.Scan() return sc.Text() } func nextInt() int { i, _ := strconv.Atoi(nextLine()) return i }