#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) cerr<<(#v)<<":";for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;
#define printVS(vs) cerr<<(#vs)<<":"<<endl;for(auto(s):(vs)){cerr<<(s)<< endl;}
#define printVV(vv) cerr<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;}
#define printP(p) cerr<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cerr<<(#vp)<<":"<<endl;for(auto(p):(vp)){cerr<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cerr << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cerr << first << " "; output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1ll << 60;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

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

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int w, h;
    int testcase = 0;
    while (cin >> w >> h, w) {
        // output("testcase = ", ++testcase);

        vector<string> s(h);
        rep(i, h) cin >> s[i];

        // if (testcase != 4) continue;

        vector<vector<bool>> used(h, vector<bool>(w));
        vector<vector<Pii>> pieces;
        vector<int> xL;
        vector<int> xR;
        vector<double> gx;

        auto inside = [&](int x, int y) {
            return 0 <= x && x < h && 0 <= y && y < w;
        };

        auto findBlock = [&](int sx, int sy) {
            // output("findBlock", sx, sy);
            queue<pair<int, int>> que;
            que.emplace(sx, sy);
            used[sx][sy] = true;

            vector<Pii> piece;
            int xl = inf, xr = -inf;
            double g = 0.;

            while (!que.empty()) {
                int x, y;
                tie(x, y) = que.front(); que.pop();
                piece.emplace_back(x, y);
                // output("x, y = ", x, y);

                if (x == h - 1 || (s[x + 1][y] != s[x][y] && s[x + 1][y] != '.')) {
                    chmin(xl, y);
                    chmax(xr, y + 1);
                }
                g += y + 0.5;

                rep(k, 4) {
                    int nx = x + dx[k], ny = y + dy[k];
                    if (inside(nx, ny) && s[nx][ny] == s[x][y] && !used[nx][ny]) {
                        que.emplace(nx, ny);
                        used[nx][ny] = true;
                    }
                }
            }

            pieces.emplace_back(piece);
            xL.emplace_back(xl);
            xR.emplace_back(xr);
            gx.emplace_back(g / 4.);
        };

        rep(i, h) {
            rep(j, w) {
                if (s[i][j] == '.' || used[i][j]) continue;
                findBlock(i, j);
            }
        }

        // rep(i, pieces.size()) {
        //     output("i =", i);
        //     for (auto p : pieces[i]) {
        //         output(p.first, p.second);
        //     }
        // }

        vector<vector<int>> b(h, vector<int>(w, -1)); // s in int
        int n = pieces.size();
        int root_id;

        rep(i, n) {
            for (auto p : pieces[i]) {
                b[p.first][p.second] = i;
                if (p.first == h - 1) {
                    root_id = i;
                }
            }
        }

        // printVV(b);
        // printV(xL);
        // printV(xR);
        // printV(gx);

        vector<set<int>> G_set(n);
        rep(i, h - 1) {
            rep(j, w) {
                if (b[i][j] != -1 && b[i + 1][j] != -1 && b[i][j] != b[i + 1][j]) {
                    G_set[b[i + 1][j]].insert(b[i][j]);
                }
            }
        }

        Graph G(n);
        rep(i, n) G[i] = vector<int>(all(G_set[i]));
        // printVV(G);

        vector<tuple<double,int,bool>> res(n);

        function<tuple<double,int,bool>(int)> dfs = [&](int v) {
            double sm = gx[v];
            int num = 1;
            bool flag = true;
            for (auto ch : G[v]) {
                double sm_ch;
                int num_ch;
                bool flag_ch;
                tie(sm_ch, num_ch, flag_ch) = dfs(ch);
                if (!flag_ch) {
                    return res[v] = make_tuple(0., 0ll, false);
                }
                sm += sm_ch;
                num += num_ch;
            }

            double M = sm / num;
            // output("v =", v, ", M =", M);
            if (!(xL[v] < M && M < xR[v])) return res[v] = make_tuple(0., 0ll, false);
            return res[v] = make_tuple(sm, num, flag);
        };

        double sm;
        int num;
        bool flag;
        tie(sm, num, flag) = dfs(root_id);

        // output("res:");
        // rep(i, n) {
        //     double sm;
        //     int num;
        //     bool flag;
        //     tie(sm, num, flag) = res[i];
        //     output(i, ":", sm, num, flag);
        // }

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

Measured memory_bytes: 3140