To find islands of 1 and 0 in matrix Given a two dimensional matrix of 0 and 1s. Find the number of island for 1s and 0s where neighbours are only in the horizontal and vertical. 

Given input:

1 1 1 0
1 1 1 0

output = 1 1
Number of 1s island = 1

xxx-
xxx-

Number of 0s island = 1 

---x
---x

------------------------------

Given input:

0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1

output = 2 2
Number of 1s island = 2

----
xxxx  &lt;-- an island of 1s
----
xxxx  &lt;-- another island of 1s

Number of 0s island = 2

xxxx  &lt;-- an island
----
xxxx  &lt;-- another island
----

------------------------------

Given input:

1 0 0
0 0 0
0 0 1
output = 2 1
Number for 1's island = 2:

x--  &lt;-- an island of 1s
---
--x  &lt;-- an island of 1s

Number of 0's island = 1:

-xx  \
xxx   &gt; 1 big island of 0s
xx-  / 


------------------------------

Given input:

1 1 0
1 0 0
output = 1 1
Number for 1's island =1 and number of 0's island = 1

------------------------------

Given input:

1 1
1 1
output = 1 0
Number for 1's island =1 and number of 0's island = 0
APL (Dyalog Unicode), 29 28 bytesSBCS

-1 thanks to @Adám



{≢∪∨.∧⍨⍣≡2&gt;+/↑|∘.-⍨⍸⍵}¨⊂,~∘⊂


Try it online!

⊂,~∘⊂ the matrix and its negation

{ }¨ for each of them do

⍸⍵ list of pairs of coords of 1s

+/↑|∘.-⍨ matrix of manhattan distances

2&gt; neighbour matrix

∨.∧⍨⍣≡ transitive closure

≢∪ number of unique rows