Knowledge Guide
HomeDSACompany Practice

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 =

Image
Image

Output: 14

Explanation: The boundary of the island constitutes 14 sides.

Example 2

Input: matrix =

Image
Image

Output: 12

Explanation: The boundary of the island constitute 12 sides.

Try it yourself

Try solving this question here:

✅ 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 =

Image
Image

Output: 14

Explanation: The boundary of the island constitutes 14 sides.

Example 2

Input: matrix =

Image
Image

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.

Image
Image

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.

java
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 , where ‘M’ is the number of rows and 'N' is the number of columns of the input matrix. This is due to the fact that we have to traverse the whole matrix to find the island.

Space Complexity

DFS recursion stack can go deep when the whole matrix is filled with '1's. Hence, the space complexity will be , where ‘M’ is the number of rows and 'N' is the number of columns of the input matrix.

🤖 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.

🪜 Hint ladder (no spoilers)

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.
🎨 Explain the approach visually

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.
🔍 Review my solution

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.
🔁 Drill the pattern

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.

📝 My notes