[Question]
  [
You're given the map of a cinema theatre as a boolean matrix: 0 represents a free seat, 1 - occupied. Each [Finn](https://finnishnightmares.blogspot.fi/) who walks in chooses the seat farthest away ([Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)) from the nearest occupied one, or if several such exist - the first among them in [row-major order](https://en.wikipedia.org/wiki/Row-major_order). Output a matrix showing the order seats will eventually be occupied in; that is, replace the 0s with 2, 3, 4, etc
```
// in
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 1 1 0
// out
 2  8  3  9  1
10  5 11  6 12
 4 13 14 15  7
16 17  1  1 18
// in
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
// out
  5  43  17  44  45  46  18  47   8  48  49   6  50  19  51   2
 52  24  53  54   1  55  56  25  57  26  58  59  27  60  28  61
 20  62  63  29  64  65   1  66  30  67  68  21  69   9  70  71
 72  73   1  74  31  75  76  77  78   1  79  80  32  81  82  11
 12  83  84   1  85  86  87  13  88  89  90  14  91  92  33  93
 94  34  95  96  97  15  98  99  35 100  36 101 102   1 103  22
104 105  37 106  38 107  39 108 109  16 110  40 111 112  41 113
  4 114 115   7 116  23 117   3 118 119  42 120   1 121 122  10
// in
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
// out
  2  38 39  26  40   6 41  42  12  43  44   7  45  46  27  47   3
 48  49 15  50  28  51 52  29  53  30  54  55  56  16  57  31  58
 32  59 60  33  61  62 17  63  64  65  18  66  67  68  34  69  35
 70  10 71  72  13  73 74  75   1  76  77  78  11  79  80  14  81
 82  83 36  84  85  86 21  87  88  89  22  90  91  37  92  93  94
 19  95 96  97  23  98 99 100  24 101 102 103  25 104 105 106  20
107 108  4 109 110 111  8 112 113 114   9 115 116 117   5 118 119
```
I/O format is flexible within the established code golfing norms for your language. You can assume the input is correct, of size at least 3x3, and doesn't consist entirely of the same boolean value. Write a function or a complete program. The shortest solution per language is considered the winner; no answer will be accepted. Standard loopholes are forbidden.
  
[Answer]
# [MATL](https://github.com/lmendo/MATL), 37 bytes
```
!t~z:Q"@yX:gG&n:!J*w:+X:&-|w/X<&X>(]!
```
[Try it online!](https://tio.run/##y00syfn/X7GkrsoqUMmhMsIq3V0tz0rRS6vcSjvCSk23plw/wkYtwk4jVvH//2gDBQLQWoE2SgzpZREGjAUA "MATL – Try It Online") Or [verify all test cases](https://tio.run/##y00syfmfEOH9X7GkrsoqUMmhMsIq3Vstz0rRS6vcSjvCSk23plw/wkYtwk4jVvG/QXKES8j/aAMFCDS0VoAxDTCYhkBoEMsFV4wDIvQZElKAQ5E1FglDbCYYEnYDJjQkpICACVBLCQcEEbaQqcSQXhZhwFgA). You may also want to [see the cinema being filled up](https://matl.suever.net/?code=%21t%7Ez%3AQ%22%40yX%3AgG%26n%3A%21J%2aw%3A%2BX%3A%26-%7Cw%2FX%3C%26X%3E%28t%21.5%26XxZcD%5Dx&inputs=%5B0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0%3B+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0%3B+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0%3B+0+0+0+0+0+0+0+0+1+0+0+0+0+0+0+0+0%3B+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0%3B+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0%3B+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0%5D&version=20.8.1) in ASCII art.
### Explanation
```
!t        % Implicit input: M×N matrix of zeros and ones. Transpose and duplicate.
          % The transpose is needed because MATL uses column-major (not row-major)
          % order. It will be undone at the end
~z        % Number of zeros, say Z
:Q        % Range, add 1 element-wise: gives the array [2, 3, ..., Z+1]. These are
          % the new values that will be written into the matrix
"         % For each k in that array
  @       %   Push k. Will be written in a position to be determined
  y       %   Duplicate from below: pushes a copy of the current matrix, that has
          %   values up to k-1 already written in
  X:      %   Linearize into an (R*C)×1 vector, in column-major order
  g       %   Convert to logical: this replaces non-zero values by 1 
  G&n     %   Push input size as two separate numbers: M, N
  :!      %   Range, transpose: gives the column vector [1; 2; ...; N]
  J*      %   Multiply by imaginary unit, 1j, element-wise
  w:      %   Swap, range: gives the row vector [1, 2, ..., M]
  +       %   Add, with broadcast. Gives an N×M complex matrix defining a grid of
          %   coordinates: [1+1j, ..., M+1j; 2+1j, ... 2+1j; ...; N+1j, ..., N+Mj]
  X:      %   Linearize into an (M*N)×1 vector, in column-major order
  &-|     %   (M*N)×(M*N) matrix of absolute differences. This gives all distances
          %   between seats. Rows of this matrix represent currently used seats,
          %   and columns correspond to potential new positions
  w/      %   Swap, divide with broadcast. This divides the rows representing
          %   occupied seats by 1, and those with unocuppied seats by 0. So the
          %   latter rows are set to infinity, which effectively removes them for
          %   the subsequent minimization
  X<      %   Mimimum of each column: this gives the minimum distance to currently
          %   occupied seats for each potential new seat
  &X>     %   Argument maximum: gives the index of the first maximizing value
  (       %   Write value k at that position, using linear indexing
]         % End
!         % Transpose. Implicit display
```
[Answer]
# JavaScript (ES6), ~~156~~ 137 bytes
*Saved 18 bytes thanks to @l4m2*
That's quite a lot of `map()` ...
```
f=(a,n=1)=>a.map(B=(r,y)=>r.map((_,x)=>a.map(b=q=>q.map(v=>b=b<(d=X*X--+Y*Y)|!v?b:d,X=x)&Y--,Y=y)|v|b<=B||(R=r,C=x,B=b)))|B?f(a,R[C]=++n):a
```
[Try it online!](https://tio.run/##xZBRi4JAFIXf51fUS8zNO@G8RlfB/kFPikjMaMYupmWLGMx/d61dFpcwBgrivMxhPs453E/VqHNafxy/RFllu67LiSssSQJ5anFQRx4Qr/HS2/pm@Rbbvz9NJ/JOt2dDnia94hmF81AIJ5pHYKaNr5cZhtTCLBICI7qA0SsKjOEbqnFNLQakAcAEft4Xb@J1Qo5TwlJ1aVWeq2K3KKo9z3nMJpPYxR/JBIfWHbWyl5uwBIAN44CxB@kj@l8ibaARcLhvFB0mSbtN95I2kEXS74AXXdJy0pOYfEfpPXY9WfcN "JavaScript (Node.js) – Try It Online")
### Commented
```
f = (a, n = 1) =>               // a = input array; n = seat counter
  a.map(B =                     // initialize B to a non-numeric value
    (r, y) =>                   // for each row r at position y in a[]:
    r.map((_, x) =>             //   for each target seat at position x in r[]:
      a.map(b =                 //     initialize b to a non-numeric value
        q =>                    //     for each row q in a[]:
        q.map(v =>              //       for each reference seat v in q[]:
          b = b < (             //         if b is less than d, defined as
            d = X * X-- + Y * Y //           the square of the Euclidean distance
          ) | !v ?              //           or the reference seat is empty
            b                   //             let b unchanged
          :                     //           else:
            d,                  //             update b to d
          X = x                 //         start with X = x
        ) & Y--,                //       end of q.map(); decrement Y
        Y = y                   //       start with Y = y
      ) |                       //     end of inner a.map()
      b <= B ||                 //     unless b is less than or equal to B,
      (R = r, C = x, B = b)     //     update B to b and save this position in (R, C)
    )                           //   end of r.map()
  ) | B ?                       // end of outer a.map(); if B was updated:
    f(a, R[C] = ++n)            //   update the best target seat and do a recursive call
  :                             // else:
    a                           //   stop recursion and return a[]
```
[Answer]
# [Haskell](https://www.haskell.org/), ~~216~~ ~~213~~ ~~185~~ 184 bytes
```
import Data.Array
m a=[snd$maximum a|a/=[]]
f k=k//m[(r,((a,b),maximum(elems k)+1::Int))|s<-[assocs k],((a,b),0)<-s,r<-[minimum[(x-a)^2+(y-b)^2|((x,y),i)<-s,i>0]]]
(until=<<((==)=<<))f
```
Takes the input as an array. Input and output are in reverse order. Credit for fixed point magic to [Laikoni](https://codegolf.stackexchange.com/questions/144393/prime-factoral-roots/144402#144402).
[Try it online!](https://tio.run/##jZJhS8MwEIa/91ccY8KFpduqiDBaQRFBmCDot1JZtmUzrElLksoG@@/z2nXqkMkI7SW9p7nLm/dDuJXM853SZWE9vMm1779YZfwCsGwiC9rcg/Ci/1iZmVeFASzMcWasnAdc2qIq7zds9zt1Z63YBBpEkjoz72qxVrqi5VYMkjTLggWsktVgoFO0HFHwKeMtgzKX2sGK9aLR6Ml4xrYuDlPhXDGjz9kBH7I4dNxSSitT/5jiOhTs/bKHm3BKcYu45hvGVQOq22FGdZcJVsarPIljxCRhFBlb7LykkySQ04GazgFxSBU43vDomjHogpWf0jpJs1lhZsJDAGlKzH8j4wHANxSdA50Af6DjZHRqp@i8nv6O6BzojJ3aBkjyoDEVyUoCl5V/9XZsoA@VyZWRjmZalIB7VZ9F2XoQOhdXc@iQ9i1Qv8hK9bp1HDQ3CJPCTAAXzvfpafi9V4JAC2Xqoof6XVhCfdO7Lw "Haskell – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), ~~200~~ 187 bytes
```
a=input()
z=len(a[0]);P=[divmod(i,z)for i in range(len(a)*z)];i=2
while 0in sum(a,[]):t,y,x=max((min((u-U)**2+(v-V)**2for V,U in P if a[V][U]),-v,-u)for v,u in P);a[-y][-x]=i;i+=1
print a
```
[Try it online!](https://tio.run/##zVHPa4MwFD43f0VuJjaC8VjJsbuuF72EHIKma6BGidGp/7xTBxsO1s7CoPAdHu@978fjVb27lCYaszJXzPO8UTJtqsYhDAZ2VQZJHgocnxjPdVuUOdJkwOfSQg21gVaaN4WWNewPWMSaReD9oq8KhtO4bgokCRf44EhPOlbIDqFCG4SaIMG@H@1RG6RzMSumJJk1T1CfoeSp4InAJGhJ0CyGLWmWMY4lD3rBg04wHes9o6Cy2jgoxyk/cLY/gN1nBjpVO9WpDM7nTfWyCFSXqcrB4@vL0drSTkuVrGsARs5DAr9ABYGrRnijQReEQoC1yH38FKWbGfd5q5x/oq486CN33ADdzNjo8R34gYc8lOw/SPS54/1OEuAD "Python 2 – Try It Online")
-13 bytes thx to a tip from [Not that Charles](https://codegolf.stackexchange.com/users/13950/not-that-charles) by removing unneeded check for cells being 0.
[Answer]
# [J](http://jsoftware.com/), ~~74~~ ~~70~~ 60 bytes
```
(+(1+>./@,)*i.@$(=]i.>./)*<./@(#|@-/])&,j./&i./@$)^:(1#.0=,)
```
[Try it online!](https://tio.run/##xVDBCoJAEL3vVwwpuqPb6igVWIYfknWIllZYOnitb9/WhJA6WBjEY2Dem3kzwzRWtVAWkIILy2NO8VYmlcBIy8rnZa2l4xhtnMi9azVPagxEI5NAO8XHfcHJk2kp0CKbSQhVNy0EAbcCVMuYoU44LKDeuSU96Jm9groaM9nDQ8uhaQw0oYdGu@jjO6Z53tzM5P0zVt8847eg/@xl7HQ8X4CvFYKhIcmGJLd3)
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 39 bytes
Thanks Cows quack for saving one byte, and ngn for saving another
```
1⌈2-≢∘⍸-⍴⍴∘⍋⍸∘~{⍵∪⍺[⊃⍒⌊/+.×⍨¨⍺∘.-⍵]}⍣≡⍸
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qG9qZv6jtgkG/w0f9XQY6T7qXPSoY8aj3h26j3q3gBCI0w3kAxl11Y96tz7qWPWod1f0o67mR72THvV06WvrHZ7@qHfFoRVAYaAiPaDGrbG1j3oXP@pcCNT3Pw1o@qPePiOQjr6pxUXJQLIkI7P4fy5YovNR7yoDBQg05MoF8oDCCjARAwIihkBowJWmkMulrs6FaSAOiGGoIZHqcKhFcxJO1WjmGRLtPkxoSKQ64syDuoTkgCTeCVRRaTigtmOqBAYXAA "APL (Dyalog) – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 43 bytes
```
,¬J;Ð€"T€Ẏ©Ʋ€ạ²Sɗþ/Ṃ€MḢị®⁸⁸ẎṀ‘¤ṛ¦€Ḣ}¦µẎċ0Ɗ¡
```
[Try it online!](https://tio.run/##y0rNyan8/1/n0Bov68MTHjWtUQoBEg939R1aeWwTmLXw0Kbgk9MP79N/uLMJKOD7cMeih7u7D6171LgDiIAqH@5seNQw49CShztnH1oG0rJjUe2hZYe2AqWOdBsc6zq08P/h9qOTHu6c8f9/dLSBjgIcGcbqKKAIGOARMAQjg9hYAA "Jelly – Try It Online")
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 44 bytes
```
{×⍵:⍵⋄≢∪∊⍺}@1@{q=⌈/,q←⌊⌿+.×⍨¨(⍸×⍵)∘.-⍳⍴⍵}⍨⍣≡
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///v/rw9Ee9W62A@FF3y6PORY86Vj3q6HrUu6vWwdChutD2UU@Hvk7ho7YJj3q6HvXs19YDqV9xaIXGo94dYK2ajzpm6Ok@6t38qHcLkFsLlH3Uu/hR58L/aSBdvX1Gj7qaH/VNLS5KBpIlGZnF/9MUTBRMgcoNFCDQUMEABzQEyQEA "APL (Dyalog Unicode) – Try It Online")
Alternate solution at 44 bytes
```
{≢∪∊⍺}@1@{q=⌈/,q←⌊⌿+.×⍨¨(⍸×⍵)∘.-⍳⍴⍵}⍨⍣(~0∊⊣)
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 121 bytes
```
Nest[p=Position;ReplacePart[#,#&@@Pick[a=#~p~0,m=Min/@DistanceMatrix[a,N@p[#,x_/;x>0]],Max@m]->Max@#+1]&,#,Total[1-#,2]]&
```
[Try it online!](https://tio.run/##zZFLq4MwEIX/ytCAm46YdFuULO7WIpfuQiiDpNzQ@kCzEMT@dVu7aLHQh8KFwoGBw3xzTkhG7s9k5GxK/R5C6DemdqoMk6K2zhb5@teUR0pNQpVTDJknZWLTg6KQncoTxyyMbR7IH1s7ylMTk6tsowg3srysN7tg3URca4ypkZn2o2GypdAeMtwWjo5K@AxXWnt9Utl8yIAF@BEsEPaKaQ0eBBLatuUIN4kOYWTwF4a4ineDM955q8erYjLxnhsV/QgdZYg573ghMZmYmHEvPOdHZlX7D0h8d73nUNf1Zw "Wolfram Language (Mathematica) – Try It Online")
[Answer]
## Clojure, 247 bytes
```
#(let[R(range(count %))C(range(count(% 0)))](loop[M % s 2](if-let[c(ffirst(sort-by last(for[x R y C :when(=((M x)y)0)][[x y](-(nth(sort(for[i R j C :when(>((M i)j)0)](+(*(- i x)(- i x))(*(- j y)(- j y)))))0))])))](recur(assoc-in M c s)(inc s))M)))
```
Input is a vec-of-vecs `M`, which is modified in a `loop` by `assoc-in`. When no free spot is found (`if-let`) then the result is returned.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~35~~ ~~33~~ ~~30~~ 29 bytes
```
ZJæịþJFạþx¥F«/MḢṬ×FṀ‘Ɗo@FṁµÐL
```
[Try it online!](https://tio.run/##y0rNyan8/z/K6/Cyh7u7D@/zcnu4a@HhfRWHlrodWq3v@3DHooc71xye7vZwZ8OjhhnHuvIdgMzGQ1sPT/D5/3D3loc7Nj1qWhN2uN39/38DBQg05IKxDNBYhkBoAAA)
Replaced the `×ı+` with `æị` (complex combine), a new dyad based on `j.` from J, saving a byte.
Here is a more efficient version for TIO. [Try it online!](https://tio.run/##y0rNyan8/z/K6/Cyh7u7D@/zcnu4a@HhfRWHlro93LlA5dBqfd@HOxY93Lnm8HSgQMOjhhnHuvIdgMzGQ1sPT/D5/3D3loc7Nj1qWhN2uN39/38DBbyQC8YyJCCPQw0XprghFv2GBO3HhIYE5PHrh9oIAA)
## Explanation
```
ZJæịþJFạþx¥F«/MḢṬ×FṀ‘Ɗo@FṁµÐL  Input: matrix M
Z                              Transpose
 J                             Enumerate indices - Get [1 .. # columns]
     J                         Enumerate indices - Get [1 .. # rows]
  æịþ                          Outer product using complex combine
                                 (multiply RHS by 1j and add to LHS)
      F                        Flatten
           F                   Flatten input
          ¥                    Dyadic chain
         x                       Times - Repeat each of LHS by each of RHS
       ạþ                        Outer product using absolute difference
            «/                 Reduce by minimum
              M                Indices of maximal values
               Ḣ               Head
                Ṭ              Untruth - Return a Boolean array with 1's at the indices
                 ×             Times
                     Ɗ         Monadic chain
                  F              Flatten input
                   Ṁ             Maximum
                    ‘            Increment
                      o@       Logical OR
                        F      Flatten input
                         ṁ     Mold - Reshape to match the input
                          µÐL  Repeat until result converges
```
[Answer]
# [K (ngn/k)](https://gitlab.com/n9n/k), ~~81~~ ~~75~~ ~~73~~ ~~72~~ 70 bytes
```
{(~&/,/){a[*>A*&/+/''b*b:i[&~A:~a]-/:\:i:+!n:(#x;#*x)]:#?a:,/x;n#a}/x}
```
[Try it online!](https://tio.run/##vdTdahpBGAbg81zFWyzGNQk7/z@70JLrsELMgUVSLIQcbBC9dft@M6YGUlEIlNWJs843M77zZJ/u1j/X@/2y20x24/a2bTaL2fTb/XTc3rTX14/Tx241G@/uu91iftd2P7pVd/Nl3U1GQz@aDs28G31fdLft0K9Hi207bPcv3ebr7HX33C0x9A@/n/rJw3Kx@tUP/Wv/3My3Vy@ziUK99BWAt476Z0fzUg17E4MEi1yLtIKH1gjQRvoO2kKz9Yjle34RS7FOzfz9mieu94vr80NODDvu@uTA4yz6kr18vPT5IWdnOSxdcmWQzkpazsHxM5NLcJFpO74yI/YsyPAaJWp4A@PgLTzzhvfwAYZthOHYBJ9hIoKCSQjltGAUgkGwMJzPIfDsEAIsb3NkgmE382yjQqwV0SByV4gOli2PNSBGxCT3MpKCJQiNZMigVGj2LZLsKXmkgBQFRUpInFqJjqyRDSwV2VKSOTnveuSAHAVPTsgZlvtTXIFRKApSRhwp7t5Ue1xDeVhWKP4IbklFWMpU8jEXfATquCaJam7MyZ@6JmvFqUBly@T4Kxm@tKxmzo6rmQLfyJs9dZHgi1B8YpD@v8t9uCpWI3kza1JjvkGSLYGJYQKOB8IESMKHxAtjBu4LSToWwVkEEyARV8I8CxImNp9KGX1RMhnTS9Dil8dEwge/SQBXvTQUBE2Fq@ThFLUIJj8iJuEo4I@C3wgTQqp4U8FLcPRb9fLwCbjqJQICpl6SI@BMwK6az0e9hFT1Cl3jjnQFrv@rVsgaVR3HQtZVtLpyTQWslsep/McUqbqIjuWRW5A28/0f "K (ngn/k) – Try It Online")
]