#include #include // #include // #include using namespace std; using namespace atcoder; // using namespace __gnu_pbds; using ll=long long; #define int ll using ld = long double; #define _overload3(_1,_2,_3,name,...) name #define _rep(i,n) repi(i,0,n) #define repi(i,a,b) for(int i=int(a);i=int(0);--i) #define fore(i,a) for(auto &i:a) #define all(x) x.begin(),x.end() #define sz(x) ((int)(x).size()) #define bp(x) (__builtin_popcount((long long)(x))) #define pb push_back #define eb emplace_back #define mpa make_pair #define bit(n) (1LL<<(n)) template using V = vector; template using VV = V>; template using max_heap = priority_queue; template using min_heap = priority_queue, greater<>>; #define P pair #define TP tuple #define F first #define S second templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b>h>>w; V s(h); rep(i,h)cin>>s[i]; const int d[]={0,1,0}; string ans; deque dq; dq.emplace_back(s[0][0],0,0); V> col(h,V(w)); while(sz(dq)){ multiset st; ans += dq.front().a; int n=sz(dq); rep(i,n){ auto [a,b,c]=dq.front(); dq.pop_front(); rep(j,2){ int nb=b+d[j]; int nc=c+d[j+1]; if(nb>=h||nc>=w||col[nb][nc])continue; st.emplace(s[nb][nc],nb,nc); col[nb][nc]=1; } } if(sz(st)==0)break; char t=st.begin()->a; for(auto [a,b,c]:st){ if(t==a)dq.emplace_back(a,b,c); else break; } } cout<