fn main [ var lines := list_break_to_lines(read_lazy(h[0])); lines := list_flatten(lines); const x := len(lines[0]); const y := len(lines); var a := list_to_array([x, y], list_join(lines, "")); a := array_flatten(a); var sum := 0; for j := 0 to y do [ for i := 0 to x do [ var plant := a[i, j]; if plant = '.' then continue; var area := 1; var perimeter := 0; var set := treeset_from_list([ mktuple2(i, j) ]); var visited := set; while true do [ var t := treeset_first(set); if t is n then break; set := treeset_clear(set, t.j); for d in [ [0, 1], [0, -1], [1, 0], [-1, 0] ] do [ var xp := t.j.v1 + d[0]; var yp := t.j.v2 + d[1]; if xp >= 0, xp < x, yp >= 0, yp < y, a[xp, yp] = plant then [ if not treeset_test(visited, mktuple2(xp, yp)) then [ area += 1; visited := treeset_set(visited, mktuple2(xp, yp)); set := treeset_set(set, mktuple2(xp, yp)); ] ] else [ perimeter += 1; ] ] ] sum += area * perimeter; while true do [ var t := treeset_first(visited); if t is n then break; visited := treeset_clear(visited, t.j); a[t.j.v1, t.j.v2] := '.'; ] ] ] write(h[1], ntos(sum) + nl); ]