Make a move on a Go board
Python 3 (557 504 488)

import sys
s=sys.stdin
M=int(next(s))+1
j=Z=M*M-M
S=s.read(Z)
P=0
b=[0]*3
while j&gt;0:j-=1+(j%M&lt;1);b[int(S[j])]|=1&lt;&lt;j;P|=1&lt;&lt;j
N=lambda x:(x&lt;&lt;1|x&gt;&gt;1|x&lt;&lt;M|x&gt;&gt;M)&amp;P&amp;~x
def h(a,b):t=a|N(a)&amp;b;return h(t,b)if t!=a else a
c,r,m=map(int,next(s).split())
o=m%2+1
p=1&lt;&lt;M*r+c
b[m]|=p
for n in(p&lt;&lt;1,p&gt;&gt;1,p&lt;&lt;M,p&gt;&gt;M):
 e=h(n&amp;P,b[o])
 if~b[m]&amp;N(e)&lt;1&lt;=n&amp;b[o]:b[o]&amp;=~e
_,B,W=b
g=~b[o]&amp;N(h(p,b[m]))&gt;=1&gt;~_&amp;p
print(+g)
q=''
while j&lt;Z:
 r=1&lt;&lt;j
 if g*j%M&gt;M-2:print(q);q=''
 else:q+='012E'[(r&amp;B&gt;0)+(r&amp;W&gt;0)*2]
 j+=1


Uses 3 bitfields to represent the board - one each for black, white, and empty spaces. Makes the find neighbors N and get chain h operations very concise.

An ungolfed version with lots of comments:
https://gist.github.com/airfrog/8429006