#include using namespace std; int main() { cin.tie(nullptr)->sync_with_stdio(false); int H, W; cin >> H >> W; vector S(H); for (auto &&e : S) { cin >> e; } string res = ""; res += S[0][0]; vector> X; X.emplace_back(0, 0); for (int i = 0; i < H + W - 2; i++) { char c = 'z'; for (const auto &[y, x] : X) { if (y + 1 < H && c > S[y + 1][x]) c = S[y + 1][x]; if (x + 1 < W && c > S[y][x + 1]) c = S[y][x + 1]; } vector> Y; for (const auto &[y, x] : X) { if (y + 1 < H && c == S[y + 1][x]) Y.emplace_back(y + 1, x); if (x + 1 < W && c == S[y][x + 1]) Y.emplace_back(y, x + 1); } res += c; swap(X, Y); } cout << res << '\n'; return 0; }