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

using ll = long long;
constexpr int INF = 1e9;

using pdd = pair<double, double>;
using pii = pair<int, int>;

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

// (M, xsum);
pdd solve(int y, int x, vector<string> const& v, vector<vector<bool>>& visited, bool& res) {
    queue<pii> que;
    que.push(make_pair(y, x));
    visited[y][x] = true;
    const char color = v[y][x];
    const int H = v.size();
    const int W = v[0].size();
    double minx = 1e9, maxx = -1e9;
    double aboveM = 0, aboveX = 0;
    double nowM = 0, nowX = 0;
    while(!que.empty()) {
        pii p = que.front();
        que.pop();
        if(p.first == 0 || v[p.first-1][p.second] != '.' && v[p.first-1][p.second] != color) {
            minx = min(minx, (double)p.second);
            maxx = max(maxx, (double)p.second+1);
        }
        nowM += 1;
        nowX += p.second + 0.5;
        for(int i=0; i<4; ++i) {
            int ny = p.first + dy[i];
            int nx = p.second + dx[i];
            if(ny < 0 || H <= ny || nx < 0 || W <= nx || v[ny][nx] == '.' || visited[ny][nx]) {
                continue;
            }
            if(ny < H && ny == p.first+1 && !visited[ny][nx] && v[ny][nx] != color) {
                auto t = solve(ny, nx, v, visited, res);
                aboveM += t.first;
                aboveX += t.second;
            }
            if(v[ny][nx] == color) {
                que.push(make_pair(ny, nx));
                visited[ny][nx] = true;
            }
        }
    }
    double totalX = aboveX + nowX;
    double totalM = aboveM + nowM;
    double g = totalX / totalM;
    res &= (minx < g && g < maxx);
    return make_pair(totalM, totalX);
}


int main() {
    int W, H;
    while(cin >> W >> H, W) {
        vector<string> v(H);
        for(int i=0; i<H; ++i) {
            cin >> v[H-i-1];
        }
        bool res = true;
        vector<vector<bool>> visited(H, vector<bool>(W));
        for(int i=0; i<H; ++i) {
            for(int j=0; j<W; ++j) {
                if(v[i][j] != '.' && !visited[i][j]) {
                    solve(i, j, v, visited, res);
                }
            }
        }
        if(res) {
            cout << "STABLE" << endl;
        } else {
            cout << "UNSTABLE" << endl;
        }
    }
}
