// #define DEBUGGING
#include <bits/stdc++.h>
#define endl '\n'
#define ALL(V) begin(V), end(V)
#define ALLR(V) rbegin(V), rend(V)
using ll = std::int64_t;
using ull = std::uint64_t;
using PLL = std::pair<ll, ll>;
using TLL = std::tuple<ll, ll, ll>;
template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); }
namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; }
#define mv_rec make_v(init, tail...)
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail> auto make_v(T init, size_t s, Tail... tail) { return V<decltype(mv_rec)>(s, mv_rec); }
#undef mv_rec
using namespace std;

#ifdef DEBUGGING
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

const size_t SIZE = 10;
const size_t SSIZE = 610;
int cnt_x[SSIZE][SIZE];
V<int> graph[SSIZE];
pair<int, int> lr[SSIZE];

void dfs(int cn, int *cnt) {
    for (int i = 0; i < SIZE; i++) cnt[i] += cnt_x[cn][i];
    for (int nxt : graph[cn]) dfs(nxt, cnt);
}

bool judge(int n) {
    if (accumulate(ALL(cnt_x[n]), 0) == 0) return true;
    int cnt[SIZE] = {};
    dfs(n, cnt);
    int sum = 0, cen = 0;
    for (int i = 0; i < SIZE; i++) {
        sum += cnt[i];
        cen += (2 * i + 1) * cnt[i];
    }
    int l, r;
    tie(l, r) = lr[n];
    r++;
    l *= 2 * sum;
    r *= 2 * sum;
    // cout << n << ", " << l << ", " << r << ", " << cen << ", " << sum << endl;
    return l < cen && cen < r;
}

struct UnionFind {
    V<int> rank, par;

    UnionFind(size_t n) : rank(n, 1), par(n) {
        iota(ALL(par), 0);
    }

    int find(int x) {
        return x == par[x] ? x : par[x] = find(par[x]);
    }

    bool unit(int x, int y) {
        int px = find(x), py = find(y);
        if (px == py) return false;
        par[py] = px;
        if (rank[px] == rank[py]) rank[px]++;
        return true;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
};

bool solve() {
    int W, H;
    cin >> W >> H;
    if (!(H + W)) return false;
    auto grid = make_v<int>(0, H, W);
    for (int i = 0; i < H; i++) {
        string s;
        cin >> s;
        for (int j = 0; j < W; j++) {
            if (s[j] != '.') grid[i][j] = s[j] - '0';
            else grid[i][j] = 0;
        }
    }

    for (auto &&e : cnt_x) fill(ALL(e), 0);
    for (auto &&e : graph) e.clear();
    fill(ALL(lr), make_pair(100, -100));
    
    int id[SSIZE] = {};
    {
        UnionFind uf(H * W);
        int dh[] = { 1, 0, -1, 0, };
        int dw[] = { 0, 1, 0, -1, };
        for (int h = 0; h < H; h++) for (int w = 0; w < W; w++) {
            if (!grid[h][w]) continue;
            for (int k = 0; k < 4; k++) {
                int nh = h + dh[k], nw = w + dw[k];
                if (!(0 <= nh && nh < H &&
                      0 <= nw && nw < W)) continue;
                if (grid[nh][nw] == grid[h][w]) uf.unit(h * W + w, nh * W + nw);
            }
        }
        for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) if (grid[i][j]) id[i * W + j] = uf.find(i * W + j) + 1;
        for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) if (grid[i][j]) grid[i][j] = id[i * W + j];
    }

    // for (auto &&e : grid) { for (auto &&f : e) cout << f; cout << endl; }

    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) {
        int u = grid[i][j];
        if (!u) continue;
        cnt_x[u][j]++;
        bool has_d = (i + 1 == H);
        if (i + 1 < H) {
            int d = grid[i + 1][j];
            if (d) {
                if (u != d) {
                    graph[d].push_back(u);
                    has_d = true;
                }
            }
        }
        if (has_d) {
            int l, r;
            tie(l, r) = lr[u];
            chmin(l, j);
            chmax(r, j);
            lr[u] = make_pair(l, r);
        }
    }

    for (auto &&e : graph) {
        sort(ALL(e));
        auto ite = unique(ALL(e));
        e.erase(ite, end(e));
    }

    bool ok = true;
    for (int i = 1; i < SSIZE; i++) ok &= judge(i);
    if (!ok) cout << "UN";
    cout << "STABLE" << endl;
    return true;
}

int main() {
    while (solve());
    return 0;
}



Measured memory_bytes: 3300