import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int h = sc.nextInt(); int w = sc.nextInt(); char[][] field = new char[h][]; for (int i = 0; i < h; i++) { field[i] = sc.next().toCharArray(); } StringBuilder ans = new StringBuilder(); HashSet current = new HashSet<>(); HashSet next = new HashSet<>(); current.add(0); for (int i = 0; i < h + w - 1; i++) { char min = (char)('z' + 1); for (int x : current) { int r = x / w; int c = x % w; if (ans.length() == i) { ans.append(field[r][c]); } if (r < h - 1) { if (field[r + 1][c] < min) { min = field[r + 1][c]; next.clear(); next.add((r + 1) * w + c); } else if (field[r + 1][c] == min) { next.add((r + 1) * w + c); } } if (c < w - 1) { if (field[r][c + 1] < min) { min = field[r][c + 1]; next.clear(); next.add(r * w + c + 1); } else if (field[r][c + 1] == min) { next.add(r * w + c + 1); } } } HashSet tmp = next; next = current; current = tmp; next.clear(); } System.out.println(ans); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }