#include #include using namespace std; typedef pair P; int main() { int h, w; cin >> h >> w; string s[3005]; for(int i = 0; i < h; i++) { cin >> s[i]; } cout << s[0][0]; vector

v; v.push_back(P(0, 0)); for(int i = 1; i < h + w - 1; i++) { char l = 'z'; for(auto [x, y] : v) { if(x < h - 1) { l = min(l, s[x + 1][y]); } if(y < w - 1) { l = min(l, s[x][y + 1]); } } cout << l; vector

z; for(auto [x, y] : v) { if(x < h - 1 && s[x + 1][y] == l && (z.empty() || z.back().second != y)) { z.push_back(P(x + 1, y)); } if(y < w - 1 && s[x][y + 1] == l && (z.empty() || z.back().first != x)) { z.push_back(P(x, y + 1)); } } swap(v, z); } cout << endl; }