As for the problem itself, it can be solved without finding
connected components. I won't post the solution right away
because it is potentially a spoiler. See
http://codeforces.com/blog/entry/53268 for problem analysis
http://codeforces.com/contest/828/submission/28637184 for an
example implementation in D.
Wow I understand the question wrongly than,
I excepted there should be two B squares here.
One 3*3 and another 1*1.
And my answer would be 6 instead 12 here.
Yes if there can be only one black square than my solution would
be much more simple.
One good thing about D community is when I ask a question here
you guys are really nice that I get inspired.