#include <bits/stdc++.h>
#define REP(i,s,e) for(int i=(s); i<(e);i++)
#define rep(i,n) REP(i,0,n)
#define rep1(i,n) REP(i,1,n)
#define repe(i,n) for(auto &&i:n)
#define all(v) (v).begin(),(v).end()
#define mp(x,y) make_pair(x,y)
#define decimal fixed<<setprecision(20)
#define fastcin() cin.tie(0);ios::sync_with_stdio(false)
using namespace std;
using LL = long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = 1e9;
const LL LLINF = 1e16;

int W, H;
char s[100][100];
bool used[100][100];
bool f = 1;

tuple<int, int, int> bfs(pair<int, int> now, set<pair<int, int>> &nxt){
    int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
    queue<pair<int, int>> que;
    que.push(now);
    int g = 0;
    set<pair<int, int>> nowp, nowp_under;

    while(!que.empty()) {
        pair<int, int> p = que.front();
        used[p.first][p.second] = 1;
        que.pop();
        nowp.insert(p);
        if(p.first == H-1 || (s[p.first][p.second] != s[p.first+1][p.second] && s[p.first+1][p.second] != '.')) nowp_under.insert(p);
        for(int i = 0; i < 4; i++) {
            char nowc = s[now.first][now.second];
            int ny = p.first + dy[i], nx = p.second + dx[i];
            if(nx < 0 || ny < 0 || nx >= W || ny >= H) continue;
            if(used[ny][nx]) continue;

            if(s[ny][nx] == nowc) que.push(mp(ny, nx));
            // nowの真上のブロックが異なるブロック
            if(i == 2 && s[ny][nx] != '.' && s[ny][nx] != nowc) nxt.insert(mp(ny, nx));
        }
    }
    int l = INF, r = -INF;
    repe(i, nowp_under){
        chmin(l, i.second);
        chmax(r, i.second);
    }
    repe(i, nowp) g += i.second;
    // 自分の重心と, 自分の下端
    return tie(g, l, r);
}

pair<int, int> rec(pair<int, int> now){
    set<pair<int, int>> nxt;
    int g, l, r;
    tie(g, l, r) = bfs(now, nxt);
    int tmp = 1;
    repe(i, nxt){
        if(!used[i.first][i.second]){
            auto j = rec(i);
            g += j.first;
            tmp += j.second;
        }
    }
    // cout << now.first << ":" << now.second << "=" << 1.*g/4/tmp << "/" << 0.+l-0.5 << "," << 0.+r+0.5 << " " << tmp << endl;
    if(!((4*l-2)*tmp < g && g < (4*r+2)*tmp)){
        f = 0;
    }
    return mp(g, tmp);
}

int main(){
    cin >> W >> H;
    while(W > 0){
        rep(i, H)rep(j, W){
            cin >> s[i][j];
        }
        memset(used, 0, sizeof(used));
        f = 1;

        rep(i, W){
            if(s[H-1][i] != '.'){
                rec(mp(H-1, i));
                break;
            }
        }
        cout << (f ? "STABLE" : "UNSTABLE") << endl;
        cin >> W >> H;
    }
}
