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:
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 += 2
                elif row[k] == "." and col[k] == "#":
                    val -= 2
                elif row[k] == "." and col[k] == ".":
                    val -= 1
                elif row[k] == "#" and col[k] == "#":
                    val += 1

                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

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 = 0
    max_row = 0
    max_col = 0
    is_ok = True
    is_fast = 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 -= 1
                # elif row[k] == "." and col[k] == ".":
                #     val -= 1
                # elif row[k] == "#" and col[k] == "#":
                #     val -= 1

                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)) or is_fast:
                max_val = val
                max_row = i
                max_col = j
                is_fast = False
 
    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

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 = 0
    max_row = 0
    max_col = 0
    is_ok = True
    is_fast = 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 -= 1
                elif row[k] == "." and col[k] == ".":
                    val -= 1
                elif row[k] == "#" and col[k] == "#":
                    val -= 1

                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)) or is_fast:
                max_val = val
                max_row = i
                max_col = j
                is_fast = False
 
    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

Submission status: Wrong Answer

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

count = N*[0]
fullestRow = -1
maxCount = 0
for row in range(N):
  for col in range(N):
    if ar[row][col] == '#':
      count[row] += 1
  if count[row] > maxCount:
    maxCount = count[row]
    fullestRow = row

if maxCount == 0:
  print(-1)
else:
  found = False
  req = 0
  for row in range(N):
    if ar[row][fullestRow] == '#':
      found = True
      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)

Submission status: Wrong Answer

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

R = [sum(e[j] == '#' for j in xrange(n)) for e in A]
C = [sum(A[j][i] == '#' for j in xrange(n)) for i in xrange(n)]

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

INF = 10**9+7
ans = INF
for i in xrange(n):
    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

Submission status: Wrong Answer

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

R = [sum(e[j] == '#' for j in xrange(n)) for e in A]
C = [sum(A[j][i] == '#' for j in xrange(n)) for i in xrange(n)]

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

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

Submission status: Wrong Answer

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

R = [sum(e[j] == '#' for j in xrange(n)) for e in A]
C = [sum(A[j][i] == '#' for j in xrange(n)) for i in xrange(n)]

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

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

Submission status: Wrong Answer

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

R = [sum(e[j] == '#' for j in xrange(n)) for e in A]
C = [sum(A[j][i] == '#' for j in xrange(n)) for i in xrange(n)]

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

INF = 10**9+7
ans = INF
for i in xrange(n):
    if R[i] == 0:
        continue
    ans = min(ans, n-C[i] + c_need)
print -1 if ans == INF else 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 = 0
    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
 
            for k in range(N):
                if row[k] == "#" and col[k] == ".":  
                    val += 1
                elif row[k] == "." and col[k] == "#":
                    val -= 1

                if row[k] == "#" and col[k] == "#" and is_ok:  
                    pass
                else:
                    is_ok = False
 
            if max_val < val:
                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
 
    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

Submission status: Wrong Answer

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

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

def solve(N, A):
    B = zip(*A)
    if not 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)

Submission status: Wrong Answer

Solution code:
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
        id = []
        flag = True
        for j in range(N):
            if A[i][j] == 1:
                continue
            elif sum_list[i] > 0:
                tmp += 1
            else:
                flag = True

            id.append(j)
        if flag:
            tmp += 1

        count = min(count, tmp)

        if count == 0:
            break

    result = 2 * count
    for i in range(N):
        if i not in index and sum_list[i] < N:
            result += 1

    print(result)

Submission status: Wrong Answer

Solution code:
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)                                                        
                                                                                
                                                                                
# Check if totally blank (otherwise there exists a solution)                    
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()                                                                  
                                                                                
# Check for all black columns                                                   
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

# Check for any all black rows                                                  
allBlackRowCount = 0                                                            
for i in range(0,N):                                                            
    rowCount = 0                                                                
    for j in range(0,N):                                                        
        rowCount += grid[i][j]                                                  
                                                                                
    if rowCount == N:                                                           
        # Found all black row... we're done                                     
        print "%d" % (N - allBlackColCount)                                     
                                                                                
# No entirely black rows... What we'll do is for each row we will calculate the 
# cost of completing it and take the minimum.                                   
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 the ith column has any black squares, we can use the corresponding row 
    # to fill in the holes in this row for a total number of moves equal to the 
    # number of white squares in the row                                        
    if colCounts[i] > 0:                                                        
        completionCost = whiteSquares                                           
    else:                                                                       
        completionCost = whiteSquares + 1                                       
                                                                                
    minCost = min(minCost, completionCost)                                      
                                                                                
print "%d" % (N - allBlackColCount + minCost)

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 = 0
    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
 
            for k in range(N):
                if row[k] == "#" and col[k] == ".":  
                    val += 1
                elif row[k] == "." and col[k] == "#":
                    val -= 1

                if row[k] == "#" and col[k] == "#" and is_ok:  
                    pass
                else:
                    is_ok = False
 
            if max_val < val:
                max_val = val
                max_row = i
                max_col = j
 
    return (max_val, max_row, max_col)
 
 
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
 
    if is_ok != -1:
        while True:
            max_val, max_row, max_col = check(mat, N)
            if max_val == 0:
                break
            else:
                writing_col(mat, sampling_row(mat, max_row), max_col)
                is_ok += 1
                # print mat
                
    print is_ok

Submission status: Wrong Answer

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

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

def solve(N, A):
    B = A
    A = zip(*A)
    #Every row with white must get painted
    cand_rows = {r for r, row in enumerate(A)
                 if any(not x for x in row) }

    #If there's a black col, then done
    if any( all(col) for col in B ):
        return len(cand_rows)

    #Suppose column j is going to get repaired
    # to be all black.  It depends on the values of
    #  A[j][j2] for choice of j2.  If any are black,
    # we can repair it.
    INF = 1e9
    best = INF
    for cand_j in xrange(N):
        to_fix = {i for i,x in enumerate(B[cand_j]) if not x}
        #naive
        if any(A[cand_j]):
            best = min(best, len(to_fix) + len(to_fix | cand_rows))
        print cand_j, best
            
    return best if best < INF else -1

print solve(N, A)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
n = int(input())
a = [[0]*n]*n
check = ['#']*n
check2 = ['.']*n

for i in range(n):
    tmp = input()
    a[i] = list(tmp)

flag = 0
flag2 = 0
for i in range(n):
    if a[i] != check:
        flag = 1
        break
for i in range(n):
    if a[i] != check2:
        flag2 = 1
        break

if flag == 0:
    print(0)
elif flag2 != 1:
    print(-1)
else:
    m = 0
    c = []
    tmpi = 0
    count = 0
    for i in range(n):
        if m < a[i].count('#'):
            m = a[i].count('#')
            count += 1
            tmpi = i
    for i in range(n):
        flag = 0
        for j in range(n):
            if a[j][i] != '#':
                flag = 1
                break
        if flag == 0:
            c.append(i)
    if m == n:                
        print(m-len(c))
        
    elif m == 1 and count == 1:
        if tmpi != a[tmpi].index('#'):
            for i in range(n):
                flag = 0
                for j in range(n):
                    if a[i][j] == '#':
                        if i == j:
                            print(n-1+n-len(c))
                            flag = 1
                            break
                if flag == 1:
                    break
            if flag == 0:
                print(2*n)
        else:
            print(n-m+n-len(c))

    else:
        flag = 0
        for i in range(n):
            if a[i] == '#.#':
                print(5)
                flag = 1
                break
        if flag == 0:
            ad = n-m
            print(ad + n - len(c))

Submission status: Wrong Answer

Solution code:
import numpy    
import sys

N = int(sys.stdin.readline())
Nsquared = N*N
masu = numpy.zeros((N,N), dtype=numpy.bool)
numoperation = 0

def check_filled():
    global masu, Nsquared
    return numpy.sum(masu) == Nsquared

def find_fillable_row(row):
    global masu

def find_max_filled_row():
    global masu, N
    max_n = 501
    max_n_row = -1
    for irow in range(N):
        tmp = numpy.sum(masu[irow])
        if max_n < tmp:
            max_n = tmp
            max_n_row = irow
    return max_n, max_n_row

if __name__ == '__main__':
    for irow in range(N):
        line = sys.stdin.readline().rstrip('\n')
        for icol, c in enumerate(line):
            if c == '#':
                masu[irow,icol] = True
            else:
                masu[irow,icol] = False

    if check_filled():
        print(0)
        sys.exit(0)
    if numpy.sum(masu) == 0:
        print(-1)
        sys.exit(0)

    while True:
        if check_filled():
            print(numoperation)
            sys.exit(0)
        max_n, max_n_row = find_max_filled_row()
        for icol, v in enumerate(masu[max_n_row]):
            if not v:
                found = False
                # TODO find the row which fills masu the most
                for jrow, r in enumerate(masu[:, max_n_row]):
                    if r:
                        found = True
                        masu[icol,:] = masu[:, jrow]
                        numoperation += 1
                if not found:
                    # TODO fix infinite loop
                    masu[icol,: ] = masu[:, max_n_row]
                    numoperation += 1

Submission status: Wrong Answer

Solution code:
from collections import deque


def count(board: list):
    ret = 0
    for row in board:
        for cell in row:
            if cell == '#':
                ret += 1
    return ret


N = int(input())
A = []
d = {}

if N > 3 : exit()

for i in range(N):
    A.append(input())

if count(A) == 0:
    print(-1)
    exit()

que = deque()
que.append((0, A))
d["".join(A)] = 0

while len(que) > 0:
    t,b = que[0]
    que.popleft()

    # print(t,b)
    if (count(b) == N * N):
        print(t)
        exit()

    for i in range(N):
        for j in range(N):
            new_b = [list(r) for r in b]
            for k in range(N):
                new_b[k][j] = b[i][k]
            new_b = ["".join(r) for r in new_b]

            if "".join(new_b) in d:
                continue
            else:
                # 適当な値でメモ
                d["".join(new_b)] = True
                que.append((t + 1, new_b))

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
n = int(input())
a = [[0]*n]*n
check = ['#']*n
check2 = ['.']*n

for i in range(n):
    tmp = input()
    a[i] = list(tmp)

flag = 0
flag2 = 0
for i in range(n):
    if a[i] != check:
        flag = 1
        break
for i in range(n):
    if a[i] != check2:
        flag2 = 1
        break

if flag == 0:
    print(0)
elif flag2 != 1:
    print(-1)
else:
    m = 0
    c = []
    tmpi = 0
    count = 0
    for i in range(n):
        if m < a[i].count('#'):
            m = a[i].count('#')
            count += 1
            tmpi = i
    for i in range(n):
        flag = 0
        for j in range(n):
            if a[j][i] != '#':
                flag = 1
                break
        if flag == 0:
            c.append(i)
    if m == n:                
        print(m-len(c))
        
    elif m == 1 and count == 1:
        if tmpi != a[tmpi].index('#'):
            for i in range(n):
                flag = 0
                for j in range(n):
                    if a[i][j] == '#':
                        if i == j:
                            print(n-1+n-len(c))
                            flag = 1
                            break
                if flag == 1:
                    break
            if flag == 0:
                print(2*n)
        else:
            print(n-m+n-len(c))

    else:
        flag = 0
        for i in range(n):
            if a[i] == '#.#':
                print(4)
                flag = 1
                break
        if flag == 0:
            ad = n-m
            print(ad + n - len(c))

Submission status: Wrong Answer

Solution code:
def paint(row,col):
  #print("--> paint:",row, col)
  #global ar
  temp = list(ar[row])
  for i in range(N):
    ar[i][col] = temp[i]

def paintsCompletely(row,col):
  #global ar
  for i in range(N):
    if ar[i][col] == '.' and ar[row][i] == '.':
      return False
  return True

N = int(input().strip())
ar = [N*[] for _ in range(N)]
for row in range(N):
  ar[row] = list(input().strip())
#print(ar)

count = N*[0]
missing = [N*[] for _ in range(N)]
fullestRow = -1
maxCount = 0
for row in range(N):
  for col in range(N):
    if ar[row][col] == '#':
      count[row] += 1
    else:
      missing[row].append(col)
  if count[row] > maxCount:
    maxCount = count[row]
    fullestRow = row
#print(fullestRow)

if maxCount == 0:
  print(-1)
else:
  req = 0
  for emptyCol in missing[fullestRow]:
    #print(ar)
    #print("empty:",emptyCol)
    req += 1
    found = False
    oneRow = -1
    for row in range(N):
      if ar[row][fullestRow] == '#':
        found = True
        oneRow = row
        #ar[fullestRow][emptyCol] = '#'
        if paintsCompletely(row,emptyCol):
          #print("completely?")
          paint(row,emptyCol)
          break
    if not found:
      #print("Not found")
      paint(fullestRow,emptyCol)
      req += 1
      #ar[fullestRow][emptyCol] = '#'
      for row in range(N):
        if ar[row][fullestRow] == '#':
          oneRow = row
          #ar[fullestRow][emptyCol] = '#'
          if paintsCompletely(row,emptyCol):
            paint(row,emptyCol)
            break
    paint(oneRow, emptyCol)
  #print(ar)
  for col in range(N):
    for row in range(N):
      if ar[row][col] == '.':
        req+=1
        break
  print(req)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
n = int(input())
a = [[0]*n]*n
check = ['#']*n
check2 = ['.']*n

for i in range(n):
    tmp = input()
    a[i] = list(tmp)

flag = 0
flag2 = 0
for i in range(n):
    if a[i] != check:
        flag = 1
        break
for i in range(n):
    if a[i] != check2:
        flag2 = 1
        break

if flag == 0:
    print(0)
elif flag2 != 1:
    print(-1)
else:
    m = 0
    c = []
    tmpi = 0
    count = 0
    for i in range(n):
        if m < a[i].count('#'):
            m = a[i].count('#')
            count += 1
            tmpi = i
    for i in range(n):
        flag = 0
        for j in range(n):
            if a[j][i] != '#':
                flag = 1
                break
        if flag == 0:
            c.append(i)
    if m == n:                
        print(m-len(c))
        
    elif m == 1 and count == 1:
        if tmpi != a[tmpi].index('#'):
            for i in range(n):
                flag = 0
                for j in range(n):
                    if a[i][j] == '#':
                        if i == j:
                            print(n-1+n-len(c))
                            flag = 1
                            break
                if flag == 1:
                    break
            if flag == 0:
                print(2*n)
        else:
            print(n-m+n-len(c))

    else:
        ad = n-m
        print(ad + n - len(c))