#include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define rep(i,n) for(int i=0;i<n;i++)
constexpr int INF = numeric_limits<int>::max()/2;
constexpr long long LINF = numeric_limits<long long>::max()/3;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
//constexpr double eps = 1e-14; 
constexpr double eps = 1e-10; 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/


int w,h;
vector<int> parent;
vector<vector<int>> child;
vector<pair<double,double>> M;

using P = pair<pair<double,double>,int>;
P memo[200];
P dfs(int node){
    if(child[node].size()==0) return mp(M[node],1);
    if(memo[node].se!=0) return memo[node];

    double x=0,y=0;
    int cnt=0;
    for(int i=0;i<child[node].size();i++){
        int v = child[node][i];
        auto p = dfs(v);

        x += p.fi.fi;
        y += p.fi.se;
        cnt += p.se;
    }
    x += M[node].fi;
    y += M[node].se;
    cnt++;
    memo[node] = mp(mp(x,y),cnt);
    return memo[node];
}

void solve(){
    // init
    parent.assign(200,-1);
    child.assign(200,vector<int>());
    M.assign(200,pii(0,0));
    for(int i=0;i<200;i++) memo[i] = mp(mp(0,0),0);

    // input
    vector<string> p(h);
    rep(i,h) cin>>p[i];

    // ?????¢????????????
    int t[60][10];
    rep(i,60) rep(j,10) t[i][j]=-1;
    int number=0;
    bool visited[60][10];
    rep(i,60) rep(j,10) visited[i][j] = false;
    for(int i=0;i<h;i++) for(int j=0;j<w;j++){
        if(p[i][j]=='.') continue;
        if(visited[i][j]) continue;
        char n = p[i][j];

        queue<pii> que;
        que.push(pii(i,j));
        visited[i][j]=true;
        t[i][j] = number;

        while(!que.empty()){
            pii q = que.front();
            que.pop();

            int x = q.fi;
            int y = q.se;

            for(int k=0;k<4;k++){
                int nx = x + dx[k];
                int ny = y + dy[k];
                if(nx<0 or ny<0 or nx>=h or ny>=w) continue;
                if(p[nx][ny]!=n) continue;
                if(visited[nx][ny]) continue;

                visited[nx][ny] = true;
                t[nx][ny] = number;
                que.push(pii(nx,ny));
            }
        }
        number++;
    }
    int maxv = -1;
    rep(i,h) rep(j,w) if(t[i][j]!=-1) maxv = max(maxv,t[i][j]);


    //// ????????¢???????§????
    rep(x,h) rep(y,w) if(t[x][y]!=-1){
        int nx=x+1;
        int me = t[x][y];
        if(0<=nx and nx<h){
            if(t[nx][y]==-1) continue;
            if(t[nx][y]==t[x][y]) continue;
            parent[me] = t[nx][y];
            child[t[nx][y]].push_back(me);
        }
    }
    for(int i=0;i<=maxv;i++){
        sort(all(child[i]));
        child[i].erase(unique(all(child[i])),child[i].end());
    }


    //// calc M
    rep(i,h) rep(j,w) if(t[i][j]!=-1){
        int n = t[i][j];
        M[n].fi += i;
        M[n].se += j;
    }
    rep(i,200){
        M[i].fi /= 4.0;
        M[i].se /= 4.0;
    }

    //// calc LR
    vector<pair<double,double>> LR(200,pii(100,0));
    rep(x,h) rep(y,w){
        if(t[x][y]==-1) continue;
        int nx = x + 1;
        int v = t[x][y];
        bool f=false;
        if(nx>=h) f=true;
        else if(t[nx][y]==-1) continue;
        else if(t[nx][y]==parent[t[x][y]]){
            f=true;
        }
        if(f){
            LR[v].fi = min(LR[v].fi,(double)y-0.5);
            LR[v].se = max(LR[v].se,(double)y+0.5);
        }
    }



    //// debug
    //cout << "table" << endl;
    //rep(i,h){
    //    for(int j=0;j<w;j++)
    //        if(t[i][j]==-1)  cout << ".. ";
    //        else printf("%2d ",t[i][j]);
    //    cout << endl;
    //}
    //cout << "parent" << endl;
    //for(int i=0;i<=maxv;i++) cout << i << " " << parent[i] << " ";
    //cout << endl;
    //cout << "child" << endl;
    //for(int i=0;i<=maxv;i++) cout << i << " " << child[i] << endl;


    for(int i=0;i<=maxv;i++){
        auto z = dfs(i);
        double x = z.fi.fi;
        double y = z.fi.se;
        int c = z.se;
        //cout << "index:" << i << endl;
        //cout << x << " " << y << endl;
        x /= c;
        y /= c;

        //cout << x << " " << y << " " << c << endl;
        //cout << LR[i] << endl;

        if(LR[i].fi<y and y<LR[i].se) continue;
        else{
            cout << "UNSTABLE" << endl;
            return;
        }
    }
    cout << "STABLE" << endl;
}

int main(){
    while(cin>>w>>h){
        if(w==0 and h==0) break;
        solve();
    }
}

Measured memory_bytes: 3132