Language: C++

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<utility>
#include<queue>
#include<string.h>
#include<string>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
#define _CRT_SECURE_NO_WARNINGS
int w, h;
char c[62][7];
bool checked[62][7] = {};

queue<P> Q;
int dx[3] = { 1,0,-1 };
int dy[3] = { 0,1,0 };

int g[62][7] = {};
int blockNum[62][7] = {};

int hash[62][7] = {};
int nowHash=1;
bool gChecked[10];

int top[7];
int l, r;
queue<P> blockQ;
int weight = 0;
int countingBlockNum;

bool ans = true;
void SetLRT(int _y,int _x) {
	for (int i = 0; i < w;i++) {
		top[i] = 100;
	}
	weight = 0;
	l = 100; r = -1;

	P p(_y, _x);
	Q.push(p);
	checked[_y][_x] = true;
	hash[_y][_x] = nowHash;
	top[_x] = min(top[_x], _y);
	if (_y + 1 < h) {
		if (c[_y + 1][_x] != '.' && c[_y + 1][_x] != c[_y][_x]) {
			l = min(l, _x); r = max(r, _x);
		}
	}
	else {
		l = min(l, _x); r = max(r, _x);
	}
	weight += _x;
	blockQ.push(p);

	while (!Q.empty()) {
		P now = Q.front();
		Q.pop();
		for (int i = 0; i < 3; i++) {
			int y = now.first + dy[i];
			int x = now.second + dx[i];
			if (c[y][x] == c[_y][_x] && !checked[y][x]) {
				p.first = y; p.second = x;
				Q.push(p);
				checked[y][x] = true;
				hash[y][x] = nowHash;
				top[x] = min(top[x], y);
				if (y + 1 < h) {
					if (c[y + 1][x] != '.' && c[y + 1][x] != c[y][x]) {
						l = min(l, x); r = max(r, x);
					}
				}
				else {
					l = min(l, x); r = max(r, x);
				}
				weight += x; 
				blockQ.push(p);
			}
		}
	}
	nowHash++;
}

void CalcG() {
	for (int i = 1; i < nowHash;i++) {
		gChecked[i] = false;
	}
	countingBlockNum = 0;
	for (int i = 0; i < w;i++) {
		if (0 < top[i] && top[i] < 100  && c[top[i] - 1][i]!='.' && !gChecked[hash[top[i] - 1][i]]) {
			weight += g[top[i] - 1][i];
			countingBlockNum += blockNum[top[i] - 1][i];
			gChecked[hash[top[i] - 1][i]] = true;
		}
	}
	float n = (float)weight / (countingBlockNum + 4);
	if ( n <= l-0.5 || r+0.5 <= n ) {
		ans = false;
		return;
	}
	while (!blockQ.empty()) {
		P now = blockQ.front(); blockQ.pop();
		g[now.first][now.second] = weight;
		blockNum[now.first][now.second] = countingBlockNum+4;
	}
}
int main() {
	while (1) {
		scanf("%d%d", &w, &h);
		if (w==0) {
			break;
		}
		for (int i = 0; i < h; i++) {
			scanf("%s", &c[i]);
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				checked[i][j] = false;
				hash[i][j] = 0;
				g[i][j] = 0;
				blockNum[i][j] = 0;
			}
		}
		ans = true;
		nowHash = 1;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (c[i][j] != '.' && !checked[i][j]) {
					SetLRT(i,j);
					CalcG();
					if (ans == false) {
						break;
					}
				}
			}
			if (ans == false) {
				break;
			}
		}

		if (ans == false) {
			printf("UNSTABLE\n");
		}
		else {
			printf("STABLE\n");
		}
	}
	return 0;
}


Compiler output:
a.cc: In function 'void SetLRT(int, int)':
a.cc:44:9: error: reference to 'hash' is ambiguous
   44 |         hash[_y][_x] = nowHash;
      |         ^~~~
In file included from /usr/include/c++/14/bits/stl_bvector.h:65,
                 from /usr/include/c++/14/vector:67,
                 from /usr/include/c++/14/queue:63,
                 from a.cc:5:
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:23:5: note:                 'int hash [62][7]'
   23 | int hash[62][7] = {};
      |     ^~~~
a.cc:67:33: error: reference to 'hash' is ambiguous
   67 |                                 hash[y][x] = nowHash;
      |                                 ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:23:5: note:                 'int hash [62][7]'
   23 | int hash[62][7] = {};
      |     ^~~~
a.cc: In function 'void CalcG()':
a.cc:91:87: error: reference to 'hash' is ambiguous
   91 |                 if (0 < top[i] && top[i] < 100  && c[top[i] - 1][i]!='.' && !gChecked[hash[top[i] - 1][i]]) {
      |                                                                                       ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:23:5: note:                 'int hash [62][7]'
   23 | int hash[62][7] = {};
      |     ^~~~
a.cc:94:34: error: reference to 'hash' is ambiguous
   94 |                         gChecked[hash[top[i] - 1][i]] = true;
      |                                  ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:23:5: note:                 'int hash [62][7]'
   23 | int hash[62][7] = {};
      |     ^~~~
a.cc: In function 'int main()':
a.cc:120:33: error: reference to 'hash' is ambiguous
  120 |                                 hash[i][j] = 0;
      |                                 ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:23:5: note:                 'int hash [62][7]'
   23 | int hash[62][7] = {};
      |     ^~~~