#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define lengthof(array) (sizeof(array) / sizeof(*array))
#define dump(a) (cerr << (#a) << " = " << (a) << endl)
#define FOR(it,c) for(__typeof((c).begin())it=(c).begin(); it!=(c).end();++it)
#define RFOR(it,c) for(__typeof((c).rbegin())it=(c).rbegin(); it!=(c).rend();++it)

template<class T> inline void chmax(T& a, const T& b) { if(b > a) a = b; }
template<class T> inline void chmin(T& a, const T& b) { if(b < a) a = b; }

template<typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os << '(' << p.first << ", " << p.second << ')';
	return os;
}

template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
	return os;
}

const int MAX_W = 10, MAX_H = 60;
const int INF = 0xfffffff;

int w, h;
int label[MAX_H][MAX_W];
string brd[MAX_H];

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

struct node {
	int xl, xr;
	vector<int> cnt, children;

	node():xl(INF), xr(-1), cnt(w, 0){}
};

bool used[MAX_H][MAX_W];
vector<node> nodes;

bool valid(int x, int y) {
	return 0 <= x && x < w && 0 <= y && y < h;
}

bool exist(const vector<int>& vec, int num) {
	return find(vec.begin(), vec.end(), num) != vec.end();
}

void label_dfs(int x, int y, int idx) {
	label[y][x] = idx;
	for(int i = 0; i < 4; ++i) {
		const int nx = x + dx[i], ny = y + dy[i];
		if(valid(nx, ny) && label[ny][nx] == -1 && brd[y][x] == brd[ny][nx])
			label_dfs(nx, ny, idx);
	}
}

int labeling() {
	int idx = 0;
	memset(label, -1, sizeof(label));
	for(int i = h - 1; i >= 0; --i)
		for(int j = 0; j < w; ++j)
			if(label[i][j] == -1 && brd[i][j] != '.')
				label_dfs(j, i, idx++);

	return idx;
}

void create(int x, int y) {
	const int id = label[y][x];
	if(y == h - 1 || (label[y + 1][x] != -1 && label[y + 1][x] != id)) {
		chmax(nodes[id].xr, x);
		chmin(nodes[id].xl, x);
	}
	++nodes[id].cnt[x];
	used[y][x] = true;

	for(int i = 0; i < 4; ++i) {
		const int nx = x + dx[i], ny = y + dy[i];
		if(!valid(nx, ny) || used[ny][nx])
			continue;

		if(id == label[ny][nx])
			create(nx, ny);
	}

	if(y > 0 && label[y - 1][x] != -1 && label[y - 1][x] != id)
		if(!exist(nodes[id].children, label[y - 1][x]))
			nodes[id].children.push_back(label[y - 1][x]);
}

vector<int> operator+(const vector<int>& l, const vector<int>& r) {
	const int sz = l.size();
	assert(l.size() == r.size());
	vector<int> res(sz, 0);
	for(int i = 0; i < sz; ++i)
		res[i] = l[i] + r[i];

	return res;
}

bool ok;
vector<int> check(int id) {
	const node& cur = nodes[id];
	vector<int> tmp(cur.cnt);
	for(int i = 0; i < (int)cur.children.size(); ++i)
		tmp = tmp + check(cur.children[i]);

	const int sum = accumulate(tmp.begin(), tmp.end(), 0);
	int nume = 0;
	for(int i = 0; i < w; ++i)
		nume += i * tmp[i];

	const double m = (double)nume / sum;
//	dump(id);
//	dump(m);
//	dump(tmp);
	if(!(cur.xl - 0.5 < m && m < cur.xr + 0.5))
		ok = false;

	return tmp;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(cin >> w >> h, w) {
		for(int i = 0; i < h; ++i)
			cin >> brd[i];

		const int numBlock = labeling();
/*
		for(int i = 0; i < h; ++i)
			for(int j = 0; j < w; ++j)
				cout << label[i][j] << (j == w - 1 ? "\n" : "\t");
*/

		nodes.clear();
		nodes.resize(numBlock);
		memset(used, false, sizeof(used));
		for(int i = h - 1; i >= 0; --i)
			for(int j = 0; j < w; ++j)
				if(label[i][j] != -1 && !used[i][j])
					create(j, i);
/*
		cout << endl;
		for(int i = 0; i < numBlock; ++i) {
			dump(i);
			dump(nodes[i].xl);
			dump(nodes[i].xr);
			dump(nodes[i].cnt);
			dump(nodes[i].children);
			cout << endl;
		}
		*/
		ok = true;
		check(0);
		cout << (ok ? "STABLE" : "UNSTABLE") << endl;
	}

	return EXIT_SUCCESS;
}

Measured memory_bytes: 1256