program lotyniskas_kvadratas;
  const PR = 'LOTYN6.DAT';
        RZ = 'LOT6.REZ';
        MAX_N = 26; { maksimalus kvadrato dydis }
  type kvadratas = array [1..MAX_N, 1..MAX_N] of char;

  procedure lot_kv (n: integer; kv: kvadratas;
                    var lot: boolean; { ar lotyni6kas }
                    var eil, stu: integer);
    var raides, aibe_eil, aibe_stu: set of char;
        i, j, kiek: integer;
        c: char;
  begin
    raides := [];  { panaudotos raidës }
    eil := 0; stu := 0;
    for i := 1 to n do
      begin
        aibe_eil := []; aibe_stu := [];
        { ið karto tikrinsime i-àjà eilutæ ir i-àjá stulpelá }
        for j := 1 to n do
          begin
            if kv[i, j] in aibe_eil { jei raidë kartojasi }
               then  eil := i
               else aibe_eil :=aibe_eil + [kv[i, j]];
            if kv[j, i] in aibe_stu { jei raidë kartojasi }
               then stu := i
               else aibe_stu :=aibe_stu + [kv[j, i]];
             raides := raides + [kv[i, j]];
          end;
      end;
    { suskaièiuosime kiek skirtingø raidþiø panaudota }
    kiek := 0;
    for c := 'A' to 'Z' do
      if c in raides then kiek := kiek + 1;
    lot := (eil = 0) and (stu = 0) and (kiek = n);
  end; { lot_kv }

  procedure skaityti (var n: integer; var kv: kvadratas);
    var f: text;
        i, j: integer;
  begin
    assign (f, PR);
    reset (f);
    readln (f, n);
    for i := 1 to n do
      begin
        for j := 1 to n do
           read (f, kv[i, j]);
        readln (f);
      end;
    close(f);
  end; { skaityti }

  procedure isvesti (lot: boolean; var eil, stu: integer);
    var f: text;
  begin
    assign (f, RZ);
    rewrite (f);
    if lot then writeln (f, 'TAIP')
           else writeln (f, 'NE');
    writeln (f, eil);
    writeln (f, stu);
    close (f);
  end; { iðvesti }

  var n, eil, stu: integer;
      kv: kvadratas;
      lot: boolean;
begin
  skaityti (n, kv);
  lot_kv (n, kv, lot, eil, stu);
  isvesti (lot, eil, stu)
end.
