#
# 
# @param matrix int整型二维数组 
# @return int整型一维数组
#
class Solution:
    def spiralOrder(self , matrix ):
        # write code here
        result = []
        m = len(matrix)
        if(m == 0): return result
        n = len(matrix[0])
        c = (2*m - 1) if (m<=n) else 2*n
        if(c<=0): return result
        i = 0
        j = 0
        result.append(matrix[0][0])
        for k in range(1,c+1,1):
            if(k%4 == 0):
                for j in range(j-1,k//4-1,-1):
                    result.append(matrix[j][i])
            elif(k%4 == 1):
                for i in range(i+1,(n-k//4),1):
                    result.append(matrix[j][i])
            elif(k%4 == 2):
                for j in range(j+1,(m-k//4),1):
                    result.append(matrix[j][i])
            elif(k%4 == 3):
                for i in range(i-1,k//4-1,-1):
                    result.append(matrix[j][i])
        return result