#include <stdio.h>
#include <algorithm>

using namespace std;

int w, h; // 1 &#8804; w &#8804; 10 かつ 1 &#8804; h &#8804; 60 
char p[60][11];
int tmp[150];
int g[150][10];
int xl[150], xr[150];
int cnt;
int f[60][10];

bool input() {
  int i;

  scanf("%d %d", &w, &h);
  if (!w && !h)
    return false;
  for (i = 0; i < h; i++) {
    scanf("%s", p[i]);
  }

  return true;
}

void funk(int x, int y) {
  int i, j, k;
  int dx[] = { 0, 1, 0, -1 };
  int dy[] = { -1, 0, 1, 0 };
  char c;
  int a[4], b[4];
  int n = 0;
  //int xl, xr;

  c = p[y][x];
  f[y][x] = cnt;
  a[n] = y;
  b[n] = x;
  n++;

  for (i = 0; i < 4; i++) {
    if (0 <= y + dy[i] && y + dy[i] < h && 0 <= x + dx[i] && x + dx[i] < w
        && c == p[y + dy[i]][x + dx[i]]) {
      y += dy[i];
      x += dx[i];
      if (f[y][x] != cnt) {
        f[y][x] = cnt;
        a[n] = y;
        b[n] = x;
        n++;
      }
      for (j = 0; j < 4; j++) {
        if (0 <= y + dy[j] && y + dy[j] < h && 0 <= x + dx[j] && x + dx[j] < w
            && c == p[y + dy[j]][x + dx[j]]) {
          y += dy[j];
          x += dx[j];
          if (f[y][x] != cnt) {
            f[y][x] = cnt;
            a[n] = y;
            b[n] = x;
            n++;
          }
          for (k = 0; k < 4; k++) {
            if (0 <= y + dy[k] && y + dy[k] < h && 0 <= x + dx[k]
                && x + dx[k] < w && c == p[y + dy[k]][x + dx[k]]) {
              y += dy[k];
              x += dx[k];
              if (f[y][x] != cnt) {
                f[y][x] = cnt;
                a[n] = y;
                b[n] = x;
                n++;
              }
              y -= dy[k];
              x -= dx[k];
            }
          }
          y -= dy[j];
          x -= dx[j];
        }
      }
      y -= dy[i];
      x -= dx[i];
    }
  }
  xl[cnt] = w;
  xr[cnt] = -1;
  for (i = 0; i < 4; i++) {
    g[cnt][b[i]]++;
    if (a[i] + 1 == h || (p[a[i] + 1][b[i]] != '.' && p[a[i] + 1][b[i]] != c)) {
      if (xl[cnt] > b[i])
        xl[cnt] = b[i];
      if (xr[cnt] < b[i])
        xr[cnt] = b[i];
    }
  }
  n = cnt;
  cnt++;
  for (i = 0; i < 4; i++) {
    if (a[i] > 0 && f[a[i] - 1][b[i]] == -1 && p[a[i] - 1][b[i]] != '.') {
      tmp[cnt] = n;
      funk(b[i], a[i] - 1);
    }
  }

}

int main() {
  int i, j;
  int a, b, c, d;

  while (input()) {
    for (i = 0; i < w; i++) {
      if (p[h - 1][i] != '.')
        break;
    }
    cnt = 0;
    fill(g[0], g[0] + 150 * 10, 0);
    fill(f[0], f[0] + 60 * 10, -1);
    tmp[cnt] = -1;
    funk(i, h - 1);
    for (i = cnt - 1; i > 0; i--) {
      for (j = 0; j < w; j++) {
        g[tmp[i]][j] += g[i][j];
      }
    }
    for (i = cnt - 1; i >= 0; i--) {
      a = 0;
      b = 0;
      c = 0;
      d = 0;
      for (j = 0; j < w; j++) {
        if (j < xl[i])
          a += g[i][j] * ((xl[i] - j) * 2 - 1);
        else
          b += g[i][j] * ((j - xl[i] + 1) * 2 - 1);
        if (j > xr[i])
          c += g[i][j] * ((j - xr[i]) * 2 - 1);
        else
          d += g[i][j] * ((xr[i] - j + 1) * 2 - 1);
      }
      //printf("%d a%d b%d c%d d%d\n", i, a, b, c, d);
      if (a >= b || c >= d) {
        printf("UNSTABLE\n");
        break;
      }
    }
    if (i == -1)
      printf("STABLE\n");
    /*
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        printf("%3d ", f[i][j]);
      }
      printf("\n");
    }

    for (i = 0; i < cnt; i++) {
      printf("%d <- %d (%d, %d)\n", i, tmp[i], xl[i], xr[i]);
      for (j = 0; j < w; j++) {
        printf("%2d ", g[i][j]);
      }
      printf("\n");
    }
    */
  }

  return 0;
}

Measured memory_bytes: 0