Company: Agoda SDE 6m Offcampus_18july
Difficulty: easy
Relationships between people are represented in a matrix of binary digits. For example, the relationships for person 0 with persons 0 through 5 might be shown as "101100" , meaning person 0 knows persons 0, 2 and 3. A relationship is transitive . If person 0 knows person 2 and person 2 knows person 3, then person 0 knows person 3 indirectly. A group consists of all people who know one another, whether directly or indirectly. Determine the number of distinct groups in the relationship matrix. Example Consider the relationships matrix related = ["110", "110", "001"] : 0 1 2 0 1 1 0 1 1 1 0 2 0 0 1 Persons 0 and 1 are connected, while person 2 is not. There are 2 groups. Input Format The first line contains a single integer n , the size of related . Each of the next n lines contains a binary string related[i] of length n . Output Format Print a single integer: the number of groups of people. Constraints 1 ≤ n ≤ 300 0 ≤ i < n Each related[i] contains a binary string of n zeros