$\ = $/; ## print behaves as puts

<DATA>;	# drop 1st input value

# take all input lines and split them to characters, 
# then append each character to array "verticals" elements:

map { chomp; $i = 0; $verticals[ $i ++ ] .= $_ for split // } <DATA>;

# construct the line of column values:

$line = join '', map {
	y/?//d; 			# delete ?
	y///cs; 			# squeeze
	/^.$/ ? $_ : q(?)		# if only one char. is left: use it, otherwise mark as ?
	} @verticals;
	
##	print $line;

##	$_ = "..?X??.X.X.?.?.?.X";	# test line
	$_ = $line;
	
# search and substitute until any is possible:

nO_Operation while 0
|| s/\.\?(?=\.)/.X/g		# .?. determine as .X.
|| s/X\?(?=X)/X./g
|| s/\Q?XX\E/.XX/g		# ?XX determine as .XX
|| s/\QXX?\E/XX./g
|| s/\Q?..\E/X../g
|| s/\Q..?\E/..X/g
;

if (/[?]/){print -1}		# -1 if there are question mark left
else{
s/(.)\1/1/g,			# two consecutive characters convert to 1
s/[^1]/0/g,				# any but 1 character convert to 0
print
}

__DATA__
4
.X??
.??.
??.?
?X.?
.X?.
