H,W = map(int,input().split()) S = [input().strip() for _ in range(H)] dp = ["" for _ in range(W)] dp[0] = S[0][0] for j in range(1,W): dp[j] = dp[j-1]+S[0][j] for i in range(1,H): dp[0] += S[i][0] for j in range(1,W): dp[j] = min(dp[j],dp[j-1])+S[i][j] print(dp[W-1])