We can use an easy breadth-first search algorithm, which follows:
We search each cell of the matrix. If the cell is a land (a 1), we increment our count, and perform bread-first search (BFS) expansion of that land, which is represented by bfs() method.
During the BFS expansion, for every connected land cell, we mark that as visited, which is recorded by the v array. The sole purpose of the bfs() is to mark the connected land cells. The reason we want to do this, is that in the later search, when a land is found, but it is part of an island we already visited, then we do not increment the count.