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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<58;
const double eps=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
    	os << v << ",";
    }
    os << "]";
    return os;
}

void solve(){
    while(true){
        int w,h;
        cin >> w >> h;
        if(!w and !h) return;
        vector<vector<char>> p(h,vector<char>(w));
        rep(i,0,h) rep(j,0,w) cin >> p[i][j];
        vector<vector<int>> graph;
        vector<vector<pii>> piece_pos;
        vector<double> left,right;
        vector<vector<int>> id(h,vector<int>(w,-1));
        vector<vector<bool>> done(h,vector<bool>(w,false));
        int cnt=0;
        function<void(const int,const int)> prepare=[&](const int y,const int x){
            id[y][x]=cnt;
            ++cnt;
            graph.emplace_back(vector<int>());
            piece_pos.emplace_back(vector<pii>());
            left.emplace_back(x);
            right.emplace_back(x);
            piece_pos[id[y][x]].emplace_back(make_pair(y,x));

            const int num=p[y][x];
            queue<pii> que;
            que.push(make_pair(y,x));
            done[y][x]=true;
            while(!que.empty()){
                const pii tmp=que.front(); que.pop();
                const int cur_y=tmp.first,cur_x=tmp.second;
                rep(i,0,4){
                    const int next_y=cur_y+dy[i],next_x=cur_x+dx[i];
                    if(next_y<0 or h<=next_y or next_x<0 or w<=next_x) continue;
                    if(dy[i]==-1 and p[next_y][next_x]!='.' and p[next_y][next_x]!=num and id[next_y][next_x]==-1){
                        prepare(next_y,next_x);
                        graph[id[y][x]].emplace_back(id[next_y][next_x]);
                        continue;
                    }
                    if(p[next_y][next_x]==num and !done[next_y][next_x]){
                        id[next_y][next_x]=id[y][x];
                        piece_pos[id[y][x]].emplace_back(make_pair(next_y,next_x));
                        if(next_y+1==h or (p[next_y+1][next_x]!='.' and p[next_y+1][next_x]!=num)){
                            left[id[y][x]]=min(left[id[y][x]],(double)next_x);
                            right[id[y][x]]=max(right[id[y][x]],(double)next_x);
                        }
                        que.push(make_pair(next_y,next_x));
                        done[next_y][next_x]=true;
                    }
                }
            }
        };
        rep(x,0,w) if(p[h-1][x]!='.'){
            prepare(h-1,x);
            break;
        }

        function<pair<double,double>(const int)> ok=[&](const int u){
            pair<double,double> res=make_pair(-1.,-1.);
            vector<pair<double,double>> child_res;
            for(const int v:graph[u]){
                child_res.emplace_back(ok(v));
                if(child_res.back().first<0) return res;
            }
            double tmp1=0,m=0;
            for(const auto& tmp:piece_pos[u]){
                tmp1+=tmp.second+0.5;
                ++m;
            }
            rep(i,0,child_res.size()){
                tmp1+=child_res[i].first*child_res[i].second;
                m+=child_res[i].first;
            }
            if(left[u]<tmp1/m and tmp1/m<right[u]+1) res=make_pair(m,tmp1/m);
            return res;
        };
        if(ok(0).first>=0) cout << "STABLE" << endl;
        else cout << "UNSTABLE" << endl;
    }
}

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    cout << fixed << setprecision(8);
    solve();
    return 0;
}

Measured memory_bytes: 3080