#include <iostream>
#include <map>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <numeric>
#include <memory.h>
#include <string>
#include <vector>
#include <cstdlib>
#include <queue>
#include <cstdio>
#include <memory>
#include <cctype>
using namespace std;

const double EPS = 1e-10;
const int INF = 1 << 30;
typedef pair<double, double> Pd;
typedef pair<int, int> P;
typedef pair<P, Pd> data;

int w, h;
int n;
char field[70][10];
int nums[70][10];
data dat[600];
vector<int> ch[600];


P dfs(int v, bool &f){
  
  if(ch[v].empty()){
    return dat[v].first;
  }
  P res = dat[v].first;
  for(int  i = 0; i < (int)ch[v].size(); i++){
    P p = dfs(ch[v][i], f);
    double l = dat[ch[v][i]].second.first;
    double r = dat[ch[v][i]].second.second;
    double g = (double)p.second / p.first;
    if(!f || g < l + EPS || r < g + EPS){
      f = false;
      return res;
    }else{
      res.first += p.first;
      res.second += p.second;
    }
  }

  return res;
}

P bfs(int i, int j, char c, int v){
  int dx[] = { 1, 0, -1, 0};
  int dy[] = {0, -1, 0, 1};
  int weight = 1;
  int N = j;
  nums[i][j] = v;
  queue<P> que;
  que.push(P(i, j));

  while(!que.empty()){
    P p = que.front();
    que.pop();
    for(int k = 0; k < 4; k++){

      int i2 = p.first + dy[k];
      int j2 = p.second + dx[k];
      if(0 <= i2 && i2 < h && 0 <= j2 && j2 < w &&
	 nums[i2][j2] == -1 && field[i2][j2] == c){
	nums[i2][j2] = v;
	weight++;
	N += j2;
	que.push(P(i2, j2));
      }
    }
  }
  return P(weight, N);
}
  
  
int main(){
  while(cin >> w >> h &&(w || h)){
    n = 0;
    memset(nums, -1, sizeof(nums));
    fill(&field[0][0], &field[69][9] + 1, '.');
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	cin >> field[h-i-1][j];
      }
    }
    /*
    for(int i = 0; i < h; i++) {
      for(int j= 0; j < w; j++){
	cout << field[i][j];
      }
      cout << endl;;
      }*/
	
    for(int i = 0; i < h; i++){
      for(int j= 0; j < w; j++){
	if(field[i][j] != '.' && nums[i][j] == -1){
	  // cout << i << " " << j << " " << nums[i][j] << endl;
	  dat[n].first = bfs(i, j, field[i][j], n);
	  //	  cout << dat[n].first.first << endl;
	  n++;
	}
      }
    }
    int l = INF;
    int r = 0;
    for(int i = 0; i < w; i++){
      if(nums[0][i] == 0) {
	l = min(l, i);
	r = max(r, i);
      }
    }
    dat[0].second = Pd(l - 0.5, r + 0.5);
    
    for(int i = 1; i < n; i++){

      int left = INF;
      int right = 0;

      for(int j = 1; j < h; j++){
	for(int k = 0; k < w; k++){
	  if(nums[j][k] == i && nums[j-1][k] >= 0 && nums[j-1][k] != i){
	    left = min(left, k);
	    right = max(right, k);
	    
	  }
	}
      }

      dat[i].second = Pd(left - 0.5, right + 0.5);
    }
    
    for(int i = 0; i < h-1; i++){
      for(int j = 0; j < w; j++){
	if(nums[i][j] >= 0 && nums[i+1][j] >= 0&&nums[i][j] != nums[i+1][j]){
	  ch[nums[i][j]].push_back(nums[i+1][j]);
	}
      }
    }
    for(int i = 0; i < n; i++) {
      sort(ch[i].begin(), ch[i].end());
      ch[i].erase(unique(ch[i].begin(), ch[i].end()), ch[i].end());
    }
    bool ok = true;
    
    P p = dfs(0, ok);
    double g = (double) p.second / p.first;
    
    if(ok && dat[0].second.first < g && g < dat[0].second.second){
      cout << "STABLE" << endl;
    }else{
      cout << "UNSTABLE" << endl;
    }
    for(int i = 0; i < n; i++) ch[i].clear();
  }
  return 0;
}

Measured memory_bytes: 0