#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; struct Node { int y; int x; char ch; Node(int y = -1, int x = -1, char ch = -1) { this->y = y; this->x = x; this->ch = ch; } bool operator>(const Node &n) const { return ch > n.ch; } }; int main() { int H, W; cin >> H >> W; vector S(H); for (int y = 0; y < H; ++y) { cin >> S[y]; } priority_queue , greater> pque; pque.push(Node(0, 0, S[0][0])); int history[H][W]; memset(history, -1, sizeof(history)); while (not pque.empty()) { Node node = pque.top(); pque.pop(); if (node.y + 1 < H && history[node.y + 1][node.x] == -1) { history[node.y + 1][node.x] = 2; pque.push(Node(node.y + 1, node.x, S[node.y + 1][node.x])); } if (node.x + 1 < W && history[node.y][node.x + 1] == -1) { history[node.y][node.x + 1] = 1; pque.push(Node(node.y, node.x + 1, S[node.y][node.x + 1])); } } string ans = ""; int cy = H - 1; int cx = W - 1; while (cy != 0 || cx != 0) { int dir = history[cy][cx]; ans += S[cy][cx]; if (dir == 1) { cx -= 1; } else { cy -= 1; } } ans += S[0][0]; reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }