#include #include #include #include using namespace std; int main(){ int h, w; cin >> h >> w; vector s(h); for(auto &it: s) cin >> it; vector>> cnt(h+w, vector>(26)); cnt[0][s[0][0]-'a'].emplace_back(0); vector> pre(h, vector(w, -1)); for(int phase = 0; phase < h+w-1; phase++){ for(int x = 0; x < 26; x++){ if(cnt[phase][x].size() == 0) continue; for(auto &it: cnt[phase][x]){ int i = it/w, j = it%w; // cerr << i << " " << j << ", "; if(i+1 < h && pre[i+1][j] == -1){ cnt[phase+1][s[i+1][j]-'a'].emplace_back((i+1)*w+j); pre[i+1][j] = i*w+j; } if(j+1 < w && pre[i][j+1] == -1){ cnt[phase+1][s[i][j+1]-'a'].emplace_back(i*w+j+1); pre[i][j+1] = i*w+j; } } // cerr << endl; break; } } string ans = ""; int cur = (h-1)*w+w-1; while(cur != -1){ ans += s[cur/w][cur%w]; cur = pre[cur/w][cur%w]; } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }