## https://yukicoder.me/problems/no/2064 def main(): H, W= map(int ,input().split()) S = [] for _ in range(H): S.append(input()) current = [(0, 0)] answer = [S[0][0]] for _ in range(H + W - 2): max_word = "wwww" max_list = [] for ci, cj in current: for di, dj in ((1, 0), (0, 1)): next_ci = ci + di next_cj = cj + dj if 0 <= next_ci < H and 0 <= next_cj < W: s = S[next_ci][next_cj] if max_word == s: max_list.append((next_ci, next_cj)) elif max_word > s: max_word = s max_list = [(next_ci, next_cj)] answer.append(max_word) current = list(set(max_list)) print("".join(answer)) if __name__ == "__main__": main()