Problem
You are given an m x n grid of integers where 1 represents land and 0 represents water. An island is a group of horizontally or vertically adjacent 1s.
Two islands are considered the same shape if one can be translated (shifted up/down/left/right) to match the other exactly. Rotations and reflections do not count as the same shape.
Return the number of distinct island shapes in the grid.
Input
-
grid
:
m x n
matrix of
0/1
Output
-
An integer: the number of distinct island shapes.
Constraints
-
1 <= m, n <= 500
(typical)
-
Grid may contain many islands
Notes
-
Adjacency is 4-directional (up/down/left/right).
-
Translation allowed; rotation/reflection not allowed.
Example
If the grid contains two L-shaped islands in different locations, they count as one distinct shape.