Pergunta de entrevista da empresa Amazon

Island matrix question in leetcode