8 my ($maxx, $maxy) = (0, 0);
11 push @m, [ split // ];
16 my %cost = ("0,0" => 0);
19 while (my $p = shift @todo) {
21 for my $d ([ 0, 1], [0, -1], [1, 0], [-1, 0]) {
22 my $dx = $sx + $d->[0];
23 my $dy = $sy + $d->[1];
24 next if $dx < 0 || $dx >= $maxx || $dy < 0 || $dy >= $maxy;
25 if (!defined $cost{$dx,$dy}
26 || $cost{$dx,$dy} > $cost{$sx,$sy} + $m[$dy][$dx]) {
27 $cost{$dx,$dy} = $cost{$sx,$sy} + $m[$dy][$dx];
28 push @todo, [$dx,$dy];
33 say $cost{$maxx-1,$maxy-1};