Four indexes: top, bottom, left and right are used in the code to label the region that have not been visited. Within one while loop, we first visit the row labeled by top index then the column by the right index, row by bottom index and lastly column by left index. The indexes are updated during each visit.

```
class Solution:
# @param matrix, a list of lists of integers
# @return a list of integers
def spiralOrder(self, matrix):
res=[]
if matrix:
top,bottom,left,right=0,len(matrix)-1,0,len(matrix[0])-1
while top<=bottom or left<=right:
# top line
res,top=res+matrix[top][left:right+1],top+1
if top>bottom:
break
# right line
ind=top
while ind<=bottom:
res.append(matrix[ind][right])
ind+=1
right-=1
if left>right:
break
# bottom line
res,bottom=res+[matrix[bottom][ii] for ii in range(right,left-1,-1)],bottom-1
if top>bottom:
break
# left Line
ind=bottom
while ind>=top:
res.append(matrix[ind][left])
ind-=1
left+=1
if left>right:
break
return res
```