#include<iostream>
#include<vector>

using namespace std;

class Piece;

// ツマツッツプツ湘ョツ陛ア
char map[10][60];
Piece *pmap[10][60];

int width;
int height;


class Piece {

public:

  // ツコツδ督スツトツδ可クツタ
  Piece(int x, int y) {
    m = 0;
    mx = 0;
    parent=NULL;
    helper(x, y, map[x][y]);
    xl = 100;
    xr = 0;
  }

  // ツ重ツ心
  int m;
  double mx;

  int rm;
  double rmx;

  // ツ支ツつヲツ点
  int xl;
  int xr;

  // ツ篠ゥツ閉ェツづ個親
  Piece *parent;
  vector<Piece*> children;

  bool check() {
    //cout << m << ' ' << mx << '\n';
    //cout << xl << ' ' << xr << "@\n";
    rm = m;
    rmx = m*mx;
    for(int i=0; i<children.size(); i++) {
      if(children[i]->check()==false) return false;
      rmx += children[i]->rm*children[i]->rmx;
      rm += children[i]->rm;
    }
    rmx /= rm;
    return xl < rmx && rmx < xr;
  }

private:
  
  void helper(int x, int y, char c) {

    if(map[x][y]!=c) return;
    map[x][y] = '.';
    pmap[x][y] = this;

    if(x>0) helper(x-1, y, c);
    if(x<width-1) helper(x+1, y, c);
    if(y>0) helper(x, y-1, c);
    if(y<height-1) helper(x, y+1, c);

    mx = (m*mx + x+0.5) / (double)(m+1);
    m++;
  }
};

int main() {

  

  while(1) {

    vector<Piece*> l;
    Piece *root;

    l.clear();

    cin >> width;
    cin >> height;

    if(width==0 && height==0) break;
    
    // ツ渉可甘コツ可サ
    for(int j=0; j<height; j++) {
      for(int i=0; i<width; i++) {
	cin>>map[i][j];
	pmap[i][j]=NULL;
      }
    }

    // ツコツδ督スツトツδ可クツト
    for(int i=0; i<width; i++) {
      for(int j=0; j<height; j++) {
	if(map[i][j]!='.') {
	  l.push_back(new Piece(i, j));
	}
      }
    }

    // ツ親ツ子ツ甘鳴係ツ津ィツ義
    for(int j=0; j<height; j++) {
      for(int i=0; i<width; i++) {
	//cout << (pmap[i][j]==NULL?'.':'*');
	if(j!=height-1) {
	  if(pmap[i][j]!=NULL && pmap[i][j+1]!=NULL && pmap[i][j]!=pmap[i][j+1]) {
	    if(pmap[i][j]->parent==NULL) {
	      pmap[i][j]->parent = pmap[i][j+1];
	      pmap[i][j+1]->children.push_back(pmap[i][j]);
	    }
	    if(i<pmap[i][j]->xl) pmap[i][j]->xl = i;
	    if(pmap[i][j]->xr<i+1) pmap[i][j]->xr = i+1;
	  }
	} else {
	  // ツ暗ェツ氾板可コ
	  if(pmap[i][j]!=NULL) {
	    //cout << "|";
	    if(i<pmap[i][j]->xl) pmap[i][j]->xl = i;
	    if(pmap[i][j]->xr<i+1) pmap[i][j]->xr = i+1;
	    root = pmap[i][j];
	  }
	}
      }
      //cout << "\n";
    }

    // ツ重ツ心ツ板サツ津ィ
    cout << (root->check() ? "STABLE" : "UNSTABLE") << "\n";
  }
  
  
}