using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static string[] SList(int n) => Enumerable.Repeat(0, n).Select(_ => ReadLine()).ToArray(); static void Main() { var c = NList; var (h, w) = (c[0], c[1]); var s = SList(h); WriteLine(Small(h, w, s)); } static string Small(int h, int w, string[] s) { var res = new List(); res.Add(s[0][0]); var q = new List<(int x, int y)>(); q.Add((0, 0)); var prevmin = s[0][0]; for (var t = 0; t < h + w - 2; ++t) { var next = new List<(int x, int y)>(); var min = '{'; foreach (var pos in q) { if (pos.x + 1 < h) min = (char)Math.Min(min, s[pos.x + 1][pos.y]); if (pos.y + 1 < w) min = (char)Math.Min(min, s[pos.x][pos.y + 1]); } foreach (var pos in q) { if (pos.y + 1 < w && s[pos.x][pos.y + 1] == min) { if (next.Count == 0 || next[next.Count - 1].x != pos.x) { next.Add((pos.x, pos.y + 1)); } } if (pos.x + 1 < h && s[pos.x + 1][pos.y] == min) { next.Add((pos.x + 1, pos.y)); } } res.Add(min); q = next; } return (string.Concat(res)); } }