#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int di[] = {1, 0, 0, -1}, dj[] = {0, 1, -1, 0};

int h, w;
vector<vector<int>> field, copyf;
bool flag;

pair<int, int> dfs(int i, int j) {
    vector<pair<int, int>> nxt;
    queue<pair<int, int>> q;
    int cur = field[i][j]; field[i][j] = 0; q.emplace(i, j);
    int cnt_x = 0, cnt_w = 0, xL = w, xR = 0;
    while (!q.empty()) {
        auto p = q.front(); q.pop();
        int i = p.first, j = p.second;
        cnt_x += j; cnt_w++;
        if (copyf[i - 1][j] && copyf[i - 1][j] != cur) {
            xL = min(xL, j); xR = max(xR, j + 1);
        }
        for (int k = 0; k < 4; k++) {
            int ni = i + di[k], nj = j + dj[k];
            if (ni <= 0 || ni >= h + 1 || nj < 0 || nj >= w) continue;
            if (field[ni][nj] == cur) {
                field[ni][nj] = 0; q.emplace(ni, nj);
            } else {
                if (k == 0 && field[ni][nj]) nxt.emplace_back(ni, nj);
            }
        }
    }
    for (auto p : nxt) if (field[p.first][p.second]) {
        auto res = dfs(p.first, p.second);
        cnt_x += res.first;
        cnt_w += res.second;
    }
    double M = 1.0 * cnt_x / cnt_w + 0.5;
    if (not(xL < M && M < xR)) flag = false;
    return {cnt_x, cnt_w};
}

int main() {
    while (cin >> w >> h, w) {
        flag = true;
        field.assign(h + 1, vector<int>(w, 0));
        copyf.assign(h + 1, vector<int>(w, 0));
        for (int i = 0; i < h; i++) {
            string s; cin >> s;
            for (int j = 0; j < w; j++) if ('1' <= s[j] && s[j] <= '9') {
                copyf[h - i][j] = field[h - i][j] = s[j] - '0';
            }
        }
        for (int j = 0; j < w; j++) copyf[0][j] = -1;
        for (int j = 0; j < w; j++) if (field[1][j]) {
            dfs(1, j);
            break;
        }
        puts(flag ? "STABLE" : "UNSTABLE");
    }
    return 0;
}
