Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Input:11110 11010 11000 00000Output:1

The first challenge of this problem is to identify that this is a graph problem. To identify that, we have to look if cells of the matrix can represent nodes, and are there any relationship between cells that can act as edges of the graph?

One hint is to the relationship between the cells of the matrix. For example, in the island problem, `0s` are water, and `1s` are land. Land can be connected to land only which is adjacent to the land and a piece of land is isolated if surrounded by water from all four sides.

To find the number of islands in the matrix, we have to find the number of connected components in the graphs as explained above. Which traversal can we use to find the number of connected components of a graph? It is a depth-first search problem.

In this case, nodes seem to be the cells with value 1(land). Most of the time, in these kinds of problems, each cell of the matrix with a certain value is a node. If we have an `n x m ` matrix, we are looking at `n x m` possible nodes. Also, here be aware that we are not interested in all the cells. Based on the problem statement, we can discard some cells. For example, in the island problem, we are interested in the cells which represent the land.

Now, what will be the edges? In these kinds of problems, a statement is given like an island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. It means that a cell with value 1 is connected to neighbor cells only if they also contain 1.

Now that we know that cells containing 1 in the matrix are nodes of the graphs and connected to neighbors in they are also 1. To count the number of islands, all we have to do is find connected components in the graph. We reduced a matrix problem to a graph problem after all. Should we create an entire graph? Well no, at any point in time, we do not need the entire graph.

### Number of islands implementation

public int numIslands(char[][] grid) { int count = 0; Set<Integer> visited = new HashSet<>(); int n = grid.length; if(n <=0) return 0; int m = grid[0].length; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ int cell = i * m + j; if(!visited.contains(cell) && grid[i][j] == '1'){ count++; dfs(i, j, visited, grid); } } } return count; } void dfs(int i, int j, Set<Integer> visited, char[][] a){ //Check if the node we are traversing is actually a valid node. if(i<0 || i>=a.length || j<0 || j>=a[0].length || a[i][j] == '0') return; int cell = i * a[0].length + j; if(visited.contains(cell)) return; visited.add(cell); //Visit neighbors dfs(i+1, j, visited, a); dfs(i-1, j, visited, a); dfs(i, j+1, visited, a); dfs(i, j-1, visited, a); }

The complexity of the above code is `O(n * m)` where `m` and `n` are rows and columns of the matrix.