@halirutan ok, thank you. I just want to ask you few questions:
1) My input is made by a matrix of 0 and 1 (that in the code I ask you to help me to implement where respectively 1 and 2). If I want to change the code, can I write the following one - after Count[{nb,#]&/@ can I write {1,2}, instead of {0,1}??
countRedBlack[a_, i_Integer?Positive, j_Integer?Positive] :=
With[{nb = Flatten@a[[i - 1 ;; i + 1, j - 1 ;; j + 1]]},
Count[nb, #] & /@ {0, 1}]