


Two islands are considered to be the same if and only if one island is equal to another(not rotated or reflected) i.e if we can translate one island on another without rotating or reflecting then it would be considered as the same islands.
1 1 0
0 0 1
0 0 1
In this example, we have two islands and they would be considered as distinct islands as we can not translate them on one another even if they have the same no of 1's.
1 1 0 0 0
1 1 0 0 0
0 0 0 1 1
0 0 0 1 1
In this example, we have two islands and they are the same as we can translate one island onto another island, so our answer should be 1.
The first two lines contain two integer values, 'N' and 'M'. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list.
From the third line onwards, the next 'N' lines or rows represent the i-th row values.
Each of the i-th rows contains 'M' column values separated by a single space.
The only line of output contains the total number of distinct Islands.
0 <= N <= 1000
0 <= M <= 1000
0 <= elements of array <= 1
Time Limit: 1 sec
Followings are the abbreviated path :
‘S’ - starting vertex
‘D’ - down
‘U’ - up
‘L’ - left
‘R’ - right
‘B’ - backtrack
Consider the following island:
1 1
1 0
Let us simulate the solution's algorithm on this island to understand its working:
At cell (0,0), path = “S”.
We move right.
At cell (0,1), path = “SR”.
We cannot go further, hence we backtrack.
At cell (0,0), path = “SRB”.
We move down.
At cell (1,0), path = “SRBD”.
We cannot go further, hence we backtrack.
At cell (0,0), path = “SRBDB”.
We cannot go further, hence we terminate.
Final path = “SRBDB”.