easy Island Perimeter
Problem Statement
You are given a 2D matrix containing only 1s (land) and 0s (water).
An island is a connected set of 1s (land) and is surrounded by either an edge or 0s (water). Each cell is considered connected to other cells horizontally or vertically (not diagonally).
There are no lakes on the island, so the water inside the island is not connected to the water around it. A cell is a square with a side length of 1.
The given matrix has only one island, write a function to find the perimeter of that island.
Example 1
Input: matrix =

Output: 14
Explanation: The boundary of the island constitutes 14 sides.
Example 2
Input: matrix =

Output: 12
Explanation: The boundary of the island constitute 12 sides.
Try it yourself
Try solving this question here:
🎯 STRICT STANDOUT — Island Perimeter easy
0. Why / judgment (K3)
Family: Grid geometry · edge counting
Each land cell contributes 4 sides minus 2 per shared edge with another land (horizontal or vertical neighbor). Formula: perimeter = 4*lands - 2*neighbor_pairs. Or count water/border adjacencies per cell.
1. Pattern + recognition + when-NOT (K12)
Pattern / template: Scan grid; for each 1: perim += 4; if up land perim-=2; if left land perim-=2 (count each pair once). Recognition: single island perimeter.
When-NOT: Not multi-island number (200). Not max area. Not DFS flood unless counting boundaries that way. Diagonals don't touch for perimeter.
2. Complexity derivation (K11)
O(mn) time, O(1) space.
3. Edge hand-run (K13)
Single cell → 4.
2×1 lands → 6.
Empty water → 0.
Problem guarantees one island typically.
4. Interviewer follow-ups & drills
Q1. Why -2 per pair?
Model answer: Each shared edge removes 1 side from each of two cells.
Q2. DFS alternative?
Model answer: From land walk, count edges to water/OOB.
Q3. Mis-tag Union-Find?
Model answer: Overkill for perimeter.
✅ Solution Island Perimeter
Problem Statement
You are given a 2D matrix containing only 1s (land) and 0s (water).
An island is a connected set of 1s (land) and is surrounded by either an edge or 0s (water). Each cell is considered connected to other cells horizontally or vertically (not diagonally).
There are no lakes on the island, so the water inside the island is not connected to the water around it. A cell is a square with a side length of 1.
The given matrix has only one island, write a function to find the perimeter of that island.
Example 1
Input: matrix =

Output: 14
Explanation: The boundary of the island constitutes 14 sides.
Example 2
Input: matrix =

Output: 12
Explanation: The boundary of the island constitute 12 sides.
Solution
The question follows the Island pattern and is quite similar to Number of Islands problem.
We will traverse the matrix linearly to find the island. We can use the Depth First Search (DFS) or Breadth First Search (BFS) to traverse the island i.e., to find all of its connected land cells.
How do we calculate the boundary if the island?
Each cell has four sides. Each side of an island cell can be shared with another cell; we can include the side in the island perimeter only if the other cell is a water.

If a cell side is on boundary, we should include that side in the perimeter.
Following piece of code will cover these two conditions:
if (x < 0 || x >= matrix.length || y < 0 || y >= matrix[0].length)
return 1; // returning 1, since this a boundary cell initiated this DFS call
if (matrix[x][y] == 0)
return 1; // returning 1, because of the shared side b/w a water and a land cell
Code (DFS)
Here is what our DFS algorithm will look like. We will use a separate matrix to mark nodes visited.
import java.util.*;
class Solution {
public int findIslandPerimeter(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
boolean[][] visited = new boolean[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (
matrix[i][j] == 1 && !visited[i][j]
) return Solution(matrix, visited, i, j); // only if the cell is a land and not visited
}
}
return 0;
}
private static int Solution(
int[][] matrix,
boolean[][] visited,
int x,
int y
) {
if (x < 0 || x >= matrix.length || y < 0 || y >= matrix[0].length) return 1; // returning 1, since this a boundary cell initiated this DFS call
if (matrix[x][y] == 0) return 1; // returning 1, because of the shared side b/w a water and a land cell
if (visited[x][y]) return 0; // we have already taken care of this cell
visited[x][y] = true; // mark the cell visited
int edgeCount = 0;
// recursively visit all neighboring cells (horizontally & vertically)
edgeCount += Solution(matrix, visited, x + 1, y); // lower cell
edgeCount += Solution(matrix, visited, x - 1, y); // upper cell
edgeCount += Solution(matrix, visited, x, y + 1); // right cell
edgeCount += Solution(matrix, visited, x, y - 1); // left cell
return edgeCount;
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(
sol.findIslandPerimeter(
new int[][] {
{ 1, 1, 0, 0, 0 },
{ 0, 1, 0, 0, 0 },
{ 0, 1, 0, 0, 0 },
{ 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0 },
}
)
);
System.out.println(
sol.findIslandPerimeter(
new int[][] {
{ 0, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 1, 0, 0 },
}
)
);
}
}
Time Complexity
Time complexity of the above algorithm will be
Space Complexity
DFS recursion stack can go
🎯 STRICT STANDOUT — Solution Island Perimeter
0. Why / judgment (K3)
Family: Grid edge accounting
Local up/left subtract avoids double counting. Equivalent to sum over land of (4 - #land neighbors).
1. Pattern + recognition + when-NOT (K12)
Pattern / template: Double loop accounting.
When-NOT: Don't build graph of cells.
2. Complexity derivation (K11)
Θ(mn)/Θ(1).
3. Edge hand-run (K13)
Hand-run [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] classic perimeter 16.
4. Interviewer follow-ups & drills
Q1. Diagonal neighbor?
Model answer: Does not reduce perimeter.
Q2. Multiple islands?
Model answer: Formula still sums all perimeters if applied globally.
Q3. Hostile huge grid?
Model answer: Linear in cells.
Recognize it: Nodes + edges, reachability / shortest-unweighted / cycles / components → BFS or DFS with a visited set.
▶ Visualize this problem (step it, predict each fork)
🤖 Don't fully get this? Learn it with Claude
Stuck on Island Perimeter? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Island Perimeter** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Island Perimeter** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Island Perimeter**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Island Perimeter**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.