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