## 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_words = {} 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] max_words[(next_ci, next_cj)] = s max_words_array = [(pos, w) for pos, w in max_words.items()] max_words_array.sort(key=lambda x : x[1]) s0 = max_words_array[0][1] answer.append(s0) next_current = [] for pos, s in max_words_array: if s == s0: next_current.append(pos) current = next_current print("".join(answer)) if __name__ == "__main__": main()