local h, w = io.read("*n", "*n", "*l") local t = {} for i = 1, h do local s = io.read() t[i] = {} for j = 1, w do t[i][j] = s:sub(j, j) end end local ret = {} local last = t[1][1] local pos = {} pos[1] = true ret[1] = last for len = 2, h + w - 1 do local nxt = "~" for i = 1, h do if pos[i] then local j = len - i if j < w and t[i][j + 1] < nxt then nxt = t[i][j + 1] end if i < h and t[i + 1][j] < nxt then nxt = t[i + 1][j] end end end local tmp = {} for i = h, 1, -1 do if pos[i] then local j = len - i if j < w and t[i][j + 1] == nxt then tmp[i] = true end if i < h and t[i + 1][j] == nxt then tmp[i + 1] = true end end end pos = {} for k, v in pairs(tmp) do pos[k] = true end table.insert(ret, nxt) last = nxt end print(table.concat(ret))