#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define si(x) int(x.size())
#define double long double
const int mod=1000000007,MAX=205,INF=1<<29;
vector<int> G[MAX];
double lef[MAX],righ[MAX],sum[MAX],size[MAX];
bool seen[MAX];
const double eps=1e-8;

vector<int> dh={0,1,0,-1},dw={1,0,-1,0};

void DFS(int u,int p){
    seen[u]=1;
    for(int to:G[u]){
        if(seen[to]) continue;
        DFS(to,u);
        sum[u]+=sum[to];
        size[u]+=size[to];
    }
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        int H,W;cin>>W>>H;
        if(H+W==0) break;
        vector<string> S(H);
        vector<vector<int>> id(H,vector<int>(W));
        for(int i=0;i<H;i++) cin>>S[i];
        
        for(int i=0;i<MAX;i++){
            G[i].clear();
            lef[i]=righ[i]=sum[i]=size[i]=seen[i]=0;
            lef[i]=INF;
        }
        int now=1;
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                if(S[i][j]=='.') continue;
                for(int k=0;k<4;k++){
                    int toi=i+dh[k],toj=j+dw[k];
                    if(toi<0||toi>=H||toj<0||toj>=W) continue;
                    if(S[toi][toj]==S[i][j]&&id[toi][toj]){
                        id[i][j]=id[toi][toj];
                    }
                }
                if(id[i][j]==0){
                    id[i][j]=now;
                    now++;
                }
                
                if(j-1>=0&&S[i][j-1]==S[i][j]&&id[i][j-1]!=id[i][j]){
                    id[i][j-1]=id[i][j];
                    now--;
                    
                    if(j-2>=0&&S[i][j-2]==S[i][j]&&id[i][j-2]!=id[i][j]){
                        id[i][j-2]=id[i][j];
                    }
                }
            }
            
            for(int j=0;j<W;j++){
                if(S[i][j]=='.') continue;
                if(i-1>=0&&id[i-1][j]){
                    G[id[i][j]].push_back(id[i-1][j]);
                }
                
                if(i==H-1||(S[i+1][j]!='.'&&S[i+1][j]!=S[i][j])){
                    chmin(lef[id[i][j]],(double)j-0.5);
                    chmax(righ[id[i][j]],(double)j+0.5);
                }
                
                size[id[i][j]]++;
                sum[id[i][j]]+=j;
            }
        }
        
        /*for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                cout<<id[i][j];
            }
            cout<<endl;
        }*/
        
        DFS(now-1,-1);
        
        bool ok=true;
        
        for(int i=1;i<now;i++){
            if(sum[i]/size[i]<lef[i]+eps||righ[i]-eps<sum[i]/size[i]) ok=false;
            //cout<<sum[i]<<" "<<size[i]<<" "<<lef[i]<<" "<<righ[i]<<endl;
        }
        
        //cout<<now-1<<" ";
        
        if(ok) cout<<"STABLE\n";
        else cout<<"UNSTABLE\n";
    }
}





Measured memory_bytes: 2992