program alex;
const dx:array[1..11]of integer=(0,1,1,1,1,1,2,3,3,3,4);
      dy:array[1..11]of integer=(0,-2,-1,0,1,2,0,-1,0,1,0);
var f:text;
    a,viz:array[-3..15,-3..15]of integer;
    b:array[1..4]of boolean;
    vfy,vfx,dir,st,sol,uz:array[0..400]of integer;
    mat:array[1..400,1..400]of integer;
    i,j,n,v,k,z,max,h:longint;
    e,ev,as:boolean;
begin
assign(f,'avioane.in');reset(f);
readln(f,n);

for i:=1 to n do
    for j:=1 to n do
        read(f,a[i,j]);
close(f);
for i:=-3 to n+4 do
    begin
    a[i,0]:=-1;a[i,-1]:=-1;a[i,n+1]:=-1;a[i,n+2]:=-1;
    a[i,-2]:=-1; a[i,-3]:=-1;a[i,n+3]:=-1;a[i,n+4]:=-1;
    a[0,i]:=-1;a[-1,i]:=-1;a[n+1,i]:=-1;a[n+2,i]:=-1;
    a[-2,i]:=-1;a[-3,i]:=-1;a[n+3,i]:=-1;a[n+4,i]:=-1;
    end;
v:=0;

for i:=1 to n do
    for j:=1 to n do
        begin
        b[1]:=true;b[2]:=true;b[3]:=true;b[4]:=true;
       for h:=1 to 11 do
           begin
            if a[i+dx[h],j+dy[h]]<>0 then b[1]:=false;
            if a[i+dy[h],j+dx[h]]<>0 then b[2]:=false;
            if a[i+dy[h],j-dx[h]]<>0 then b[3]:=false;
            if a[i-dx[h],j+dy[h]]<>0 then b[4]:=false;
            end;
        for h:=1 to 4 do
            if b[h]=true then begin
                              v:=v+1;
                              vfx[v]:=i;
                              vfy[v]:=j;
                              dir[v]:=h;
                              end;

        end;

for i:=1 to v-1 do
    for j:=i+1 to v do
        begin
        if(vfx[i]=vfx[j])and(vfy[i]=vfy[j])then e:=false
        else begin
        case dir[i] of
        1:for h:=1 to 11 do viz[vfx[i]+dx[h],vfy[i]+dy[h]]:=i;
        2:for h:=1 to 11 do viz[vfx[i]+dy[h],vfy[i]+dx[h]]:=i;
        3:for h:=1 to 11 do viz[vfx[i]+dy[h],vfy[i]-dx[h]]:=i;
        4:for h:=1 to 11 do viz[vfx[i]-dx[h],vfy[i]+dy[h]]:=i;
        end;
        e:=true;
        case dir[j] of
        1:for h:=1 to 11 do if viz[vfx[j]+dx[h],vfy[j]+dy[h]]=i then e:=false;
        2:for h:=1 to 11 do if viz[vfx[j]+dy[h],vfy[j]+dx[h]]=i then e:=false;
        3:for h:=1 to 11 do if viz[vfx[j]+dy[h],vfy[j]-dx[h]]=i then e:=false;
        4:for h:=1 to 11 do if viz[vfx[j]-dx[h],vfy[j]+dy[h]]=i then e:=false;
        end;
              end;
        if e=false then begin
                        mat[i,j]:=-1;
                        mat[j,i]:=-1;
                        end;
        end;


k:=1;
st[k]:=0;
z:=0;
max:=0;
while k>0 do
      begin
      repeat
      if st[k]<v then begin
                      st[k]:=st[k]+1;
                      as:=true;
                      end
      else as:=false;
      if as then begin
                 if uz[st[k]]=0 then begin
                                     e:=true;
                                     i:=k;
                                     repeat
                                     i:=i-1;
                                     if i>0 then if mat[st[k],st[i]]=-1 then e:=false;
                                     until(i<=1)or(e=false);
                                     if e=false then ev:=false
                                     else begin
                                          uz[st[k]]:=1;
                                          ev:=true;
                                          end;
                                     end
                                else ev:=false;
                 end;
      until(as and ev)or(not as);
      if as then begin
                 if k<=v then begin
                              if k>max then begin
                                           max:=k;
                                           for i:=1 to k do
                                               sol[i]:=st[i];
                                           z:=k;
                                           end;
                              if k<v then begin
                                          k:=k+1;
                                          st[k]:=st[k-1];
                                          end;
                             end;
                 end
            else begin
                 k:=k-1;
                 uz[st[k]]:=0;
                 end;
      end;

for i:=1 to z do
    begin
    case dir[sol[i]]of
    1:for h:=1 to 11 do a[vfx[sol[i]]+dx[h],vfy[sol[i]]+dy[h]]:=i;
    2:for h:=1 to 11 do a[vfx[sol[i]]+dy[h],vfy[sol[i]]+dx[h]]:=i;
    3:for h:=1 to 11 do a[vfx[sol[i]]+dy[h],vfy[sol[i]]-dx[h]]:=i;
    4:for h:=1 to 11 do a[vfx[sol[i]]-dx[h],vfy[sol[i]]+dy[h]]:=i;
    end;
    end;
assign(f,'avioane.out');rewrite(f);
writeln(f,max);
for i:=1 to n do
    begin
    for j:=1 to n do
        write(f,a[i,j],' ');
    writeln(f);
    end;
close(f);
end.
