```
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
rows = len(matrix)
cols = len(matrix[0]) if rows else 0
n = rows*cols
start = 0
answer = []
while(n):
# keep a count n of number of elts in answer
# start from top left corner
i, j = start, start
while n and j < cols:
answer.append(matrix[i][j])
n, j = n-1, j+1
i, j = i+1, j-1
while n and i < rows:
answer.append(matrix[i][j])
n, i = n-1, i+1
i, j = i-1, j-1
while n and j >= start:
answer.append(matrix[i][j])
n, j = n-1, j-1
i, j = i-1, j+1
while n and i > start:
answer.append(matrix[i][j])
n, i = n-1, i-1
start += 1
rows, cols = rows-1, cols-1
return answer
```