#include <iostream>
#include <vector>
#include <utility>
using namespace std;
const int inf = 1e9;
const double EPS = 1e-10;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void fill(int y, int x, int c, int &l, int &r, int &sx, int &nb, vector<pair<int,int> > &top, vector<vector<char> > &field){
    field[y][x] = 'o';
    sx += x;
    nb ++;
    if(field[y-1][x] != c && '1' <= field[y-1][x] && field[y-1][x] <= '9'){
        top.push_back(make_pair(y-1, x));
    }
    if(field[y+1][x] == 'x'){
        l = min(l, x);
        r = max(r, x);
    }
    for(int i=0; i<4; i++){
        if(field[y+dy[i]][x+dx[i]] == c){
            fill(y+dy[i], x+dx[i], c, l, r, sx, nb, top, field);
        }
    }
}
void cleanup(int y, int x, vector<vector<char> > &field){
    field[y][x]='x';
    for(int i=0; i<4; i++){
        if(field[y+dy[i]][x+dx[i]] == 'o'){
            cleanup(y+dy[i], x+dx[i], field);
        }
    }
}
 
pair<int, int> solve(int y, int x, vector<vector<char> > &field){
    if(field[y][x] == 'x') return make_pair(0, 0);
    int color = field[y][x];
    int left=inf, right=-inf;
    int sumx=0, numblock=0;
    vector<pair<int,int> > top;
    fill(y, x, color, left, right, sumx, numblock, top, field);
    cleanup(y, x, field);

    for(int i=0; i<(int)top.size(); i++){
        pair<int,int> ret = solve(top[i].first, top[i].second, field);
        sumx += ret.first;
        numblock += ret.second;
        if(ret.first == -1) return make_pair(-1,-1);
    }
    
    double centroid = (double)sumx/numblock;
    if(centroid-EPS < left-0.5 || right+0.5 < centroid+EPS){
        return make_pair(-1, -1);
    }else{
        return make_pair(sumx, numblock);
    }
}
 
int main(){
    while(1){
        int w,h;
        cin >> w >> h;
        if(w==0) break;
         
        vector<vector<char> > field(h+3, vector<char>(w+2, '.'));
        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                cin >> field[i][j];
            }
        }

        int sx;
        for(int j=1; j<=w; j++){
            field[h+1][j] = 'x';
            if(field[h][j] != '.') sx = j;
        }
        if(solve(h, sx, field).second == -1){
            cout << "UNSTABLE" << endl;
        }else{
            cout << "STABLE" << endl;
        }
    }
    return 0;
}

