class Map < Array
  Next = [[1, 0], [-1, 0], [0, 1], [0, -1]]
  def flood_fil(y, x, p)
    h = self.size
    w = self.first.size
    return unless y.between?(0, h - 1)
    return unless x.between?(0, w - 1)
    target = self[y][x]
    self[y][x] = p
    Next.each do |dy, dx|
      ny = y + dy
      nx = x + dx
      next unless ny.between?(0, h - 1)
      next unless nx.between?(0, w - 1)
      next unless self[ny][nx] == target
      p.copy(p.merge(Piece.new(nx + 0.5)))
      flood_fil(ny, nx, p)
    end
    self
  end
end

class Piece
  attr_reader :x, :weight, :lx, :rx, :upon
  def initialize(x, w = 1.0, l = nil, r = nil, u = Array.new)
    @x = x.to_f
    @weight = w.to_f
    @lx = l
    @rx = r
    @upon = u
  end
  def copy(p)
    @x, @weight, @lx, @rx, @upon = p.x, p.weight, p.lx, p.rx, p.upon
  end
  def ends(l, r)
    @lx = l unless @lx
    @rx = r unless @rx
    @lx = [@lx, l].min
    @rx = [@rx, r].max
  end
  def pile(p)
    @upon << p unless @upon.include?(p)
  end
  def merge(p)
    x = (@x * @weight + p.x * p.weight) / (@weight + p.weight)
    w = @weight + p.weight
    l = @lx
    r = @rx
    u = p.upon.dup
    Piece.new(x, w, l, r, u)
  end
  def above
    result = self.dup
    @upon.each do |u|
      result.copy(result.merge(u.above))
    end
    result
  end
  def stable?
    x = above.x
    @lx < x && x < @rx
  end
end

until (w, h = gets.split.map(&:to_i)).all?(&:zero?)
  map = Map.new
  h.times do
    map.push(Map.new.push(*gets.chomp.chars))
  end

  piece = Array.new
  h.times do |i|
    w.times do |j|
      next unless map[i][j] =~ /[0-9]/
      piece << Piece.new(j + 0.5)
      map.flood_fil(i, j, piece.last)
    end
  end
  map.push(Map.new(w, Piece.new(0, 0, 0, 0, [])))           # 番兵
  h.times do |i|
    w.times do |j|
      next unless map[i][j].kind_of?(Piece)
      next unless map[i + 1][j].kind_of?(Piece)
      next if map[i][j] == map[i + 1][j]
      map[i][j].ends(j, j + 1)
      map[i + 1][j].pile(map[i][j])
    end
  end

  puts piece.all?(&:stable?) ? "STABLE" : "UNSTABLE"
end