program perpiestas_ornamentas;
  { 39 u‘davinys }
const byla = 'ORNAM.DAT';
      MAX_X = 50;
      MAX_Y = 50;

type ornam = array [1..MAX_Y] of string [MAX_X];
     atsakymas = (teis, klaid, papild, pasal);

var o: ornam; { perpiežtas ornamentas }

  procedure pakeisk (y, x: integer);
  begin
    if o[y, x]='1' then o[y, x]:= '0'
                   else o[y, x]:= '1';
  end; { pakeisk }

  procedure tikrink (nerasta : boolean; { ar nerasta klaida }
                     var rez : atsakymas; { atsakymo tipas }
                     var x_rez, y_rez : integer { langelio koordinat‚s });
  { rekursin‚ proced–ra, kuri tikrina, ar teisingai perpiežtas }
  { ornamentas; radus klaid…, bandoma j… taisyti (proced–ra kreipiasi }
  {  Ť save su parametru nerasta = false); }
  { Kai proced–ros parametras  }
  { nerasta = False, tai joki— pakeitim— ornamente daryti negalima; }
  {  nerasta = True,  tai galime pakeisti vien… langelŤ.  }
  {  vartojamas globalus kintamasis o }
   var x_min, x_max, y_min, y_max,  { ribin‚s piežinio koordinat‚s }
       x, y: integer;
       ats: atsakymas;  { pagalbiniai kintamieji }
  begin
    x_min := MAX_X + 1;          { randame x_min ir x_max }
    x_max := 0;
    for y := 1 to MAX_Y do
      for x:= 1 to MAX_X do
        if o[y, x] = '1' then
           begin
            if x < x_min then x_min := x;
            if x > x_max then x_max := x
           end;
    y_min := MAX_Y + 1;             { randame y_min ir y_max }
    y_max := 0;
    for y := 1 to MAX_Y do
      if pos ('1', o[y]) > 0 then
         begin
           if y < y_min then y_min := y;
           if y > y_max then y_max := y
         end;
    { tikrinamas simetrižkumas }
    ats:= teis;
    y := y_min;
    repeat
      x := x_min;
      repeat
        if (o[y, x] <> o[y, x_max+x_min-x]) or
           (o[y, x] <> o[y_max+y_min-y, x]) then
           if nerasta then
                begin
                  pakeisk (y, x); { pataisome piežinŤ }
                  { tikriname, ar pataisytas ornamentas }
                  tikrink (false, rez, x_rez, y_rez);
                  if rez = teis
                     then begin
                            x_rez:= x;
                            y_rez:= y;
                            if o[y, x] = '0'
                               then ats:= pasal { reik‚jo pažalinti }
                               else ats:= papild { reik‚jo papildyti }
                          end
                     else ats := klaid;
                  pakeisk (y, x); { atstatome piežinŤ }
                end
           else ats:= klaid;
        x := x + 1;
      until (ats = klaid) and not nerasta or
            { pataisytas piežinys nesimetrižkas }
            (ats in [pasal, papild]) or     { pavyko pataisyti }
            (x > x_max);             { baig‚si tikrinama eilut‚ }
      y := y + 1
    until (ats = klaid) and not nerasta or
          (ats in [pasal, papild]) or
          (y > y_max);               { per‘i–r‚tas visas piežinys }
    if ats = teis
       then if ((y_min - y_max) mod 2 <> 0) and ((x_max - x_min) mod 2 <> 0)
               then rez := teis
               else rez := klaid
       else rez := ats;
   end; { tikrinti }

  var fl: text;
      i,
      x_rez, y_rez: integer;  { vietos, kuri… reikia pataisyti, koordinat‚s }
      rez: atsakymas;
begin
  assign (fl, byla);
  reset (fl);
  for i := 1 to MAX_Y do
    readln (fl, o[i]);
  close (fl);
  x_rez:= 0; y_rez:=0;
  tikrink (true, rez, x_rez, y_rez);
  case rez of
    teis: writeln ('PERPIEźTA TEISINGAI.');
    papild: writeln ('REIKIA PAPILDYTI LANGELIU (',x_rez,', ',y_rez,').');
    pasal: writeln ('REIKIA PAźALINTI LANGEL­ (',x_rez,', ',y_rez,').');
    klaid: writeln ('YRA DAUGIAU KAIP VIENA KLAIDA.')
  end;
  WriteLn;
  WriteLn ('paspauskite enter klaviž… ...');
  readLn
end.



