program remeliai;
  { 70 u‘davinys }
  const max_d = 30; { maksimalios piežinio dimensijos }
  type piesinys = array [1..max_d, 1..max_d] of char;
       remel = array ['A'..'Z'] of record
                                        x1, y1,
                                        x2, y2 : integer;
                                      end;
                 { r‚melio apatinio kairiojo ir viržutinio dežiniojo }
                 { kamp— koordinat‚s }
        aibe = set of char; { aib‚ skirting— raid‘i—, iž kuri— }
                            { sudaryti r‚meliai }

  var ps : piesinys; { sud‚liot— r‚meli— piežinys }
      a, p : integer;  { piežinio aukžtis ir plotis }
      { global–s kintamieji visoms proced–roms }

  {--------------------------------------------------------}
  procedure nuimti (remaib : aibe; rem : remel;
                    var raide : char); { raid‚, iž kurios sudarytas }
                                       { nuimtasis r‚melis }
    { nuima viržutinj r‚mel, jo vietoje sudeda tažko simbolius }
    var rasta : boolean;
        r : char;
        i : integer;
  begin
    { rasime r‚mel, kur reikia nuimti }
    rasta := false;
    r := 'A';
    while not rasta do  { tikrinsime, ar negalime nuimti r‚melio, }
      if r in remaib    { sudaryto iž raid‘i— r }
           then begin
                  rasta := true;
                  { tikriname vertikalias r‚melio sienas }
                  for i := rem[r].x1 to rem[r].x2 do
                   begin
                      rasta := (rasta and
                               (ps[i, rem[r].y1] in [r, '.'])) and
                               (ps[i, rem[r].y2] in [r, '.']);
                   end;
                  { tikriname horizontalias r‚melio sienas }
                  for i := rem[r].y1 to rem[r].y2 do
                      rasta :=  rasta and
                               ((ps[rem[r].x1, i] in [r, '.']) and
                                (ps[rem[r].x2, i] in [r, '.']));
                   if not rasta
                      then r := succ(r)
                end
           else r := succ (r);
    raide := r;
    { nuimame raides }
    for i := rem[r].x1 to rem[r].x2 do
       begin
         ps[i, rem[r].y1] := '.';
         ps[i, rem[r].y2] := '.';
       end;
    for i := rem[r].y1 to rem[r].y2 do
       begin
         ps[rem[r].x1, i] := '.';
         ps[rem[r].x2, i] := '.';
       end;
  end;

  procedure koordinates (remaib : aibe; var rem : remel);
    { suranda vis— r‚meli— koordinates - u‘pildo masyv… rem }
    var r : char;
        i, j : integer;
  begin
    { inicializuojame masyv… rem }
    for r := 'A' to 'Z' do
      if r in remaib
           then begin
                  rem[r].x1 := max_d;
                  rem[r].y1 := max_d;
                  rem[r].x2 := 0;
                  rem[r].y2 := 0;
                end;
     { u‘pildome j }
     for i := 1 to a do
       for j := 1 to p do
         if ps[i, j] <> '.'
            then begin
                   r := ps[i, j];
                   { tikriname eilutŠ }
                   if i < rem[r].x1
                      then rem[r].x1 := i;
                   if i > rem[r].x2
                      then rem[r].x2 := i;
                   { tikriname stulpel }
                   if j < rem[r].y1
                      then rem[r].y1 := j;
                   if j > rem[r].y2
                      then rem[r].y2 := j;
                 end;
  end;


  procedure ieskoti (var tvarka : string);
    { surandama kokia tvarka buvo sud‚lioti r‚meliai }
    var remaib : aibe;
        rem : remel;
        i, j : integer;
        raide : char;
  begin
    tvarka := '';
    remaib := [];
    { surasime visas raides iž kuri— sudaryti r‚meliai }
    for i := 1 to a do
       for j := 1 to p do
           if ps[i, j] <> '.'
              then remaib := remaib + [ps[i, j]];
    { rasime kiekvieno r‚melio koordinates }
    koordinates (remaib, rem);
    while remaib <> [] do { kol nenuimti visi r‚meliai }
      begin
        nuimti (remaib, rem, raide);  { nuimamas viržutinis r‚melis }
        tvarka := tvarka + raide;
        remaib := remaib - [raide];
      end
  end;
{----------------------------------------------------------}
  var f : text;
      i, j : integer;
      tvarka : string;
begin
  assign (f, 'remeliai.dat');
  reset (f);
  readln (f, a);
  readln (f, p);
  for i := a downto 1 do
    begin
      for j := 1 to p do
         read (f, ps[i, j]);
      readln (f);
    end;
  close (f);

  ieskoti (tvarka);

  assign (f, 'remeliai.rez');
  rewrite (f);
  writeln (f, tvarka);
  close (f)
end.