Count the number of connected components in a matrix.