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