program VariationGameLife; var infile, outfile: text; c, cellcounter, g, neighbor, r, x, y, z: integer; grid, next: array[0..51,0..51] of char; counter: integer; s:string; continue,birth:array[1..8] of integer; continueflag, birthflag: boolean; begin assign(infile,'DATA21.txt'); reset(infile); assign(outfile,'OUT21.txt'); rewrite(outfile); for counter := 1 to 5 do begin for x:=0 to 51 do for y:=0 to 51 do begin grid[x,y]:='.'; next[x,y]:='.'; end; for x:=1 to 8 do begin continue[x]:=9; birth[x]:=9; 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; readln(infile,s); x:=1; while s[x]<>'/' do begin val(s[x],continue[x]); x:=x+1; end; delete(s,1,x); for x := 1 to length(s) do val(s[x],birth[x]); for g:= 1 to 25 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); birthflag := false; for z:=1 to 8 do if birth[z]=neighbor then birthflag := true; if (grid[x,y]='.') and (birthflag) then next[x,y]:='X'; continueflag := false; for z:=1 to 8 do if continue[z]=neighbor then continueflag := true; if (grid[x,y]='X') and (continueflag) then next[x,y]:='X'; 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; cellcounter:=0; for x:=1 to r do for y:= 1 to c do if grid[x,y]='X' then inc(cellcounter); writeln(outfile,cellcounter); end; close(infile); close(outfile); end.