#include #include #include using namespace std; int main() { int H, W; cin >> H >> W; vector S(H); for (int i = 0; i < H; i++) { cin >> S[i]; } vector > f(H, vector(W, 0)); f[0][0] = 1; string answer(1, S[0][0]); for (int i = 0; i < H + W - 2; i++) { char minch = '~'; for (int x = 0; x < H; x++) { int y = i - x; if (!(0 <= y && y < W)) { continue; } if (f[x][y] == 1) { if (x != H - 1) { minch = min(minch, S[x + 1][y]); } if (y != W - 1) { minch = min(minch, S[x][y + 1]); } } } for (int x = 0; x < H; x++) { int y = i - x; if (!(0 <= y && y < W)) { continue; } if (f[x][y] == 1) { if (x != H - 1 && S[x + 1][y] == minch) { f[x + 1][y] = 1; } if (y != W - 1 && S[x][y + 1] == minch) { f[x][y + 1] = 1; } } } answer += minch; } cout << answer << endl; return 0; }