XYZ interview question

Matrix and dp based question.