program problem3; var infile, outfile: text; c, cellcounter, g, neighbor, r, x, y: integer; grid, next: array[0..51,0..51] of char; begin assign(infile,'DATA21.txt'); reset(infile); assign(outfile,'OUT21.txt'); rewrite(outfile); for x:=0 to 51 do for y:=0 to 51 do begin grid[x,y]:='.'; next[x,y]:='.'; end; readln(infile,r,c); for x:= 1 to r do begin for y:=1 to c do read(infile,grid[x,y]); readln(infile); end; close(infile); for g:= 1 to 100 do begin for x:=1 to r do for y:= 1 to c do begin neighbor:=0; if grid[x-1,y-1]='X' then inc(neighbor); if grid[x-1,y]='X' then inc(neighbor); if grid[x-1,y+1]='X' then inc(neighbor); if grid[x,y-1]='X' then inc(neighbor); if grid[x,y+1]='X' then inc(neighbor); if grid[x+1,y-1]='X' then inc(neighbor); if grid[x+1,y]='X' then inc(neighbor); if grid[x+1,y+1]='X' then inc(neighbor); if (grid[x,y]='.') and (neighbor=3) then next[x,y]:='X'; if (grid[x,y]='X') and ((neighbor=3) or (neighbor=2)) then next[x,y]:='X'; end; if g in [1,5,10,50,100] then begin cellcounter:=0; for x:=1 to r do for y:= 1 to c do if next[x,y]='X' then inc(cellcounter); writeln(outfile,cellcounter); end; for x:=1 to r do for y:=1 to c do begin grid[x,y]:=next[x,y]; next[x,y]:='.'; end; end; close(outfile); end.