Google Interview Question

walls and gates leetcode question