Recall that the first-layer ring consists of the outer values of a given m×n matrix.
To obtain the 2nd-layer ring:
Repeat the steps above on the 1st-layer ring to also obtain the 3rd-layer ring, the 4th-layer ring, and so on.
Knowing this, modify the program such that instead of unwinding the outer-layer ring, it unwinds the nth-layer ring.