#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;

int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};

void dfs(int i, int j, vector<vector<char>> &g, vector<vector<int>> &grid, int num){
    grid[i][j] = num;
    int h = g.size();
    int w = g[0].size();
    char c = g[i][j];
    for(int ii = 0; ii < 4; ii++){
        int ni = i + di[ii];
        int nj = j + dj[ii];
        if(ni < 0 or h <= ni or nj < 0 or w <= nj) continue;
        
        if(c == g[ni][nj] and grid[ni][nj] == 0) dfs(ni, nj, g, grid, num);
    }
}

signed main(){
    freopen("/dev/null", "w", stderr);
    while(1){   
        int w, h; cin >> w >> h;
        if(w == 0) break;
        vector<vector<char>> g(h, vector<char> (w));
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> g[i][j];
            }
        }
        vector<vector<int>> grid(h, vector<int> (w, 0));
        
        int num = 1;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(g[i][j] != '.' && grid[i][j] == 0){
                    dfs(i, j, g, grid, num);
                    num++;
                }
            }
        }
        
        cerr << "grid" << endl;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){

                cerr << grid[i][j] << " ";
            }
            cerr << endl;
        }
        

        // (ブロックの個数,  x の総和, 下のブロック)
        vector<pair<pair<int, int>, int> > vp(num, {{0, 0}, -1});
        
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(grid[i][j] == 0) continue;
                int val = grid[i][j] - 1;
                vp[val].first.first++;
                vp[val].first.second += j;
                int ui = i + 1;
                int uj = j;
                if(ui < h and grid[ui][uj] != 0 and grid[i][j] != grid[ui][uj]){
                    vp[val].second = grid[ui][uj] - 1;
                }
            }
        }
        
        /*
        cout << "vp " << endl;
        for(int i = 0; i < num; i++){
            cout << vp[i].first.first << " " << vp[i].first.second << " " << vp[i].second << endl;
        }
        */
        
        vector<int> d(num, 0);
        for(int i = 0; i < num - 1; i++){
            int to = vp[i].second;
            if(to == -1) continue;
            d[to]++;
        }


        // 次数が 0 の頂点から処理していき、全部クリアしたら ok
        vector<bool> used(num - 1, false);
        queue<int> que;
        for(int i = 0; i < num - 1; i++){
            if(d[i] == 0){
                que.push(i);
            }
        }
        
        
        while(!que.empty()){
            int node = que.front();
            que.pop();
            
            int ff = vp[node].first.first;
            int fs = vp[node].first.second;
            int s = vp[node].second;
            if(s == -1) continue;
            vp[s].first.first += ff;
            vp[s].first.second += fs;
            
            d[s]--;
            if(d[s] == 0) que.push(s);
        }
        
        
        vector<pair<int, int>> xl(num - 1, {INF, 0});    
        vector<pair<int, int>> xr(num - 1, {0, 0});    
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                int val = grid[i][j] - 1;
                
                if(val == -1) continue;
                if(i == h - 1){
                    xl[val] = min(xl[val], {j, INF});   
                    xr[val] = max(xr[val], {j, INF});   
                }else{
                    int si = i + 1;
                    int sj = j;
                    int sval = grid[si][sj] - 1;
                    if(sval == -1) continue;
                    if(sval != val){
                        xl[val] = min(xl[val], {j, INF});   
                        xr[val] = max(xr[val], {j, INF});   
                    }
                }

            }
        }
        
        
        cerr << "vp" << endl;
        for(int i = 0; i < num - 1; i++){
            cerr << i << " " << vp[i].first.first << " " << vp[i].first.second << " " << vp[i].second << endl;
        }

        cerr << "xl xr" << endl;
        for(int i = 0; i < num - 1; i++){
            cerr << xl[i].first << " " << xr[i].first << endl;
        }
        
            
        bool ok = true;
        for(int i = 0; i < num - 1; i++){
            
            int left = 2 * vp[i].first.first * xl[i].first - vp[i].first.first;
            int mid = 2 * vp[i].first.second;
            int right = 2 * vp[i].first.first * xr[i].first + vp[i].first.first;
            if(!(left < mid and mid < right)){
                
                cerr << "不安定なブロックは,,, お前だー! " << i << " " << left << " " << mid << " " << right << endl;
                ok = false;
            }
        }

        if(ok) cout << "STABLE" << endl;
        else cout << "UNSTABLE" << endl;
        
    }

    return 0;
}



Measured memory_bytes: 3112