matrix traversal problems hackerrank solution