less than 1 minute read

Given a two-dimensional integer matrix of 1s and 0s where 0 represents empty cell and 1 represents a block that forms a shape, return the perimeter of the shape. There is guaranteed to be exactly one shape, and the shape has no holes in it.

Constraints

  • 1 ≤ n, m ≤ 250 where n and m are the number of rows and columns in matrix

https://binarysearch.com/problems/Island-Shape-Perimeter

Examples

Example 1

Input

  • matrix =
[[0,1,1],
 [0,1,0]]

Output

  • answer = 8

Solution

Leave a comment