#include int ri() { int n; scanf("%d", &n); return n; } int main() { int h = ri(); int w = ri(); std::string s[h]; for (auto &i : s) std::cin >> i; std::string res{s[0][0]}; std::vector cur = {0}; for (int i = 1; i < h + w - 1; i++) { std::vector xs; for (auto j : cur) { if (!xs.size() || xs.back() < j) xs.push_back(j); if (j + 1 < h && (!xs.size() || xs.back() < j + 1)) xs.push_back(j + 1); } char min = 'z' + 1; std::vector mindex; for (auto x : xs) { if (i - x < 0 || i - x >= w) continue; if (min > s[x][i - x]) { min = s[x][i - x]; mindex = {x}; } else if (min == s[x][i - 1]) mindex.push_back(x); } assert(mindex.size()); cur = mindex; res.push_back(min); } std::cout << res << std::endl; return 0; }