Problem:
 Score : 1300 points    Problem Statement There is a square-shaped grid with N vertical rows and N horizontal columns.
We will denote the square at the i -th row from the top and the j -th column from the left as (i,\ j) .  Initially, each square is either white or black.
The initial color of the grid is given to you as characters a_{ij} , arranged in a square shape.
If the square (i,\ j) is white, a_{ij} is . . If it is black, a_{ij} is # .  You are developing a robot that repaints the grid.
It can repeatedly perform the following operation:   Select two integers i , j ( 1 ≤ i,\ j ≤ N ). Memorize the colors of the squares (i,\ 1) , (i,\ 2) , ... , (i,\ N) as c_1 , c_2 , ... , c_N , respectively. Then, repaint the squares (1,\ j) , (2,\ j) , ... , (N,\ j) with the colors c_1 , c_2 , ... , c_N , respectively.   Your objective is to turn all the squares black.
Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive.      Constraints  2 ≤ N ≤ 500  a_{ij} is either . or # .       Partial Score  In a test set worth 300 points, N ≤ 3 .         Input The input is given from Standard Input in the following format:  N  a_{11} ... a_{1N}  :  a_{N1} ... a_{NN}       Output If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective.
If it is impossible, print -1 instead.        Sample Input 1 2
#.
.#      Sample Output 1 3  For example, perform the operation as follows:   Select i = 1 , j = 2 .  Select i = 1 , j = 1 .  Select i = 1 , j = 2 .   The transition of the colors of the squares is shown in the figure below:          Sample Input 2 2
..
..      Sample Output 2 -1       Sample Input 3 2
##
##      Sample Output 3 0       Sample Input 4 3
.#.
###
.#.      Sample Output 4 2       Sample Input 5 3
...
.#.
...      Sample Output 5 5

Solution (Python):
n = int(input())
a = [list(input()) for i in range(n)]
cntr, cntc = [i.count(".") for i in a], [i.count(".") for i in zip(*a)]
if sum(cntr) == 0:
    exit(print(0))
if sum(cntr) == n * n:
    exit(print(-1))
if any(i == 0 for i in cntr):
    cnt = 0
    for i in cntc:
        cnt += 1 if i > 0 else 0
    exit(print(cnt))
cnt2 = 0
ans = float("inf")
for i in cntc:
    cnt2 += 1 if i > 0 else 0
for i in range(n):
    ans = min(ans, cntr[i] + cnt2 + 1) if cntc[i] == n else min(ans, cntr[i] + cnt2)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
a = [list(input().rstrip()) for _ in range(N)]

ans = float('inf')
b = 0
for j in range(N):
    if any(a[i][j] == '.' for i in range(N)):
        b += 1
for i in range(N):
    if any(a[k][i] == '
        ans = min(ans, a[i].count('.') + b)
    elif any(a[i][k] == '
        ans = min(ans, a[i].count('.') + b + 1)
print(ans if ans < float('inf') else -1)

Solution (Python):
n = int(input())
s = [input() for i in range(n)]


sharp_cnt = 0
for i in range(n):
    sharp_cnt += s[i].count("
if sharp_cnt == 0:
    print(-1)
    exit()

ans = n
for j in range(n):
    tate_all_sharp = all([s[i][j] == "
    if tate_all_sharp:
        ans -= 1


need_cnt = n
for j in range(n):
    tate_any_sharp = any([s[i][j] == "
    if not tate_any_sharp:
        continue
    tmp = 0
    for i in range(n):
        if s[j][i] == ".":
            tmp += 1
    need_cnt = min(tmp, need_cnt)
    
for i in range(n):
    tmp = 0
    for j in range(n):
        if s[i][j] == ".":
            tmp += 1
    need_cnt = min(tmp + 1, need_cnt)

ans += need_cnt
print(ans)

Solution (Python):
n = int(input())
a = [input() for _ in range(n)]

cnt_col = [0]*n
cnt_row = [0]*n

for i in range(n):
    for j in range(n):
        if(a[i][j] == '
            cnt_col[i] += 1
            cnt_row[j] += 1

if(sum(cnt_col)==0):
    print(-1)
    exit()

b_row = cnt_row.count(n)


ans = 2*n
for i in range(n):
    if(cnt_col[i] == n):
        ans = min(ans, n-b_row)
        continue

    if(cnt_row[i] > 0):
        ans = min(ans, n-cnt_col[i] + n-b_row)
    else:
        ans = min(ans, n-cnt_col[i] + n-b_row + 1)

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
a = [input()[:-1] for _ in range(N)]
row_white = [0]*N
col_white = [0]*N

for i in range(N):
    for j in range(N):
        if a[i][j]=='.':
            row_white[i] += 1

for i in range(N):
    for j in range(N):
        if a[j][i]=='.':
            col_white[i] += 1

if row_white==[N]*N:
    print(-1)
    exit()

ans = 10**18
C = 0

for i in range(N):
    if col_white[i]>0:
        C += 1

for i in range(N):
    if col_white[i]<N:
        ans = min(ans, row_white[i]+C)
    else:
        ans = min(ans, row_white[i]+1+C)

print(ans)

Solution (Python):
N=int(input())
a=["" for i in range(N)]
data=[0]*N
for i in range(N):
    a[i]=input()
    data[i]=a[i].count("

if sum(data)==0:
    print(-1)
elif sum(data)==N**2:
    print(0)
else:
    data2=[0 for i in range(N)]
    for i in range(N):
        for j in range(N):
            data2[j]+=(a[i][j]=="
    count=0
    for i in range(N):
        if data2[i]!=N:
            count+=1
    ans=10**10
    for i in range(N):
        test=(N-data[i])+count+(data2[i]==0)
        ans=min(ans,test)

    print(ans)

Solution (Python):
def main():
    n = int(input())
    grid = [input() for _ in [0]*n]
    for i in grid:
        if "
            break
    else:
        print(-1)
        return
    ans = 10**20
    
    black_cnt = [0]*n
    
    exist = [False]*n
    for i in range(n):
        for j in range(n):
            if grid[i][j] == "
                black_cnt[i] += 1
                exist[j] = True
    
    all_cnt = 0
    for j in range(n):
        for i in range(n):
            if grid[i][j] == ".":
                break
        else:
            all_cnt += 1
    for i in range(n):
        if exist[i]:
            ans = min(ans, n-black_cnt[i]+n-all_cnt)
        else:
            ans = min(ans, 2*n-black_cnt[i]-all_cnt+1)
    print(ans)


main()

Solution (Python):
N = int(input())
X = [[1 if a == "
C = [sum([X[i][j] for i in range(N)]) for j in range(N)]
a = N - max(sum(X[i]) - (0 if C[i] else 1) for i in range(N))
b = N - C.count(N)
print(-1 if sum(C) == 0 else a + b)

Solution (Python):
N = int(input())
A = [input() for i in range(N)]
row_w = [row.count('.') for row in A]
col_w = [col.count('.') for col in zip(*A)]

if all(w==0 for w in row_w):
    print(0)
    exit()
if all(w==N for w in row_w):
    print(-1)
    exit()
if any(w==0 for w in row_w):
    print(len([1 for w in col_w if w>0]))
    exit()

ng_col = len([1 for w in col_w if w>0])
ans = 10**10
for i,rw in enumerate(row_w):
    tmp = rw + (col_w[i]==N) + ng_col
    ans = min(ans, tmp)
print(ans)

Solution (Python):
import sys
def input():
	return sys.stdin.readline()[:-1]
n = int(input())
a = [input() for _ in range(n)]

min1 = 1000000
all_white = True
all_black = False
row_exist = [False for _ in range(n)]
row_white = [0 for _ in range(n)]

for i in range(n):
	cnt = 0
	for j in range(n):
		if a[i][j] == "
			cnt += 1
			row_exist[j] = True
	if cnt > 0:
		all_white = False
	if cnt == n:
		all_black = True
	row_white[i] = n - cnt

if all_white:
	print(-1)
	sys.exit()
if all_black:
	cnt = 0
	for i in range(n):
		for j in range(n):
			if a[j][i] == ".":
				cnt += 1
				break
	print(cnt)
else:
	ans = 0
	first_min = n
	for i in range(n):
		if row_exist[i]:
			first_min = min(first_min, row_white[i])
		else:
			first_min = min(first_min, row_white[i] + 1)
	ans += first_min
	cnt = 0
	for i in range(n):
		for j in range(n):
			if a[j][i] == ".":
				cnt += 1
				break
	ans += cnt
	print(ans)

Solution (Python):
n=int(input())
a=[list(input()) for i in range(n)]
if max([a[i].count("
  print(-1)
  exit()
minus=0
b=list(zip(*a))
for i in range(n):
  if b[i].count("
    minus=max(minus,a[i].count("
  else:
    minus=max(minus,a[i].count("
for j in range(n):
  if b[j].count(".")==0:
    minus+=1
print(2*n-minus)

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
state = [list(input().rstrip()) for _ in range(N)]

ColState = [0]*N
fullcols = 0
for w in range(N):
    isfull = True
    isempty = True
    for h in range(N):
        if state[h][w] == "
            isempty = False
        else:
            isfull = False
    if isfull:
        fullcols += 1
    if isempty:
        ColState[w] = -1

ans = 10**14
for h in range(N):
    emp = 0
    for w in range(N):
        if state[h][w] == ".":
            emp += 1
    if ColState[h] == -1:
        emp += 1
    ans = min(ans, emp+(N-fullcols))

if ans == 2*N+1:
    print(-1)
else:
    print(ans)

Solution (Python):
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7



N = int(sys.stdin.buffer.readline())
A = [list(sys.stdin.buffer.readline().decode().rstrip()) for _ in range(N)]
A = np.array(A, dtype='U1')

col_has_black = np.any(A == '
row_has_black = np.any(A == '
col_all_black = np.all(A == '
row_all_black = np.all(A == '


if np.all(~col_has_black):
    print(-1)
    exit()


def count_row_to_black(i):
    
    whites = np.count_nonzero(A[i] == '.')
    
    if col_has_black[i]:
        
        return whites
    else:
        
        return whites + 1



ans = INF
for i in range(N):
    cnt = count_row_to_black(i) + (N - col_all_black.sum())
    ans = min(ans, cnt)
print(ans)

Solution (Python):
n=int(input())
ii,jj=[0]*n,[0]*n
b=0
for i in range(n):
  s=input()
  for j in range(n):
    k=1if"
    ii[i]+=k
    jj[j]+=k
    b+=k
if b==0:print(-1);exit()
c=n-jj.count(n)
mi=10000
for i in range(n):
  x=n-ii[i]
  if jj[i]==0:x+=1
  mi=min(x,mi)
print(mi+c)

Solution (Python):
import sys

N = int(input())

a = []

ybl = 0
tbl = 0

for i in range(N):

    A = list(input())

    if "." not in A:
        ybl += 1

    a.append(A)


blexist = False
for j in range(N):

    blnum = 0

    for i in range(N):

        if a[i][j] == "
            blexist = True
            blnum += 1

    if blnum == N:
        tbl += 1



if not blexist:
    print (-1)
    sys.exit()

if ybl != 0:
    print (N - tbl)
    sys.exit()


ans = float("inf")

for i in range(N):

    bexi = False
    for j in range(N):
        if a[j][i] == "
            bexi = True
            break

    

    chnum = 0
    for j in range(N):
        if a[i][j] == ".":
             chnum += 1

    if bexi:
        ans = min(ans,chnum + (N - tbl))

    else:
        ans = min(ans,chnum + 1 + (N - tbl))

        

print (ans)

Solution (Python):
N = int(input())
grid = []
for _ in range(N):
    grid.append(input())

col_info = [0] * N
for j in range(N):
    cnt = 0
    for i in range(N):
        if grid[i][j] == '
            cnt += 1
        else:
            cnt -= 1
    if cnt == N:
        col_info[j] = 1
    elif cnt == -N:
        col_info[j] = -1

if sum(col_info) == N:
    print(0)
    exit()
elif sum(col_info) == -N:
    print(-1)
    exit()

ans = 10**9
for i in range(N):
    ans_new = N - col_info.count(1)
    if col_info[i] == -1:
        ans_new += 1
    for j in range(N):
        if grid[i][j] == '.':
            ans_new += 1
    ans = min(ans, ans_new)

print(ans)

Solution (Python):









N = int(input())
A = [input().replace("
A_T_bit = [int("".join(a), 2) for a in zip(*A)]

if A_T_bit.count(0) == N:
    print(-1)
    exit()
ans = float("inf")
for row, col in zip(A, A_T_bit):
    if col > 0:
        ans = min(ans, N - row.count("1"))
    else:
        ans = min(ans, N - row.count("1") + 1)
ans += N - A_T_bit.count((1<<N)-1)
print(ans)

Solution (Python):
n = int(input())
a = [input() for _ in range(n)]
ans1 = float('inf')
for i in range(n):
    for j in range(n):
        if a[j][i] == '
            break
    else:
        continue
    break
else:
    print(-1)
    exit(0)
for i in range(n):
    cnt = 1
    for j in range(n):
        if a[j][i] == '
            cnt -= 1
            break
    for j in range(n):
        if a[i][j] == '.':
            cnt += 1
    ans1 = min(ans1, cnt)
ans2 = 0
for i in range(n):
    for j in range(n):
        if a[j][i] == '.':
            ans2 += 1
            break
if ans1 == float('inf'):
    print(-1)
else:
    print(ans1 + ans2)

Solution (Python):
import sys
import numpy as np
input = sys.stdin.readline

def main():
    N = int(input())
    a = np.array([[0 if c == "." else 1 for c in list(input().strip())] for i in range(N)])

    c = 0
    ok = [False] * N
    all_white = True
    for i in range(N):
        tmp = np.count_nonzero(a[:,i] == 0)
        if tmp > 0:
            c += 1
        if tmp != N:
            all_white = False
            ok[i] = True
    
    if all_white:
        print(-1)
        return
    
    ans = int(1e9)
    for i in range(N):
        if ok[i]:
            tmp = np.count_nonzero(a[i] == 0)
        else:
            tmp = np.count_nonzero(a[i] == 0) + 1
        ans = min(ans, tmp + c)
    
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
ii,jj=[0]*n,[0]*n
b=0
for i in range(n):
  s=input()
  for j in range(n):
    k=1if"
    ii[i]+=k
    jj[j]+=k
    b+=k
if b==0:print(-1);exit()
c=n-jj.count(n)
mi=10000
for i in range(n):
  x=n-ii[i]
  if jj[i]==0:x+=1
  mi=min(x,mi)
print(mi+c)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)

import numpy as np

buffer = sys.stdin.buffer
N = int(buffer.readline())
grid = np.frombuffer(buffer.read(),'S1').reshape(N,-1)[:,:N] == b'
if (~grid).all():
    print(-1)
    exit()



cnt_row_op = np.sum(~grid,axis=1)
cnt_row_op += (~grid).all(axis=0)
answer = cnt_row_op.min() + np.sum((~grid).any(axis=0))
print(answer)

Solution (Python):
N = int(input())
G = [[1 if s == '
Gm = list(map(sum, G))
GT = list(map(list, zip(*G)))
geta = sum(sum(g) == N for g in GT)
exist = [sum(g) > 0 for g in GT]
ans = -1
if sum(Gm) != 0:
    ans = N - geta + min(N - Gm[i] + 1 - exist[i] for i in range(N))
print(ans)

Solution (Python):
N = int(input())
nMAP = [["" for i in range(N)] for j in range(N)]
tMAP = [["" for i in range(N)] for j in range(N)]
c = 0
flag = 0
for i in range(N):
    s = list(str(input()))
    if "
        flag = 1
    nMAP[i] = s
        
for i in range(N):
    for j in range(N):
        tMAP[i][j] = nMAP[j][i]
        
for i in range(N):
    if "." in tMAP[i]:
        c+=1
        
MIN = N
for i in range(N):
    x = nMAP[i].count(".")
    if "
        MIN = min(MIN,x)
    else:
        MIN = min(MIN,x+1)
    
if flag == 0:
    print(-1)
else:
    print(MIN+c)

Solution (Python):
n,*a=open(0);n=int(n);*b,=zip(*a);f,e=[sum((c in t)^1for t in b)for c in'.

Solution (Python):
n,*a=open(0).readlines();n=int(n);*b,=zip(*a);f,e=[sum((c in t)^1for t in b)for c in'.

Solution (Python):
n,*a=open(0).readlines();n=int(n);*b,=zip(*a);f,e=[sum((c in t)^1for t in b)for c in'.

Solution (Python):
n,*a=open(0).readlines();n=int(n);*b,=zip(*a);f,e=[sum((c in t)^1for t in b)-1for c in'.

Solution (Python):
n,*a=open(0).readlines();n=int(n);*b,=zip(*a);f,e=[sum(n==t.count(c)for t in b)for c in'

Solution (Python):
s='

Solution (Python):
n,*a=open(0).readlines();n=int(n);f,e=[sum(n==t.count(c)for t in zip(*a))for c in'

Solution (Python):
s='

Solution (Python):
s='

Solution (Python):
n=int(input());a=eval('list(input()),'*n);f=sum(n==t.count('

Solution (Python):
n=int(input());a=eval('list(input()),'*n);f=sum(n==t.count('

Solution (Python):
n=int(input())
a=eval('list(input()),'*n)
if all(t.count('
  print(-1)
  exit()
f=sum(n==t.count('
c=10**18
for i,b in enumerate(a):
  v=b.count('
  t=0
  if b[i]!='
    for j in range(n):
      if a[j][i]=='
        break
    else:
      t+=1
    t+=n-v
  else:
    t+=n-v
  c=min(c,t+n-f)
print(c)

Solution (Python):
n=int(input())
a=eval('list(input()),'*n)
if all(t.count('
  print(-1)
  exit()
f=sum(n==t.count('
c=10**18
for i,b in enumerate(a):
  v=b.count('
  t=0
  if b[i]!='
    for j in range(n):
      if a[j][i]=='
        break
    else:
      t+=1
    t+=n-v
  else:
    t+=n-v
  c=min(c,t+n-f)
print(c)

Solution (Python):
N=int(input())
Grid=[list(input()) for i in range(N)]
Grid_v=[list(x) for x in zip(*Grid)]
W,H=N,0
if Grid==[["."]*N for i in range(N)]:
  print(-1)
  exit()
for i in range(N):
  if Grid_v[i]!=["."]*N:
    W=min(W,N-Grid[i].count("
  else:
    W=min(W,N-Grid[i].count("
for i in range(N):
  if Grid_v[i]!=["
    H+=1
print(W+H)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    n = I()
    a = [[1 if c == '
    b = [n-sum(a[i]) for i in range(n)]

    t = 0
    for i in range(n):
        if not all([a[j][i] == 1 for j in range(n)]):
            t += 1

    if min(b) == 0:
        return t

    if min(b) == n:
        return -1

    r = min(b) + 1 + t
    for i in range(n):
        ai = a[i]
        for j in range(n):
            if ai[j] == 0:
                continue
            if r > b[j] + t:
                r = b[j] + t

    return r


print(main())

Solution (Python):
from operator import itemgetter
n=int(input())
a=[list(map(lambda x:x=='
cr,cc=list(map(sum,a)),[sum(map(itemgetter(i),a)) for i in range(n)]
mx=max(cr)
print(-1 if mx==0 else (not sum([cc[i] for i in range(n) if cr[i]==mx]))+n-mx+sum(map(lambda x:x<n,cc)))

Solution (Python):
N=int(input())
a=[input() for i in range(N)]
blackExistCol=[False for i in range(N)]
blackNumRow=[0 for i in range(N)]


flag=False
Max=-1
already=0

for i in range(N):
  for j in range(N):
    if a[i][j]=="
      blackExistCol[j]=True
      blackNumRow[i]+=1
      flag=True

if flag==False:
  print("-1")
else:
  for i in range(N):
    if blackExistCol[i]:
      if Max<blackNumRow[i]:
        Max=blackNumRow[i]
  Min=N-Max
  for j in range(N):
    for i in range(N):
      if a[i][j]==".":
        break
    else:
      already+=1
  temp=max(blackNumRow)
  Min2=N-temp+1
  
  ans=min(Min,Min2)+N-already
  
  print(ans)

Solution (Python):
def solve(n, a):
    if a == ['.'*n]*n:
        return -1
    white_column = sum(any(a[j][i] == '.' for j in range(n)) for i in range(n))
    res = float('inf')
    for r in range(n):
        all_white = all(a[i][r] == '.' for i in range(n))
        res = min(res, white_column + all_white + sum(1 for i in range(n) if a[r][i] == '.'))
    return res


n = int(input())
a = [input() for _ in range(n)]
print(solve(n, a))

Solution (Python):
n=int(raw_input())
a=[raw_input() for _ in xrange(n)]
cnt=0
for i in xrange(n):
    for j in xrange(n):
        if a[i][j]==".":
            cnt+=1
if cnt==n*n:
    print -1
    exit()

cnt=[0]*n
y=[False]*n
for i in xrange(n):
    for j in xrange(n):
        if a[i][j]==".":
            cnt[i]+=1
            y[i]=True
t=[False]*n
nokori=n
for j in xrange(n):
    tmp=0
    for i in xrange(n):
        if a[i][j]=="
            tmp+=1
    if tmp==n:
        nokori-=1
    if tmp!=0:
        t[j]=True

ans=9999999999
for i in xrange(n):
    if y[i]:
        if t[i]:
            ans=min(ans,cnt[i]+nokori)
        else:
            ans=min(ans,cnt[i]+nokori+1)
    else:
        ans=min(ans,nokori)
print ans

Solution (Python):
N = int(input().strip())
ar = [N*[] for _ in range(N)]
for row in range(N):
  ar[row] = input().strip()

fullestRows = []
maxCount = 0
for row in range(N):
  count = 0
  for col in range(N):
    if ar[row][col] == '
      count += 1
  if count > maxCount:
    maxCount = count
    fullestRows = [row]
  elif count == maxCount:
    fullestRows.append(row)

if maxCount == 0:
  print(-1)
else:
  found = False
  req = 0
  for fr in fullestRows:
    for row in range(N):
      if ar[row][fr] == '
        found = True
        break
    if found:
      break
  if not found:
    req += 1
  req += N - maxCount
  
  for col in range(N):
    for row in range(N):
      if ar[row][col] == '.':
        req+=1
        break
  print(req)

Solution (Python):
n = input()
A = [raw_input() for i in xrange(n)]

R = [sum(e[j] == '
C = [sum(A[j][i] == '

c_need = sum(e < n for e in C)

INF = 10**9+7
ans = INF
for i in xrange(n):
    if R[i] == C[i] == 0:
        continue
    if C[i] == 0:
        ans = min(ans, n-R[i] + c_need + 1)
    else:
        ans = min(ans, n-R[i] + c_need)
print -1 if ans == INF else ans

Solution (Python):
N = int(input())
A = [[0 if x == "." else 1 for x in input()] for i in range(N)]

if sum([sum(x) for x in A]) == 0:
    print(-1)
else:
    sum_list = [0] * N
    for i in range(N):
        s = 0
        for j in range(N):
            s += A[j][i]
        sum_list[i] = s

    count = N + 1
    index = []
    for i in range(N):
        tmp = 0
        flag = False
        for j in range(N):
            if A[i][j] == 0:
                tmp += 1
            if sum_list[i] == 0:
                flag = True

        if flag:
            tmp += 1

        count = min(count, tmp)

        if count == 0:
            break

    for i in range(N):
        for j in range(N):
            if A[j][i] == 0:
                count += 1
                break

    print(count)

Solution (Python):
n = int(raw_input())
a = []

total = 0
for i in range(n):
    ai = raw_input()
    ai = map(lambda x: x == '
    a.append(ai)
    total += sum(ai)

if total == 0:
    print -1
    exit()

b = [0] * n
for i in range(n):
    flag = 0
    for j in range(n):
        flag |= a[j][i]
    b[i] = flag^1

col = 0
for j in range(n):
    flag = 1
    for i in range(n):
        flag &= a[i][j]
    col += flag

ans = float('inf')
for i in range(n):
    total = 0
    flag = 0
    for j in range(n):
        if a[i][j] == 0:
            total += 1
            flag |= 1
    ans = min(ans, total + (flag&b[i]) + n-col)

print ans

Solution (Python):
N = int(raw_input())
A = [raw_input() for _ in xrange(N)]
counts = map(lambda x: x.count("

if sum(counts) == 0:
	print -1
	exit(0)

for i in xrange(N):
	has_hash = False
	for j in xrange(N):
		if A[j][i] == "
			has_hash = True
	if not has_hash:
		counts[i] -= 1
ans = N+N-max(counts)

for j in xrange(N):
	complete = True
	for i in xrange(N):
		if A[i][j] != "
			complete = False
	if complete:
		ans -= 1

print ans

Solution (Python):
import sys                                                                      
                                                                                
grid = []                                                                       
                                                                                
N = int(raw_input())                                                            
                                                                                
for i in range(0,N):                                                            
    rowChars = list(raw_input())                                                
    rowNums = []                                                                
    for square in rowChars:                                                     
        if square == '.':                                                       
            rowNums.append(0)                                                   
        else:                                                                   
            rowNums.append(1)                                                   
    grid.append(rowNums)                                                        
                                                                                
                                                                                

blackSquares = 0                                                                
for i in range(0,N):                                                            
    for j in range(0,N):                                                        
        blackSquares += grid[i][j]                                              
                                                                                
if blackSquares == 0:                                                           
    print "-1"                                                                  
    sys.exit()                                                                  
                                                                                

colCounts = [0] * N                                                             
for i in range(0,N):                                                            
    for j in range(0,N):                                                        
        colCounts[j] += grid[i][j]                                              
                                                                                
allBlackColCount = 0                                                            
for i in range(0,N):                                                            
    if colCounts[i] == N:                                                       
        allBlackColCount += 1                                                   
                                                                                

allBlackRowCount = 0                                                            
for i in range(0,N):                                                            
    rowCount = 0                                                                
    for j in range(0,N):                                                        
        rowCount += grid[i][j]                                                  
                                                                                
    if rowCount == N:                                                           
        
        print "%d" % (N - allBlackColCount)                                     
        sys.exit()                                                              
                                                                                


minCost = N                                                                     
for i in range(0,N):                                                            
    blackSquares = 0                                                            
    for j in range(0,N):                                                        
        blackSquares += grid[i][j]                                              
                                                                                
    whiteSquares = N - blackSquares                                             
                                                                                
    completionCost = 0                                                          
    
    
    
    if colCounts[i] > 0:                                                        
        completionCost = whiteSquares                                           
    else:                                                                       
        completionCost = whiteSquares + 1                                       
                                                                                
    minCost = min(minCost, completionCost)                                      
                                                                                
print "%d" % (N - allBlackColCount + minCost)

Solution (Python):
from sys import stdin
rr = lambda: stdin.readline().strip()

N = int(rr())
A = []
for _ in xrange(N):
    A.append(map(lambda x: x=='

def solve(N, A):
    B = zip(*A)
    if not any(any(row) for row in A):
        return -1
    return len({c for c, col in enumerate(B)
                      if not all(col)}) +\
           min(N - sum(row) + (not any(B[r]))
               for r, row in enumerate(A))

print solve(N, A)

Solution (Python):
N = int(input())
a = [ [str=="


n =0
ans =9999999999999
all_black_row = sum(all(a[i][j] for i in range(N)) for j in range(N))
black_count_line  = [(sum(a[i][j] for j in range(N))) for i in range(N)]
black_count_row = [(sum(a[i][j] for i in range(N))) for j in range(N)]
for line_no,blacks in enumerate(black_count_line):
    tmp_ans = N -  all_black_row
    if black_count_row[line_no] ==0:
        tmp_ans +=1
    tmp_ans += N-blacks
    ans = min(ans,tmp_ans)

if not any(black_count_line) and not any(black_count_row):
    ans = -1
print(ans)

Solution (Python):
import sys

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def solve():
    N = int(input())
    A = [[0 if j == '.' else 1 for j in input()] for i in range(N)]
    ans = 0
    min_r = float('inf')
    num_f = 0

    check = 0
    for i in range(N):
        check += sum(A[i])
    if check == 0:
        print(-1)
        return None

    for j in range(N):
        for i in range(N):
            if not A[i][j]:
                break
        else:
            num_f += 1

    if num_f == N:
        print(0)
        return None

    exits = [False] * N
    for j in range(N):
        for i in range(N):
            if A[i][j]:
                exits[j] = True
                break

    
    for i in range(N):
        tmp = 0
        tmp += (not exits[i])

        for j in range(N):
            if A[i][j] == 0:
                tmp += 1

        min_r = min(tmp, min_r)

    ans = (N - num_f) + min_r
    print(ans)

if __name__ == '__main__':
    solve()

Solution (Python):
from operator import itemgetter
n = int(input())
data = [list(map(lambda x: x == '
row_cnt = list(map(sum, data))
col_cnt = [sum(map(itemgetter(i), data)) for i in range(n)]

mr = max(row_cnt)
if not mr:
  print(-1)
else:
  temp = sum([col_cnt[i] for i in range(n) if row_cnt[i] == mr])
  k0 = 0 if temp else 1
  k1 = n - mr
  k2 = sum(map(lambda x: x < n, col_cnt))
  print(k0+k1+k2)