#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cassert>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <fstream>
#include <cmath>
using namespace std;

#define REP2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for(ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n) & 1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-12;

int main(){
  int W, H;
  int dx[] = {0, -1, 0, 1};
  int dy[] = {1, 0, -1, 0};
  
  while(cin >> W >> H && W + H){
    int count = 0;
    vector<string> sblocks(H);
    vector<int> b_size;
    vector<int> total_y;
    
    REP(i, H) cin >> sblocks[i];
    vector<vector<int> > iblocks(H, vector<int>(W, -1));
    
    REP(i, H)REP(j, W)if(isdigit(sblocks[i][j])){
      queue<int> quex, quey;
      quex.push(i);
      quey.push(j);
      char	c    = sblocks[i][j];
      int	sz   = 1;
      int	toty = j;

      sblocks[i][j] = '.';
      iblocks[i][j] = count;
      while(!quex.empty()){
	int x = quex.front(); quex.pop();
	int y = quey.front(); quey.pop();
	
	REP(k, 4){
	  int nx = x + dx[k];
	  int ny = y + dy[k];
	  if(0 <= nx && nx < H &&
	     0 <= ny && ny < W &&
	     sblocks[nx][ny] == c){
	    
	    sblocks[nx][ny] = '.';
	    iblocks[nx][ny] = count;
	    sz++;
	    toty += ny;
	    quex.push(nx);
	    quey.push(ny);
	  }
	}
      }

      b_size.push_back(sz);
      total_y.push_back(toty);
      count++;
    }
    
    bool stable = true;
    REP(i, b_size.size()){
      double yl = INF;
      double yr = -INF;
      
      int &tot = total_y[i];
      int &cnt = b_size[i];

      set<int> used;
      
      REP(x, H)REP(y, W)if(iblocks[x][y] == i){
	if(x == H - 1 ||
	   (iblocks[x+1][y] != -1 && iblocks[x+1][y] != i)){
	  yl = min(yl, (double)y);
	  yr = max(yr, (double)y);
	}

	if(x > 0){
	  int id = iblocks[x-1][y];
	  if(id >= 0 && used.count(id) == 0 && id != i){
	    tot += total_y[id];
	    cnt += b_size[id];
	    used.insert(id);
	  }
	}
      }
      double g = (double)tot / cnt + 0.5;
      if(g < yl + EPS ||  g > yr + 1 - EPS) stable = false;
    }
    cout << (stable ? "STABLE" : "UNSTABLE") << endl;
  }  
  return 0;
}