#include <bits/stdc++.h>
using namespace std;

// template {{{
#define  range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)
#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

// constexpr i32 mod   = 998244353;
constexpr i32 mod   = 1e9 + 7;
constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

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

struct IoSetup { IoSetup(int x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}

// union find {{{
class union_find {

    using data_type = int_fast32_t;
    std::vector< data_type > data_;

    public:
    union_find(std::size_t size) : data_(size, -1) {}

    std::size_t size() const {
        return data_.size();
    }

    data_type get_root(data_type x) {
        return (data_[x] < 0 ? x : data_[x] = get_root(data_[x]));
    }

    bool is_root(data_type x) {
        return x == get_root(x);
    }

    bool is_same(data_type x, data_type y) {
        return get_root(x) == get_root(y);
    }

    void unite(data_type x, data_type y) {
        x = get_root(x); y = get_root(y);
        if (x == y) return;

        if (data_[x] > data_[y]) std::swap(x, y);
        data_[x] += data_[y];
        data_[y] = x;
    }

    data_type element_count(data_type x) {
        return -data_[get_root(x)];
    }
};
// }}}

void solver(int w, int h) {
    vector< string > p(h);
    for (auto &s: p) cin >> s;

    union_find uft(w * h);
    range(i, 1, h) range(j, 0, w) {
        if (p[i][j] == '.') continue;
        if (p[i][j] == p[i - 1][j]) {
            uft.unite(i * w + j, (i - 1) * w + j);
        }
    }

    range(i, 0, h) range(j, 1, w) {
        if (p[i][j] == '.') continue;
        if (p[i][j] == p[i][j - 1]) {
            uft.unite(i * w + j, i * w + j - 1);
        }
    }

    auto as = make_vector(h, w, -1);
    int idx = 0;
    range(i, 0, h) range(j, 0, w) {
        if (p[i][j] == '.') continue;
        int v = i * w + j;
        if (uft.is_root(v)) {
            as[i][j] = idx;
            idx++;
        }
    }

    range(i, 0, h) range(j, 0, w) {
        int rt = uft.get_root(i * w + j);
        int y = rt / w;
        int x = rt % w;

        as[i][j] = as[y][x];
    }

    auto xs = make_vector(idx, w, 0);
    range(i, 0, h) range(j, 0, w) {
        if (as[i][j] == -1) continue;
        xs[as[i][j]][j]++;
    }

    union_find uf(idx);
    vector< vector< int > > g(idx);

    range(i, 1, h) range(j, 0, w) {
        if (as[i - 1][j] == -1) continue;
        if (as[i][j] == -1) continue;

        int from = as[i][j];
        int to = as[i - 1][j];
        if (from != to && !uf.is_same(from, to)) {
            uf.unite(from, to);
            g[from].push_back(to);
        }
    }

    auto check = [&](vector< int > x, int xL, int xR) {
        int cnt = whole(accumulate, x, 0);
        int M = 0;
        range(i, 0, x.size()) {
            M += (2 * i + 1) * x[i];
        }

        if ((xL * cnt < M) && (M < xR * cnt)) return true;
        return false;
    };

    function< pair<bool, vector<int>>(int)> dfs = [&](int v) {
        vector< int > x = xs[v];

        int xL = inf, xR = -1;
        range(i, 1, h + 1) range(j, 0, w) {
            if (as[i - 1][j] != v) continue;
            if (i == h || (as[i][j] != -1 && as[i][j] != v)) {
                chmin(xL, 2 * j);
                chmax(xR, 2 * (j + 1));
            }
        }

        for (int to: g[v]) {
            auto v = dfs(to);
            if (v.first == false) return make_pair(false, x);
            range(i, 0, w) {
                x[i] += v.second[i];
            }
        }

        bool f = check(x, xL, xR);
        return make_pair(f, x);
    };

    range(j, 0, w) {
        if (as.back()[j] == -1) continue;
        if (dfs(as.back()[j]).first) {
            cout << "STABLE" << endl;
        } else {
            cout << "UNSTABLE" << endl;
        }
        return;
    }
}

signed main(int argc, char *argv[]) {
    int w, h;
    while (cin >> w >> h, w) {
        solver(w, h);
    }
}



Measured memory_bytes: 3132