import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int h = sc.nextInt(); int w = sc.nextInt(); char[][] s = new char[h][w]; for (int i = 0; i < h; i++) { s[i] = sc.next().toCharArray(); } sc.close(); StringBuilder sb = new StringBuilder(); int hw = h + w - 1; Set set = new HashSet<>(); set.add(0); for (int i = 0; i < hw; i++) { Map> map = new HashMap<>(); for (char j = 'a'; j <= 'z'; j++) { map.put(j, new HashSet<>()); } int x0 = set.iterator().next(); int y0 = i - x0; sb.append(s[x0][y0]); for (int x : set) { int y = i - x; if (x < h - 1) { map.get(s[x + 1][y]).add(x + 1); } if (y < w - 1) { map.get(s[x][y + 1]).add(x); } } for (char j = 'a'; j <= 'z'; j++) { Set wk = map.get(j); if (!wk.isEmpty()) { set = wk; break; } } } System.out.println(sb.toString()); } }