### constant

DXYS = [[1, 0], [0, 1], [-1, 0], [0, -1]]

### subroutines

def get_piece(x0, y0, used)
  b0 = $blks[y0][x0]
  return nil if b0 == '.' || used[y0][x0]

  pc = [[x0, y0]]
  used[y0][x0] = true
  q = [[x0, y0]]

  while ! q.empty?
    ux, uy = q.shift

    DXYS.each do |dx, dy|
      vx = ux + dx
      vy = uy + dy
      if vx >= 0 && vx < $w && vy >= 0 && vy < $h &&
          $blks[vy][vx] == b0 && ! used[vy][vx]
        used[vy][vx] = true
        pc << [vx, vy]
        q << [vx, vy]
      end
    end
  end

  pc
end

def find_pieces
  used = $h.times.map{$w.times.map{false}}

  pcs = []

  for y in (0...$h)
    for x in (0...$w)
      b = $blks[y][x]
      if b != '.' && ! used[y][x]
        pcs << get_piece(x, y, used)
      end
    end
  end

  pcs
end

def contact(pc0, pc1)
  cnt = false
  min_x = $w
  max_x = 0

  pc0.each do |x0, y0|
    if pc1.include?([x0, y0 + 1])
      cnt = true
      min_x = x0 if min_x > x0
      max_x = x0 + 1 if max_x <= x0
    end
  end

  cnt ? [min_x, max_x] : nil
end

def cg_piece(pc)
  sum = 0.0
  pc.each do |x, y|
    sum += (x + 0.5)
  end
  [sum, pc.length]
end

def cg_tree(k, min_x, max_x)
  sum, len = cg_piece($pcs[k])

  for i in (0...$npcs)
    cnt = $cnts[k][i]
    if ! cnt.nil?
      sl0 = cg_tree(i, cnt[0], cnt[1])
      return nil if sl0.nil?

      sum += sl0[0]
      len += sl0[1]
    end
  end

  x = sum / len
  (min_x < x && x < max_x) ? [sum, len] : nil
end

### main

loop do
  $w, $h = gets.split.map(&:to_i)
  break if ($w | $h) == 0

  $blks = $h.times.map{gets.strip}.reverse

  $pcs = find_pieces()
  $npcs = $pcs.length

  $cnts = $npcs.times.map{$npcs.times.map{nil}}

  for i in (0...$npcs)
    for j in (0...$npcs)
      $cnts[i][j] = (i != j) ? contact($pcs[i], $pcs[j]) : nil
    end
  end
  #p $cnts

  fpc = (0...$w).map{|x| [x, -1]}
  cnt0 = contact(fpc, $pcs[0])

  sl = cg_tree(0, cnt0[0], cnt0[1])
  puts (sl ? 'STABLE' : 'UNSTABLE')
end