# Spiral Matrix (Leetcode 54)

Problem Link: <https://leetcode.com/problems/spiral-matrix/>

```python
class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        
        m=len(matrix)
        n=len(matrix[0])
        l=0
        r=n-1
        t=0
        b=m-1
        d=0
        ans=[]
        while(l<=r and t<=b):
            if(d==0):
                for i in range(l,r+1):
                    ans.append(matrix[t][i])
                t=t+1
                d=(d+1)%4
            
            elif(d==1):
                for i in range(t,b+1):
                    ans.append(matrix[i][r])
                r=r-1
                d=(d+1)%4
            elif(d==2):
                for i in range(r,l-1,-1):
                    ans.append(matrix[b][i])
                b=b-1
                d=(d+1)%4
            else:
                for i in range(b,t-1,-1):
                    ans.append(matrix[i][l])
                l=l+1
                d=(d+1)%4
        return ans  
```
