How to count groups of same cells in a 2d array?
Set all black squares as nodes. Connection between black squares (if the squares are next to each other) will be an edge.
This gives you a graph.
A DFS in the graph will get you all the groups. Note that DFS is recursive by nature.