Island Perimeter

Easy

Topics
ArrayDFSBFSMatrix

Given a binary grid where 1 is land and 0 is water, return the total perimeter of the land. Each land cell contributes one unit of perimeter for every edge that borders water or the grid boundary.

Example 1

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

Example 2

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

Constraints

  • 1 <= m, n <= 100
  • grid[i][j] is 0 or 1.
Run ⌘' · Submit ⌘⏎