h, w = map(int, input().split()) S = [list(map(ord, list(input()))) for _ in range(h)] Directions = [(1, 0), (0, 1)] ans = chr(S[0][0]) C = set([0]) for _ in range(h + w - 2): minT = 10**18 NC = set() for c in C: ci, cj = divmod(c, w) for di, dj in Directions: ni = ci + di nj = cj + dj if ni < h and nj < w: if S[ni][nj] == minT: NC.add(ni * w + nj) elif S[ni][nj] < minT: minT = S[ni][nj] NC = set([ni * w + nj]) ans += chr(minT) C = NC print(ans)