program Jango_lentele;
  const PRAD = 'JANG.DAT';
        REZ = 'JANG.REZ';
        MAX_ST = 10+1; { maksimalus eilu‡i— ir stulpeli— skai‡ius }
  type lentele = array [1..MAX_ST, 1..MAX_ST] of integer;

   procedure iterpti_elementa (var jang: lentele; var eil: integer;
                                                      sk: integer);
     var i, j, tarp: integer;
         iterpta: boolean;
   begin
     i := 1; j := 1; iterpta := false;
     while not iterpta do
       if jang[i, j] = 0 { terpsime eilut‚s gale }
          then begin
                 iterpta := true;
                 jang[i, j] := sk;
               end
       else if jang[i, j] > sk   { jei radome terpimo viet… }
               then begin
                      tarp := jang[i, j];
                      jang[i, j] := sk;
                      sk := tarp;
                      i := i + 1; j := 1;
                    end
       else j := j + 1;
     if i > eil then eil := i;
   end; { terpti_element… }

  procedure skaityti (var jang: lentele; var eil, sk: integer);
  { perskaito pradinius duomenis }
    var f: text;
        i, j, elem: integer;
        pabaiga: boolean; { ar pasiekta eilu‚s pabaiga }
  begin
    assign (f, PRAD);
    reset (f);
    readln (f, eil, sk);
    { perskaitoma lentel‚ }
    for i := 1 to MAX_ST do
      begin
        pabaiga := eil < i;
        for j := 1 to MAX_ST do
          begin
            if not pabaiga
               then read (f, elem);
            jang[i, j] := elem;
            pabaiga := elem = 0;
          end;
      end;
     close (f);
   end; { skaityti }

   procedure spausdinti (jang: lentele; eil: integer);
     var i, j: integer;
   begin
     for i := 1 to eil do
       begin
         for j := 1 to MAX_ST do
           if jang[i, j] <> 0
              then write (jang[i, j], ' ');
       end;
   end; { spausdinti }

   var jang: lentele;
       eil, sk: integer;
begin
  skaityti (jang, eil, sk);
  iterpti_elementa (jang, eil, sk);
  spausdinti (jang, eil);
end.