At Genexa Bio, a cell-based assay is represented as a 2D grid where each entry is either 1 (cell with a target phenotype) or 0 (non-target). Write a function to count how many distinct phenotype clusters appear in the grid.
A cluster is a group of horizontally or vertically adjacent 1s. Diagonal cells are not connected.
grid of integers where each value is 0 or 11sExample 1
Input: grid = [
[1,1,0,0],
[1,0,0,1],
[0,0,1,1],
[0,0,0,0]
]
Output: 2
Explanation: The top-left group forms one cluster, and the connected cells on the right form the second cluster.
Example 2
Input: grid = [
[1,0,1],
[0,0,0],
[1,0,1]
]
Output: 4
Explanation: Each 1 is isolated, so each one forms its own cluster.
1 <= len(grid) <= 3001 <= len(grid[0]) <= 300grid[i][j] is either 0 or 1