We’re preparing your current view and syncing the latest data.
You are given a 2D grid map of '1's (land) and '0's (water). The grid represents a map where each cell is either land or water. The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected lands). An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You need to find the perimeter of the island.
A 2D array grid where grid[i][j] is 1 if it represents land and 0 if it represents water.
An integer representing the perimeter of the island in the given grid.
The grid width and height do not exceed 100.
Example 1
Input
[[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output
16
Explanation
The island has a perimeter of 16, counting each edge that borders water or the grid boundary.