Simulate the traversal. Maintain a direction flag: up-right or down-left. Move in the current direction until you hit a boundary, then flip direction.
When moving up-right (row decreases, col increases), you hit a boundary when row < 0 or col >= n. When moving down-left (row increases, col decreases), you hit a boundary when row >= m or col < 0.
The boundary handling requires care. If you go past the top edge, increment the row. If you go past the right edge, increment the row by and decrement col. Similar logic applies for the other direction.