($w,$h)=<>=~/\d+/g; $/=\1; for$y(1..$h){ for$x(1..$w+1){ $f{$x,$y}=<>; } } l1:{ for$y(1..$h){ for$x(1..$w){ if($f{$x,$y}eq'.'){ $f{$x,$y}=A; last l1; } } } } for$y(1..$h){ for$x(1..$w){ if($f{$x,$y}eq'.'){ if($f{$x-1,$y}eq A || $f{$x+1,$y}eq A || $f{$x,$y-1}eq A || $f{$x,$y+1}eq A ){ $f{$x,$y}=A; } } } } for$l(0..99){ for$y(1..$h){ for$x(1..$w){ if($f{$x,$y}eq 'A'){ if($f{$x-1,$y}eq '.'|| $f{$x+1,$y}eq '.' || $f{$x,$y-1}eq '.' || $f{$x,$y+1}eq '.' ){ print $l; exit; } if($f{$x-1,$y}eq'#'){ $f{$x-1,$y}=B; } if($f{$x+1,$y}eq'#'){ $f{$x+1,$y}=B; } if($f{$x,$y-1}eq'#'){ $f{$x,$y-1}=B; } if($f{$x,$y+1}eq'#'){ $f{$x,$y+1}=B; } } } } for$y(1..$h){ for$x(1..$w){ if($f{$x,$y}eq B){ $f{$x,$y}=A; } } } }