--- /dev/null
+#!/usr/bin/perl -w
+
+use v5.16;
+
+my @m = map { chomp; [ split // ]; } <>;
+my $maxy = $#m;
+my $maxx = $#{$m[0]};
+say "$maxx x $maxy";
+
+my $sum;
+for my $y (0 .. $maxy) {
+ for my $x (0 .. $maxx) {
+ my $val = $m[$y][$x];
+ next if $y > 0 && $m[$y-1][$x] <= $val;
+ next if $y < $maxy && $m[$y+1][$x] <= $val;
+ next if $x > 0 && $m[$y][$x-1] <= $val;
+ next if $x < $maxx && $m[$y][$x+1] <= $val;
+ say "found at $x $y";
+ $sum += $val + 1;
+ }
+}
+
+say $sum;
+
--- /dev/null
+#!/usr/bin/perl -w
+
+use v5.16;
+
+my @m = map { chomp; [ split // ]; } <>;
+my $maxy = $#m;
+my $maxx = $#{$m[0]};
+say "$maxx x $maxy";
+
+my @low;
+for my $y (0 .. $maxy) {
+ for my $x (0 .. $maxx) {
+ my $val = $m[$y][$x];
+ next if $y > 0 && $m[$y-1][$x] <= $val;
+ next if $y < $maxy && $m[$y+1][$x] <= $val;
+ next if $x > 0 && $m[$y][$x-1] <= $val;
+ next if $x < $maxx && $m[$y][$x+1] <= $val;
+ push @low, [$x, $y];
+ }
+}
+
+my @sizes;
+
+for my $start (@low) {
+ my $added = 0;
+ my $size =1;
+ my @points = ($start);
+ my %pts = (join(',', @$start) => 1);
+ do {
+ $added = 0;
+ for my $p (@points) {
+ for my $add (([0, 1], [1, 0], [-1, 0], [0, -1])) {
+ my ($x, $y) = ($p->[0]+$add->[0], $p->[1]+$add->[1]);
+ # say "trying $x, $y";
+ next if $pts{join(',',$x,$y)};
+ next if $x < 0 || $x > $maxx;
+ next if $y < 0 || $y > $maxy;
+ next if $m[$y][$x] == 9;
+ $added = 1;
+ $pts{join(',',$x,$y)} = 1;
+ push @points, [$x, $y];
+ # say "adding $x, $y = $m[$y][$x]";
+ $size++;
+ }
+ }
+ } while ($added);
+ # say "$start->[0], $start->[1] : $size";
+ push @sizes, $size;
+}
+
+@sizes = sort { $b <=> $a } @sizes;
+
+say $sizes[0]*$sizes[1]*$sizes[2];
+
+
+