code: def spiral_matrix(matrix):
      n = len(matrix)
      if n == 0:
          return []
  
      m = len(matrix[0])
      if m == 0:
          return []
  
      total = n * m
      res = []
  
      n -= 1
      xDir, yDir = 1, 1
      x, y = 0, -1
  
      while len(res) < total:
          for i in range(m):
              y += yDir
              res.append(matrix[x][y])
          m -= 1  # decrease horizontal moving steps
          yDir *= -1  # change the Y direction
  
          for i in range(n):
              x += xDir
              res.append(matrix[x][y])
          n -= 1  # decrease vertical moving steps
          xDir *= -1  # change the Y direction
  
      return res
  
label_desc: Use image node_id 1 for calling a global function with example usage: spiral_matrix(matrix) and returns: res, list, list