#include #include using namespace std; int main(void){ int H,W; cin >> H >> W; vector map(H); for(int i = 0; i < H; i++){ cin >> map[i]; } priority_queue< pair, vector>, greater> > que; string vv{map[0][0]}; que.push(make_pair(vv,0)); while(!que.empty()){ pair v = que.top(); que.pop(); //cout << v.first << " " << v.second << endl; int h = v.second / 3001; int w = v.second % 3001; if(h+1 == H && w+1 == W){ cout << v.first << endl; return 0; } if(w+1 != W){ que.push(make_pair(string(v.first+map[h][w+1]),v.second+1)); } if(h+1 != H){ que.push(make_pair(string(v.first+map[h+1][w]),v.second+3001)); } } }