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

001001111020100311000123
Every shaded cell contributes one unit of perimeter for each of its four sides that touches water or the outside edge. Shared sides between two land cells cancel, so the total here is 16.
2D grid — one row per line.
Input:  grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16

Example 2

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

Constraints

  • 1 <= m, n <= 100
  • grid[i][j] is 0 or 1.
Editorial — intuition, approach, complexity and pitfalls

Intuition

No traversal is needed. Perimeter is a local property: each land cell has four sides, and a side contributes to the perimeter exactly when the thing on the other side is water or the edge of the grid.

So sweep every cell, and for each land cell add 1 for each of its four neighbours that is not land. No DFS, no visited set, and it works even if the land is in several disconnected pieces — the problem says one island, but nothing in this method depends on that.

An equivalent framing some find easier: perimeter = 4 * land - 2 * adjacent_pairs, since each shared edge removes one side from each of two cells. Same answer, one pass.

Approach

  1. For each cell that is land, check its four neighbours.
  2. Add 1 for each neighbour that is out of bounds or water.

Dry Run

grid = [[1,1],
        [1,0]]

cell (0,0) land: up out, left out, down=1 land, right=1 land   -> +2
cell (0,1) land: up out, right out, left=1 land, down=0 water  -> +3
cell (1,0) land: left out, down out, up=1 land, right=0 water  -> +3
cell (1,1) water: skipped

perimeter 2+3+3 = 8

cross-check: 3 land cells, 2 adjacent pairs
             4*3 - 2*2 = 8  ✓

Complexity

  • Time — O(rows * cols): every cell visited once, four constant checks each.
  • Space — O(1): a counter. A DFS solution would need O(rows*cols) for the visited set and the recursion stack, for no benefit — worth naming as the thing not to do here.

Common Pitfalls

  • Reaching for DFS or BFS. The problem looks like an island-traversal problem and is not; perimeter needs no connectivity information.
  • Checking bounds after indexing rather than before, which reads out of range at the borders.
  • Counting a shared edge once instead of twice. Each of the two cells loses a side, which is why the pair formula subtracts 2 per adjacency.
  • Assuming land is connected or that there are no lakes. This method is indifferent to both; an interior water cell correctly contributes to the perimeter of its neighbours.

Full Solution — all languages

class Solution:
    def islandPerimeter(self, grid):
        R, C = len(grid), len(grid[0])
        per = 0
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    if r == 0 or grid[r-1][c] == 0: per += 1
                    if r == R-1 or grid[r+1][c] == 0: per += 1
                    if c == 0 or grid[r][c-1] == 0: per += 1
                    if c == C-1 or grid[r][c+1] == 0: per += 1
        return per
SensAI
Beta
Tap the mic to stop voice mode