#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000009;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
int h, w;
char b[62][12];
vector<int> G[200];
string ans;
void init() {
	rep(i, 62) {
		rep(j, 12) {
			b[i][j] = '.';
		}
	}
	rep1(i, h) {
		rep1(j, w) {
			cin >> b[i][j];
		}
	}
	rep(i, 200) {
		G[i].clear();
	}
	ans = "STABLE";
}
struct block {
	int le, ri; ld cog;
};
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,-1,0,1 };
block bl[200];
void make_graph() {
	int cur = 1;
	map<P, int> mp;
	bool used[62][12] = {};
	vector<vector<P>> gv;
	per1(i, h) {
		rep1(j, w) {
			if (used[i][j] || b[i][j] == '.')continue;
			used[i][j] = true;
			vector<P> v; v.push_back({ i,j });
			queue<P> q; q.push({ i,j });
			while (!q.empty()) {
				P x = q.front(); q.pop();
				rep(k, 4) {
					int ni = x.first + dx[k];
					int nj = x.second + dy[k];
					if (used[ni][nj] || b[i][j] != b[ni][nj])continue;
					used[ni][nj] = true; q.push({ ni,nj }); v.push_back({ ni,nj });
				}
			}
			ld sum = 0;
			rep(k, 4) {
				mp[{v[k].first, v[k].second}] = cur;
				sum += v[k].second;
			}
			sum /= 4.0; sum += 0.5;
			int mi = mod; int ma = -mod;
			rep(k, 4) {
				int ni = v[k].first + 1; int nj = v[k].second;
				if (b[ni][nj] != '.'&&b[ni][nj] != b[v[k].first][v[k].second]) {
					mi = min(mi, nj);
					ma = max(ma, nj + 1);
				}
			}
			bl[cur] = { mi,ma,sum };
			cur++;
			gv.push_back(v);
		}
	}

	rep(i, (int)gv.size()) {
		vector<P> c = gv[i];
		int id = mp[{ c[0].first,c[0].second }];
		int  chk = -1;
		rep(j, 4) {
			int ni = c[j].first + 1; int nj = c[j].second;
			if (b[ni][nj] != '.'&&b[ni][nj] != b[c[j].first][c[j].second]) {
				chk = mp[{ni, nj}];
			}
		}
		if (chk >= 0) {
			G[chk].push_back(id);
		}
	}
	int mi = mod; int ma = -mod;
	rep1(i, w) {
		if (b[h][i] == '.')continue;
		mi = min(mi, i);
		ma = max(ma, i+1);
	}
	bl[1].le = mi; bl[1].ri = ma;
}
LDP dfs(int id) {
	ld sum = bl[id].cog; ld cnt =1;
	ld le = bl[id].le, ri = bl[id].ri;
	rep(j, G[id].size()) {
		int to = G[id][j];
		//cout << id << " " << to << endl;
		LDP nex = dfs(to);
		sum += nex.first*nex.second; cnt += nex.second;
	}
	sum /= cnt;
	//cout << id << " " << sum << endl;
	if (sum < le + eps || ri - eps < sum)ans = "UNSTABLE";
	return { sum,cnt };
}
int main(){
	while (cin >> w >> h,w) {
		init(); make_graph(); dfs(1);
		cout << ans << endl;
		/*rep1(i, 3) {
			cout << bl[i].le << " " << bl[i].ri << " " << bl[i].cog << endl;
		}*/
	}
	return 0;
}


Measured memory_bytes: 3168