[Question]
  [
You are given a matrix of forward and back slashes, for instance:
```
//\\
\//\
//\/
```
A slash cuts along the diagonal of its cell corner-to-corner, splitting it in two pieces.
Pieces from adjacent (horizontally or vertically) cells are glued together.
Your task is to count the number of resulting pieces. For the same example, the pieces are easier to see in this illustration - 8 of them:
[![enter image description here](https://i.stack.imgur.com/XKYpq.png)](https://i.stack.imgur.com/XKYpq.png)
Write a function or a complete program. Input is a non-empty matrix in any convenient form. You may choose any pair of values (characters or numbers) to represent `/` and `\`; in the tests below we use 0=`/` and 1=`\`. Loopholes forbidden. Shortest wins.
```
in:
[[0,0,1,1],
 [1,0,0,1],
 [0,0,1,0]]
out:
8
in:
[[1]]
out:
2
in:
[[1,0],
 [1,1],
 [0,1],
 [0,0]]
out:
6
in:
[[1,0,1,1,0,1,0,0,0,1,1,1],
 [1,0,1,0,1,1,1,1,1,1,1,0],
 [1,1,1,0,1,1,0,1,1,1,1,0],
 [0,1,0,1,0,1,0,0,1,0,1,1],
 [1,1,1,1,0,0,1,1,1,0,0,1]]
out:
19
in:
[[1,0,1,1,0,1,0,0,0,1,1,1,1,0,1,0,1],
 [1,1,1,1,1,1,0,1,1,1,0,1,1,0,1,1,1],
 [1,0,0,1,0,1,0,1,0,0,1,0,1,1,1,1,1],
 [1,0,0,1,1,1,0,0,1,0,0,1,0,1,1,1,1],
 [0,1,0,0,0,0,1,0,1,0,0,1,0,1,1,1,1],
 [0,1,0,0,1,0,0,0,1,0,1,0,0,1,1,1,0],
 [0,1,1,1,1,1,0,0,1,0,1,0,0,1,1,1,0]]
out:
27
in:
[[0,1,1,1,1,1,1,1,0,0,1,0,1,0,0,0,0],
 [1,1,1,0,0,0,1,1,1,1,1,0,1,1,0,1,0],
 [1,0,0,1,1,1,0,0,0,1,0,1,0,0,1,1,1],
 [0,0,0,1,1,0,1,0,0,0,1,1,0,1,1,1,0],
 [1,1,0,1,0,0,1,0,0,1,0,1,0,0,0,1,0],
 [0,1,0,1,0,0,0,1,0,1,0,1,0,1,1,0,0],
 [0,1,1,1,0,0,1,0,1,0,0,0,0,1,1,1,1]]
out:
32
```
  
[Answer]
# [MATL](https://github.com/lmendo/MATL), 21 bytes
```
3XytPJ*-X*Xj~4&1ZIunq
```
The input is a matrix with `1` for `\` and `j` (imaginary unit) for `/`.
[Try it online!](https://tio.run/##y00syfn/3ziisiTAS0s3Qisiq85EzTDKszSv8P//6CydLB1DHUNrBUMdMNNaASKSFQsA) Or [verify all test cases](https://tio.run/##dVE9C8IwEN39FZ0cyjkcunV10cnBoVgKuj5UEOrg4l@PttfkcklKKYTc@8h797gNd3d12/YznI71pq1bfHdrvhzez5fbn10HAjFxUzFNx6aSG/SrjsefMM6mexnK5UgSIM0SXsQP9RMFillhAFIOlO0ZrOpyWnZXISXHdsY85C2ZpxCOxgbknw@jtQDhAiwqgRMjA/mnzko1OMQlg5Dlh9@DjZR5yf5LBcNuE4VSgmK817Tm2VpTZ0n80/sf).
With some extra code, you can [see the different pieces in random colours](https://matl.suever.net/?code=3XytPJ%2a-X%2aXj~4%261ZItunq7B~w3%26r%26v2ZG1YG&inputs=%5Bj%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2Cj%2Cj%2C1%2Cj%2C1%2Cj%2Cj%2Cj%2Cj%3B+1%2C1%2C1%2Cj%2Cj%2Cj%2C1%2C1%2C1%2C1%2C1%2Cj%2C1%2C1%2Cj%2C1%2Cj%3B+1%2Cj%2Cj%2C1%2C1%2C1%2Cj%2Cj%2Cj%2C1%2Cj%2C1%2Cj%2Cj%2C1%2C1%2C1%3B+j%2Cj%2Cj%2C1%2C1%2Cj%2C1%2Cj%2Cj%2Cj%2C1%2C1%2Cj%2C1%2C1%2C1%2Cj%3B+1%2C1%2Cj%2C1%2Cj%2Cj%2C1%2Cj%2Cj%2C1%2Cj%2C1%2Cj%2Cj%2Cj%2C1%2Cj%3B+j%2C1%2Cj%2C1%2Cj%2Cj%2Cj%2C1%2Cj%2C1%2Cj%2C1%2Cj%2C1%2C1%2Cj%2Cj%3B+j%2C1%2C1%2C1%2Cj%2Cj%2C1%2Cj%2C1%2Cj%2Cj%2Cj%2Cj%2C1%2C1%2C1%2C1%5D&version=21.0.0). Or [increase the resolution for a better looking result](https://matl.suever.net/?code=10XytPJ%2a-X%2aXj~4%261ZItunq7B~w3%26r%26v2ZG1YG&inputs=%5Bj%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2Cj%2Cj%2C1%2Cj%2C1%2Cj%2Cj%2Cj%2Cj%3B+1%2C1%2C1%2Cj%2Cj%2Cj%2C1%2C1%2C1%2C1%2C1%2Cj%2C1%2C1%2Cj%2C1%2Cj%3B+1%2Cj%2Cj%2C1%2C1%2C1%2Cj%2Cj%2Cj%2C1%2Cj%2C1%2Cj%2Cj%2C1%2C1%2C1%3B+j%2Cj%2Cj%2C1%2C1%2Cj%2C1%2Cj%2Cj%2Cj%2C1%2C1%2Cj%2C1%2C1%2C1%2Cj%3B+1%2C1%2Cj%2C1%2Cj%2Cj%2C1%2Cj%2Cj%2C1%2Cj%2C1%2Cj%2Cj%2Cj%2C1%2Cj%3B+j%2C1%2Cj%2C1%2Cj%2Cj%2Cj%2C1%2Cj%2C1%2Cj%2C1%2Cj%2C1%2C1%2Cj%2Cj%3B+j%2C1%2C1%2C1%2Cj%2Cj%2C1%2Cj%2C1%2Cj%2Cj%2Cj%2Cj%2C1%2C1%2C1%2C1%5D&version=21.0.0).
## Explanation
Consider input `[1,j; 1,1; j,1; j,j]` as an example. This corresponds to
```
\/
\\
/\
//
```
`3Xy` creates a 3×3 identity matrix:
```
1 0 0
0 1 0
0 0 1
```
`tP` pushes a copy of this matrix and flips it vertically. `J*` multiplies each entry by the imaginary unit, to give
```
0 0 j
0 j 0
j 0 0
```
`-` subtracts the two matrices:
```
 1     0    -j
 0   1-j     0
 -j    0     1
```
`X*` takes the input matrix implicitly and computes the Kronecker product. This replaces each entry in the input matrix by its product with the above 3×3 matrix:
```
 1    0   -j    j    0    1
 0  1-j    0    0  1+j    0
-j    0    1    1    0    j
 1    0   -j    1    0   -j
 0  1-j    0    0  1-j    0
-j    0    1   -j    0    1
 j    0    1    1    0   -j
 0  1+j    0    0  1-j    0
 1    0    j   -j    0    1
 j    0    1    j    0    1
 0  1+j    0    0  1+j    0
 1    0    j    1    0    j
```
`Xj` takes the real part:
```
1 0 0 0 0 1
0 1 0 0 1 0
0 0 1 1 0 0
1 0 0 1 0 0
0 1 0 0 1 0
0 0 1 0 0 1
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1
0 0 1 0 0 1
0 1 0 0 1 0
1 0 0 1 0 0
```
Note how the above matrix is a "pixelated" version of
```
\/
\\
/\
//
```
`~` applies logical negation, that is, swaps `0` and `1`:
```
0 1 1 1 1 0
1 0 1 1 0 1
1 1 0 0 1 1
0 1 1 0 1 1
1 0 1 1 0 1
1 1 0 1 1 0
1 1 0 0 1 1
1 0 1 1 0 1
0 1 1 1 1 0
1 1 0 1 1 0
1 0 1 1 0 1
0 1 1 0 1 1
```
`4&1ZI` specifies `4`-connectivity, and finds connected components considering `1` as foreground and `0` as background. The result is a matrix of labelled connected components, where each original `1` is replaced by an integer label:
```
0 3 3 3 3 0
1 0 3 3 0 5
1 1 0 0 5 5
0 1 1 0 5 5
2 0 1 1 0 5
2 2 0 1 1 0
2 2 0 0 1 1
2 0 4 4 0 1
0 4 4 4 4 0
4 4 0 4 4 0
4 0 4 4 0 6
0 4 4 0 6 6
```
`unq` computes the number of unique elements and subtracts `1`. This gives the number of components, which is implicitly displayed.
[Answer]
# MSX-BASIC, ~~226~~ 199 bytes
```
1SCREEN2:READC,L:W=256/C:H=192/L:FORJ=1TOL:FORI=1TOC:A=H:B=H:READD:IFDTHENA=0:B=-H
2LINE(I*W,J*H-A)-STEP(-W,B):NEXTI,J:FORY=0TO191:FORX=0TO255:IFPOINT(X,Y)=4THENR=R+1:PAINT(X,Y)
3NEXTX,Y:SCREEN0:?R
```
This script draw slashes on a whole screen and use PAINT operator to count closed areas.
[![The MSX-BASIC script in process](https://i.stack.imgur.com/RLYso.png)](https://i.stack.imgur.com/RLYso.png)
[![Result](https://i.stack.imgur.com/jmlri.png)](https://i.stack.imgur.com/jmlri.png)
To test:
* copy to clipboard the script with test cases data (see below)
* open online emulator <https://webmsx.org/>
* press `Alt-V` and `Ctrl+V` to past test script into MSX
* press `Enter` and `F5` to run test script
* wait a some time to see a result (press `Shift-Alt-T` to switch in `CPU Turbo 8X` mode to save your time)
```
1SCREEN2:READC,L:W=256/C:H=192/L:FORJ=1TOL:FORI=1TOC:A=H:B=H:READD:IFDTHENA=0:B=-H
2LINE(I*W,J*H-A)-STEP(-W,B):NEXTI,J:FORY=0TO191:FORX=0TO255:IFPOINT(X,Y)=4THENR=R+1:PAINT(X,Y)
3NEXTX,Y:SCREEN0:?R
10 ' this and below lines are not counted
20 ' the script runs first uncommented test case.
30 ' comment unnecessary test cases
100 '
110 'test case 1: expected output=8
120 'DATA 4,3
130 'DATA 0,0,1,1,1,0,0,1,0,0,1,0
200 '
210 'test case 2: expected output=2
220 'DATA 1,1
230 'DATA 1
300 '
310 'test case 3: expected output=6
320 'DATA 2,4
330 'DATA 1,0,1,1,0,1,0,0
400 '
410 'test case 4: expected output=19
420 'DATA 12,5
430 'DATA 1,0,1,1,0,1,0,0,0,1,1,1
440 'DATA 1,0,1,0,1,1,1,1,1,1,1,0
450 'DATA 1,1,1,0,1,1,0,1,1,1,1,0
460 'DATA 0,1,0,1,0,1,0,0,1,0,1,1
470 'DATA 1,1,1,1,0,0,1,1,1,0,0,1
500 '
510 'test case 5: expected output=27
520 DATA 17,7
530 DATA 1,0,1,1,0,1,0,0,0,1,1,1,1,0,1,0,1
540 DATA 1,1,1,1,1,1,0,1,1,1,0,1,1,0,1,1,1
550 DATA 1,0,0,1,0,1,0,1,0,0,1,0,1,1,1,1,1
560 DATA 1,0,0,1,1,1,0,0,1,0,0,1,0,1,1,1,1
570 DATA 0,1,0,0,0,0,1,0,1,0,0,1,0,1,1,1,1
580 DATA 0,1,0,0,1,0,0,0,1,0,1,0,0,1,1,1,0
590 DATA 0,1,1,1,1,1,0,0,1,0,1,0,0,1,1,1,0
600 '
610 'test case 5: expected output=32
620 DATA 17, 7
630 DATA 0,1,1,1,1,1,1,1,0,0,1,0,1,0,0,0,0
640 DATA 1,1,1,0,0,0,1,1,1,1,1,0,1,1,0,1,0
650 DATA 1,0,0,1,1,1,0,0,0,1,0,1,0,0,1,1,1
660 DATA 0,0,0,1,1,0,1,0,0,0,1,1,0,1,1,1,0
670 DATA 1,1,0,1,0,0,1,0,0,1,0,1,0,0,0,1,0
680 DATA 0,1,0,1,0,0,0,1,0,1,0,1,0,1,1,0,0
690 DATA 0,1,1,1,0,0,1,0,1,0,0,0,0,1,1,1,1
```
[Answer]
# JavaScript (ES6), ~~175~~ 174 bytes
This is probably overcomplicated.
```
a=>a.map((r,y)=>r.map((v,x)=>[2,4].map(s=>v&s||(n++,g=(x,y,s,i,v=(r=a[y])&&r[x])=>!(v&(s^=i%2==v%2&&6))/v&&g(x+1-(r[x]|=s,s&2),y++,s^6)|g(x,y-=v%2*2^s&2,s,v))(x,y,s))),n=0)|n
```
[Try it online!](https://tio.run/##fZLvaoMwFMW/@xTuQ0Oy3rY32eg2RvoiYkG6VhydFjOCgu/u/NcmUSeCcM25v3s8N9@RjtQpT26/mzT7OtcXWUfyEG1/ohulOZRMHvK@0FA0RSDgNew@KHnQRFUVTddriCUtoAQFCWhJcxkFZcgIyYMibJqeqCZUHWWyElLqlSBkz9hOExLTYs03tJVVUoEigkHZ4NRxz6q4RW5a/bM4NkcNXTPWz2GMQSqRVWl9ylKVXc/baxbTCw383c5/93w/QEDgwENoCw5d2Rf9CYZeyNin580ARNezINgP0Dv9QTYjFrr5x90Th/6NMNi1Dd8F5jHzwO52DhFMLxqK3cnNtCGXhSjeFryaUS7eNubYdLYxZ3NOxi2JI7R/GB3mgozPSEfx8dFQV/Z/Vi9iTJhScLxEBJykhvau3RAmjsydnlsRTm8OzsT5II/v0HhRgw03q8kfPpLvsqr/AA "JavaScript (Node.js) – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 70 bytes
```
≔×³Ｌθη≔×³Ｌ§θ⁰ζＢζηψＦＬθ«Ｊ¹⊕×³ιＦ§θι«¿κＰ\²Ｐ/²Ｍ³→»»Ｆη«Ｊ⁰ιＦζ«⊞υ¬℅ＫＫ¤#→»»⎚ＩΣυ
```
[Try it online!](https://tio.run/##dY5PT8JAFMTP7ad4wctrssZWb3BCEhOMaKPcag9Nu3Q3bLewfwjW8Nlrlw0pHDzOmzfzm5IVqmwL0fdzrXktcc0bqvGJwBuVtWG4jyICLJqF//hzs5QVPeKeQBy53274fW6P2A0xAj@D2rQKcKyD3zB4tc1u3WJCYClLRRsqDa3Gbj40zcLgHLwCcB8O@AZwG8HKCsN3ikuD028Cjy4SUKHpjfNwcVbtgbry6SevmXGnU3jy49j1qNiBLvTOE1OrGVoC763BD1VxWQhMKd1i5JcGL1wInNxNRtItZiFooXBQ6XnVotAGv2yD1uX7PsuymMQkIUlOQoAsIWfphXfiPM/7@4P4Aw "Charcoal – Try It Online") Link is to verbose version of code. Uses the same format as the examples (except Charcoal requires the list of inputs as an outer array). Explanation:
```
≔×³Ｌθη≔×³Ｌ§θ⁰ζ
```
Like @LuisMendo, we're going to be drawing the matrix at 3x scale, so calculate that in advance.
```
Ｂζηψ
```
Draw an empty rectangle of that size so that we can fill the edge pieces.
```
ＦＬθ«Ｊ¹⊕×³ιＦ§θι«
```
Loop over the rows and columns.
```
¿κＰ\²Ｐ/²Ｍ³→»»
```
Draw each slash at triple size and move to the next one.
```
Ｆη«Ｊ⁰ιＦζ«
```
Loop over all of the squares.
```
⊞υ¬℅ＫＫ
```
Record whether this square was empty.
```
¤#→»»
```
But try filling it anyway before moving on to the next square. (`Fill` does nothing if the current square is not empty.)
```
⎚ＩΣυ
```
Clear the canvas and output the total number of empty squares found, which equals the number of pieces (because each piece would have been immediately filled in as soon as we counted it).
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc), 292 bytes
```
n=>{var x=n.SelectMany(l=>"\0".Select(g=>l.SelectMany(r=>r.Skip(g).Concat(r.Take(g))).ToList())).ToList();int i=0,j=0,t=0;for(;i<x.Count;i++)for(j=0;j<x[0].Count;k(i,j++))t+=x[i][j]%2;void k(int a,int b){try{if(x[a][b]<50){x[a][b]='2';k(a+1,b);k(a-1,b);k(a,b-1);k(a,b+1);}}catch{}}return t;}
```
The `\0` should be a literal null byte.
`112` for `/`, `211` for `\`
[Try it online!](https://tio.run/##ZZBBS8QwEIXRo7@iFGQTmi1twVOaXERBUBB2b7WHtKY13ZpKmq5dSn57Tdmoq3vI8DHv5Q0zZb8uezHfD7JMH@7k8M4VK1r@h3uthKwpRUJqWpFZEjrtmfJGIsMNb3mpn5g8gJZQ/yW6uPRdE9SEtqcGRagKNzvxAWoY3nayZBqocMt23DYgDLfdo@g1OEVsR3qCRKixT5MIV50CWKSj/T9IjUUQwKVlZdykYxblTtgBgRorQh2QMRN51uTXCd534tWzkg1lyzZeASetDpOowJixPCvy9CaCk2OySlY2iAUxKuAC629AxTp2EFgwxq5Svk3GKK4HJT2NzYyvnu3ZNKiA5J9ZPh2rH8eJj1xN4thV87Mxcr6j9t997jvPW/jXZ07Oiucv "C# (Visual C# Interactive Compiler) – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~50~~ 46 bytes
```
®_ż+¥"SƝż€"Jż‘$$ḞṖ
ZJḤ©Żż‘$;€þJ;ÇẎfƇ@Ẏ¥€`Q$ÐLL
```
[Try it online!](https://tio.run/##y0rNyan8///Quvije7QPLVUKPjb36J5HTWuUvIBUwwwVlYc75j3cOY0ryuvhjiWHVh7dDRG2Bio5vM/L@nD7w119acfaHYDUoaVAwYRAlcMTfHz@u1s/aphzaJuCrp3Co4a5QHVcQP2PGmZ6PNyx2ORwO1Bl1qPGfYe2Hdr2/380V3S0gY6BjqGOYawOl0K0oQ6YB2ZDxA1igZzoaEMoBeRD1MHUwNXCFYAMg2jVgRqNZDhMHgHhBuog60WWM9BB6DRAmIGkzxBhE9QD@ByDMA7FCGSrURyCHDLYHIJFlSGSChR1SB4yQDERtypDLCpRA8cQzUZUVeCgwAh1FKUGaLFgoGOAESgGSHGF6kkMS@HJB1vwG2DEuwGWwIKbi5YC0CMB6gaUoMDwGzxYY7liAQ "Jelly – Try It Online")
A full program taking a matrix as its input with `-0.5` as `/` and `0.5` as `\`. Returns an integer with the number of pieces. 
Full explanation to follow, but works by generating a list of all the pairs of connected cells, and then merging overlapping sets until there’s no change. The final number of sets is the desired answer. 
]