463. Island Perimeter¶
Edited by Charles
Easy · Python · Graph
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
This problem can be solved using DFS.
The key difference between this problem and problems such as LeetCode 695. Max Area of Island is what we count.
Original study notes follow, including intermediate snippets and earlier attempts.
Idea¶
This problem can be solved using DFS.
The key difference between this problem and problems such as LeetCode 695. Max Area of Island is what we count.
For island area:
For island perimeter:
Therefore, we start DFS from a land cell and explore its four directions.
For each direction, there are three possible situations:
1. Outside the grid → perimeter + 1
2. Water → perimeter + 1
3. Visited land → perimeter + 0
4. Unvisited land → continue DFS
DFS¶
For every land cell, we explore:
The DFS returns the perimeter contributed by the current island.
Case 1: Outside the Grid¶
If DFS moves outside the grid:
This means the current land cell has an edge touching the outside of the grid.
For example:
That edge contributes 1 to the perimeter.
Case 2: Reach Water¶
If DFS reaches a water cell:
This means there is an edge between land and water:
Therefore, this edge also contributes 1.
In the code, the outside-grid case and water case can be combined:
Case 3: Reach Visited Land¶
We mark visited land as:
If DFS reaches a cell with value 2:
Why 0?
Because this is an edge between two land cells:
This is an internal edge of the island, so it does not contribute to the perimeter.
Case 4: Reach Unvisited Land¶
If the current cell is an unvisited land cell:
mark it as visited:
Then continue DFS in all four directions:
The sum of these four DFS calls gives the perimeter contributed by the entire connected island.
Hand Simulation¶
Consider:
Start DFS from the center land cell.
Check its four directions:
Every direction reaches water:
Therefore:
Another Example¶
Consider:
The two land cells share one edge.
The shared edge is inside the island:
When DFS moves from one land cell to the already visited land cell:
it returns:
because the shared edge is not part of the perimeter.
The remaining six edges touch water or the outside:
Therefore:
Why Do We Mark Land as 2?¶
We cannot simply leave visited land as 1.
Otherwise, DFS could repeatedly move between neighboring land cells:
So we mark visited land as:
Then:
When DFS reaches 2, we know that this land cell has already been processed.
Core DFS Logic¶
The most important part of this problem can be summarized as:
From a land cell:
outside grid
↓
return 1
water
↓
return 1
visited land
↓
return 0
unvisited land
↓
mark visited
↓
DFS 4 directions
So the perimeter is essentially:
The number of times DFS crosses from land into water or outside the grid.
Difference from LeetCode 695¶
LeetCode 695: Max Area of Island¶
A land cell itself contributes to the answer:
because:
LeetCode 463: Island Perimeter¶
A land cell itself does not directly contribute 1.
Instead:
Therefore:
This is the most important difference between the two problems.
Complexity Analysis¶
Time Complexity
Each land cell is visited at most once.
Space Complexity
In the worst case, the DFS recursion stack may contain O(m × n) cells.
Summary¶
1. Find an unvisited land cell.
2. Start DFS.
3. Mark visited land as 2.
4. If DFS reaches water, return 1.
5. If DFS goes outside the grid, return 1.
6. If DFS reaches visited land, return 0.
7. Add the results from all four directions.
The key idea is:
Every time DFS crosses from land to water or outside the grid, we discover one edge of the island's perimeter.
Study record
| Field | Value |
|---|---|
| Attempts | 2 |
| Mastery | ❚❚ |
| The Latest Attempt | August 20, 2026 |
Python solution¶
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
res = 0
def dfs(i, j):
if i < 0 or i >= m or j < 0 or j >= n or grid[i][j] == 0:
return 1
if grid[i][j] == 2:
return 0
grid[i][j] = 2
return dfs(i - 1, j) + dfs(i + 1, j) + dfs(i, j - 1) + dfs(i, j + 1)
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
res += dfs(i, j)
break
return res