#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0, -1};
 
map<l_l, ll> srch;
ll cnt = 0;
vector<pair<l_l, ll> > par;
vector<l_l> g;
vector<vector<char> > p;
vector<vector<char> > aho;
vector<vector<ll> > child;
vector<l_l> side;
ll w, h;
 
l_l rec(char c, l_l po) {
    p[po.first][po.second] = '/';
    srch[l_l(po.first, po.second)] = cnt;
    g[cnt].first += po.second;
    if(po.first == 0 || (p[po.first - 1][po.second] != '.' && aho[po.first][po.second] != aho[po.first - 1][po.second])) {
        side[cnt].first = min(side[cnt].first, po.second);
        side[cnt].second = max(side[cnt].second, po.second);
    }
    l_l res = l_l(-1, -1);
    if(po.first - 1 >= 0 && p[po.first - 1][po.second] != '.' && aho[po.first - 1][po.second] != aho[po.first][po.second]) res = l_l(po.first - 1, po.second);
    REP(i, 4) {
        ll nh = po.first + dh[i];
        ll nw = po.second + dw[i];
        if(nh >= 0 && nh < h && nw >= 0 && nw < w && p[nh][nw] == c) {
            res = max(res, rec(c, i_i(nh, nw)));
        }
    }
    return res;
}
 
bool d;
 
void dfs(ll n) {
    REP(i, child[n].size()) {
        dfs(child[n][i]);
        g[n].first += g[child[n][i]].first;
        g[n].second += g[child[n][i]].second;
    }
    if((2 * side[n].first * g[n].second >= 2 * g[n].first + g[n].second || 2 * g[n].first + g[n].second >= 2 * (side[n].second + 1) * g[n].second)) {
        d = false;
    }
}
 
int main(void) {
    while(true) {
        srch.erase(srch.begin(), srch.end());
        cnt = 0;
        par.resize(0);
        g.resize(0);
        d = true;
        child.resize(0);
        side.resize(0);
 
        cin>>w>>h;
        if(w == 0 && h == 0) break;
        p.resize(h);
        REP(i, h) p[i].resize(w);
        REP(i, h) {
            REP(j, w) cin>>p[i][j];
        }
        aho.resize(h);
        REP(i, h) aho[i].resize(w);
        REP(i, h) {
            REP(j, w) {
                aho[i][j] = p[i][j];
            }
        }
        reverse(ALL(aho));
        reverse(ALL(p));
        REP(i, h) {
            REP(j, w) {
                if(p[i][j] != '.' && p[i][j] != '/') {
                    g.pb(l_l(0, 4));
                    side.pb(l_l(INF, -INF));
                    par.pb(pair<l_l, ll>(rec(p[i][j], i_i(i, j)), cnt));
                    if(i == 0) par[0].first = l_l(-1, -1);
                    ++cnt;
                }
            }
        }
        vector<l_l> parhoge(par.size());
        REP(i, par.size()) {
            if(par[i].first != l_l(-1, -1)) parhoge[i].first = srch[par[i].first];
            else parhoge[i].first = -1;
            parhoge[i].second = par[i].second;
        }
        child.resize(cnt);
        REP(i, parhoge.size()) {
            if(parhoge[i].first != -1) child[parhoge[i].first].pb(parhoge[i].second);
        }
        dfs(0);
        if(d) cout<<"STABLE"<<endl;
        else cout<<"UNSTABLE"<<endl;
    }
}


Measured memory_bytes: 3152