#include #include #include #include #include #include #include using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) template using vi = vector; template using vii = vector>; template using viii = vector>; using pii = pair; struct status { int x, y; }; int main() { int h, w; cin >> h >> w; vi s(h); rep(i, h) cin >> s[i]; queue q; q.push({ 0, 0 }); char c = s[0][0]; cout << c; vii visited(h, vi(w)); const int dx[2] = { 1, 0 }; const int dy[2] = { 0, 1 }; for (int sm = 0; sm < h + w - 2; sm++) { char nc = 'z'; while (!q.empty() && q.front().x + q.front().y == sm) { status v = q.front(); q.pop(); if (s[v.x][v.y] != c) continue; if (visited[v.x][v.y]) continue; visited[v.x][v.y] = true; rep(i, 2) { int nx = v.x + dx[i]; int ny = v.y + dy[i]; if (nx >= h || ny >= w) continue; if (s[nx][ny] <= nc) { q.push({ nx, ny }); nc = s[nx][ny]; } } } c = nc; cout << c; } cout << endl; return 0; }