def solve(): H,W=map(int,input().split()) S=[] for _ in range(H): S.append(list(input())) T=[S[0][0]] Y={(0,0)} for _ in range(H+W-2): X=Y Y=set() c=chr(ord("z")+1) for i,j in X: if i+1<H and S[i+1][j]<c: c=S[i+1][j] if j+1<W and S[i][j+1]<c: c=S[i][j+1] for i,j in X: if i+1<H and S[i+1][j]==c: Y.add((i+1,j)) if j+1<W and S[i][j+1]==c: Y.add((i,j+1)) T.append(c) return "".join(T) print(solve())