#include <iostream>
#include <cassert>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <complex>
#include <cmath>

using namespace std;
typedef long long ll;

template <int N>
struct UnionFind {
    int ig[N];
    vector<int> gi[N];
    int gn;
    void init(int n = N) {
        for (int i = 0; i < n; ++i) {
            ig[i] = i;
            gi[i] = {i};
        }
        gn = n;
    }
 
    void merge(int a, int b) {
        if (same(a, b)) return;
        gn--;
        int x = ig[a], y = ig[b];
        if (gi[x].size() < gi[y].size()) swap(x, y);
        for (int j: gi[y]) {
            ig[j] = x;
        }
        gi[x].insert(gi[x].end(), gi[y].begin(), gi[y].end());
        gi[y].clear();
    }
 
    bool same(int a, int b) {
        return ig[a] == ig[b];
    }

    int group_num() {
        return gn;
    }
};

typedef long double R;
typedef pair<int, int> P;
const R EPS = 1e-10;
const R PI = acos((R)(-1));

/*
 -1 -> neg
  0 -> near 0
  1 -> pos
  */
int sgn(R a) {
    if (a < -EPS) return -1;
    if (a > EPS) return 1;
    return 0;
}

int sgn(R a, R b) {
    return sgn(b-a);
}
const int d4[4][2] = {
    {0, 1},
    {1, 0},
    {0, -1},
    {-1, 0}
};

const int MH = 100;
const int MW = 15;
const int MN = MH*MW;

UnionFind<MN> uf;
int w, h;
string s[MH];
int g[MH][MW];

int root[MN];
vector<int> ch[MN];
P rng[MN];
int wei[MN];
R pos[MN];
bool bc(int x, int y) {
    return (0 <= x && x < w && 0 <= y && y < h);
}
int xy2id(int x, int y) {
    return y * w + x;
}
P id2xy(int id) {
    return P(id % w, id / w);
}

bool solve(int id) {
    int we = 4;
    R t = 0;
    for (int p : uf.gi[id]) {
        P pp = id2xy(p);
        t += pp.first+0.5;
    }
    for (int d: ch[id]) {
        if (!solve(d)) return false;
        we += wei[d];
        t += pos[d];
    }
    R po = t / we;
    if (sgn(po, rng[id].first) != -1) return false;
    if (sgn(rng[id].second, po) != -1) return false;
    wei[id] = we;
    pos[id] = t;
    return true;
}

int main() {
    while (true) {
        fill_n(rng, MN, P(100, -1));
        fill_n(root, MN, -1);
        for (int i = 0; i < MN; i++) {
            ch[i].clear();
        }
        uf.init();
        cin >> w >> h;
        if (!w && !h) break;
        for (int i = 0; i < h; i++) {
            cin >> s[i];
        }
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (s[y][x] == '.') continue;
                for (int i = 0; i < 4; i++) {
                    int nx = x + d4[i][0], ny = y + d4[i][1];
                    if (!bc(nx, ny)) continue;
                    if (s[y][x] == s[ny][nx]) {
                        uf.merge(xy2id(x, y), xy2id(nx, ny));
                    }
                }
            }
        }
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (s[y][x] == '.') {
                    g[y][x] = -1;
                    continue;
                }
                g[y][x] = uf.ig[xy2id(x, y)];
            }
        }
/*        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                printf("%3d ", g[y][x]);
            } printf("\n");
        }*/
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (g[y][x] == -1) continue;
                int id = uf.ig[g[y][x]];
                int ny = y+1, nx = x;
                if (y == h-1) {
                    rng[id].first = min(rng[id].first, x);
                    rng[id].second = max(rng[id].second, x+1);
                    continue;
                }
                if (g[ny][nx] == -1) continue;
                int nid = uf.ig[g[ny][nx]];
                if (nid == id) continue;
                if (root[id] == -1) {
                    root[id] = nid;
                    ch[nid].push_back(id);
                }
                rng[id].first = min(rng[id].first, x);
                rng[id].second = max(rng[id].second, x+1);
            }
        }
        bool f = false;
        for (int i = 0; i < MN; i++) {
            if (uf.gi[i].size() == 4 && root[i] == -1) {
                f = solve(i);
            }
        }
        if (f) {
            cout << "STABLE" << endl;
        } else {
            cout << "UNSTABLE" << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 1396