H,W = map(int,input().split()) S = [input() for _ in range(H)] ans = [] s = set() s.add((0,0)) ans.append(S[0][0]) for _ in range(H + W - 2): nx = set() c = "z" for x,y in s: if x + 1 < H: u = S[x + 1][y] if u < c: c = u if y + 1 < W: u = S[x][y + 1] if u < c: c = u ans.append(c) for x,y in s: if x + 1 < H and S[x + 1][y] == c: nx.add((x + 1,y)) if y + 1 < W and S[x][y + 1] == c: nx.add((x,y + 1)) s = nx print("".join(ans))