#463

Island Perimeter

newbie · 235 · lc easy +18 · verified · 74.3% accepted · 7,287 likes · top 85%

play →

Description

Given a row x col binary matrix grid (1 = land, 0 = water) with exactly one island (a connected group of land cells, no interior lakes, fully surrounded by water), compute and return the perimeter of that island. Each cell is a unit square.

Example 1:

Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image above.

Example 2:

Input: grid = [[1]]
Output: 4

Example 3:

Input: grid = [[1,0]]
Output: 4

Code

1
2
3