Here\'s the example (counting black ones):
input:
output:
You can use algorithm for connected component labeling with 4-connectivity