#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<utility>
#include<algorithm>
using namespace std;
#define INF (1<<29)

int dx[]={0,0,1,-1};
int dy[]={-1,1,0,0};
struct P{
	int xsum;
	int m;
	bool stable;
};
int w,h;
char p[62][12];

P dfs(int y,int x){
	int xl=INF,xr=-1;
	P up={-1},res={0,0,true};
	stack<pair<int,int> > q;
	pair<int,int>  a,b;
	q.push(make_pair(y,x));
	char id = p[y][x];
	if(p[y+1][x]!='.')xl=xr=x;
	while(!q.empty()){
		a = q.top();q.pop();//cout<<a.first<<' '<<a.second<<endl;
		if(p[a.first][a.second]=='.')continue;
		res.m++;
		res.xsum+=a.second;
		for(int i=0;i<4;i++){
			b = make_pair(a.first+dy[i],a.second+dx[i]);
			if(i==0 && p[b.first][b.second]!=id && p[b.first][b.second]!='.'){
				up = dfs(b.first,b.second);
				if(!up.stable){
					res.stable=false;
					break;
				}
				res.xsum += up.xsum;
				res.m += up.m;
			}else if(i==1 && p[b.first][b.second]!=id && p[b.first][b.second]!='.'){
				xl = min(xl,b.second);
				xr = max(xr,b.second);
			}
			if(p[b.first][b.second]==id){
				q.push(b);	
			}
		}
		p[a.first][a.second]='.';
	}
	int m = res.m;
	//printf("%d,%d,%c %d,%d,%d,%d\n",y,x,id,xl,xr,res.xsum,m);
	//printf(" %d %d %d\n",xl*m*2,res.xsum*2+m,(xr+1)*m*2);
	if(xl!=INF && !(xl*m*2<res.xsum*2+m && res.xsum*2+m<(xr+1)*m*2))res.stable=false;
	return res;
}
int main(){
	while(cin>>w>>h,w||h){
		memset(p,'.',sizeof(p));
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++)cin>>p[i][j];
		}
		bool ok=true;
		for(int x=1;x<=w;x++){
			p[h+1][x]='0';
		}
		for(int x=1;x<=w;x++){
			if(p[h][x]!='.' && !dfs(h,x).stable)ok=false;
		}
		cout<<(ok?"STABLE":"UNSTABLE")<<endl;
	}
	return 0;
}