#include using namespace std; inline void min_self(string &a, string &b) { if (b < a) { a = b; } } int main() { iostream::sync_with_stdio(0), cin.tie(0), cout.tie(0); int h, w; cin >> h >> w; vector grid(h); for (int i = 0; i < h; i++) { cin >> grid[i]; } vector dp(w, "{"); dp[0] = ""; for (int i = 0; i < h; i++) { vector ndp(w, "{"); for (int j = 0; j < w; j++) { string nst = dp[j] + grid[i][j]; min_self(ndp[j], nst); if (j + 1 < w) { min_self(dp[j + 1], nst); } } swap(dp, ndp); } cout << dp[w - 1] << '\n'; return 0; }