require 'set'

class Piece
	def initialize
		@blocks = []
		@children = Set.new
		@weight = 0
	end

	def add_block(y, x)
		@blocks << [y, x]
	end

	def add_child(piece)
		@children << piece
	end

	def check(root = false)
		@children.each(&:check)
		if @children.empty?
			@total_weight = 4
			@total_cg = @blocks.map(&:last).inject(:+) / 4.0 + 0.5 
		else
			@children.each do |c|
				touched = c.blocks.select{|y, x| @blocks.include?([y-1, x])}.map(&:last)
				xl, xr = touched.min, touched.max + 1
				raise if c.total_cg <= xl || xr <= c.total_cg
			end

			@total_weight = 4 + @children.map(&:total_weight).inject(:+)
			my_cg = @blocks.map(&:last).inject(:+) / 4.0 + 0.5 
			@total_cg = (my_cg * 4 + @children.map{|c| c.total_weight * c.total_cg}.inject(:+)) / @total_weight
		end

		if root
			touched = @blocks.select{|y, x| y == 0}.map(&:last)
			xl, xr = touched.min, touched.max + 1
			raise if @total_cg <= xl || xr <= @total_cg
		end
	end

	attr_reader :blocks, :total_weight, :total_cg
end


loop do
	w, h = gets.split.map &:to_i
	break if w == 0
	a = []
	h.times do
		a.unshift gets.chomp.split("")
	end

	hash = {}
	root = nil
	(0..h-1).each do |y|
		(0..w-1).each do |x|
			next if a[y][x] == ?.
			next if hash[[y, x]]
			piece = Piece.new
			hash[[y, x]] = piece
			root = piece if y == 0

			stack = [[y,x]]
			until stack.empty?
				i, j = stack.pop
				piece.add_block(i, j)

				v = [i+1, j]
				if i < h-1 && a[i][j] == a[i+1][j] && !hash[v]
					hash[v] = piece	
					stack << v
				end

				v = [i, j+1]
				if j < w-1 && a[i][j] == a[i][j+1] && !hash[v]
					hash[v] = piece	
					stack << v
				end

				v = [i, j-1]
				if j > 0 && a[i][j] == a[i][j-1] && !hash[v]
					hash[v] = piece	
					stack << v
				end
			end
		end
	end

	(0..h-2).each do |y|
		(0..w-1).each do |x|
			p_piece = hash[[y, x]]
			next if !p_piece
			if (c_piece = hash[[y+1, x]]) && (c_piece != p_piece)
				p_piece.add_child(c_piece)
			end
		end
	end

	begin
		root.check(true)
		puts 'STABLE'
	rescue
		puts 'UNSTABLE'
	end
end