#include <bits/stdc++.h>
struct UnionFind {
    int sz;
    std::vector<int> par;
    UnionFind(int sz) {
        this->sz = sz;
        par.clear();
        par.resize(sz, -1);
    }
    int root(int x) {
        return par[x] < 0 ? x : par[x] = root(par[x]);
    }
    void unite(int a, int b) {
        int roota = root(a);
        int rootb = root(b);
        if(roota == rootb) return;
        if(par[rootb] < par[roota]) std::swap(roota, rootb);
        par[roota] += par[rootb];
        par[rootb] = roota;
    }
    bool same(int a, int b) {
        return (root(a) == root(b));
    }
    int size(int a) {
        return -par[root(a)];
    }
};
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const ld eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
struct Piece {
  int l = INF, r = -1, cnt = 0;
  ld g = 0.0, su = 0.0;
  Piece() {};
};
void operator+=(Piece &p1, const Piece &p2) {
  p1.cnt += p2.cnt;
  p1.su += p2.su;
  p1.g = p1.su / p1.cnt;
}
pair<bool, Piece> dfs(int now, int prev, vector<set<int>> &v, map<int, Piece> &mp) {
  bool resb = true;
  Piece resp = mp[now];
  for(auto &e: v[now]) {
    int next = e;
    if(next != prev) {
      auto tmp = dfs(next, now, v, mp);
      if(!tmp.first) return make_pair(false, Piece());
      Piece chp = tmp.second;
      resp += chp;
    }
  }
  resb &= resp.l - 0.5 + eps < resp.g && resp.g < resp.r + 0.5 - eps;
  return make_pair(resb, resp);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  while(1) {
    int h, w; cin >> w >> h;
    UnionFind uf(h*w);
    if(h == 0) break;
    vector<vector<char>> b(h, vector<char>(w));
    for(int i=0;i<h;++i) {
      for(int j=0;j<w;++j) {
        cin >> b[i][j];
      }
    }
    for(int i=0;i<h;++i) {
      for(int j=0;j<w;++j) {
        if(b[i][j] == '.') continue;
        for(int k=0;k<4;++k) {
          int ar = i + dr[k], ac = j + dc[k];
          if(0 <= ar && ar < h && 0 <= ac && ac < w && b[i][j] == b[ar][ac]) {
            uf.unite(i*w+j, ar*w+ac);
          }
        }
      }
    }
    vvi b2(h, vi(w, -1));
    int root = -1;
    set<int> st;
    for(int i=0;i<h;++i) {
      for(int j=0;j<w;++j) {
        if(b[i][j] == '.') continue;
        b2[i][j] = uf.root(i*w+j);
        st.insert(b2[i][j]);
        root = b2[i][j];
      }
    }
    vector<set<int>> tr(h*w);
    bool ok = true;
    for(int i=0;i<h;++i) {
      if(!ok) break;
      for(int j=0;j<w;++j) {
        if(i == 0 || b2[i][j] == -1 || b2[i-1][j] == -1) continue;
        if(b2[i][j] != b2[i-1][j]) {
          tr[b2[i][j]].insert(b2[i-1][j]);
        }
      }
    }
    if(!ok) {
      cout << "UNSTABLE" << endl;
      continue;
    }
    map<int, Piece> mp;
    for(auto &e: st) {
      for(int i=0;i<h;++i) {
        for(int j=0;j<w;++j) {
          if(b2[i][j] == e) {
            if(i == h-1 || (b2[i+1][j] != -1 && b2[i+1][j] != b2[i][j])) mp[e].l = min(mp[e].l, j);
            if(i == h-1 || (b2[i+1][j] != -1 && b2[i+1][j] != b2[i][j])) mp[e].r = max(mp[e].r, j);
            mp[e].cnt++;
            mp[e].su += j;
            mp[e].g = mp[e].su / mp[e].cnt;
          }
        }
      }
    }
    auto ans = dfs(root, -1, tr, mp);
    cout << (ans.first ? "STABLE" : "UNSTABLE") << endl;
  }
}



Measured memory_bytes: 3052