Language: C++

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<class T>
istream& operator >> (istream& is, vector<T>& v)
{
	for (auto &i : v) is >> i;
	return is;
}
template<class T>
ostream& operator<<(ostream& os, vector<T>& v)
{
	const string delimiter = "\n";
	REP(i, v.size())
	{
		os << v[i];
		if (i != v.size() - 1) os << delimiter;
	}
	return os;
}
/*--------------------template--------------------*/

const int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
//const int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy[] = { 0, -1, 1, -1, 1, 0, -1, 1 };
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
int place(int x, int y, int w) { return w*x + y; }

const pair<ld, ld> Fail(-1, -1);

int w, h;
vs fld;
vector<vi> index;

pair<ld, ld> solve(int v) // weight, x
{
	vector<ld> x;
	set<int> on;
	REP(i, h)REP(j, w)
	{
		if (index[i][j] != v) continue;
		x.push_back(j);
		if (i > 0 && index[i - 1][j] != -1 && index[i - 1][j] != v) on.insert(index[i - 1][j]);
	}
	for (auto u : on)
	{
		auto tmp = solve(u);
		if (tmp.first == -1) return Fail;
		int l = w, r = 0;
		FOR(i, 1, h)REP(j, w)
		{
			if (index[i][j] == v && index[i - 1][j] == u)
			{
				chmin(l, j);
				chmax(r, j);
			}
		}
		if (tmp.second + 0.5 <= l || tmp.second +0.5 >= r + 1) return Fail;
		REP(i, tmp.first) x.push_back(tmp.second);
	}
	ld sum = 0;
	for (auto i : x) sum += i;
	return make_pair(x.size(), sum / x.size());
}

int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	while (cin >> w >> h, w)
	{
		fld.clear(); index.clear();
		fld.resize(h); cin >> fld;
		index.resize(h, vi(w, -2));
		int tmp = 0;
		REP(i, h)REP(j, w)
		{
			if (fld[i][j] == '.') index[i][j] = -1;
			if (index[i][j] >= -1) continue;
			queue<pii> que;
			que.emplace(i, j);
			char c = fld[i][j];
			index[i][j] = tmp;
			while (!que.empty())
			{
				int x = que.front().first, y = que.front().second;
				que.pop();
				REP(i, 4)
				{
					int nx = x + dx[i], ny = y + dy[i];
					if (!valid(nx, ny, h, w) || index[nx][ny] >= -1 || fld[nx][ny] != c) continue;
					index[nx][ny] = tmp;
					que.emplace(nx, ny);
				}
			}
			tmp++;
		}
		int root, l = w, r = 0;
		REP(j, w)
		{
			if (index[h - 1][j] != -1)
			{
				root = index[h - 1][j];
				chmin(l, j);
				chmax(r, j);
			}
		}
		auto res = solve(root);
		if (res.first >= 0 && (res.second + 0.5 > l || res.second + 0.5 < r + 1)) cout << "STABLE" << endl;
		else cout << "UNSTABLE" << endl;
	}
	return 0;
}

Compiler output:
a.cc:43:12: error: 'std::vector<std::vector<int> > index' redeclared as different kind of entity
   43 | vector<vi> index;
      |            ^~~~~
In file included from /usr/include/string.h:462,
                 from /usr/include/c++/14/cstring:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:121,
                 from a.cc:1:
/usr/include/strings.h:50:20: note: previous declaration 'const char* index(const char*, int)'
   50 | extern const char *index (const char *__s, int __c)
      |                    ^~~~~
a.cc: In function 'std::pair<long double, long double> solve(int)':
a.cc:51:26: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   51 |                 if (index[i][j] != v) continue;
      |                          ^
a.cc:53:35: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   53 |                 if (i > 0 && index[i - 1][j] != -1 && index[i - 1][j] != v) on.insert(index[i - 1][j]);
      |                                   ^
a.cc:53:60: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   53 |                 if (i > 0 && index[i - 1][j] != -1 && index[i - 1][j] != v) on.insert(index[i - 1][j]);
      |                                                            ^
a.cc:53:92: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   53 |                 if (i > 0 && index[i - 1][j] != -1 && index[i - 1][j] != v) on.insert(index[i - 1][j]);
      |                                                                                            ^
a.cc:62:34: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   62 |                         if (index[i][j] == v && index[i - 1][j] == u)
      |                                  ^
a.cc:62:54: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   62 |                         if (index[i][j] == v && index[i - 1][j] == u)
      |                                                      ^
a.cc: In function 'int main()':
a.cc:81:36: error: overloaded function with no contextual type information
   81 |                 fld.clear(); index.clear();
      |                                    ^~~~~
a.cc:83:23: error: overloaded function with no contextual type information
   83 |                 index.resize(h, vi(w, -2));
      |                       ^~~~~~
a.cc:87:52: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   87 |                         if (fld[i][j] == '.') index[i][j] = -1;
      |                                                    ^
a.cc:88:34: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   88 |                         if (index[i][j] >= -1) continue;
      |                                  ^
a.cc:92:30: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   92 |                         index[i][j] = tmp;
      |                              ^
a.cc:100:74: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  100 |                                         if (!valid(nx, ny, h, w) || index[nx][ny] >= -1 || fld[nx][ny] != c) continue;
      |                                                                          ^
a.cc:101:46: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  101 |                                         index[nx][ny] = tmp;
      |                                              ^
a.cc:110:34: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  110 |                         if (index[h - 1][j] != -1)
      |                                  ^
a.cc:112:45: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  112 |                                 root = index[h - 1][j];
      |                                             ^