#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // #include using ll = long long; using ld = long double; using namespace std; // using namespace atcoder; using P = pair; using Graph = vector>; using Priority = priority_queue, greater>;// 昇順 using Priority_pair = priority_queue, greater

>; // using mint_17 = modint1000000007; // using mint = modint998244353; #define mod 1000000007 #define MAX_WIDTH 60 #define MAX_HEIGHT 60 #define INF 1e18 #define MOD 998244353 #define PI 3.141592653589793 #define rep(i, a, b) for(ll i=(a);i<(b);i++) #define rrep(i, a, b) for(ll i=(a);i>=(b);i--) #define fore(i, a) for(auto &i: a) #define all(v) (v).begin(), (v).end() #define YES(a) cout << ((a) ? "YES" : "NO") << endl #define Yes(a) cout << ((a) ? "Yes" : "No") << endl #define pb(a) push_back(a) #define vi vector #define vll vector #define vs vector #define vc vector #define vp vector> #define mll map #define msl map #define COUT(n) cout << (n) << endl #define isInGrid(a, b, h, w) (0 <= (a) && (a) < (h) && 0 <= (b) && (b) < (w)) template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1}; struct edge { ll to, cost; }; ll H, W, G[3010][3010]; char C[3010][3010]; int main() { cin >> H >> W; rep(i,0,H) { rep(j,0,W) { cin >> C[i][j]; ll n = C[i][j]-'a'+1; G[i][j] = n; } } string ans; queue> que; ll dist[3010][3010]; dist[0][0] = 0; que.push({0, 0}); while(!que.empty()) { pair v = que.front(); que.pop(); ll h = v.first, w = v.second; ll a = INF, b = INF; if(h+1 < H) { a = G[h+1][w]; } if(w+1 < W) { b = G[h][w+1]; } if(a == INF && b == INF) { //pass } else if(a == INF) { dist[h][w+1] = dist[h][w]+1; que.push(make_pair(h, w+1)); } else if(b == INF) { dist[h+1][w] = dist[h][w]+1; que.push(make_pair(h+1, w)); } else { if(a == b) { dist[h+1][w] = dist[h][w]+1; que.push(make_pair(h+1, w)); dist[h][w+1] = dist[h][w]+1; que.push(make_pair(h, w+1)); } else if(a < b) { dist[h+1][w] = dist[h][w]+1; que.push(make_pair(h+1, w)); } else { dist[h][w+1] = dist[h][w]+1; que.push(make_pair(h, w+1)); } } } ll now_h = H-1, now_w = W-1; while(1) { ans = ans + C[now_h][now_w]; if(now_h == H-1 && now_w == W-1) { break; } if(now_h == 0) { now_w--; } else if(now_w == 0) { now_h--; } else { if(dist[now_h-1][now_w] < dist[now_h][now_w-1]) { now_h--; } else { now_w--; } } } reverse(all(ans)); COUT(ans); return 0; }