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 1grid = [[1,1,0,0],[1,0,0,1],[0,0,1,1],[0,0,0,0]]Output2WhyThere are two connected groups of `1`s using only horizontal and vertical adjacency.grid = [[1,0,1],[0,0,0],[1,0,1]]Output4WhyAll four phenotype cells are isolated, so each forms its own cluster.grid = [[1,1,1],[1,1,1]]Output1WhyAll target cells are connected into one large cluster.1 <= len(grid) <= 3001 <= len(grid[0]) <= 300grid[i][j] is either 0 or 1Adjacency is horizontal or vertical onlydef count_cell_clusters(grid):