#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

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

P merge(P m1, P m2) {
    return P(m1.first + m2.first, m1.second + m2.second);
}
bool check(ll xsum, ll w, ll l, ll r) {
    xsum *= 2;
    l = l * 2 - 1;
    r = r * 2 + 1;
    // l < xsum/w < r
    return l*w < xsum && xsum < r*w;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll w, h;
    while (cin >> w >> h, w || h) {
        vector<string> m(h); cin >> m;
        ll n = 0;
        vector<vector<ll>> g(h, vector<ll>(w, -1));
        auto inRange = [&](ll x, ll y) {
            return 0 <= x && x < w && 0 <= y && y < h;
        };
        function<void(ll, ll, ll)> fill = [&](ll x, ll y, ll id) {
            assert(id >= 0);
            g[y][x] = id;
            // cout << x << " " << y << endl;
            rep(d, 4) {
                ll nx = x + dx[d];
                ll ny = y + dy[d];
                if (inRange(nx, ny) && m[ny][nx] == m[y][x] && g[ny][nx] < 0) {
                    fill(nx, ny, id);
                }
            }
        };
        rep(y, h) rep(x, w) {
            if (g[y][x] < 0 && m[y][x] != '.') {
                fill(x, y, n);
                ++n;
            }
        }
        vector<ll> par(n, -1);
        rep(y, h-1) rep(x, w) {
            ll id1 = g[y][x];
            ll id2 = g[y+1][x];
            if (id1 < 0 || id2 < 0) continue;
            if (id1 != id2) {
                par[id1] = id2;
            }
        }
        ll root = -1;
        vector<vector<ll>> G(n);
        rep(i, n) {
            if (par[i] < 0) root = i;
            else G[par[i]].pb(i);
        }
        assert(root >= 0);
        vector<ll> xl(n, linf), xr(n, -linf);
        rep(y, h) rep(x, w) {
            if (g[y][x] >= 0 && (y == h-1 || (g[y+1][x] >= 0 && g[y][x] != g[y+1][x]))) {
                chmin(xl[g[y][x]], x);
                chmax(xr[g[y][x]], x);
            }
        }
        vector<P> mat(n, P(0, 0)); // xsum, wsum
        rep(y, h) rep(x, w) {
            ll id = g[y][x];
            if (id < 0) continue;
            mat[id] = merge(mat[id], P(x, 1));
        }
        vector<P> dp(n);
        function<P(ll)> dfs = [&](ll v) {
            P res = mat[v];
            each(to, G[v]) {
                res = merge(res, dfs(to));
            }
            return dp[v] = res;
        };
        dfs(root);
        // cout << g << endl;
        // rep(i, n) {
        //     cout << ":" << dp[i].first << " " << dp[i].second << " " << xl[i] << " " << xr[i] << endl;
        // }
        try {
            rep(i, n) {
                if ( !check(dp[i].first, dp[i].second, xl[i], xr[i]) ) {
                    throw 1;
                }
            }
            cout << "STABLE" << endl;
        }
        catch (int err) {
            cout << "UNSTABLE" << endl;
        }
    }
}

Measured memory_bytes: 3180