def main(): H, W = map(int, input().split()) letters = [list(input()) for _ in range(H)] strings = "" current_h, current_w = 0, 0 while current_h < (H-1) or current_w < (W-1): strings += letters[current_h][current_w] if current_h == H-1 or current_w == W-1: current_w = min(current_w+1, W-1) current_h = min(current_h+1, H-1) elif letters[current_h+1][current_w] < letters[current_h][current_w+1]: current_h += 1 else: current_w += 1 strings += letters[H-1][W-1] print(strings) if __name__ == "__main__": main()