#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>


using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int popcount(T n) { return n ? 1 + popcount(n & (n - 1)) : 0; }
#endif
#ifdef __GNUC__
template <class T> int popcount(T n);
template <> int popcount(unsigned int n) { return n ? __builtin_popcount(n) : 0; }
template <> int popcount(int n) { return n ? __builtin_popcount(n) : 0; }
template <> int popcount(unsigned long long n) { return n ? __builtin_popcountll(n) : 0; }
template <> int popcount(long long n) { return n ? __builtin_popcountll(n) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };
bool valid_pos(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }

template <class T> void print(T a, int n, int br = 1, const string& deli = ", ") { cout << "{ "; for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } cout << " }"; while (br--) cout << endl; }
template <class T> void print(const vector<T>& v, int br = 1, const string& deli = ", ") { print(v, v.size(), br, deli); }


int w, h;
char p[64][16];
struct Block
{
	int sum_x, n;
	int min_x, max_x;
	vector<Block> child;

	Block()
		: n(0), sum_x(0), min_x(1919), max_x(-3333) {}
};
bool visit[64][16];
Block build(int x, int y)
{
	char c = p[y][x];
	Block res;

	queue<pint> q;
	visit[y][x] = true;
	q.push(pint(x, y));
	while (!q.empty())
	{
		int x = q.front().first, y = q.front().second;
		q.pop();

		res.sum_x += x;
		++res.n;

		for (int i = 0; i < 4; ++i)
		{
			int nx = x + dx[i], ny = y + dy[i];
			if (valid_pos(nx, nx, w, h + 1))
			{
				if (p[ny][nx] == c)
				{
					if (!visit[ny][nx])
					{
						visit[ny][nx] = true;
						q.push(pint(nx, ny));
					}
				}
				else if (p[ny][nx] != '.')
				{
					if (i == 0 && !visit[ny][nx])
						res.child.push_back(build(nx, ny));
					else if (i == 2)
					{
						min_swap(res.min_x, x);
						max_swap(res.max_x, x + 1);
					}
				}
			}
		}
	}

	return res;
}
bool is_stable(Block& b)
{
	for (int i = 0; i < b.child.size(); ++i)
	{
		if (!is_stable(b.child[i]))
			return false;
		b.sum_x += b.child[i].sum_x;
		b.n += b.child[i].n;
	}

	int min_x = 2 * b.min_x;
	int max_x = 2 * b.max_x;
	int sum_x = 2 * b.sum_x;

	int gx = (sum_x + b.n) / b.n;
	int rem = (sum_x + b.n) % b.n;
	bool left = gx > min_x || gx == min_x && rem > 0;
	bool right = gx < max_x;
	return left && right;
}
int main()
{
	ios::sync_with_stdio(false);

	while (cin >> w >> h, w)
	{
		CL(visit, 0);
		CL(p, '.');
		for (int i = 0; i < w; ++i)
			p[0][i] = '0';
		for (int i = h; i > 0; --i)
			cin >> p[i];

		bool stable = true;
		for (int x = 0; x < w; ++x)
		{
			if (p[1][x] != '.' && !visit[1][x])
			{
				Block b = build(x, 1);
				stable &= is_stable(b);
			}
		}

		cout << (stable ? "STABLE" : "UNSTABLE") <<endl;
	}
}

Measured memory_bytes: 0