horizontalstreets = 30;
verticalstreets = 30;
PropaInfec = 40 / 100; (* Probabilidade de Infeccao *)
densyHidro = 20 /100; (* Densidade Colecoes hidricas *)
densyHidroInfec = 30 / 1000; (* Densidade Colecoes hidricas Infectadas *)
densyPop = 40 / 100; (* Densidade Populacional *)
densyPopInfec = 40 / 1000; (* Densidade Populacional Infectada *)

migraNHidriHidri = 20 / 100;
migraNHidriNHidri = 70 / 100;
migraHidriHidri = 15 / 100;
migraHidriNHidri = 85 / 100;

NextPosInfec = {{0,_} -> 0, {1,_} -> 0, {2, 0} -> 0,{2, 2} ->0, {2, 1} -> 1};
NextTabInfec = {{0,_} -> 0, {1,0} -> 0, {1, 1} -> 1};
NextInfec = {{0,a_} -> a, {1,_} -> 2};
NextMigraCalc = {{0,_} -> 0,{a_/;a>0, 0}-> 1,{a_/;a>0,b_/;b>0}-> 2};
NextMigra = {{0,_,_} -> 0,{1, a_, _}-> a,{2,_, b_}-> b};

ShowInfectedArea[a_]:=Show[ArrayPlot[a,Mesh->True,ColorRules->{0->White,1-> Blue, 2->Red}]]
EightNeighbors[a_] :=a+(RotateRight /@ a) + (RotateLeft /@ a) + RotateRight /@(RotateRight[a])+ (RotateRight[a])+ RotateLeft /@(RotateRight[a])+ RotateRight /@(RotateLeft[a])+ (RotateLeft[a])+ RotateLeft /@(RotateLeft[a]);
Prob[a_, d1_, d2_] := Which[a < d2 , 2, a < d1 , 1, True, 0];
(* Gera Tabela de Probabilidade de Infeccao *)
GenTabInfecProb[] := Table[If[Random[] < PropaInfec, 1, 0],{horizontalstreets},{verticalstreets}];
GenTabNHidri[] = Table[Prob[Random[],migraNHidriNHidri,migraNHidriHidri ],{horizontalstreets},{verticalstreets}];
GenTabHidri[] = Table[Prob[Random[],migraHidriNHidri,migraHidriHidri ],{horizontalstreets},{verticalstreets}];
(* Gera Tabela de Colecoes Hidricas *)
colecoeshidricas = Table[Prob[Random[],densyHidro, densyHidroInfec ],{horizontalstreets},{verticalstreets}];
(* Gera Tabela de Populacao *)
populacao = Table[Prob[Random[],densyPop, densyPopInfec ],{horizontalstreets},{verticalstreets}];;

(* Calcula Posiveis Infectaveis *)
CalTabPosInfec[a_, b_] := (Transpose/@ Transpose[{ a, b}]) /. NextPosInfec;
(* Calcula Infectados *)
CalTabInfec[a_, b_] := (Transpose/@ Transpose[{ CalTabPosInfec[a, b], GenTabInfecProb[]}]) /. NextTabInfec;
(* Retorna a infectado por b *)
Infec[a_, b_] := (Transpose/@ Transpose[{ CalTabInfec[b, a], a}]) /. NextInfec;

CalMigra[a_, b_] := (Transpose/@ Transpose[{ a, b}]) /. NextMigraCalc;
CalMigracao[a_, b_, c_] := (Transpose/@ Transpose[{ a, b, c}]) /. NextMigra;

Repopular[pop_, colecoes_, migra_] := (
NextLimparTab = {{a_, 0}-> a,{_, b_/;b>0}-> 0 };
ret = (Transpose/@ Transpose[{ pop, migra}]) /. NextLimparTab;

tabPopCole = (Transpose/@ Transpose[{ ret, colecoes}]);
colecoesVazias = Position[tabPopCole, {0, a_/;a>0}];
celulasVazias = Position[tabPopCole, {0, 0}];
pessoas = DeleteCases[Flatten[
(Transpose/@ Transpose[{ pop, migra}]) /. {{_, 0} -> 0,{b_ , a_/;a>0} -> {b, a}}, 1],0];
Do[
Which[pessoas[[n, 2]] == 2,
If[ Length[colecoesVazias] >0,
i = RandomInteger[{1,Length[colecoesVazias]}];
ret[[colecoesVazias[[i, 1]], colecoesVazias[[i, 2]]]] =  pessoas[[n, 1]];
Delete[colecoesVazias, i],
i = RandomInteger[{1,Length[celulasVazias]}];
ret[[celulasVazias[[i, 1]], celulasVazias[[i, 2]]]] = pessoas[[n, 1]];
Delete[celulasVazias, i]],
pessoas[[n, 2]] == 1,
i = RandomInteger[{1,Length[celulasVazias]}];
ret[[celulasVazias[[i, 1]], celulasVazias[[i, 2]]]] =  pessoas[[n, 1]];
Delete[celulasVazias, i]],
{n,  Length[pessoas]}];
Return [ret];
)

ShowWord[x_, popula_, coleh_] := (
inter = x;
pop = popula;
coh = coleh;
While[ inter-->0 ,
novaPopu = Infec[coh, pop];
coh = Infec[pop, coh];
migradores = CalMigracao[CalMigra[novaPopu,coh ], GenTabNHidri[], GenTabHidri[] ];
pop = Repopular[novaPopu, coh, migradores];
];
Return [ShowInfectedArea[pop]];
)

Manipulate[ShowWord[x, populacao, colecoeshidricas], {x, 0, 100, 10}]