# 54. Spiral Matrix
###### tags: `spiral`
This the best for spiral!! ref: https://leetcode.com/problems/spiral-matrix/discuss/1466413/Python-simulate-process-explained
```python=
class Solution:
def spiralOrder(self, matrix):
if not matrix or not matrix[0]:
return None
m = len(matrix)
n = len(matrix[0])
i, j, di, dj = 0, 0, 0, 1
seen = set()
ans = []
for _ in range(m*n):
good = 0<=i+di<m and 0<=j+dj<n and (i+di,j+dj) not in seen
if not good:
di, dj = dj, -di # rotating clockwise
# right 0, 1
# down 1, 0
# left 0, -1
# up -1, 0
ans.append(matrix[i][j])
seen.add((i,j))
i, j = i+di, j+dj
return ans
```