Dsa
Matrix
Easy
Count Island Perimeter
You are given a grid representing a map where '1' represents land and '0' represents water. The grid is rectangular, and you need to calculate the perimeter of the island (the land cells) in the map. You should consider that each edge of the land cell contributes to the perimeter unless it is adjacent to another land cell.
Input Format:
- The first line contains two integers, R (number of rows) and C (number of columns).
- The next R lines contain C characters (either '0' or '1') representing the grid.
Output Format:
- A single integer representing the total perimeter of the island.
Example:
Input:
4 5
0 0 0 0 0
0 1 1 0 0
0 1 0 0 0
0 0 0 0 0
Output:
8
Key concepts
matrixdfscounting
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now