public class NumberOfIslands {
public static void main(String[] args) {
int[][] grid = {
{ 1, 1, 1, 1, 0 },
{ 1, 1, 0, 1, 0 },
{ 1, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0 }
};
System.out.println(numIslands(grid));
}
private static int numIslands(int[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int numIslands = 0;
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
if (grid[row][col] == 1) {
numIslands++;
resetIslandLand(grid, row, col);
}
}
}
return numIslands;
}
public static void resetIslandLand(int[][] grid, int row, int col) {
if (row < 0 || row >= grid.length || col < 0
|| col >= grid[0].length || grid[row][col] == 0) {
return;
}
grid[row][col] = 0;
resetIslandLand(grid, row + 1, col);
resetIslandLand(grid, row - 1, col);
resetIslandLand(grid, row, col + 1);
resetIslandLand(grid, row, col - 1);
}
}