#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<cassert>
using namespace std;

#define REP(i,a,b) for(i=a; i<b; ++i)
#define rep(i,n) REP(i,0,n)

string input[90];
int in[90][20];
bool visited[90][20];

struct Block {
  int num;
  vector<pair<int,int> > blocks;
  int bl,br,xsum;
  vector<int> above;
  double G;

  void getG() {
    int sum = 0;
    for(int i=0; i<blocks.size(); ++i)
      sum += blocks[i].second;

    xsum = sum+2;
    G = (double)sum/4;
  }

};

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

bool rec(int pos,vector<Block> &bv) {
  int i;
  rep(i,bv[pos].above.size()) {
    if(!rec(bv[pos].above[i], bv)) return false;
  }

  //cout<<pos<<" "<<bv[pos].bl<<" "<<bv[pos].G<<" "<<bv[pos].br<<endl;
  if(!(bv[pos].bl >= bv[pos].G || bv[pos].br <= bv[pos].G)) return true;
  return false;
}

//calc centroid
pair<int,int> calcG(int pos,vector<Block> &bv) {
  if(bv[pos].above.empty()) {
    bv[pos].G = bv[pos].xsum/(double)4;
    return make_pair(4,bv[pos].xsum);
  }

  int bcount = 0,i;
  int Gsum = 0;
  rep(i, bv[pos].above.size()) {
    pair<int,double> tmp = calcG(bv[pos].above[i], bv);
    bcount += tmp.first;
    Gsum += tmp.second;
  }
  bcount += 4;
  Gsum += bv[pos].xsum;
  bv[pos].xsum = Gsum;
  assert(bcount%4 == 0);
  //cout<<"calc G "<<pos<<" "<<Gsum<<" "<<bcount<<" "<<(double)Gsum/bcount<<endl;
  bv[pos].G = (Gsum/(double)bcount);
  return make_pair(bcount, Gsum);
}

int main() {
  int w,h,i,j,k;
  while(cin>>w>>h, w|h) {
    rep(i,h) {
      cin>>input[i];
    }

    memset(in, -1, sizeof(in));
    memset(visited, false, sizeof(visited));
    vector<Block> bv;
    int bnum = 0;
    rep(i,h) {
      rep(j,w) {
	if(input[i][j] != '.' && !visited[i][j]) {
	  Block tmp;
	  queue<pair<int,int> > Q;
	  Q.push(make_pair(i,j));
	  tmp.blocks.push_back(make_pair(i,j));
	  tmp.num = bnum++;
	  visited[i][j] = true;
	  while(!Q.empty()) {
	    pair<int,int> pi = Q.front(); Q.pop();
	    rep(k,4) {
	      int x = pi.first+dx[k], y = pi.second+dy[k];
	      if(x > -1 && x < h && y >= 0 && y < w ) {
		  if(input[i][j] == input[x][y] && !visited[x][y]) {
		    visited[x][y] = true;
		    tmp.blocks.push_back(make_pair(x,y));
		    Q.push(make_pair(x,y));
		    input[x][y] = '.';
		  }
	      }
	    }
	  }
	  bv.push_back(tmp);
	}
      }
    }

    rep(i,bv.size()) {
      rep(j, bv[i].blocks.size()) {
	in[bv[i].blocks[j].first][bv[i].blocks[j].second] = bv[i].num;
      }
      bv[i].getG();
    }

    int root = bv.size()-1;
    rep(i, bv.size()) {
      int bmin = 100,bmax = -1;
      rep(j, bv[i].blocks.size()) {
	int y = bv[i].blocks[j].first, x = bv[i].blocks[j].second;
	if(y > 0 && in[y-1][x] != in[y][x] && in[y-1][x] != -1) {
	  bv[i].above.push_back(in[y-1][x]);
	}
      }
      sort(bv[i].above.begin(), bv[i].above.end());
      vector<int>::iterator it = unique(bv[i].above.begin(), bv[i].above.end());
      bv[i].above.erase(it, bv[i].above.end());
    }

    rep(i, bv.size()) {
      int maxy = -1,bmin= 100,bmax = -1;
      rep(j,bv[i].blocks.size()) {
	int y = bv[i].blocks[j].first,x = bv[i].blocks[j].second;
	if(y < h-1 && in[y+1][x] == -1) continue;
	if(y < h-1 && in[y+1][x] == in[y][x]) continue;
	bmin = min(bmin, x);
	bmax = max(bmax, x);
      }
      bv[i].bl = bmin;
      bv[i].br = bmax+1;
    }

    /*
    rep(i,h) {
      rep(j,w) cout<<in[i][j]<<" ";
      cout<<endl;
    }

    rep(i,bv.size()) {
      cout<<"pos : "<<i<<" above = "<<bv[i].above.size()<<endl;
      cout<<"      "<<bv[i].bl<<" "<<bv[i].br<<" G: "<<bv[i].G<<endl;
    }
    */

    rep(j,w) if(in[h-1][j] != -1) root = in[h-1][j];

    calcG(root,bv);
    rep(i, bv.size()) {
      //cout<<i<<" G : "<<bv[i].G<<" "<<bv[i].xsum<<endl;
      ;
    }
    if(rec(root,bv)) cout<<"STABLE"<<endl;
    else cout<<"UNSTABLE"<<endl;
  }
}

Measured memory_bytes: 0