class Solution:
    def generateMatrix(self, n):
        """
        :type n: int
        :rtype: List[List[int]]
        """
        matrix=[[0 for i in range(n)]for j in range(n)]
        l,r,u,d=0,n-1,0,n-1
        count=1
        while True:
            #go right
            for col in range(l,r+1,1):
                matrix[u][col]=count
                count+=1
            u+=1
            if u>d:
                break
            #go down
            for row in range(u,d+1,1):
                matrix[row][r]=count
                count+=1
            r-=1
            if r<l:
                break
            #go left
            for col in range(r,l-1,-1):
                matrix[d][col]=count
                count+=1
            d-=1
            if d<u:
                break
            #go up
            for row in range(d,u-1,-1):
                matrix[row][l]=count
                count+=1
            l+=1
            if l>r:
                break
        return matrix
s=Solution()
s.generateMatrix(3)