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

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main(){
  while(1){
    int w,h;
    cin >> w >> h;
    if(w==0) return 0;
    vector<vector<char>> field(h+2,vector<char>(w+2,'.'));
    vector<vector<pair<int,int>>> wei(h+2,vector<pair<int,int>>(w+2,{0,0}));
    for(int i=1;i<=h;i++){
      for(int j=1;j<=w;j++){
        cin >> field[i][j];
        if(field[i][j]!='.') wei[i][j]={j,1};
      }
    }
    char num;
    queue<pair<int,int>> bfs,ss;
    pair<int,int> now;
    pair<int,int> wnow;
    pair<int,int> sita,init={-1,-1};
    int minx,maxx;
    int v[5]={0,1,0,-1,0};
    int nx,ny;
    for(int i=1;i<=h;i++){
      for(int j=1;j<=w;j++){
        if(field[i][j]!='.'){
          num=field[i][j];
          bfs.push({i,j});
          field[i][j]='.';
          wnow={0,0};
          while(!bfs.empty()){
            now=bfs.front();
            bfs.pop();
            ss.push(now);
            // printf("(%d,%d)\n",now.second,now.first);
            wnow.first+=wei[now.first][now.second].first;
            wnow.second+=wei[now.first][now.second].second;
            for(int k=0;k<4;k++){
              ny=now.first+v[k];
              nx=now.second+v[k+1];
              if(field[ny][nx]==num){
                field[ny][nx]='.';
                bfs.push({ny,nx});
              }
            }
          }
          sita=init;
          minx=w+1,maxx=-1;
          bool bottom=false;
          while(!ss.empty()){
            now=ss.front();
            ss.pop();
            ny=now.first+1;
            nx=now.second;
            if(now.first==h){
              minx=min(minx,nx);
              maxx=max(maxx,nx);
              bottom=true;
            }
            if(field[ny][nx]!='.'){
              sita={ny,nx};
              minx=min(minx,nx);
              maxx=max(maxx,nx);
            }
          }
          // cout << wnow.first SP wnow.second << endl;
          // cout << sita.first SP sita.second << endl;
          // cout << minx SP maxx << endl;
          if(sita==init){
            if(bottom){
              if((LD)wnow.first/wnow.second<=minx-0.5||maxx+0.5<=(LD)wnow.first/wnow.second){
                cout << "UNSTABLE" << endl;
              }else{
                cout << "STABLE" << endl;
              }
              goto nextset;
            }else{
              cout << "UNSTABLE" << endl;
              goto nextset;
            }
          }else{
            if((LD)wnow.first/wnow.second<=minx-0.5||maxx+0.5<=(LD)wnow.first/wnow.second){
              cout << "UNSTABLE" << endl;
              goto nextset;
            }
            wei[sita.first][sita.second].first+=wnow.first;
            wei[sita.first][sita.second].second+=wnow.second;
          }
        }
      }
    }
    nextset:
    continue;
  }
  return 0;
}



Measured memory_bytes: 3040