Google Interview Question

Matrix Island question using Graphs