#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(),(c).end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MINF(a) memset(a,0x3f,sizeof(a))
#define POW(n) (1LL<<(n))
#define IN(i,a,b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T& a,T b) { if(a>b) { a=b; return 1; } return 0; }
template <typename T> inline bool CHMAX(T& a,T b) { if(a<b) { a=b; return 1; } return 0; }
template <typename T> inline void SORT(T& a) { sort(ALL(a)); }
template <typename T> inline void REV(T& a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T& a) { sort(ALL(a)); a.erase(unique(ALL(a)),a.end()); }
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
/* ---------------------------------------------------------------------------------------------------- */

int dy[] = {0,0,1};
int dx[] = {-1,1,0};

int w,h;
string p[65];
int cnt,id[65][15];
vector<double> ps[160],bottom[160];
vector<int> G[160];
bool stable;
using P = pair<double,int>;

void dfs(int y, int x, int _id) {
  id[y][x] = _id;
  ps[_id].push_back(x+0.5);
  REP(i,3) {
    int ny = y+dy[i];
    int nx = x+dx[i];
    if (IN(ny,0,h-1) && IN(nx,0,w-1) && p[ny][nx] == p[y][x] && id[ny][nx] < 0) {
      dfs(ny,nx,_id);
    }
  }
}


P get_g(int _id) {
  double g = 0;
  int s = 4;
  for (auto p : ps[_id]) g += p;
  for (auto i : G[_id]) {
    P tmp = get_g(i);
    g += tmp.first*tmp.second;
    s += tmp.second;
  }
  g /= s;
  if (!(bottom[_id].front() < g && g < bottom[_id].back()+1)) {
    stable = false;
  }
  return P(g,s);
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  while (cin >> w >> h, w) {
    REP(i,h) cin >> p[i];
    cnt = 0;
    MINUS(id);
    REP(i,160) {
      ps[i].clear();
      bottom[i].clear();
      G[i].clear();
    }
    REP(i,h) REP(j,w) {
      if (p[i][j] == '.' || id[i][j] >= 0) continue;
      dfs(i,j,cnt);
      cnt++;
    }
    int start_id = -1;
    REP(i,h) REP(j,w) {
      if (p[i][j] == '.') continue;
      if (i+1 == h || (p[i+1][j] != '.' && p[i+1][j] != p[i][j])) {
        bottom[id[i][j]].push_back(j);
        if (i+1 < h) {
          G[id[i+1][j]].push_back(id[i][j]);
        } else {
          start_id = id[i][j];
        }
      }
    }
    REP(i,cnt) {
      UNI(bottom[i]);
      UNI(G[i]);
    }
    stable = true;
    REP(i,cnt) P g = get_g(i);
    cout << (stable ? "STABLE" : "UNSTABLE") << endl;
  }

  return 0;
}
