#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
using namespace std;
typedef long long int ll;
#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=(x);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)

int w, h;
int d[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
string M[80];
int visit[80][20];
int par[160];
struct P{
	vector<double> ps;
	int gl;
	int gr;
	P(){ps.clear();gl=100;gr=0;}
} peace[160];

void check(int y, int x, char c, int idx){
//	printf("check %d %d %c %d\n", y, x, c, idx);
	visit[y][x] = 1;
	peace[idx].ps.push_back(x+0.5);
	if(y+1 == h || (M[y+1][x] != M[y][x] && M[y+1][x] != -1)){
		peace[idx].gr = max(peace[idx].gr, x+1);
		peace[idx].gl = min(peace[idx].gl, x);
	}
	M[y][x] = idx;
	REP(i, 4){
		int dy=y+d[i][1];
		int dx=x+d[i][0];
		if(dx<0||dy<0||w<=dx||h<=dy) continue;
		if(!visit[dy][dx] && M[dy][dx] == c) check(dy, dx, c, idx);
	}
}

int dfs(int r){
	REP(i, 160) if(par[i] == r){
		if(!dfs(i)) return 0;
		peace[r].ps.insert(peace[r].ps.end(), peace[i].ps.begin(), peace[i].ps.end());
	}
//	REP(i, peace[r].ps.size()) printf("%.2f ", peace[r].ps[i]);
//	puts("");
	if(peace[r].ps.size() == 0) return 1;
	double m = (double)accumulate(peace[r].ps.begin(), peace[r].ps.end(), 0.0) / peace[r].ps.size();
//	printf("%d -> %d %.2f %d\n", r, peace[r].gl, m, peace[r].gr);
	return peace[r].gl+1e-8 < m && peace[r].gr > m+1e-8;
}

int solve(){
	int idx = 0;
	REP(i, h){
		REP(j, w){
			if(M[i][j]!=-1 && !visit[i][j]){
				check(i, j, M[i][j], idx++);
			}
		}
	}
	REP(i, 160) par[i] = -1;
	REP(i, h-1){
		REP(j, w){
			if(M[i][j] != -1 && M[i+1][j] != -1 && M[i][j]!=M[i+1][j]){
				par[M[i][j]] = M[i+1][j];
			}
		}
	}
	REP(i, 160) if(par[i] == -1) return dfs(i);
	return 0;
}

int main(){
	int i,j;
	while(cin >> w >> h, w){
		REP(i, 160) peace[i] = P();
		memset(visit, 0, sizeof(visit));
		REP(i, h){
			cin >> M[i];
			REP(j, w) if(M[i][j] == '.') visit[i][j] = 1, M[i][j] = -1;
		}
		cout << (solve() ? "STABLE":"UNSTABLE") << endl;
	}
	return 0;
}