program Snakes; uses crt; var infile,outfile:text; x,y,z,y1,z1:integer; r,c:integer; grid:array[0..101,0..101] of char; numberattached, nexty, nextz: integer; largestuncoiled, largestcoiled:integer; uncoiled:boolean; numparts:integer; procedure finduncoiled(numparts,nexty,nextz:integer); var numberattached:integer; nextrow,nextcol:integer; begin numberattached:=0; if grid[nexty-1,nextz-1]='X' then begin inc(numberattached); nextrow:=nexty-1; nextcol:=nextz-1; end; if grid[nexty-1,nextz]='X' then begin inc(numberattached); nextrow:=nexty-1; nextcol:=nextz; end; if grid[nexty-1,nextz+1]='X' then begin inc(numberattached); nextrow:=nexty-1; nextcol:=nextz+1; end; if grid[nexty,nextz-1]='X' then begin inc(numberattached); nextrow:=nexty; nextcol:=nextz-1; end; if grid[nexty,nextz+1]='X' then begin inc(numberattached); nextrow:=nexty; nextcol:=nextz+1; end; if grid[nexty+1,nextz-1]='X' then begin inc(numberattached); nextrow:=nexty+1; nextcol:=nextz-1; end; if grid[nexty+1,nextz]='X' then begin inc(numberattached); nextrow:=nexty+1; nextcol:=nextz; end; if grid[nexty+1,nextz+1]='X' then begin inc(numberattached); nextrow:=nexty+1; nextcol:=nextz+1; end; if numberattached = 1 then begin grid[nexty,nextz]:='Z'; finduncoiled(numparts+1,nextrow,nextcol); end else begin if numberattached = 0 then begin uncoiled:=true; grid[nexty,nextz]:='Z'; if numparts+1 > largestuncoiled then largestuncoiled := numparts+1; end; end; end; procedure findcoiled(r,c:integer); begin grid[r,c]:='.'; if numparts>largestcoiled then largestcoiled:=numparts; if grid[r-1,c-1]='X' then begin inc(numparts); findcoiled(r-1,c-1); end; if grid[r-1,c]='X' then begin inc(numparts); findcoiled(r-1,c); end; if grid[r-1,c+1]='X' then begin inc(numparts); findcoiled(r-1,c+1); end; if grid[r,c-1]='X' then begin inc(numparts); findcoiled(r,c-1); end; if grid[r,c+1]='X' then begin inc(numparts); findcoiled(r,c+1); end; if grid[r+1,c-1]='X' then begin inc(numparts); findcoiled(r+1,c-1); end; if grid[r+1,c]='X' then begin inc(numparts); findcoiled(r+1,c); end; if grid[r+1,c+1]='X' then begin inc(numparts); findcoiled(r+1,c+1); end; end; begin assign(infile,'DATA21.txt'); reset(infile); assign(outfile,'OUT21.txt'); rewrite(outfile); for x:=1 to 5 do begin for y:=0 to 101 do for z:= 0 to 101 do begin grid[y,z]:='.'; end; readln(infile,r,c); for y:= 1 to r do begin for z:=1 to c do begin read(infile,grid[y,z]); end; readln(infile); end; largestuncoiled:=0; for y:=1 to r do for z:=1 to c do begin if grid[y,z]='X' then begin numberattached:=0; if grid[y-1,z-1]='X' then begin inc(numberattached); nexty:=y-1; nextz:=z-1; end; if grid[y-1,z]='X' then begin inc(numberattached); nexty:=y-1; nextz:=z; end; if grid[y-1,z+1]='X' then begin inc(numberattached); nexty:=y-1; nextz:=z+1; end; if grid[y,z-1]='X' then begin inc(numberattached); nexty:=y; nextz:=z-1; end; if grid[y,z+1]='X' then begin inc(numberattached); nexty:=y; nextz:=z+1; end; if grid[y+1,z-1]='X' then begin inc(numberattached); nexty:=y+1; nextz:=z-1; end; if grid[y+1,z]='X' then begin inc(numberattached); nexty:=y+1; nextz:=z; end; if grid[y+1,z+1]='X' then begin inc(numberattached); nexty:=y+1; nextz:=z+1; end; if numberattached = 1 then begin uncoiled := false; grid[y,z]:='Z'; finduncoiled(1,nexty,nextz); for y1:=1 to r do for z1:=1 to c do if grid[y1,z1]='Z' then if uncoiled then grid[y1,z1]:='.' else grid[y1,z1]:='X'; end else begin if numberattached = 0 then begin grid[y,z]:='.'; if largestuncoiled = 0 then largestuncoiled := 1; end; end; end; end; largestcoiled := 0; for y:=1 to r do begin for z:=1 to c do begin if grid[y,z]='X' then begin numparts:=1; findcoiled(y,z); end; end; end; writeln(outfile,largestcoiled,' ',largestuncoiled); end; close(infile); close(outfile); end.