#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;

#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)

struct Node {
  int minj, maxj, m, cnt;
  Node():minj(100), maxj(-100), m(0), cnt(0){}
};

int w, h;
string s[61];

int realoc[61][11];
bool c_realoc[61][11];

bool path[61][61];
Node node[61];
int node_w[61];

int root_id, m_id;

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

void realocate(int i, int j, int id, char c) {
  s[i][j] = '.';
  realoc[i][j] = id;
  rep(z,4) {
    int ti = i + di[z], tj = j + dj[z];
    if (ti < 0 || tj < 0 || ti >= h || tj >= w) continue;
    if (s[ti][tj] == c) realocate(ti, tj, id, c);
  }
}

void setgraph(int i, int j, int id) {
  c_realoc[i][j] = false;
  node[id].m += j;
  if (id == root_id) {
    node[id].maxj = max(j+1, node[id].maxj);
  }
  rep(z,4) {
    int ti = i + di[z], tj = j + dj[z], t_id;
    if (ti < 0 || tj < 0 || ti >= h || tj >= w) continue;
    if (!c_realoc[ti][tj]) continue;
    t_id = realoc[ti][tj];
    // another piece
    if (t_id != -1 && t_id != id && z == 1) {
      path[id][t_id] = true;
      node[t_id].minj = min(tj, node[t_id].minj);
      node[t_id].maxj = max(tj+1, node[t_id].maxj);
      setgraph(ti, tj, t_id);
    }
    // same piece
    else if (t_id == id) {
      if (realoc[ti+1][tj] != -1 && realoc[ti+1][tj] != t_id) {
        node[t_id].minj = min(tj, node[t_id].minj);
        node[t_id].maxj = max(tj+1, node[t_id].maxj);
      }
      setgraph(ti, tj, id);
    }
  }
}

int calc_cnt(int id) {
  int res = 4;
  rep(i,m_id)
  if (path[id][i]) {
    res += calc_cnt(i);
  }
  return res;
}

int calc_m(int id) {
  int res = node_w[id];
  rep(i,m_id)
  if (path[id][i]) {
    res += calc_m(i);
  }
  return res;
}

void solve() {
  memset(path, false, sizeof path);
  memset(realoc, -1, sizeof realoc);
  memset(c_realoc, true, sizeof c_realoc);
  
  rep(i,h) cin>>s[i];
  
  // reallocate id
  int id = 0;
  rep(i,h)
  rep(j,w)
  if (s[i][j] != '.'){
    realocate(i, j, id, s[i][j]);
    // init
    node[id].m = 2;
    node[id].cnt = 4;
    node[id].minj = 100;
    node[id].maxj = -100;
    id++;
  }
  m_id = id;
  
  // set graph
  rep(i,w)
  if (realoc[h-1][i] != -1) {
    root_id = realoc[h-1][i];
    node[root_id].minj = i;
    setgraph(h-1, i, root_id);
    break;
  }
  
  // calc M
  rep(i,m_id) node_w[i] = node[i].m;
  
  rep(i,m_id) {
    int rec = node[i].m;
    node[i].m += calc_m(i);
    node[i].m -= rec;
    node[i].cnt += calc_cnt(i) - 4;
  }
      
  // check
  bool flag = true;
  rep(i,m_id) {
    int icnt = node[i].cnt, iminj = node[i].minj, imaxj = node[i].maxj;
    if (!(icnt * iminj < node[i].m && node[i].m < icnt * imaxj)) {
      flag = false; //break;
    }
  }
  
  if (!flag) cout<<"UN";
  cout<<"STABLE"<<endl;;
}

int main (int argc, char const* argv[]) {
  while (cin>>w>>h, w||h) {
    solve();
  }
  return 0;
}