Invisible Island

Hard
matrix island-counting dfs border-detection
Problem Description

In a matrix of 0s (water) and 1s (land), find islands that are completely surrounded by water with no border contact. Islands touching matrix borders don't count.

Input Format
First line: m n. Next m lines: matrix with 0s and 1s.
Output Format
Number of invisible islands (completely surrounded, no border contact).
Constraints
• 1 ≤ m, n ≤ 100
• Matrix contains only 0s and 1s
• Islands connected by 4-directions (up, down, left, right)
• Islands touching borders are not invisible
• Count separate invisible islands
Sample Input/Output
Input:
4 4
0 0 0 0
0 1 1 0
0 1 0 0
0 0 0 0
Output:
1
Explanation

Island at positions (1,1), (1,2), (2,1) is completely surrounded by water and doesn't touch borders.

Solution Hints
125
Points
3000ms
Time Limit
256MB
Memory Limit
Code Editor
Register to Submit
Register to Access Code Editor

Create a free account to solve coding problems and track your progress.

Register Now
Feedback