#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

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

signed main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(true) {
    int h, w;
    cin >> w >> h;
    if(!h) break;
    vector<string> s(h);
    REP(i, h) cin >> s[i];

    VVI used(h, VI(w, 0));
    int xl, xr;
    function<void(int,int)> dfs1 = [&](int x, int y) {
      used[y][x] = 1;
      if(y == h-1 || (s[y+1][x] != '.' && s[y+1][x] != s[y][x])) {
        chmin(xl, x); chmax(xr, x);
      }
      REP(i, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if(IN(0LL, w, nx) && IN(0LL, h, ny) && s[y][x] == s[ny][nx] && !used[ny][nx]) {
          dfs1(nx, ny);
        }
      }
    };

    VVI used2(h, VI(w, 0));
    int cnt = 0;
    double mid = 0;
    function<void(int,int)> dfs2 = [&](int x, int y) {
      // cout << x << " " << y << endl;
      used2[y][x] = 1;
      cnt++;
      mid += (x+0.5);
      REP(i, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if(i == 2) {
          if(IN(0LL, w, nx) && IN(0LL, h, ny) && s[ny][nx] != '.' && !used2[ny][nx]) {
            dfs2(nx, ny);
          }
        } else {
          if(IN(0LL, w, nx) && IN(0LL, h, ny) && s[ny][nx] == s[y][x] && !used2[ny][nx]) {
            dfs2(nx, ny);
          }
        }
      }
    };

    bool flag = true;
    REP(i, h) REP(j, w) {
      if(used[i][j] || s[i][j] == '.') continue;
      // (i,j)と同じ数字のところを探索してx_l、x_rを求める
      xl = INF, xr = -1;
      dfs1(j, i);
      xr++;
      // (i,j)から上方向に探索して重心Mを求める
      // 同じ数字なら上下左右に進む
      // 違う数字でも上なら進む
      cnt = 0, mid = 0;
      used2.assign(h, VI(w, 0));
      dfs2(j, i);
      mid /= cnt;
      // cout << i << " " << j << " " << xl << " " << xr << " " << mid << " " << cnt << endl;
      if(xl >= mid || mid >= xr) {
        flag = false;
        goto end;
      }
    }

    end:;
    if(flag) cout << "STABLE" << endl;
    else cout << "UNSTABLE" << endl;
  }

  return 0;
}

