#include<bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int h, w;
char p[61][11];
int get_bottom_y(int y, int x, int key) {
    queue<pii> q;
    int reached[h][w]; rep(i, h) rep(j, w) reached[i][j] = 0;
    q.push({y, x});
    reached[y][x] = 1;
    int by = y;
    while (!q.empty()) {
        int yy = q.front().first, xx = q.front().second;
        q.pop();
        rep(k, 4) {
            int ny = yy + dy[k], nx = xx + dx[k];
            if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
            if (reached[ny][nx] || p[ny][nx] != p[y][x]) continue;
            reached[ny][nx] = 1;
            q.push({ny, nx});
            if (key == 1) chmax(by, ny);
            else chmin(by, ny);
        }
    }
    return by;
}
int limx(int y, int x) {
    queue<pii> q;
    int reached[h][w]; rep(i, h) rep(j, w) reached[i][j] = 0;
    q.push({y, x});
    reached[y][x] = 1;
    int lx = inf, rx = 0;
    if (y == h - 1) { //ground
        chmin(lx, x); chmax(rx, x);
    } else {
        if (p[y + 1][x] != p[y][x] && p[y + 1][x] != '.') chmin(lx, x); chmax(rx, x);
    }
    while (!q.empty()) {
        int yy = q.front().first, xx = q.front().second;
        q.pop();
        rep(k, 4) {
            int ny = yy + dy[k], nx = xx + dx[k];
            if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
            if (reached[ny][nx] || p[ny][nx] != p[y][x]) continue;
            //cout << "ny " << ny << " " << nx << endl;
            reached[ny][nx] = 1;
            q.push({ny, nx});
            if (ny == h - 1) { //ground
                chmin(lx, nx); chmax(rx, nx);
            } else {
                if (p[ny + 1][nx] == '.' || p[ny + 1][nx] == p[y][x]) continue;
                chmin(lx, nx); chmax(rx, nx);
            }
        }
    }
    //rep(k, h) {rep(l, w) cout << reached[k][l] << " "; cout << endl;}
    lx *= 10000;
    lx += rx;
    return lx;
}
int check[61][11];
void make_check(int y, int x, int c) {
    queue<pii> q;
    q.push({y, x});
    check[y][x] = c;
    while (!q.empty()) {
        int yy = q.front().first, xx = q.front().second;
        q.pop();
        rep(k, 4) {
            int ny = yy + dy[k], nx = xx + dx[k];
            if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
            if (check[ny][nx] || p[ny][nx] != p[y][x]) continue;
            check[ny][nx] = c;
            q.push({ny, nx});
        }
    }
}
int table[61 * 11][2];
int main() {
    while (cin >> w >> h) {
        if (w == 0 && h == 0) break;
        rep(i, h) rep(j, w) cin >> p[i][j];
        int flg = 0;
        rep(i, h) rep(j, w) check[i][j] = 0;
        int count = 1; //for check
        rep(i, h) {
            rep(j, w) {
                if (p[i][j] == '.' || check[i][j]) continue;
                //cout << i << " " << j << endl;
                make_check(i, j, count);
                count++;
                queue<pii> q; q.push({i, j});
                int reached[h][w]; rep(ii, h) rep(jj, w) reached[ii][jj] = 0;
                reached[i][j] = 1;
                double m = j + 0.5;
                int c = 1;
                int lx = limx(i, j);
                int rx = lx % 10000 + 1;
                lx /= 10000;
                while (!q.empty()) {
                    int y = q.front().first, x = q.front().second;
                    q.pop();
                    rep(k, 4) {
                        int ny = y + dy[k], nx = x + dx[k];
                        if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
                        if (reached[ny][nx] || p[ny][nx] == '.') continue;
                        //if (y == 29 && x == 2) cout << check[y][x] << " " << check[i][j] << " " << check[ny][nx] << endl;
                        if (check[y][x] == check[i][j] && check[ny][nx] != check[i][j] && k != 1) continue;
                        if (check[ny][nx] != check[y][x] && check[y][x] != check[i][j] && k != 1) continue;
                        //cout << y << " " << x << "   " << ny << " " << nx << endl;
                        reached[ny][nx] = 1;
                        q.push({ny, nx});
                        m += nx + 0.5;
                        c++;
                    }
                }
                //rep(k, h) {rep(l, w) cout << reached[k][l] << " "; cout << endl;}
                //cout << "m " << m << " " << c << endl;
                m /= c;
                //cout << lx << " " << rx << " " << m << endl;
                if (m <= lx || rx <= m) {
                    cout << "UNSTABLE" << endl;
                    flg = 1;
                    break;
                }
                
            } //rep(j, w)
            if (flg) break;
        } //rep(i, h)
        if (flg == 0) cout << "STABLE" << endl;
    } //while
   
}




Measured memory_bytes: 3088