text: import numpy as np
  
  
  def rle_encoding(x):
      '''
      x: numpy array of shape (height, width), 1 - mask, 0 - background
      Returns run length as list
      '''
  
      # .T sets Fortran order down-then-right
      dots = np.where(x.T.flatten() == 1)[0]
      run_lengths = []
      prev = -2
      for b in dots:
          if (b > prev + 1):
              run_lengths.extend((b + 1, 0))
          run_lengths[-1] += 1
          prev = b
      return " ".join([str(i) for i in run_lengths])
  
  