<>=~s/#/++$a["@-"]/ger for 1..<>; $t{$b[@b]="$_ $#b"}=@b for<>; for$b((sort{$a-$b}@b)x9){ $r[$t{$b}][$_]<3&&$a[$b+$_]and++$r[$t{$b}][$_],--$a[$b+$_],last for 0..2; } for$r(@r){ print map($$r[$_]-->0?'#':'.',0..2),$/for 0..2; }