376. Perimeter of the Land
A grid of 0 and 1 characters is given. Print the total length of the
boundary around all the land, counting each cell as a 1 by 1 square.
A land cell contributes one unit of perimeter for each of its four sides that does NOT have another land cell against it. A side against water counts, and so does a side on the edge of the grid.
This one deserves attention because it is a graph problem that needs no traversal at all. Every side is decided by looking at one cell and its four neighbours, so two nested loops and a bounds check are the entire solution.
A useful check on your own thinking: a single land cell has perimeter 4, and gluing two cells together hides one side on each, so a domino has perimeter 6, not 8.
Constraints - `1 ≤ r ≤ 1000` - `1 ≤ c ≤ 1000` - Every character is `0` or `1`.
Input
The first line contains two integers r and c, the number of rows and
columns. Each of the next r lines contains c characters, each 0 or 1,
with no spaces between them.
Output
Print one integer, the total perimeter.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.