#include using namespace std; using ll = long long; int main(){ int h, w; cin >> h >> w; vector A(h); for(auto &&s:A)cin >> s; vector> dp(h, vector(w)); dp[0][0] = true; string ans; ans += A[0][0]; for(int len = 0; len < h + w - 2; len++){ vector> q; for(int y = 0; y <= len; y++){ int x = len - y; if(x < 0 || y >= h || y < 0 || x >= w)continue; if(!dp[y][x])continue; if(y + 1 < h)q.emplace_back(A[y + 1][x], y + 1, x); if(x + 1 < w)q.emplace_back(A[y][x + 1], y, x + 1); } sort(q.begin(), q.end()); char c, o = get<0>(q[0]); int y, x; ans += o; for(int i = 0; i < q.size() && get<0>(q[i]) == o; i++){ tie(c, y, x) = q[i]; dp[y][x] = true; } } cout << ans << '\n'; }