//
#include "bits/stdc++.h"
using namespace std;

vector<long double>gravs(200);
struct carry {
	int id;
	double l;
	double r;
};

pair<bool,pair<long double,int>> getans(const vector<vector<carry>>&carrys,int now) {
	int weightsum = 4;
	long double posisum = weightsum*gravs[now];
	for (auto i : carrys[now]) {
		auto ans(getans(carrys, i.id));
		if (ans.first) {
			if (ans.second.first > i.l&&ans.second.first < i.r) {
				const int plusweightsum = ans.second.second;
				const long double plusposisum = plusweightsum*ans.second.first;
				weightsum += plusweightsum;
				posisum += plusposisum;
			}
			else {
				return make_pair(false, make_pair(0,0));
			}
		}
		else {
			return make_pair(false, make_pair(0, 0));
		}
	}
	return make_pair(true, make_pair(posisum / weightsum, weightsum));
}
int X, Y;
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };

void check(vector<vector<int>>&nums, const int y, const int x,const int id,const int from) {
	nums[y][x] = id;
	for (int i = 0; i < 4; ++i) {
		const int ny = y + dy[i];
		const int nx = x + dx[i];
		if (ny >= Y || ny < 0 || nx >= X || nx < 0) {
			continue;
		}
		else {
			if (nums[ny][nx] == from) {
				check(nums, ny, nx,id, from);
			}
		}
	}
	return;
}

int main() {
	while (1) {
	 cin >> X >> Y;
		if (!X)break;
		vector<vector<pair<int,int>>>places(200);
		vector<vector<carry>>carrys(200);
		vector<vector<int>>nums(Y);
		gravs.clear();
		gravs.resize(200);
		for (int i = 0; i < Y; ++i) {
			string st; cin >> st;
			for (int j = 0; j < X; ++j) {
				if (st[j] == '.') {
					nums[i].push_back(0);
				}
				else {
					const int peaceid = st[j] - '0';
					nums[i].push_back(peaceid);

				}
			}
		}
		int num = 11;
		for (int i = 0; i < Y; ++i) {
			for (int j = 0; j < X; ++j) {
				if (nums[i][j] >= 1 && nums[i][j] <= 9) {
					check(nums, i, j, num,nums[i][j]);
					num++;
				}
			}
		}
		for (int i = 0; i < Y; ++i) {
			for (int j = 0; j < X; ++j) {
				if (nums[i][j]) {
					places[nums[i][j]].push_back(make_pair(i, j));
				}
			}
		}
		for (int i = 0; i < 200; ++i) {
			if (places[i].size()) {
				long double x_sum = 2;
				for (int j = 0; j < 4; ++j) {
					x_sum += places[i][j].second;
				}
				gravs[i] = x_sum / 4;
			}
		}
		for (int i = 0; i < Y-1; ++i) {
			for (int j = 0; j < X; ++j) {
				if (nums[i][j] && nums[i + 1][j]&&nums[i][j]!=nums[i+1][j]) {
					const int up = nums[i][j];
					const int down = nums[i + 1][j];
					auto it=find_if(carrys[down].begin(), carrys[down].end(), [=](const carry&c) {return c.id == up; });
					if (it == carrys[down].end()) {
						carrys[down].push_back(carry{ up,j+ 0.001,j + 1- 0.001 });
					}
					else {
						it->l = min(it->l, double(j + 0.001));
						it->r = max(it->r, double(j + 1 - 0.001));
					}
				}
			}
		}
		for (int j = 0; j < X; ++j) {
			if (nums[Y-1][j]) {
				const int down = 0;
				const int up = nums[Y - 1][j];
				auto it = find_if(carrys[down].begin(), carrys[down].end(), [=](const carry&c) {return c.id == up; });
				if (it == carrys[down].end()) {
					carrys[down].push_back(carry{ up,j + 0.001,j + 1 - 0.001 });
				}
				else {
					it->l = min(it->l, double(j + 0.001));
					it->r = max(it->r, double(j + 1 - 0.001));
				}
			}
		}
		pair<int, pair<long double, int>>ans(getans(carrys, 0));
		if (ans.first) {
			cout << "STABLE" << endl;
		}
		else {
			cout << "UNSTABLE" << endl;
		}

	}
	return 0;
}

Measured memory_bytes: 3220