#include <bits/stdc++.h>

#ifndef LOCAL_
#define fprintf if( false ) fprintf
#endif // LOCAL_
// #define dump() fprintf(stderr, "#%s.%d\n", __func__, __LINE__);
#define dumpl(x1) fprintf(stderr, "#%s.%d (%s) = (%ld)\n", __func__, __LINE__, #x1, x1);
#define dumpll(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%ld, %ld)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define dumplll(x1, x2, x3) fprintf(stderr, "#%s.%d (%s, %s, %s) = (%ld, %ld, %ld)\n", __func__, __LINE__, #x1, #x2, #x3, x1, x2, x3);
#define dumpd(x1) fprintf(stderr, "#%s.%d (%s) = (%lf)\n", __func__, __LINE__, #x1, x1);
#define dumpdd(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%lf, %lf)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define loop for(;;)
typedef std::vector<long> LI;

template<typename T> void scan1(T& x) { fprintf(stderr, "unknown type\n"); }
template<> void scan1(long& x) { if( scanf("%ld", &x) < 0 ) exit(0); }
template<> void scan1(std::string& x) { if( not ( std::cin >> x ) ) exit(0); }
void scan() {}
template<typename Head, typename... Tail>
void scan(Head& x, Tail&... xs) {
  scan1(x); scan(xs...);
}

struct Solver {
   Solver() { fprintf(stderr, "--------Solver begin--------\n"); }
   ~Solver() { fprintf(stderr, "--------Solver end--------\n"); }
   std::string field[64];
   long w, h;
   struct Point { Point(long x_, long y_) : x(x_), y(y_) {} long x, y; };
   bool visited[64][16];
   bool res;
   std::pair<long, long> dfs(long bx, long by) {
      long totalX = 0, n = 0;
      long left = 100000, right = -1;
      std::queue<Point> q;
      q.push(Point(bx, by));
      long dx[] = {1, 0, -1, 0};
      long dy[] = {0, 1, 0, -1};
      while( not q.empty() ) {
         Point p = q.front(); q.pop();
         long x = p.x, y = p.y;
         if( visited[y][x] ) continue;
         dumpll(x, y);
         visited[y][x] = true;
         totalX += 2 * x + 1;
         n += 1;
         for(long i = 0; i < 4; ++i) {
            long nx = x + dx[i], ny = y + dy[i];
            if( not ( 0 <= nx and nx < w and 0 <= ny and ny < h ) ) continue;
            if( not ( field[by][bx] == field[ny][nx] ) ) continue;
            q.push(Point(nx, ny));
         }
         if( y != 0 ) {
            if( field[y-1][x] != '.' and field[y-1][x] != field[y][x] and not visited[y-1][x] ) {
               auto t = dfs(x, y-1);
               totalX += t.first;
               n += t.second;
            }
         }
         if( field[y+1][x] != '.' and field[y+1][x] != field[y][x] ) {
            left = std::min(left, x);
            right = std::max(right, x);
         }
      }
      dumpll(bx, by);
      dumpll(left, right);
      dumpll(totalX, n);
      if( not ( 2 * left * n < totalX and totalX < 2 * (right + 1) * n ) ) res = false;
      dumpl(res ? (long)1 : (long)0);
      return std::make_pair(totalX, n);
   }
   void solve() {
      scan(w, h);
      if( w == 0 and h == 0 ) exit(0);
      for(long i = 0; i < h; ++i) scan(field[i]);
      field[h] = "##########";
      res = true;
      for(long i = 0; i < 64; ++i) for(long k = 0; k < 16; ++k) visited[i][k] = false;
      for(long i = 0; i < w; ++i) {
         if( field[h-1][i] != '.' ) {
            dfs(i, h-1);
            puts(res ? "STABLE" : "UNSTABLE");
            break;
         }
      }
   }
};

int main() {
  loop std::unique_ptr<Solver>(new Solver())->solve();
}

Measured memory_bytes: 3124