Pinterest Interview Question

some algorithm question using matrix