#include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>

using namespace std;

typedef long long ll;

int w, h;
int field[10][60];
vector <string> temp;
int num;
vector <pair <double, double> > memo(150);	// 重心(x)と質量
bool ans;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
bool is_in(int x, int y) {
  return x >= 0 && y >= 0 && x < w && y < h;
}

// int calcSum(int x, int y, int n) {
//   int ret = x;
//   for (int i = 0; i < 4; i++) {
//     int nx = x+dx[i];
//     int ny = y+dy[i];

//     if (is_in(nx, ny) && field[nx][ny] == n) {
//       ret += calcSum(nx, ny, n);
//     }
//   }

//   return ret;
// }

pair <double, double> rec(int x, int y) {
  int col = field[x][y];
  if (memo[col].first != -1) return memo[col];

  pair<double, double> ret = make_pair(0, 0);
  vector <pair<double, double> > list;

  double xl, xr;
  xl = 100;
  xr = 0;
  double sum = 0;

  bool vis[10][60];
  memset(vis, false, sizeof(vis));
  bool vis2[150];		// 色
  memset(vis2, false, sizeof(vis2));
  vis2[col] = true;
  stack <int> st;
  st.push(x);
  st.push(y);
  vis[x][y] = true;
  sum += double(x)+0.5;
  while (!st.empty()) {
    int py = st.top(); st.pop();
    int px = st.top(); st.pop();

    for (int i = 0; i < 4; i++) {
      int nx = px+dx[i];
      int ny = py+dy[i];

      if (!is_in(nx, ny) || vis[nx][ny] || field[nx][ny] == -1) continue;
      if (dx[i] == 0 && dy[i] == 1 &&
	  field[nx][ny] != col) {
	xl = min(xl, double(nx));
	xr = max(xr, double(nx));
      }
      if (dx[i] == 0 && dy[i] == -1 &&
	  field[nx][ny] != col &&
	  !vis2[field[nx][ny]]) {
	list.push_back(rec(nx, ny));
	vis2[field[nx][ny]] = true;
      }
      if (field[nx][ny] == col) {
	sum += double(nx)+0.5;
	vis[nx][ny] = true;
	st.push(nx);
	st.push(ny);
      }
    }
  }

  double down = 0;
  double up = 0;
  for (int i = 0; i < list.size(); i++) {
    up += list[i].second*list[i].first;
    down += list[i].second;
  }
  ret.first = (up+sum)/(down+4);
  ret.second = 4+down;

  if (xl == 100) {
    for (int i = 0; i < w; i++) {
      if (field[i][y] == col) {
	xl = i;
	break;
      }
    }
  }
  if (xr == 0) {
    for (int i = w-1; i >= 0; i--) {
      if (field[i][y] == col) {
	xr = i;
	break;
      }
    }    
  }
  if (ret.first <= xl || ret.first >= xr+1) {
    // std::cout << col << ":" << xl << " " << xr << "::" << ret.first << std::endl;
    ans = false;
  }
  return memo[col] = ret;
}

void calcNum() {
  int cnt = 0;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (temp[y][x] != '.') {
	int col = temp[y][x]-'0';
	field[x][y] = cnt;
	stack <int> st;
	st.push(x);
	st.push(y);
	while(!st.empty()) {
	  int py = st.top(); st.pop();
	  int px = st.top(); st.pop();
	  temp[py][px] = '.';
	  
	  for (int i = 0; i < 4; i++) {
	    int nx = px+dx[i];
	    int ny = py+dy[i];

	    if (is_in(nx, ny) && temp[ny][nx]-'0' == col) {
	      st.push(nx);
	      st.push(ny);
	      field[nx][ny] = cnt;
	    }
	  }
	}
	cnt++;
      }
    }
  }
}

void viewField() {
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
	if (field[x][y] == -1) {
	  std::cout << ". ";
	}else {
	  std::cout << field[x][y] << " ";
	}
      }
      std::cout << "" << std::endl;
    }
    std::cout << "" << std::endl;
}

int main() {
  while(true) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;

    vector <string> mainField;
    string str;
    for (int y = 0; y < h; y++) {
      cin >> str;
      mainField.push_back(str);
    }
    temp = mainField;

    memset(field, -1, sizeof(field));
    calcNum();

    for (int i = 0; i < 150; i++) {
      memo[i].first = -1;
      memo[i].second = 0;
    }

    ans = true;
    for (int i = 0; i < w; i++) {
      if (field[i][h-1] != -1) {
    	rec(i, h-1);
    	break;
      }
    }
    if (ans) {
      std::cout << "STABLE" << std::endl;
    }else {
      std::cout << "UNSTABLE" << std::endl;
    }

  }
}

Measured memory_bytes: 1272