#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
typedef pair<int, int>P;

bool stable;
char mp[62][12];
int flg[62][12];
void initMp(int w, int h){
    for(int y=0;y<62;y++){
        for(int x=0;x<12;x++){
            mp[y][x]='.';
            flg[y][x]=0;
        }
    }
    for(int y=1;y<=h;y++){
        for(int x=1;x<=w;x++){
            cin>>mp[y][x];
        }
    }
    for(int x=1;x<=w;x++){
        mp[h+1][x] = 'G';
    }
}

P solve(int y, int x){
    int weight = 0;
    int xSum = 0;
    
    int xl = 20, xr = -10;

    int dx[4]={0, 1, 0, -1};
    int dy[4]={1, 0, -1, 0};
    queue<int>X, Y;

    X.push(x);
    Y.push(y);
    flg[y][x]=1;

    while(!X.empty()){
        x = X.front();
        y = Y.front();
        X.pop();
        Y.pop();
        xSum += x;
        weight++;
        if(mp[y+1][x]!='.' && mp[y+1][x]!=mp[y][x]){
            xl = min(xl, x);
            xr = max(xr, x);
        }
        for(int i=0;i<4;i++){
            if(flg[y+dy[i]][x+dx[i]]==0 && mp[y+dy[i]][x+dx[i]]==mp[y][x]){
                flg[y+dy[i]][x+dx[i]]=1;
                X.push(x+dx[i]);
                Y.push(y+dy[i]);
            }
        }
        if(mp[y-1][x]!='.' && mp[y-1][x]!=mp[y][x] && flg[y-1][x]==0){
            P p = solve(y-1, x);
            xSum += p.first;
            weight += p.second;
        }
    }
    if((2*xSum <= weight * (2*xl-1) ) || (weight * (2*xr+1) <= 2*xSum))stable = false;
    return P(xSum, weight);
}

int main(){

    for(int w, h;cin>>w>>h,w;){
        initMp(w, h);
        stable = true;
        for(int x=1;x<=w;x++){
            if(mp[h][x]!='.'){
                solve(h, x);
                break;
            }
        }
        cout<<(stable?"STABLE":"UNSTABLE")<<endl;
    }

    return 0;
}
