The matrix representation is a bit confusing by looking at it. It appears to be asking to number the disjoint sets in the graph. But what it actually asks is who are forming a single disjoint set and how many disjoint sets are there. M[i][i] can simply be ignored. M[i][j] means i and j are connected.