s = 0
unseen = 0
seen(v) = bit test(s, v)
see(v) = s = bit or(s, 2^v); while (seen(unseen), unseen++)

T = matrix(W=50*2+1,W, x,y, -oo)
z0 = (1+I)*(1+W\2)

get(z) = my (x=real(z+z0), y=imag(z+z0)); if (min(x,y)>0 && max(x,y)<=#T, T[x,y], -oo)
set(z,v) = my (x=real(z+z0), y=imag(z+z0)); T[x,y]=v

dz = vector(4, k, I^k)

other(z) = {
	my (x=[], w=[]);
	for (i=1, #dz,
		my (v=get(z+dz[i]));
		if (v>=0,
			w = concat(w,v),
			for (j=1, #dz,
				my (vv=get(z+dz[i]+dz[j]));
				if (vv>=0,
					x=concat(x,vv);
				);
			);
		);
	);
	w=Set(w);
	x=Set(x);
	for (v=if (#w, max(0, unseen-w[1]), 0), oo,
		if (!setsearch(x,v) && !#select(o -> seen(o+v), w),
			apply(o -> see(o+v), w);
			set(z,v);
			return (v);
		);
	);
}

{
	my (z=0, dir=+1, n=0);
	for (w=1, oo,
		for (t=1, 2,
			for (s=1, w,
				my (v = other(z));
				print (n++ " " v);
				if (n==#T^2,
					break (3);
				);
				z += dir;
			);
			dir *= I;
		);
	);
}

quit