#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
using namespace std;
typedef long long int LL;
typedef pair<double,double> P;
typedef pair<int,pair<int,int> > PP;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void print_YES_NO(bool answer){cout<<(answer?"YES":"NO")<<endl;}
void print_Yes_No(bool answer){cout<<(answer?"Yes":"No")<<endl;}
void print_POSSIBLE_IMPOSSIBLE(bool answer){cout<<(answer?"POSSIBLE":"IMPOSSIBLE")<<endl;}

int n,m;
char ca[70][20];
int data[70][20];
double bottom[200][2];
double g[200];
vector<int> path[200];
int place[200][4][2];

void check2(int x,int y,int t,char c){
	if(x<0 || n<=x || y<0 || m<=y)return;
	if(data[y][x]!=-1 || ca[y][x]!=c)return;
	data[y][x]=t;
	int i;
	for(i=0;place[t][i][0]!=-1;i++);
	place[t][i][0]=x,place[t][i][1]=y;
	check2(x-1,y,t,c),check2(x+1,y,t,c);
	check2(x,y-1,t,c),check2(x,y+1,t,c);
}

int check(int x,int y,int t){
	check2(x,y,t,ca[y][x]);
	int p,q,r;
	r=t+1;
	for(int i=0;i<4;i++){
		p=place[t][i][0],q=place[t][i][1];
		if(q==m-1 || ca[q+1][p]=='.' || ca[q+1][p]==ca[y][x] || data[q+1][p]!=-1)continue;
		path[t].push_back(r);
		r=check(p,q+1,r);
	}
	return r;
}

P check3(int t){
	P pa,pb;
	pa=make_pair(g[t],1);
	for(int i=0;i<path[t].size();i++){
		pb=check3(path[t][i]);
		if(pb.second<0)return pb;
		pa.first=(pa.first*pa.second+pb.first*pb.second)/(pa.second+pb.second);
		pa.second+=pb.second;
	}
	if(pa.first<=bottom[t][0] || bottom[t][1]<=pa.first)pa=make_pair(0,-1);
	return pa;
}

int main(){
	int i,j,k;
	int a,b,c;
	int p;
	while(cin>>n>>m){
		if(n==0)break;
		for(i=m-1;i>=0;i--){
			cin>>ca[i];
		}
		for(i=0;i<200;i++)path[i].clear();
		memset(place,-1,sizeof(place));
		memset(data,-1,sizeof(data));
		memset(g,0,sizeof(g));
		for(i=0;ca[0][i]=='.';i++);
		p=check(i,0,0);
		for(i=0;i<p;i++){
			bottom[i][0]=100,bottom[i][1]=0;
			for(j=0;j<4;j++){
				a=place[i][j][0],b=place[i][j][1];
				g[i]+=a;
				if(b==0 || (ca[b-1][a]!=ca[b][a] && ca[b-1][a]!='.')){
					bottom[i][0]=min(bottom[i][0],(double)a);
					bottom[i][1]=max(bottom[i][1],(double)a+1);
				}
			}
			g[i]=g[i]/4.0+0.5;
		}
		P pa=check3(0);
		if(pa.second<0)cout<<"UNSTABLE"<<endl;
		else cout<<"STABLE"<<endl;
	}
}

