Description
You are given a 2D grid where 1 represents land and 0 represents water. There is exactly one island. Return the perimeter of the island.
Examples
Input:
grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]Output:
16Explanation:
Perimeter is 16.
Input:
grid = [[1,1,1],[1,0,1],[1,1,1]]Output:
16Explanation:
The island forms a ring shape with a hole in the middle. The 8 land cells contribute to both the outer perimeter (12 edges) and the inner perimeter around the hole (4 edges), totaling 16.
Input:
grid = [[1,1],[1,1]]Output:
8Explanation:
This is a 2x2 square island. The perimeter consists of 4 cells, each contributing 2 external sides: top row contributes 2 top sides, bottom row contributes 2 bottom sides, left column contributes 2 left sides, and right column contributes 2 right sides, totaling 8.
Constraints
- •
1 ≤ row, col ≤ 100