#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using uInt = long long unsigned int;
using Double = long double;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//edit
class Solve {
public:
    Int w, h;
    vector<vector<Int>> p;
    vector<vector<Int>> col;
    using Cood = pair<Int, Int>;
    vector<vector<Cood>> block; //(y, x)
    vector<Cood> center; // グループ[i]のセンター
    vector<vector<Int>> G;

    bool ok = true; // this is answer;

    void group() {
        // ブロックごとにidを割り振る
        // ついでに木を作る
        vector<vector<bool>> check(h + 1, vector<bool>(w, false));

        Int cnt = 0;
        auto paint = [&](auto self, int y, int x, int c) -> void {
            if (y < 0 || x < 0 || y > h || x >= w) return;
            if (check[y][x]) return;
            if (p[y][x] != c) return;
            check[y][x] = true;
            block[cnt].push_back(Cood(y, x));
            col[y][x] = cnt;

            int dy[] = {-1, 0, 1, 0};
            int dx[] = {0, -1, 0, 1};
            for (int k = 0; k < 4; ++k) {
                int ny = y + dy[k];
                int nx = x + dx[k];
                self(self, ny, nx, c);
            }
        };

        for (int y = 0; y <= h; ++y) {
            for (int x = 0; x < w; ++x) {
                if (!check[y][x] && p[y][x] >= 0) {
                    block.push_back(vector<Cood>());
                    paint(paint, y, x, p[y][x]);
                    cnt++;
                }
            }
        }

        // 木を作る
        G.resize(cnt);
        for (int y = 1; y <= h; ++y) {
            for (int x = 0; x < w; ++x) {
                if (col[y][x] != col[y - 1][x] && col[y][x] != -1 && col[y - 1][x] != -1) {
                    Int from = col[y - 1][x];
                    Int to = col[y][x];
                    G[from].push_back(to);
                }
            }
        }
        for (int i = 0; i < G.size(); ++i) {
            sort(all(G[i]));
            G[i].erase(unique(all(G[i])), G[i].end());
        }
        center.resize(cnt);
        for (int i = 0; i < cnt; ++i) {
            Int xl = INFl, xr = -INFl;
            Int yd = INFl, yu = -INFl;
            for (auto e : block[i]) {
                chmin(yd, e.first);
                chmax(yu, e.first + 1);
                chmin(xl, e.second);
                chmax(xr, e.second + 1);
            }
            center[i] = make_pair((yd + yu) / 2., (xl + xr) / 2.);
        }
    }

    // first : モーメント, second : 個数
    pair<Double, Int> dfs(Int v) {
        Double M = 0.0; //総モーメント
        Int C = 0; // 総個数

        //加算処理
        for (auto e : G[v]) {
            auto tmp = dfs(e);
            M += tmp.first;
            C += tmp.second;
        }

        // v == 0ならここでリターンでも良い
        if (v == 0) return make_pair(0.0, 0);

        //block[v]の考慮
        Int xl = INFl, xr = -INFl;
        bool added = false;
        for (auto e : block[v]) {
            Double x = e.second + 0.5;
//            Double x = center[col[e.first][e.second]].second;
//            if (!added) {
//                C++;
//                M += x;
//                added = true;
//            }
//            M += x / (block[v].size());
            M += x ;
            C++;

            if (col[e.first][e.second] != col[e.first - 1][e.second] && col[e.first - 1][e.second] != -1) {
                chmin(xl, e.second);
                chmax(xr, e.second + 1);
            }
        }
//        C++;


        //重心確認
        Double x = M / C;

        if (x <= xl || x >= xr) {
            ok = false;
        }


        return make_pair(M, C);
    }

    bool solve() {
        cin >> w >> h;
        if (w == 0) return false;

        // 1.グループ分け
        p.resize(h + 1, vector<Int>(w, -1));
        col.resize(h + 1, vector<Int>(w, -1));
        for (int i = 0; i < w; ++i) {
            p[0][i] = 0;
        }
        for (int i = h; i >= 1; --i) {
            for (int j = 0; j < w; ++j) {
                char c;
                cin >> c;
                if (c != '.') {
                    p[i][j] = c - '0';
                }
            }
        }
        group();


        // 2.dfsする
        dfs(0);

        if (ok) {
            cout << "STABLE" << endl;
        } else {
            cout << "UNSTABLE" << endl;
        }


        return true;
    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    while (Solve().solve());


    return 0;
}



Measured memory_bytes: 3240