#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<ll> Size;
    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1);
        for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        Size[x] += Size[y];
        return true;
    }

    ll size(int x){
        return Size[root(x)];
    }
};

int W, H;
int field[65][65];
int root;
map<int, int> parent;
map<int, vector<int> > children;
map<int, vector<i_i> > grids;
bool clear;
map<int, int> wsum;
map<int, int> wnum;
void dfs(int from) {
    for(int i = 0; i < grids[from].size(); i++) {
        i_i next = grids[from][i];
        next.first++;
        if(next.first > H) continue;
        if(field[next.first][next.second] == -2) continue;
        if(field[next.first][next.second] == from) continue;
        //if(field[next.first][next.second] == parent[from]) continue;
        if(parent[field[next.first][next.second]] != -1) continue;
        parent[field[next.first][next.second]] = from;
        children[from].push_back(field[next.first][next.second]);
        dfs(field[next.first][next.second]);
    }
    //cerr << from << endl;
    //cerr << "parent: " << parent[from] << endl;
    //cerr << "children: ";
    //for(int i = 0; i < children[from].size(); i++) cerr << children[from][i] << " ";
    //cerr << endl;
}

void dfs2(int from) {
    wsum[from] = 0;
    wnum[from] = 4;
    int wmax = -INF;
    int wmin = INF;
    for(int i = 0; i < grids[from].size(); i++) {
        int wnow = grids[from][i].second;
        int hnow = grids[from][i].first;
        wsum[from] += wnow;
        if(field[hnow-1][wnow] == parent[from] || (parent[from] == -1 && hnow == 1)) wmax = max(wmax, wnow);
        if(field[hnow-1][wnow] == parent[from] || (hnow == 1 && parent[from] == -1)) wmin = min(wmin, wnow);
    }
    for(int i = 0; i < children[from].size(); i++) {
        int to = children[from][i];
        dfs2(to);
        wsum[from] += wsum[to];
        wnum[from] += wnum[to];
    }
    if((wmin - 1) * 2 * wnum[from] >= 2 * wsum[from] - wnum[from] || (wmax * 2) * wnum[from] <= 2 * wsum[from] - wnum[from]) {
        clear = false;
        //cerr << "FAIL: " << from << endl;
    }
    //cerr << from << " " << wnum[from] << " " << wsum[from] << " " << wmin << " " << wmax << endl;
}

UnionFind uni;
int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0, -1};

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true) {
        clear = true;
        cin >> W >> H;
        if(W == 0) break;
        for(int i = 0; i <= 60000; i++) {
            children[i].clear();
            parent[i] = -1;
            grids[i].clear();
        }
        for(int h = 1; h <= 61; h++) {
            for(int w = 1; w <= W; w++) field[h][w] = 0;
        }
        for(int h = H; h >= 1; h--) {
            string now;
            cin >> now;
            now = "#" + now;
            for(int i = 1; i <= W; i++) field[h][i] = now[i] - '0';
            /*
            for(int w = 1; w <= W; w++) {
                if(now[w] != '.') grids[field[h][w]].push_back({h, w});
            }
            */
        }
        uni.init(100000);
        for(int h = 1; h <= H; h++) {
            for(int w = 1; w <= W; w++) {
                //cerr << h << " " << w << endl;
                if(field[h][w] == (int)('.' - '0')) continue;
                if(h < H) {
                    if(field[h][w] == field[h+1][w]) uni.merge(h*100+w, h*100+100+w);
                }
                if(w < W) {
                    if(field[h][w] == field[h][w+1]) uni.merge(h*100+w, h*100+w+1);
                }
            }
        }
        for(int h = 1; h <= H; h++) {
            for(int w = 1; w <= W; w++) {
                if(uni.size(h*100+w) == 1) continue;
                field[h][w] = uni.root(h*100+w);
            }
        }
        //cout << (int)('.') << endl;
        for(int h = H; h >= 1; h--) {
            for(int w = 1; w <= W; w++) {
                if(field[h][w] != '.') grids[field[h][w]].push_back({h, w});
                //cerr << field[h][w] << " ";
            }
            //cerr << endl;
        }
        for(int w = 1; w <= W; w++) {
            if(field[1][w] != -2) root = field[1][w];
        }
        //cerr << root << endl;
        dfs(root);
        /*
        for(char a = '0'; a <= '9'; a++) {
            //cerr << a << " " << parent[a] << endl;
        }
        */
        dfs2(root);
        if(clear) cout << "STABLE" << endl;
        else cout << "UNSTABLE" << endl;
    }
    return 0;
}


Measured memory_bytes: 16908