#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<string>
#include<vector>
#include<climits>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define	mp	make_pair
#define	pb	push_back
#define	pf	push_front

using namespace std;

typedef	vector<int>			vi;
typedef	vector<vi>			vvi;
typedef	pair<int,int>		pii;
typedef	pair<double,int>	pdi;

int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

struct P{
	P(){}
	P(int xx,int yy){x=xx,y=yy;}
	int x,y;
};

struct Piece{
	Piece(){}
	vector<P> blk;
	int xl,xr;
	double m;
};

int w,h,chk[60][10];
char bld[60][11];

Piece makePiece(const vector<P> &blk)
{
	int xl=10,xr=0;
	for(int i=0;i<blk.size();i++){
		int x=blk[i].x,y=blk[i].y;
		if(y==0 || (bld[y-1][x]!='.' && chk[y][x]!=chk[y-1][x])){
			xl=min(xl,x);
			xr=max(xr,x+1);
		}
	}

	Piece pic;
	pic.blk=blk;
	pic.xl=xl;
	pic.xr=xr;
	return pic;
}

void search(P now,char c,int id,vector<P> &blk)
{
	int x=now.x,y=now.y;

	if(bld[y][x]!=c || chk[y][x]!=-1)	return;

	blk.pb(now);
	chk[y][x]=id;

	for(int i=0;i<4;i++){
		int xx=x+dx[i],yy=y+dy[i];
		if(xx<0||w<=xx||yy<0||h<=yy)	continue;
		search(P(xx,yy),c,id,blk);
	}
}

vvi branch;
vector<Piece> piece;
pdi calcCenter(int id)
{
	int num=piece[id].blk.size();
	double xsum=0;
	for(int i=0;i<num;i++)	xsum+=piece[id].blk[i].x+0.5;

	for(int i=0;i<branch[id].size();i++){
		pdi data=calcCenter(branch[id][i]);
		xsum+=data.first;
		num+=data.second;
	}
	piece[id].m=xsum/num;
	return mp(xsum,num);
}

int main()
{
	for(;cin>>w>>h,w;){
		for(int y=h-1;y>=0;y--)	cin>>bld[y];

		for(int y=0;y<h;y++)for(int x=0;x<w;x++)chk[y][x]=-1;

		piece.clear();
		int id=0;
		for(int y=0;y<h;y++)for(int x=0;x<w;x++){
			if(bld[y][x]!='.' && chk[y][x]==-1){
				vector<P> block;
				search(P(x,y),bld[y][x],id++,block);
				piece.pb(makePiece(block));	// the root piece is always piece[0]
			}
		}

		branch=vvi(piece.size());	// adjacent list
		for(int i=0;i<branch.size();i++){
			for(int j=0;j<piece[i].blk.size();j++){
				P p=piece[i].blk[j];
				if(p.y<h-1 && chk[p.y+1][p.x]!=-1 && chk[p.y+1][p.x]!=i){
					branch[i].pb(chk[p.y+1][p.x]);
				}
			}
			sort(branch[i].begin(),branch[i].end());
			branch[i].erase(unique(branch[i].begin(),branch[i].end()),branch[i].end());
		}

		calcCenter(0);

		bool ok=true;
		for(int i=0;i<piece.size();i++){
			if(!(piece[i].xl<piece[i].m && piece[i].m<piece[i].xr))	ok=false;
		}
		if(ok)	cout<<"STABLE"<<endl;
		else	cout<<"UNSTABLE"<<endl;
	}

	return 0;
}