import sys input = sys.stdin.readline H, W = map(int, input().split()) S = [input()[:-1] for _ in range(H)] now = {(0, 0)} ans = [S[0][0]] for _ in range(H+W-2): m = 'z' nex = set() for x, y in now: if x+1<H: if m==S[x+1][y]: nex.add((x+1, y)) elif m>S[x+1][y]: m = S[x+1][y] nex = {(x+1, y)} if y+1<W: if m==S[x][y+1]: nex.add((x, y+1)) elif m>S[x][y+1]: m = S[x][y+1] nex = {(x, y+1)} ans.append(m) now = nex print(''.join(ans))