from heapq import heappush, heappop H, W = map(int, input().split()) S = [input() for _ in range(H)] T = [S[0][0]] Q = set([(0, 0)]) Q2 = [] for _ in range(H + W - 2): # 次の候補の座標をQ2に追加 # 重複するけど気にしない for h, w in Q: if w < W - 1: heappush(Q2,(S[h][w + 1], h, w + 1)) if h < H - 1: heappush(Q2,(S[h + 1][w], h + 1, w)) # Qを空にしてから文字が最小の物だけ抽出してQに追加 Q = set() c,h,w = heappop(Q2) c0 = c T.append(c) while c == c0: Q.add((h, w)) if len(Q2) == 0: break c,h,w = heappop(Q2) print("".join(T))