Spiral sort in two d array

question link:https://www.geeksforgeeks.org/print-a-given-matrix-in-spiral-form/
answer link:https://ide.codingblocks.com/s/52145
It is giving wrong answer for one test case.
PLease check

Your code doesn’t work well in cases when n is not equal to m
For example

When input is:
3 4
1 2 3 4
5 6 7 8
9 10 11 12

Your Output:
1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7, 6, END

which is not correct.
Try to implement the code again keeping in mind such corner cases!