Island Perimeter - Problem
You are given a row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100.
Determine the perimeter of the island.
Input & Output
Example 1 — Small Island
$
Input:
grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
›
Output:
16
💡 Note:
The island has an irregular shape. Each land cell contributes edges that face water or boundaries. Total perimeter is 16.
Example 2 — Single Cell
$
Input:
grid = [[1]]
›
Output:
4
💡 Note:
A single land cell has 4 edges, all facing the boundary (water), so perimeter is 4.
Example 3 — Square Island
$
Input:
grid = [[1,1],[1,1]]
›
Output:
8
💡 Note:
A 2x2 square island. Each of the 4 cells contributes 2 edges to the perimeter (4×2=8).
Constraints
- row, col ≤ 100
- grid[i][j] is 0 or 1
- There is exactly one island
Visualization
Tap to expand
Understanding the Visualization
1
Input Grid
2D grid where 1=land, 0=water
2
Find Perimeter
Count edges between land and water/boundary
3
Output Count
Total perimeter length
Key Takeaway
🎯 Key Insight: Count edges where land cells touch water or grid boundaries
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code