#include<bits/stdc++.h>

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

using namespace std;

const string YES = "STABLE";
const string NO  = "UNSTABLE";

struct Data {
  int adj;
  double centroid;
  double xmin,xmax;
  double denominator;
};

int h,w,IDMap[60][10];
string c[60];
Data arr[200];
bool visited[60][10];
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};

inline bool isValid(int x,int y) { return 0 <= x && x < w && 0 <= y && y < h; }

void makeMap() {
  memset(IDMap,-1,sizeof IDMap);
  int ID = 0;
  rep(i,h) rep(j,w) if( c[i][j] != '.' && !visited[i][j] ){
    visited[i][j] = true;
    IDMap[i][j] = ID;
    deque<int> deq;
    deq.push_back(j+i*w);
    while( !deq.empty() ) {
      int cur = deq.front(); deq.pop_front();
      int x = cur % w, y = cur / w;
      rep(k,4) {
        int nx = x + dx[k], ny = y + dy[k];
        if( !isValid(nx,ny) ) continue;
        if( c[ny][nx] != c[i][j] ) continue;
        if( visited[ny][nx] ) continue;
        visited[ny][nx] = true;
        IDMap[ny][nx] = ID;
        deq.push_back(nx+ny*w);
      }
    }
    ++ID;
  }
  memset(visited,false,sizeof visited);
}

void compute() {
  memset(visited,false,sizeof visited);
  rep(i,200) arr[i].adj = -1, arr[i].centroid = 0, arr[i].xmin = DBL_MAX, arr[i].xmax = -DBL_MAX, arr[i].denominator = 0;
  makeMap();
  vector<int> IDs;
  rep(y,h) rep(x,w) if( c[y][x] != '.' && !visited[y][x] ) {
    //int ID = c[y][x] - '0';
    int ID = IDMap[y][x];
    assert( ID != -1 );
    IDs.push_back(ID);
    deque<int> deq;
    deq.push_back(x+y*w);
    visited[y][x] = true;
    while( !deq.empty() ) {
      int cur = deq.front(); deq.pop_front();
      int cx = cur % w, cy = cur / w;
      arr[ID].centroid += ( (double)cx + 0.5);
      arr[ID].denominator = 1;
      rep(i,4) {
        int nx = cx + dx[i], ny = cy + dy[i];
        if( !isValid(nx,ny) ) {
          if( i == 0 ) {
            arr[ID].xmin = min(arr[ID].xmin,(double)cx);
            arr[ID].xmax = max(arr[ID].xmax,(double)cx+1.0);
          }
          continue;
        }
        if( c[ny][nx] != c[y][x] ) {
          if( c[ny][nx] != '.' && i == 0 ) {
            arr[ID].adj = IDMap[ny][nx];
            arr[ID].xmin = min(arr[ID].xmin,(double)cx);
            arr[ID].xmax = max(arr[ID].xmax,(double)cx+1.0);
          }
          continue;
        }
        if( !visited[ny][nx] ) {
          visited[ny][nx] = true;
          deq.push_back(nx+ny*w);
        }
      }
    }
  }
  rep(i,(int)IDs.size()) if( arr[IDs[i]].adj != -1 ) {
    arr[arr[IDs[i]].adj].centroid += arr[IDs[i]].centroid;
    arr[arr[IDs[i]].adj].denominator += arr[IDs[i]].denominator;
  }
  /*
  rep(i,(int)IDs.size()) {
    cout << "ID = " << IDs[i] << endl;
    cout << "adj = " << arr[IDs[i]].adj << endl;
    cout << "normal   = " << arr[IDs[i]].centroid << endl;
    cout << "centroid = " << arr[IDs[i]].centroid/(arr[IDs[i]].denominator*4.0) << endl;
    cout << "xmin = " << arr[IDs[i]].xmin << ", xmax = " << arr[IDs[i]].xmax << endl;
    cout << "denominator = " << arr[IDs[i]].denominator << endl;
  }
  */
  rep(i,(int)IDs.size()) if( arr[IDs[i]].xmin != DBL_MAX ) {
    double centroid = arr[IDs[i]].centroid / ( 4.0 * arr[IDs[i]].denominator );
    if( !( arr[IDs[i]].xmin < centroid && centroid < arr[IDs[i]].xmax ) ) {
      cout << NO << endl;
      return;
    }
  }
  cout << YES << endl;
}

int main() {
  while( cin >> w >> h, w|h ) {
    rep(i,h) cin >> c[i];
    compute();
  }
  return 0;
}

Measured memory_bytes: 1252