($@,$?)=(@ARGV,9,9);$:=1-$?*$@;print"# ",'##'x($@-2),"# # ";$_[0][$_]=$_[$?+1][$_]=8for 0..$@;$_[$_][0]=$_[$_][$@+1]=8for 0..$?;$/=1+rand$?,$.=1+rand$@;@@=[$.,$/];$_[$/][$.]=$^=4;while($:) {$"=$^-->3?rand 4:++$"%4;1>$"?$.--:2>$"?$/--:2^$"?++$/:$.++;if($_[$/][ $.]&&1){pop@@,$^=4if!$^;($.,$/)=@{$@[-1]};next}$_[$/][$.]=$^=4;$"< 2?$_[$/][$.]|=1<<$":($_[$@[-1][1]][$@[-1][0]]|=1<<$"-2);push@@ ,[$.,$/];++$:}print"#",map({$_%2?' ':' #'}@$_[1..$@])," #",map({2&$_?' #':'##'}@$_[1..$@]),"\n"for@_[1..$?]