#include using namespace std; int main() { int n, m; scanf("%d %d", &n, &m); pair dp[n + 1][m + 1]; string s; for (int i = 0; i < n; i++) { cin >> s; cin.ignore(); for(int j = 0; j < m; j++) dp[i][j].second = s[j]; } for (int i = 0; i <= m; i++) dp[n][i] = {INT_MAX, 'z' + 1}; for (int i = 0; i <= n; i++) dp[i][m] = {INT_MAX, 'z' + 1}; dp[n][m - 1].first = dp[n - 1][m].first = 0; for (int i = n - 1; i >= 0; i--) for (int j = m - 1; j >= 0; j--) dp[i][j].first = 1 + min(dp[i + 1][j].first, dp[i][j + 1].first); string ans = ""; ans.push_back(dp[0][0].second); int i = 0,j = 0; while (i < n - 1 || j < m - 1) { if (dp[i][j + 1] < dp[i + 1][j]) { ans.push_back(dp[i][j + 1].second); j++; } else { ans.push_back(dp[i + 1][j].second); i++; } } cout << ans; return 0; }