Unwind the nth-layer ring

Recall that the first-layer ring consists of the outer values of a given m×n matrix.

To obtain the 2nd-layer ring:

  1. The values of the outer-layer ring are removed, obatining a new matrix that a smaller size.
  2. The outer values on the four "edges" of this new matrix form 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.

Enter the matrix:
Enter the layer to be unwinded: