#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct Block {
  Block() : xl(0), xr(0), m(0), mg(0) {}

  int xl, xr;
  int m, mg;

  vector<Block> children;
};

void MakeTree(Block &b, int y, int x, char n,
	      vector<vector<char> > &q, int ly) {
  if (q[y][x] == '.') {
    return;
  }

  if ((q[y][x] & 191) != n && ly == y + 1) {
    b.xl = min(x, b.xl);
    b.xr = max(x, b.xr);
    return;
  }

  if (q[y][x] & 64) {
    return;
  }

  if (q[y][x] != n && ly == y - 1) {
    Block child;
    child.xl = child.xr = x;
    MakeTree(child, y, x, q[y][x], q, y);
    b.children.push_back(child);
    return;
  }

  if (q[y][x] != n) {
    return;
  }

  b.m++;
  b.mg += x;
  q[y][x] |= 64;

  MakeTree(b, y, x - 1, n, q, y);
  MakeTree(b, y, x + 1, n, q, y);
  MakeTree(b, y - 1, x, n, q, y);
  MakeTree(b, y + 1, x, n, q, y);
}

bool Stable(Block &b) {
  bool stable = true;
  for (int i = 0; i < b.children.size(); i++) {
    stable = stable && Stable(b.children[i]);

    b.m += b.children[i].m;
    b.mg += b.children[i].mg;
  }

  double g = (double)b.mg / b.m - 0.5;

  return stable && g > b.xl - 1 && g < b.xr;
}

int main() {
  int w, h;
  while (cin >> w >> h, w != 0) {
    vector<vector<char> > q(h + 2);
    for (int i = 0; i < h + 2; i++) {
      q[i].resize(w + 2);
      fill(q[i].begin(), q[i].end(), '.');
    }

    for (int y = h; y >= 1; y--) {
      for (int x = 1; x <= w; x++) {
        cin >> q[y][x];
      }
    }

    Block root;
    for (int x = w; x >= 1; x--) {
      if (q[1][x] != '.') {
        root.xr = x;
        break;
      }
    }

    for (int x = 1; x <= w; x++) {
      if (q[1][x] != '.') {
        root.xl = x;
        MakeTree(root, 1, x, q[1][x], q, 1);
        break;
      }
    }

    cout << (Stable(root) ? "STABLE" : "UNSTABLE") << endl;
  }

  return 0;
}