<?php
fscanf(STDIN, "%d%d", $H, $W);
for($i = 0; $i < $H; $i++){
  $str = fgets(STDIN);
  for($j = 0; $j < $W; $j++){
    $MAP[$i][$j] = $str[$j];
  }
}
for($y = 0; $y < $H; $y++){
    for($x = 0; $x < $W; $x++){
        if($MAP[$y][$x] == "."){
            list($Y, $X, $distance) = bfs($y, $x, ".", ",");
            list($Y, $X, $distance) = bfs($Y, $X, ",", "#");
            echo $distance;
            exit;
        }
    }
}
function bfs($y, $x, $f, $t){
    global $MAP;
    $q[] = [$y, $x];
    $MAP[$y][$x] = $t;
    $d = 0;
    while($q){
        $qq = [];
        foreach($q as list($y, $x)){
            foreach([-1,1] as $speed){
                foreach(["dy","dx"] as $dir){
                    $dx = $dy = 0;
                    $$dir += $speed;
                    if(isset($MAP[$y+$dy][$x+$dx]) && $MAP[$y+$dy][$x+$dx] == $f){
                        $MAP[$y+$dy][$x+$dx] = $t;
                        $qq[] = [$y+$dy,$x+$dx];
                    }
                }
            }
        }
        if($qq)$d++;
        $q = $qq;
    }
    return [$y, $x, $d];
}


function o(...$val){
    if(count($val)==1)$val = array_shift($val);
    $trace = debug_backtrace();
    echo $trace[0]['line'].")";
    if(is_array($val)){
        if(count($val) == 0){
            echo "empty array";
        }elseif(!is_array(current($val))){
            echo "array:";
            echo implode(" ", $val)."\n";
        }else{
            echo "array:array\n";
            foreach($val as $row){
                echo implode(" ", $row)."\n";
            }
        }
    }else{
        echo $val."\n";
    }
}