###GolfScript, 107 characters### .n?):L;'1'/{,}%{1$+)}*;][]\{:A{{+}+[1L.~)-1]%&}+1$\,.@^\[[[A]]+{|}*]+}/{.{L%}{%$..&1$,1$,/*$=}:C~\{L/}C&},, The input must be given on STDIN. Examples: 11 01 - 0 111 111 - 1 100 001 001 - 2 11100 10101 11100 - 1 101 010 101 - 5