Language: C++

#include <iostream>
#include <map>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdlib>
#include <queue>
#include <cstdio>
#include <memory>
#include <cctype>
using namespace std;

const double EPS = 1e-10;
const int INF = 1 << 30;
typedef pair<double, double> Pd;
typedef pair<int, int> P;
typedef pair<P, Pd> data;

int w, h;
int n;
char field[70][10];
int nums[70][10];
data dat[600];
vector<int> ch[600];


P dfs(int v, bool &f){
  
  if(ch[v].empty()){
    return dat[v].first;
  }
  P res = dat[v].first;
  for(int  i = 0; i < (int)ch[v].size(); i++){
    P p = dfs(ch[v][i], f);
    double l = dat[ch[v][i]].second.first;
    double r = dat[ch[v][i]].second.second;
    double g = (double)p.second / p.first;
    if(!f || g < l + EPS || r < g + EPS){
      f = false;
      return res;
    }else{
      res.first += p.first;
      res.second += p.second;
    }
  }

  return res;
}

P bfs(int i, int j, char c, int v){
  int dx[] = { 1, 0, -1, 0};
  int dy[] = {0, -1, 0, 1};
  int weight = 1;
  int N = j;
  nums[i][j] = v;
  queue<P> que;
  que.push(P(i, j));

  while(!que.empty()){
    P p = que.front();
    que.pop();
    for(int k = 0; k < 4; k++){

      int i2 = p.first + dy[k];
      int j2 = p.second + dx[k];
      if(0 <= i2 && i2 < h && 0 <= j2 && j2 < w &&
	 nums[i2][j2] == -1 && field[i2][j2] == c){
	nums[i2][j2] = v;
	weight++;
	N += j2;
	que.push(P(i2, j2));
      }
    }
  }
  return P(weight, N);
}
  
  
int main(){
  while(cin >> w >> h &&(w || h)){
    n = 0;
    memset(nums, -1, sizeof(nums));
    fill(&field[0][0], &field[69][9] + 1, '.');
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	cin >> field[h-i-1][j];
      }
    }
    /*
    for(int i = 0; i < h; i++) {
      for(int j= 0; j < w; j++){
	cout << field[i][j];
      }
      cout << endl;;
      }*/
	
    for(int i = 0; i < h; i++){
      for(int j= 0; j < w; j++){
	if(field[i][j] != '.' && nums[i][j] == -1){
	  // cout << i << " " << j << " " << nums[i][j] << endl;
	  dat[n].first = bfs(i, j, field[i][j], n);
	  //	  cout << dat[n].first.first << endl;
	  n++;
	}
      }
    }
    int l = INF;
    int r = 0;
    for(int i = 0; i < w; i++){
      if(nums[0][i] == 0) {
	l = min(l, i);
	r = max(r, i);
      }
    }
    dat[0].second = Pd(l - 0.5, r + 0.5);
    
    for(int i = 1; i < n; i++){

      int left = INF;
      int right = 0;

      for(int j = 1; j < h; j++){
	for(int k = 0; k < w; k++){
	  if(nums[j][k] == i && nums[j-1][k] >= 0 && nums[j-1][k] != i){
	    left = min(left, k);
	    right = max(right, k);
	    
	  }
	}
      }

      dat[i].second = Pd(left - 0.5, right + 0.5);
    }
    
    for(int i = 0; i < h-1; i++){
      for(int j = 0; j < w; j++){
	if(nums[i][j] >= 0 && nums[i+1][j] >= 0&&nums[i][j] != nums[i+1][j]){
	  ch[nums[i][j]].push_back(nums[i+1][j]);
	}
      }
    }
    for(int i = 0; i < n; i++) {
      sort(ch[i].begin(), ch[i].end());
      ch[i].erase(unique(ch[i].begin(), ch[i].end()), ch[i].end());
    }
    bool ok = true;
    
    P p = dfs(0, ok);
    double g = (double) p.second / p.first;
    
    if(ok && dat[0].second.first < g && g < dat[0].second.second){
      cout << "STABLE" << endl;
    }else{
      cout << "UNSTABLE" << endl;
    }
    for(int i = 0; i < n; i++) ch[i].clear();
  }
  return 0;
}

Compiler output:
a.cc:24:1: error: reference to 'data' is ambiguous
   24 | data dat[600];
      | ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:18:21: note:                 'typedef struct std::pair<std::pair<int, int>, std::pair<double, double> > data'
   18 | typedef pair<P, Pd> data;
      |                     ^~~~
a.cc: In function 'P dfs(int, bool&)':
a.cc:31:12: error: 'dat' was not declared in this scope; did you mean 'data'?
   31 |     return dat[v].first;
      |            ^~~
      |            data
a.cc:33:11: error: 'dat' was not declared in this scope; did you mean 'data'?
   33 |   P res = dat[v].first;
      |           ^~~
      |           data
a.cc: In function 'int main()':
a.cc:83:5: error: 'memset' was not declared in this scope
   83 |     memset(nums, -1, sizeof(nums));
      |     ^~~~~~
a.cc:12:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
   11 | #include <cctype>
  +++ |+#include <cstring>
   12 | using namespace std;
a.cc:102:11: error: 'dat' was not declared in this scope; did you mean 'data'?
  102 |           dat[n].first = bfs(i, j, field[i][j], n);
      |           ^~~
      |           data
a.cc:116:5: error: 'dat' was not declared in this scope; did you mean 'data'?
  116 |     dat[0].second = Pd(l - 0.5, r + 0.5);
      |     ^~~
      |     data