#include <bits/stdc++.h>
#define sts stable_sort
#define B begin()
#define rB rbegin()
#define E end()
#define rE rend()
#define F first
#define S second
#define pb push_back
#define ppb pop_back()
#define pf push_front
#define eb = emplace_back
#define ppf = pop_front()
#define L(x) ((x)<<1)
#define pii pair<int, int>
#define rB rbegin()
#define rE rend()
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
const int MAXN = 1e4;
const int MOD = 1e9 + 7;
const int inf = INT_MAX;
const ll INF = LONG_LONG_MAX;
char v[MAXN][MAXN];
string ans[MAXN][MAXN];
int n, m;
void bfs(){
queue<pair<pair<int, int>, string> > q;
string aux = "";
aux.pb(v[1][1]);
q.push({{1, 1}, aux});
while(!q.empty()){
int x = q.front().F.F, y = q.front().F.S;
string s = q.front().S;
q.pop();
if(x > n || y > m)continue;
if(ans[x][y] == ""){
ans[x][y] = s;
}else if(ans[x][y] < s)continue;
ans[x][y] = s;
q.push({{x + 1, y}, s + v[x + 1][y]});
q.push({{x, y + 1}, s + v[x][y + 1]});
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> v[i][j];
}
}
bfs();
cout << ans[n][m] << '\n';
}
/**
*/
Compilation message
/tmp/ccmQm3Pr.o: in function `bfs()':
pohlepko.cpp:(.text+0x1f6): relocation truncated to fit: R_X86_64_PC32 against symbol `v' defined in .bss section in /tmp/ccmQm3Pr.o
pohlepko.cpp:(.text+0x39e): relocation truncated to fit: R_X86_64_PC32 against symbol `v' defined in .bss section in /tmp/ccmQm3Pr.o
pohlepko.cpp:(.text+0x4c7): relocation truncated to fit: R_X86_64_PC32 against symbol `v' defined in .bss section in /tmp/ccmQm3Pr.o
/tmp/ccmQm3Pr.o: in function `main':
pohlepko.cpp:(.text.startup+0x31): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
pohlepko.cpp:(.text.startup+0x38): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
pohlepko.cpp:(.text.startup+0x61): relocation truncated to fit: R_X86_64_PC32 against symbol `v' defined in .bss section in /tmp/ccmQm3Pr.o
pohlepko.cpp:(.text.startup+0x96): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
pohlepko.cpp:(.text.startup+0xd9): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cout' defined in .bss._ZSt4cout section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
/tmp/ccmQm3Pr.o: in function `_GLOBAL__sub_I_v':
pohlepko.cpp:(.text.startup+0x14b): relocation truncated to fit: R_X86_64_PC32 against `.bss'
pohlepko.cpp:(.text.startup+0x165): relocation truncated to fit: R_X86_64_PC32 against `.bss'
/usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(vterminate.o): in function `__gnu_cxx::__verbose_terminate_handler()':
(.text._ZN9__gnu_cxx27__verbose_terminate_handlerEv+0x1e): additional relocation overflows omitted from the output
/usr/bin/ld: failed to convert GOTPCREL relocation; relink with --no-relax
collect2: error: ld returned 1 exit status