h, w = map(int, input().split()) S = [input() for _ in range(h)] def f(i, j): return i * w + j ans = [S[0][0]] se = {0} for _ in range(h + w - 2): nex = set() mi = "z" for p in se: i = p // w j = p - i * w if i != h - 1: s = S[i + 1][j] if s < mi: mi = s nex = {f(i + 1, j)} elif s == mi: nex.add(f(i + 1, j)) if j != w - 1: s = S[i][j + 1] if s < mi: mi = s nex = {f(i, j + 1)} elif s == mi: nex.add(f(i, j + 1)) ans.append(mi) se = nex print(*ans, sep="")