#include <cstdio>
#include <cstdint>
#include <vector>
#include <set>
#include <algorithm>
#include <utility>
#include <string>
#include <tuple>

using zahyo = std::pair<size_t, size_t>;
constexpr size_t m1 = -1;
constexpr size_t di[] = {m1, 0, 1, 0};
constexpr size_t dj[] = {0, m1, 0, 1};

class union_find {
  mutable std::vector<intmax_t> c;

public:
  union_find(size_t n): c(n, -1) {}

  size_t find(size_t v) const {
    if (c[v] < 0) return v;
    return (c[v] = find(c[v]));
  }

  bool unite(size_t u, size_t v) {
    u = find(u);
    v = find(v);
    if (u == v) return false;
    if (-c[u] > -c[v]) std::swap(u, v);
    c[v] += c[u];
    c[u] = v;
    return true;
  }

  bool connected(size_t u, size_t v) const { return (find(u) == find(v)); }

  size_t size() const { return c.size(); }
  size_t size(size_t v) const {
    return -c[find(v)];
  }
};

std::vector<zahyo> components(std::vector<std::string> const& s,
                              size_t g, union_find const& uf) {
  size_t h = s.size();
  size_t w = s[0].length();
  std::vector<zahyo> res;
  for (size_t i = 0; i < h; ++i)
    for (size_t j = 0; j < w; ++j)
      if (uf.find(i*w+j) == g) res.emplace_back(i, j);
  return res;
}

std::pair<double, double> pomae(std::vector<std::string> const& s,
                                size_t i, size_t j, union_find const& uf) {

  // fprintf(stderr, "s[%zu][%zu]: %c\n", i, j, s[i][j]);
  size_t h = s.size();
  size_t w = s[0].length();
  std::vector<zahyo> comp = components(s, uf.find(i*w+j), uf);
  std::set<size_t> seen;
  double L = w;
  double R = 0;
  std::vector<zahyo> next;
  double weight = 4;
  double jushin = 0;
  for (auto const& p: comp) {
    size_t pi, pj;
    std::tie(pi, pj) = p;
    seen.insert(uf.find(pi*w+pj));

    double pj_d = pj;
    if (pi == h-1 || (s[pi+1][pj] != '.' && !uf.connected(pi*w+pj, (pi+1)*w+pj))) {
      L = std::min(L, pj_d);
      R = std::max(R, pj_d+1);
    }

    jushin += (pj_d + pj_d + 1) * 0.5;
    if (!(pi-1 < h)) continue;
    if (s[pi-1][pj] != '.') next.emplace_back(pi-1, pj);
  }

  for (auto const& z: next) {
    size_t pi, pj;
    std::tie(pi, pj) = z;
    if (seen.count(uf.find(pi*w+pj))) continue;
    double c1;
    double w1;
    std::tie(c1, w1) = pomae(s, pi, pj, uf);
    if (w1 < 0) return {-1, -1};
    weight += w1;
    jushin += w1 * c1;
    seen.insert(uf.find(pi*w+pj));
  }

  jushin /= weight;
  // fprintf(stderr, "L, R: %f, %f\n", L, R);
  // fprintf(stderr, "center: %f, weight: %f\n", jushin, weight);
  if (L < jushin && jushin < R) return {jushin, weight};
  return {-1, -1};
}

bool stable(std::pair<double, double> const& d) {
  return d.first >= 0;
}

int testcase_ends() {
  size_t h, w;
  scanf("%zu %zu", &w, &h);
  if (h == 0 && w == 0) return 1;

  std::vector<std::string> s(h);
  for (auto& si: s) {
    char buf[128];
    scanf("%s", buf);
    si = buf;
  }

  union_find uf(h*w);
  for (size_t i = 0; i < h; ++i)
    for (size_t j = 0; j < w; ++j)
      for (size_t k = 0; k < 4; ++k) {
        size_t ni = i + di[k];
        size_t nj = j + dj[k];
        if (!(ni < h && nj < w)) continue;
        if (s[i][j] == s[ni][nj]) uf.unite(i*w+j, ni*w+nj);
      }

  for (size_t j = 0; j < w; ++j) {
    if (s[h-1][j] == '.') continue;
    if (!stable(pomae(s, h-1, j, uf))) return puts("UNSTABLE"), 0;
  }
  puts("STABLE");
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}



Measured memory_bytes: 2832