Google interview question

Matrix Island question using Graphs