#K88562. Island Perimeter
Island Perimeter
Island Perimeter
Given a 2D grid of integers where 0 represents water and 1 represents land, compute the perimeter of the island. Each land cell contributes 4 to the perimeter, but every shared border between two adjacent land cells reduces the total by 2. In other words, for each cell, the perimeter contribution can be computed as \(4 - 2 \times (\text{number of adjacent land cells})\). If there are no land cells, the perimeter is 0.
inputFormat
The input starts with two integers (n) and (m) representing the number of rows and columns in the grid. This is followed by (n) lines, each containing (m) space-separated integers (either 0 or 1).
outputFormat
Output a single integer representing the total perimeter of the island.## sample
4 4
0 1 0 0
1 1 1 0
0 1 0 0
1 1 0 0
16