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



Sample input:
2
#.
.#


Sample output:
3

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [list(input()) for i in range(n)]
if all(a[i][j] == "." for j in range(n) for i in range(n)):
    print(-1)
else:
    l = []
    for i in range(n):
        if "#" in a[i]:
            l.append(a[i].count("."))
        else:
            l.append(a[i].count(".") + 1)
    ans = min(l)
    for i in range(n):
        ans += 0 if all(a[j][i] == "#" for j in range(n)) else 1
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [list(input()) for i in range(n)]
if all(a[i][j] == "." for j in range(n) for i in range(n)):
    print(-1)
else:
    ans = float("inf")
    for i in range(n):
        if "#" in a[i]:
            ans = min(ans, a[i].count("."))
        else:
            ans = min(ans, a[i].count(".") + 1)
    for i in range(n):
        ans += not(all(a[j][i] == "#" for j in range(n)))
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [input() for _ in range(N)]
I=10**9
b = sum(any(a[i][j] == '.' for i in range(N)) for j in range(N))
ans=b+min(a[i].count('.')+(0 if any(a[k][i] == '#' for k in range(N)) else (1 if any(a[i][k] == '#' for k in range(N)) else 0)) for i in range(N))
print(ans if ans<I else -1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
a = [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 all(a[k][i] == '.' for k in range(N)):
        continue
    c = a[i].count('.') + b
    ans = min(ans, c)
print(ans if ans < float('inf') else -1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
a = [input().rstrip() for _ in range(N)]
ans = float('inf')
b = 0
for j in range(N):
    for i in range(N):
        if a[i][j] == '.':
            b += 1
            break
for i in range(N):
    for k in range(N):
        if a[k][i] == '.':
            continue
        c = a[i].count('.') + b
        ans = min(ans, c)
print(ans if ans < float('inf') else -1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
a = [input().rstrip() for _ in range(N)]
ans = float('inf')
for i in range(N):
    if a[i][i] == '.':
        continue
    c = a[i].count('.')
    for j in range(N):
        for k in range(N):
            if a[k][j] == '.':
                c += 1
                break
    ans = min(ans, c)
print(ans if ans < float('inf') else -1)

Submission status: Wrong Answer

Solution code:
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] == "#" for i in range(n)])
    if tate_all_sharp:
        ans -= 1

# 横一列#をつくる
need_cnt = n
for j in range(n):
    tate_any_sharp = any([s[i][j] == "#" for i in range(n)])
    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)

ans += need_cnt
print(ans)

Submission status: Wrong Answer

Solution code:
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=0
        if data2[i]!=0:
            test=(N-data[i])+count
            ans=min(ans,test)
    print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    grid = [input() for _ in [0]*n]
    for i in grid:
        if "#" in i:
            break
    else:
        print(-1)
        return
    ans = 10**20
    # i行目に何個黒があるか
    black_cnt = [0]*n
    # i列目に黒が一つでもあればTrue
    exist = [False]*n
    # 縦一列が既に黒の列の数
    all_cnt = 0
    for i in range(n):
        flag = True
        for j in range(n):
            if grid[i][j] == "#":
                black_cnt[i] += 1
                exist[j] = True
            else:
                flag = False
        if flag:
            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]+1)
    print(ans)


main()

Submission status: Wrong Answer

Solution code:
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])
	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)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
board = [list(input()) for _ in range(n)]
board = [list(i) for i in zip(*board)]
res = 10 ** 10

allsharp = 0
for i in range(n):
    cnt = 0
    for j in range(n):
        if board[j][i] == '#':
            cnt += 1
    if cnt == n:
        allsharp += 1

for i in range(n):
    for j in range(n):
        if board[i][j] == '.':
            continue
        # 上からj列
        cnt = 0
        for k in range(n):
            if board[j][k] != '#':
                cnt += 1
        cnt += n - allsharp
        res = min(res, cnt)

if res == 10 ** 10:
    res = -1

print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
board = [list(input()) for _ in range(n)]
res = 10 ** 10

allsharp = 0
for i in range(n):
    cnt = 0
    for j in range(n):
        if board[j][i] == '#':
            cnt += 1
    if cnt == n:
        allsharp += 1

for i in range(n):
    for j in range(n):
        if board[i][j] == '.':
            continue
        # 上からj列
        cnt = 0
        for k in range(n):
            if board[j][k] != '#':
                cnt += 1
        cnt += n - allsharp
        res = min(res, cnt)

if res == 10 ** 10:
    res = -1

print(res)

# board = [list(i) for i in zip(*board)]

Submission status: Wrong Answer

Solution code:
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

#print (blexist,tbl,ybl)

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

    if bexi:

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

        ans = min(ans,chnum + (N - tbl))

print (ans)

Submission status: Wrong Answer

Solution code:
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)
    k = 2 if col_info[i] == -1 else 1
    for j in range(N):
        if grid[i][j] == '.':
            ans_new += k
    ans = min(ans, ans_new)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [input() for _ in range(n)]
ans1 = float('inf')
for i in range(n):
    cnt = 0
    for j in range(n):
        if a[j][i] == '#':
            break
    else:
        continue
    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)

Submission status: Wrong Answer

Solution code:
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
    for i in range(N):
        tmp = np.count_nonzero(a[:,i] == 0)
        if tmp > 0:
            c += 1
        if tmp != N:
            ok[i] = True
    INF = int(1e9)
    ans = INF
    for i in range(N):
        if ok[i]:
            tmp = np.count_nonzero(a[i] == 0)
            ans = min(ans, tmp + c)
    if ans == INF:
        ans = -1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
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.count_nonzero(~grid,axis=1)
cnt_row_op += (~grid).all(axis=0)

answer = cnt_row_op.min() + np.count_nonzero((~grid).any(axis=0))
print(answer)

Submission status: Wrong Answer

Solution code:
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]

if (grid == b'.').all():
    print(-1)
    exit()

# 行ごとに、埋まった行を作る手数を計算する

cnt_row_op = np.count_nonzero(grid == b'.',axis=1)
cnt_row_op += (grid == b'.').all(axis=0)

answer = cnt_row_op.min() + np.count_nonzero((grid == b'.').any(axis=0))
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

def main():
    N = int(input())
    a = np.array([input().strip() for i in range(N)])

    count = [0] * N
    all_black = [True] * N
    for i in range(N):
        for j in range(N):
            if a[i][j] == ".":
                count[i] += 1
                all_black[j] = False
    
    ans = min(count)
    if ans == N:
        print(-1)
        return
    
    for i in range(N):
        if not all_black[i]:
            ans += 1
    # print(count)
    # print(all_black)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
G = [[1 if s == '#' else 0 for s in input().strip()] for _ in range(N)]
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] for i in range(N) if exist[i])
print(ans)

Submission status: Wrong Answer

Solution code:
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 "." in s:
        c+=1
    if "#" in s:
        flag = 1
    nMAP[i] = s
        
for i in range(N):
    for j in range(N):
        tMAP[i][j] = nMAP[j][i]
        
MIN = N
for i in range(N):
    x = nMAP[i].count(".")
    if "#" in tMAP[i]:
        MIN = min(MIN,x)
    else:
        MIN = min(MIN,x+1)
    
if flag == 0:
    print(-1)
else:
    print(MIN+c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#B問題
N = int(input())
nMAP = [list(str(input())) for i in range(N)]
tMAP = [["" for i in range(N)] for j in range(N)]
bflag = 0
for i in range(N):
    for j in range(N):
        if nMAP[j][i] == "#":
            bflag = 1
        tMAP[i][j] = nMAP[j][i]

yokomax = 0
yokoind = -1
tatemax = 0
tateind = -1

for i in range(N):
    y = nMAP[i].count("#")
    t = tMAP[i].count("#")
    if yokomax <= y:
        yokomax = y
        yokoind = i
    if tatemax <= t:
        tatemax = t
        tateind = i
        
count = 0
if yokomax >= tatemax:
    bM = 0
    memo = ""
    diag_flag = 0
    if nMAP[yokoind][yokoind] == ".":
        diag_flag = 1
                    
    if diag_flag == 1:
        ch = -1
        for i in range(N):
            if nMAP[yokoind][i] == "#":
                if tMAP[i].count("#") != N:
                    ch = i
                    break
        tMAP[ch] = nMAP[yokoind]
        for i in range(N):
            if tMAP[i][yokoind] == "#":
                if bM <= tMAP[i].count("#"):
                    bM = tMAP[i].count("#")
                    memo = tMAP[i]
        for i in range(N):
            if tMAP[i][yokoind] != "#":
                tMAP[i] = memo
                count+=1
        for i in range(N):
            if tMAP[i].count("#")!=N:
                count+=1
        
    else:
        for i in range(N):
            if tMAP[i][yokoind] == "#":
                if bM <= tMAP[i].count("#"):
                    bM = tMAP[i].count("#")
                    memo = tMAP[i]
        for i in range(N):
            if tMAP[i][yokoind] != "#":
                tMAP[i] = memo
                count+=1
        for i in range(N):
            if tMAP[i].count("#")!=N:
                count+=1

else:
    bM = 0
    memo = ""
    diag_flag = 0
    if tMAP[tateind][tateind] == ".":
        diag_flag = 1
                    
    if diag_flag == 1:
        ch = -1
        for i in range(N):
            if tMAP[tateind][i] == "#":
                if nMAP[i].count("#") != N:
                    ch = i
                    break
        nMAP[ch] = tMAP[tateind]
        for i in range(N):
            if nMAP[i][tateind] == "#":
                if bM <= nMAP[i].count("#"):
                    bM = nMAP[i].count("#")
                    memo = nMAP[i]
        for i in range(N):
            if nMAP[i][tateind] != "#":
                nMAP[i] = memo
                count+=1
        for i in range(N):
            if nMAP[i].count("#")!=N:
                count+=1
        
    else:
        for i in range(N):
            if nMAP[i][tateind] == "#":
                if bM <= nMAP[i].count("#"):
                    bM = nMAP[i].count("#")
                    memo = nMAP[i]
        for i in range(N):
            if nMAP[i][tateind] != "#":
                nMAP[i] = memo
                count+=1
        for i in range(N):
            if nMAP[i].count("#")!=N:
                count+=1

#print(yokoind,tateind,diag_flag)
if bflag == 1:
    print(count)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
#B問題
N = int(input())
nMAP = [list(str(input())) for i in range(N)]
tMAP = [["" for i in range(N)] for j in range(N)]
bflag = 0
for i in range(N):
    for j in range(N):
        if nMAP[j][i] == "#":
            bflag = 1
        tMAP[i][j] = nMAP[j][i]

yokomax = 0
yokoind = -1
tatemax = 0
tateind = -1

for i in range(N):
    y = nMAP[i].count("#")
    t = tMAP[i].count("#")
    if yokomax <= y:
        yokomax = y
        yokoind = i
    if tatemax <= t:
        tatemax = t
        tateind = i
        
count = 0
if yokomax >= tatemax:
    bM = 0
    memo = ""
    diag_flag = 0
    if nMAP[yokoind][yokoind] == ".":
        diag_flag = 1
                    
    if diag_flag == 1:
        ch = [0,-1]
        for i in range(N):
            if nMAP[yokoind][i] == "#":
                if ch[0] <= tMAP[i].count("#"):
                    ch[0] = tMAP[i].count("#")
                    ch[1] = i
        tMAP[ch[1]] = nMAP[yokoind]
        for i in range(N):
            if tMAP[i][yokoind] == "#":
                if bM <= tMAP[i].count("#"):
                    bM = tMAP[i].count("#")
                    memo = tMAP[i]
        for i in range(N):
            if tMAP[i][yokoind] != "#":
                tMAP[i] = memo
                count+=1
        for i in range(N):
            if tMAP[i].count("#")!=N:
                count+=1
        
    else:
        for i in range(N):
            if tMAP[i][yokoind] == "#":
                if bM <= tMAP[i].count("#"):
                    bM = tMAP[i].count("#")
                    memo = tMAP[i]
        for i in range(N):
            if tMAP[i][yokoind] != "#":
                tMAP[i] = memo
                count+=1
        for i in range(N):
            if tMAP[i].count("#")!=N:
                count+=1

else:
    bM = 0
    memo = ""
    diag_flag = 0
    if tMAP[tateind][tateind] == ".":
        diag_flag = 1
                    
    if diag_flag == 1:
        ch = [0,-1]
        for i in range(N):
            if tMAP[tateind][i] == "#":
                if ch[0] <= nMAP[i].count("#"):
                    ch[0] = nMAP[i].count("#")
                    ch[1] = i
        nMAP[ch[1]] = tMAP[tateind]
        for i in range(N):
            if nMAP[i][tateind] == "#":
                if bM <= nMAP[i].count("#"):
                    bM = nMAP[i].count("#")
                    memo = nMAP[i]
        for i in range(N):
            if nMAP[i][tateind] != "#":
                nMAP[i] = memo
                count+=1
        for i in range(N):
            if nMAP[i].count("#")!=N:
                count+=1
        
    else:
        for i in range(N):
            if nMAP[i][tateind] == "#":
                if bM <= nMAP[i].count("#"):
                    bM = nMAP[i].count("#")
                    memo = nMAP[i]
        for i in range(N):
            if nMAP[i][tateind] != "#":
                nMAP[i] = memo
                count+=1
        for i in range(N):
            if nMAP[i].count("#")!=N:
                count+=1

#print(yokoind,tateind,diag_flag)
if bflag == 1:
    print(count)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
c = s.count('#' * n)
t = [list(i) for i in list(zip(*s))]
d = t.count(['#'] * n)

if c != 0:
    print(n - d)
    exit()

res = 10 ** 10

for i in range(n):
    for j in range(n):
        if s[i][j] == '#':
            p = s[j].count('.')
            res = min(res, p + n - d)

print(-1 if res == 10 ** 10 else res)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
c = s.count('#' * n)
t = [list(i) for i in list(zip(*s))]
d = t.count(['#'] * n)

if c != 0:
    print(n - d)
    exit()

res = 10 ** 10

for i in range(n):
    if s[i][i] == '#':
        p = s[i].count('.')
        res = min(res, p + n - d)

print(-1 if res == 10 ** 10 else res)

Submission status: Wrong Answer

Solution code:
n,*a=open(0).readlines();n=int(n);*b,=zip(*a);print(-all(t.count('#')<1for t in a)or min(n*2-sum(not'.'in t for t in b)-a[i].count('#')+(not'#'in b[i])for i in range(n)))

Submission status: Wrong Answer

Solution code:
s='#';n,*a=open(0).readlines();n=int(n);f,e=[sum(i==t.count(s)for t in zip(*a))for i in(n,0)];r=range(n);print(-(e==n)or min(n*2-f-a[i].count(s)+all(s<a[j][i]for j in r)for i in r))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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 == '#' else 0 for c in S()] for _ in range(n)]
    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 = inf
    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())

Submission status: Wrong Answer

Solution code:
# coding:utf-8

num = int(input())
board_row = [list(input())for i in range(num)]
board_col = [[board_row[i][j] for i in range(num)] for j in range(num)]

num_has_white_col = 0
for col in board_col:
    if "." in col:
        num_has_white_col += 1

l_counter = []
bool_black_row = False
for index, row in enumerate(board_row):
    counter = 0
    for item in row:
        if item == ".":
            counter += 1

    if counter == 0:
        bool_black_row = True
        l_counter.append("OK")
        break

    bool_all_black = False

    for row_2 in board_row:
        if row_2[index] == "#":
            bool_all_black = True
            break

    if bool_all_black:
        l_counter.append(counter)

if len(l_counter) == 0:
    print(-1)
else:
    if bool_black_row:
        answer = num_has_white_col
    else:
        max_make_black_row = max(l_counter)
        answer = max_make_black_row + num_has_white_col
    print(answer)

Submission status: Wrong Answer

Solution code:
import sys

INF = 10**9+7

stdin = sys.stdin
def na(): return map(int, stdin.readline().split())
def nal(): return list(map(int, stdin.readline().split()))
def ns(): return stdin.readline().strip()
def nsl(): return list(stdin.readline().strip())
def ni(): return int(stdin.readline())

N = ni()

a = []
for i in range(N):
    a.append(input())

lst = []
ans  = -1
c = 0
for i in range(N):
    for j in range(N):
        if(a[j][i] == '.'):
            c += 1
            break

for i in range(N):
    cnt = 0
    for j in range(N):
        if(a[i][j] == '#'):
            cnt += 1
            ans = 0
    lst.append(cnt)
    if(cnt == N):
        print(c)
        exit()

if(ans == -1):
    print(ans)
    exit()

for i in range(N):
    for j in range(N):
        if(a[j][i] == '#'):
            lst[i] = N - lst[i] + c

print(max(lst))

Submission status: Wrong Answer

Solution code:
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
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 j in range(N):
    for i in range(N):
      if a[i][j]==".":
        break
    else:
      already+=1
  print(2*N-max(blackNumRow)+1-already)

Submission status: Wrong Answer

Solution code:
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)*2
  
  ans=min(Min,Min2)+N-already
  
  print(ans)

Submission status: Wrong Answer

Solution code:
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]
  ans=N-max
  for j in range(N):
    for i in range(N):
      if a[i][j]==".":
        break
    else:
      already+=1
  print(ans+N-already)

Submission status: Wrong Answer

Solution code:
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
p=[[] for _ in xrange(n)]
for i in xrange(n):
    for j in xrange(n):
        if a[i][j]==".":
            cnt[i]+=1
            p[i].append(i)
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=9999999
for i in xrange(n):
    tmp=0
    for j in p[i]:
        if t[j]:
            continue
        else:
            tmp+=1
    ans=min(ans,tmp+cnt[i]+nokori)
print ans

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy
 
def sampling_row(mat, row):
    row_str = copy.deepcopy(mat[row, :])
    return row_str
 
def writing_col(mat, row_str, col):
 
    row = 0
    for s in row_str:
        mat[row, col] = s
        row+=1
 
def check(mat, N):
    max_val = -2*N
    max_row = 0
    max_col = 0
    is_ok = True
    for i in range(N):
        for j in range(N):
            row = mat[i, :]
            col = mat[:, j]
            val = 0
 
            no_change = True
            for k in range(N):
                if row[k] == "#" and col[k] == ".":  
                    val += 1
                elif row[k] == "." and col[k] == "#":
                    val -= 2
                elif row[k] == "." and col[k] == ".":
                    val -= 1
                elif row[k] == "#" and col[k] == "#":
                    val += 0

                if row[k] == "#" and col[k] == "#" and is_ok: 
                    pass
                else:
                    is_ok = False

                if row[k] == col[k] and no_change:
                    pass
                else:
                    no_change = False
 
            if max_val < val and not(no_change):
                max_val = val
                max_row = i
                max_col = j
 
    return (max_val, max_row, max_col, is_ok)
 
 
if __name__ == "__main__":
    mat_str = []
 
    N = raw_input()
    N = int(N)
    mat = np.zeros((N, N), str)
    for i in range(N):
        s = raw_input()
        for j in range(N):
            mat[i][j] = s[j]
 
    # row = sampling(mat, 0)
    # writing(mat, row, 1)
 
    is_ok = -1
 
    for s in mat.reshape(N*N):
        if s == "#":
            is_ok = 0
            break
 
    if is_ok != -1:
        while True:
            max_val, max_row, max_col, c_ok = check(mat, N)
            if c_ok == True:
                break
            else:
                writing_col(mat, sampling_row(mat, max_row), max_col)
                is_ok += 1
                # print mat
                
    print is_ok