public static void DFS(char[][] grid, int i, int j) {
if (i < 0  i >= grid.length  j < 0  j >= grid[0].length)
return;
if (grid[i][j] == '1') {
grid[i][j] = '2';
DFS(grid, i + 1, j);
DFS(grid, i  1, j);
DFS(grid, i, j + 1);
DFS(grid, i, j  1);
}
}
public static int numIslands(char[][] grid) {
if (grid == null  grid.length == 0)
return 0;
int row = grid.length, column = grid[0].length, count = 0;
for (int i = 0; i < row; i++)
for (int j = 0; j < column; j++) {
if (grid[i][j] == '1') {
count++;
DFS(grid, i, j);
}
}
return count;
}
JAVAEasy Version To Understand!!!!!!!!!!


@alpesh9999
You could replace the just visited '1' with any digit . Be it a '0' or a '2', '3'. The point is just to make sure that you visited 'that part' of the Island already. Hope it's clear now :)