import sys input = sys.stdin.readline h, w = map(int, input().split()) s = [input() for _ in range(h)] ans = [s[0][0]] tij = {(0, 0, s[0][0])} m = s[0][0] for _ in range(h+w-2): tij_new = set() m_new = "z" for i, j, t in tij: if t == m and i < h-1: tij_new.add((i+1, j, s[i+1][j])) m_new = min(m_new, s[i+1][j]) if t == m and j < w-1: tij_new.add((i, j+1, s[i][j+1])) m_new = min(m_new, s[i][j+1]) tij = set(tij_new) m = m_new ans.append(m) print("".join(ans))