class Solution:
    def spiral(self, matrix):
        rows_count = len(matrix[0])
        cols_count = len(matrix)
        row, col = 0, -1
        d_row, d_col = 0, 1
        row_leg, col_leg = rows_count, cols_count - 1
        leg_count = 0
        spiral = []

        for _ in range(rows_count * cols_count):
            row += d_row
            col += d_col
            spiral.append(matrix[row][col])
            leg_count += 1

            if (d_row == 0 and leg_count == row_leg) or (d_col == 0 and leg_count == col_leg):
                # change direction
                # decrease the right leg (possible number of moves through the axis)
                if d_row == 0:
                    row_leg -= 1
                else:
                    col_leg -= 1
                
                # get the new direction
                d_row, d_col = d_col, - d_row
                leg_count = 0
        
        return spiral
    
    def print_matrix(self, matrix):
        for row in matrix:
            print(row)

solution = Solution()
matrix = [[1,2,3],[5,6,7],[9,10,11],[13,14,15]]
solution.print_matrix(matrix)
print(solution.spiral(matrix))

                
