Language: C++

#include<iostream>
#include<queue>
#include<vector>
#include<string>
#include<map>
#define all(c) c.begin(),c.end()
#define uni(c) c.erase(unique(all(c)),c.end())
#define pb push_back
using namespace std;
typedef pair<int,int> P;
typedef pair<double,int> Pdi;
int w,h;
vector<string> s;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
bool infalpha(int y,int x){
	if(y>=0&&y<h&&x>=0&&x<w&&s[y][x]!='.')
		return true;
	return false;
}
Pdi ret(int y,int x,vector<vector<bool> > &c){
	double zyu=0;
	int cnt=0;
	vector<P> next;
	queue<P> que;
	que.push(P(y,x));
	char col=s[y][x];
	while(!que.empty()){
		P t=que.front();
		que.pop();
		if(c[t.first][t.second]){
			continue;
		}
		c[t.first][t.second]=true;
		cnt++;
		zyu+=(double)t.second+(double)0.5;
		for(int i=0;i<4;i++){
			int nx=t.second+dx[i];
			int ny=t.first+dy[i];
			if(!infalpha(ny,nx))
				continue;
			if(c[ny][nx]){
				continue;
			}
			if(i==1){
				if(s[ny][nx]==col){
					que.push(P(ny,nx));
				}else{
					next.pb(P(ny,nx));
				}
			}else{
				if(s[ny][nx]==col){
					que.push(P(ny,nx));
				}
			}
		}
		
	}
	sort(all(next));
	uni(next);
	for(int i=0;i<next.size();i++){
		P a=ret(next[i].first,next[i].second,c);
		zyu+=(double)a.first;
		cnt+=a.second;
	}
	return P(zyu,cnt);
}
P rl(int y,int x,vector<vector<bool> > &f){
	queue<P> que;
	que.push(P(y,x));
	char col=s[y][x];
	int l=100,r=0;
	while(!que.empty()){
		P t=que.front();
		que.pop();
		if(f[t.first][t.second]){
			continue;
		}
		f[t.first][t.second]=true;
		if(t.first==h-1){
			r=max(r,t.second);
			l=min(l,t.second);
		}
		for(int i=0;i<4;i++){
			int nx=t.second+dx[i];
			int ny=t.first+dy[i];
			if(!infalpha(ny,nx))
				continue;
			if((i==0&&s[ny][nx]!=col)){
				r=max(r,nx);
				l=min(l,nx);
			}
			if(f[ny][nx]||s[ny][nx]!=col){
				continue;
			}
			que.push(P(ny,nx));
		}
		
	}
	return P(r+1,l);
}
int main(){
	while(cin>>w>>h,w||h){
		s.resize(h);
		for(int i=0;i<h;i++){
			cin>>s[i];
		}
		bool flag=true;
		vector<vector<bool> > f(h,vector<bool>(w,false));
		for(int i=0;i<h;i++){//y
			for(int j=0;j<w;j++){//x
				if(s[i][j]!='.'&&!f[i][j]){
					vector<vector<bool> > c(h,vector<bool>(w,false));
					Pdi sum=ret(i,j,c);
					P side=rl(i,j,f);
					//cout<<"    "<<side.first<<"  "<<side.second<<endl;
					//cout<<"     lk:p    "<<sum.first<<"  "<<sum.second<<endl;
					double zyushin=(double)sum.first/(double)sum.second;
					//cout<<zyushin<<endl;
					//cout<<"    "<<side.second<<"  "<<side.first<<endl;
					if(!((double)side.second*(double)sum.second<(double)sum.first&&(double)side.first*(double)sum.second>(double)sum.first)){
						flag=false;
					}
					/*if(!(zyushin>side.second&&zyushin<side.first)){
						flag=false;
					}*/
				}
			}
		}
		if(flag)
		cout<<"STABLE"<<endl;
		else
		cout<<"UNSTABLE"<<endl;
	}
	return 0;
}

Compiler output:
a.cc: In function 'Pdi ret(int, int, std::vector<std::vector<bool> >&)':
a.cc:58:9: error: 'sort' was not declared in this scope; did you mean 'short'?
   58 |         sort(all(next));
      |         ^~~~
      |         short
a.cc:7:24: error: 'unique' was not declared in this scope
    7 | #define uni(c) c.erase(unique(all(c)),c.end())
      |                        ^~~~~~
a.cc:59:9: note: in expansion of macro 'uni'
   59 |         uni(next);
      |         ^~~