#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using uint = unsigned; using vi = vector; using vl = vector; using vll = vector; using vd = vector; using vvi = vector; using vvl = vector; using vvll = vector; using vc = vector; using vs = vector; using vb = vector; using pii = pair; using pcc = pair; using pll = pair; using pdd = pair; using vpii = vector; using vpll = vector; typedef complex xy_t; templatebool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } #define rep(i, n) for (ll i = 0; i < ll(n); i++) #define repback(i, n) for (ll i = n-1; i >= 0; i--) #define REP(i, a, b) for (ll i = a; i < ll(b); i++) #define REPBACK(i, a, b) for (ll i = a-1; i >= ll(b); i--) #define all(x) (x).begin(), (x).end() #define EPS (1e-10) #define equals(a,b) (fabs((a) - (b)) < EPS) #define debug(x) cout << "debug:" << x << endl #define debug2(x, y) cout << "debug:" << x << " " << y << endl static const double pi = acos(-1.0); const long long INFL = pow(10,18); const long long INFLMAX = 9223372036854775807; const int INF = pow(10,9); const int INFMAX = 2147483647; const int mod1 = 1000000007; const int mod2 = 998244353; const vi dx = {1,0,-1,0}; const vi dy = {0,1,0,-1}; const vi dx2 = {0, 1, 1, 1, 0, -1, -1, -1, 0}; const vi dy2 = {1, 1, 0, -1, -1, -1, 0, 1, 1}; int H = 3000; int W = 3000; vs S(H); int main(){ cin.tie(0); ios_base::sync_with_stdio(false); cin >> H >> W; rep(i,H) cin >> S[i]; //0-indexed H--; W--; int nowy = 0; int nowx = 0; string ans = ""; ans += S[0][0]; while(nowy != H || nowx != W){ if(nowy == H){ nowx++; ans += S[nowy][nowx]; continue; } if(nowx == W){ nowy++; ans += S[nowy][nowx]; continue; } if(S[nowy+1][nowx] > S[nowy][nowx+1]){ nowx++; ans += S[nowy][nowx]; continue; } if(S[nowy+1][nowx] < S[nowy][nowx+1]){ nowy++; ans += S[nowy][nowx]; continue; } if(S[nowy+1][nowx] == S[nowy][nowx+1]){ ans += S[nowy][nowx+1]; nowx++; } } cout << ans << endl; }