Dsa
Graphs Dfs
Hard
Submerged Island Travel
Given a 2D grid representing land and water, where '1' represents land and '0' represents water, find the longest path of connected land cells starting from any cell in the water. A path can only move to adjacent land cells (up, down, left, right). The function should return the length of the longest land path you can visit starting from the water cells.
Input Format:
- The first line contains two integers, m and n, representing the number of rows and columns in the grid.
- The next m lines each contain n integers (0 or 1) representing the grid.
Output Format:
- Print a single integer, the length of the longest path of connected land cells starting from the water cells.
Example Input:
3 4
1 0 1 0
1 1 0 0
0 1 1 0
Example Output:
5
Key concepts
graphsdfspathfinding
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