999. Island Perimeter

EasyGraphsGridGraph

Given a grid of 1s (land) and 0s (water) containing exactly one island (connected 4-directionally, no lakes), return the perimeter of the island. The input is JSON {grid}.

Input: JSON {grid}.

Output: Integer — the island perimeter.

Examples

Example 1
Input: {"grid":[[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]}
Output: 16
Explanation: Total exposed edges.
Example 2
Input: {"grid":[[1]]}
Output: 4
Explanation: A single cell has 4 sides.
Example 3
Input: {"grid":[[1,1]]}
Output: 6
Explanation: Two adjacent cells share one edge.

Constraints

Asked by

OracleAppleGoogleBloombergAmazonMicrosoft
Solve this problem in the editor →