IBM interview question

Magic Carpet question that was easy Leetcode question.