#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <set>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int w, h;
int n;
char inifield[100][100];
int field[100][100];
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
set<int> tree[1000];
pair<int, int> range[1000];
int weight[1000];

void printFiled() {
  REP(y, h) {
    REP(x, w) {
      printf("%3d", field[y][x]);
    }
    puts("");
  }
}

void dfs(int y, int x, char c, int num) {
  weight[num] += x * 2 + 1;
  field[y][x] = num;
  inifield[y][x] = '.'; 
  REP(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (inifield[ny][nx] != c) { continue; }
    dfs(ny, nx, c, num);
  }
}

pair<int, int> calc(int index) {
  pair<int, int> total = make_pair(0, 0);
  FORIT(it, tree[index]) {
    pair<int, int> ntotal = calc(*it);
    if (ntotal.first == -1) { return ntotal; }
    total.first += ntotal.first;
    total.second += ntotal.second;
  }
  total.first += weight[index];
  total.second += 4;
  double g = (double)total.first / total.second;
  //cout << index << " " << g << " " << range[index].first << " " << range[index].second << endl;
  if (g <= range[index].first || range[index].second <= g) { return make_pair(-1, -1); }
  return total;
}

int main() {
  while (scanf("%d %d", &w, &h), w|h) {
    MEMSET(field, -1);
    MEMSET(inifield, -1);
    MEMSET(weight, 0);
    REP(y, h) {
      REP(x, w) {
        char c;
        scanf(" %c ", &c);
        inifield[y][x] = c;
      }
    }
    n = 0;
    for (int y = h - 1; y >= 0; y--) {
      REP(x, w) {
        if (inifield[y][x] == '.') { continue; }
        dfs(y, x, inifield[y][x], n);
        tree[n].clear();
        range[n] = make_pair(1000, -1);
        n++;
      }
    }
    REP(y, h) {
      REP(x, w) {
        if (field[y][x] != -1 && field[y + 1][x] != field[y][x]) {
          if ((field[y][x] == 0 && y == h - 1) || field[y + 1][x] != -1) {
            int index = field[y][x];
            int nindex = field[y + 1][x];
            if (nindex != -1) {
              tree[nindex].insert(index);
            }
            range[index].first = min(range[index].first, 2 * x);
            range[index].second = max(range[index].second, 2 * x + 2);
          }
        }
      }
    }
    if (calc(0).first != -1) {
      puts("STABLE");
    } else {
      puts("UNSTABLE");
    }
  }
}