Pregunta de entrevista de Amazon

Island matrix question in leetcode