You are given a row x col grid that represents a map where:
grid[i][j] = 1 → represents land,
grid[i][j] = 0 → represents water.
Each cell is connected horizontally or vertically (but not diagonally).
The grid is completely surrounded by water, and there is exactly one island (one or more connected land cells).
Your task is to find the perimeter of the island.
Each land cell contributes up to 4 edges, but shared edges between adjacent land cells reduce the total count.
Input: grid = [[1,1,0,0],[1,1,0,0],[0,1,0,0],[0,1,1,0]]
Output: 14
Explanation:
The island forms a connected block of land, and the total perimeter of this island is 14.
Input: grid = [[1,0,1,0]]
Output: 8
Explanation:
There are two separate land cells, each contributing 4 edges, so the total is 8.
Input: grid = [[1,1],[1,0]]
Output: 8
Explanation:
The island’s shape contributes 8 total edges to the perimeter.
Accepted:
Submission: