#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <utility>
#include <memory>
#include <functional>
#include <deque>
#include <cctype>
#include <ctime>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll;
typedef vector<vector<long long> > vvll;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i,n) _rep2((i),0,(n))
#define _rep2(i,a,b) for(int i=(a);i<(b);++i)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define fi first
#define se second
#define mkp make_pair
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

struct piece{
	double x, y;
	double w;
	piece(double xx, double yy, double ww){
		x = xx;
		y = yy;
		w = ww;
	}
};
typedef pair<double, double> pdd;

vector<piece> v;
vector<set<int>> edge;
vector<pdd> r;
int w, h;
vvint used;

bool inside(int a, int b){
	return 0<=a&&a<h&&0<=b&&b<w;
}

piece add(piece a, piece b){
	double m = a.w + b.w;
	double x = a.x * a.w / m + b.x * b.w / m;
	double y = a.y * a.w / m + b.y * b.w / m;
	return piece(x, y, m);
}

bool dfs(int x){
	// cout<<x<<endl;
	for(auto y: edge[x]){
		if(!dfs(y)) return false;
		v[x] = add(v[x], v[y]);
	}
	if(r[x].fi+EPS < v[x].x && v[x].x < r[x].se-EPS){
		return true;
	}
	// cout<<r[x].fi<<" "<<v[x].x<<" "<<r[x].se<<endl;
	// cout<<x<<" "<<"false"<<endl;
	return false;
}

void foo(int a, int b, int c, int d, vector<string> &in){
	if(used[a][b]) return;
	if(in[a][b] != c) return;
	used[a][b] = 1;
	in[a][b] = d;
	int dd[] = {0,1,0,-1,0};
	rep(i,4){
		int na = a+dd[i];
		int nb = b+dd[i+1];
		if(inside(na, nb)){
			foo(na, nb, c, d,in);
		}
	}
}

void mainmain(){
	while(cin>>w>>h, w||h){
		int cnt = 0;
		vector<string> vv(h);
		initvv(used, h, w, 0);
		rep(i,h) cin>>vv[i];
		rep(i,h) rep(j,w){
			if(vv[i][j]=='.') vv[i][j]=-1;
			else vv[i][j] -= '0';
		}
		// rep(i,h){
		// 	rep(j,w){
		// 		cout<<(int)vv[i][j];
		// 	}
		// 	cout<<endl;
		// }
		// cout<<endl;
		rep(i,h){
			rep(j,w){
				if(vv[i][j]==-1) continue;
				if(used[i][j]) continue;
				foo(i,j,vv[i][j],cnt,vv);
				cnt++;
			}
		}
		// rep(i,h){
		// 	rep(j,w){
		// 		cout<<(int)vv[i][j];
		// 	}
		// 	cout<<endl;
		// }
		v=vector<piece>(cnt, piece(0,0,0));
		edge=vector<set<int>>(cnt);
		r=vector<pdd>(cnt, pdd(INF,-INF));
		// cout<<"hoge"<<endl;
		rep(i,h){
			rep(j,w){
				if(vv[i][j]>=0&&(i==h-1||(vv[i+1][j]>=0&&vv[i][j]!=vv[i+1][j]))){
					// cout<<(int)vv[i][j]<<" "<<i<<" "<<j<<endl;
					if(i<h-1) edge[vv[i+1][j]].insert(vv[i][j]);
					// cout<<"before "<<r[vv[i][j]].se<<endl;
					mins(r[vv[i][j]].fi, 1.*j);
					maxs(r[vv[i][j]].se, 1.*j+1);
					// cout<<"after  "<<r[vv[i][j]].se<<endl;
					// cout<<i<<" "<<j<<endl;
				}
				if(vv[i][j]>=0){
					v[vv[i][j]] = add(piece(j+0.5,i,1), v[vv[i][j]]);
				}
			}
		}
		// rep(i,cnt){
		// 	cout<<"piece "<<v[i].x<<" "<<v[i].y<<" "<<v[i].w<<endl;
		// }
		rep(i,w){
			// cout<<vv[h-1][i]<<endl;
			if(vv[h-1][i]>=0){
				if(dfs(vv[h-1][i])){
					cout<<"STABLE"<<endl;
				}
				else{
					cout<<"UNSTABLE"<<endl;
				}
				break;
			}
		}
	}	
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}

Measured memory_bytes: 3160