#include "bits/stdc++.h"
#pragma comment (linker, "/STACK:526000000")
#define  _CRT_SECURE_NO_WARNINGS

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144*2LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

void init() {
	iostream::sync_with_stdio(false);
	cout << fixed << setprecision(20);
}

const int dx[4] = { 1,-1,0,0 };
const int dy[4] = { 0,0,1 ,-1 };

vector<string> inputs;
int grid[60][60];
void solve(){
	vector<string> final_ans;
	while(true){
		int w, h;
		cin >> w >> h;
		if (w == 0) {
			REP(i, final_ans.size()) {
				cout << final_ans[i] << endl;
			}
			return;
		}
		REP(i, 60) {
			REP(q, 60) {
				grid[i][q] = 0;
			}
		}
		inputs.clear();
		REP(i, h) {
			string s;
			cin >> s;
			inputs.push_back(s);
		}
		reverse(ALL(inputs));
		int now_itr = 1;
		for (int i = 0; i < h; ++i) {
			REP(q, w) {
				if (inputs[i][q] == '.') continue;
				queue<pair<int, int>> next;
				int target = inputs[i][q];
				inputs[i][q] = '.';
				next.push(mp(i, q));
				while (next.empty() == false) {
					pair<int, int> now = next.front();
					next.pop();
					grid[now.first][now.second] = now_itr;
					REP(q, 4) {
						int x = now.first + dx[q];
						int y = now.second + dy[q];
						if (x >= 0 && x < h && y >= 0 && y < w) {
							if (inputs[x][y] == target) {
								inputs[x][y] = '.';
								next.push(mp(x, y));
							}
						}
					}
				}
				now_itr++;
			}
		}
		final_ans.push_back("");
		for (int now_val = 1; now_val < now_itr; ++now_val) {
			int visited[60][60] = {};
			queue<pair<int, int>> next;
			ll now_x_max = 0;
			ll now_x_min = 1e9;
			REP(i, h) {
				REP(q, w) {
					if (grid[i][q] != now_val) continue;
					visited[i][q] = 1;
					next.push(mp(i, q));
					if (i == 0 || (grid[i - 1][q] != 0&&grid[i-1][q] != grid[i][q])) {
						now_x_max = max(now_x_max, q);
						now_x_min = min(now_x_min, q);
					}
				}
			}
			while (next.empty() == false) {
				pair<int, int> now = next.front();
				next.pop();
				REP(q, 4) {
					int x = now.first + dx[q];
					int y = now.second + dy[q];
					if (!(x >= 0 && x < h && y >= 0 && y < w)) continue;
					if (grid[x][y] == 0) continue;
					if (q != 0) {
						if (grid[x][y] != grid[now.first][now.second]) continue;
					}
					if (visited[x][y] == 0) {
						visited[x][y] = 1;
						next.push(mp(x, y));
					}
				}
			}
			int cnt = 0;
			int val = 0;
			REP(i, h) {
				REP(q, w) {
					cnt += visited[i][q] * (2 * q + 1);
					if (visited[i][q] == 1) {
						val++;
					}
				}
			}
			now_x_max++;
			if (!(now_x_min *2 * val < cnt && cnt < now_x_max * val * 2)) {
				final_ans.back() = "UN";
				break;
			}
		}
		final_ans.back() += "STABLE";
	}
}
#undef int
int main() {
	init();
	solve();
}
