Freshworks Interview Question

Island problem using graphs or dp