Sum of Remoteness of All Cells
Label each connected region once; every cell in a region shares the value sitting outside it, so the answer is a sum of region sizes times outside totals.
Label each connected region once; every cell in a region shares the value sitting outside it, so the answer is a sum of region sizes times outside totals.