Buggy python submission (verdict: Runtime Error):
from collections import deque
n = input()
A = ''.join(raw_input() for i in xrange(n))

if n > 5:
    exit(1)

ALL = "#"*(n*n)

deq = deque()

deq.append((A, 0))
memo = {}
while deq:
    state, cost = deq.popleft()
    if state == ALL:
        print cost
        break
    if state in memo:
        continue
    s = list(state)
    memo[state] = 1
    for i in xrange(n):
        C = [state[i*n+k] for k in xrange(n)]
        for j in xrange(n):
            n_state = s[:]
            for k in xrange(n):
                n_state[k*n+j] = C[k]
            deq.append((''.join(n_state), cost+1))
else:
    print -1


Fixed submission:
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] == 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

Buggy python submission (verdict: Runtime Error):
n = input()
A = ''.join(raw_input() for i in xrange(n))

if n > 5:
    exit(1)

ALL = "#"*(n*n)

memo = {}
def dfs(state):
    if state in memo:
        return memo[state]
    if state == ALL:
        return 0
    s = list(state)
    memo[state] = res = 10**9
    for i in xrange(n):
        C = [state[i*n+k] for k in xrange(n)]
        for j in xrange(n):
            n_state = s[:]
            for k in xrange(n):
                n_state[k*n+j] = C[k]
            if ''.join(n_state) == state:
                continue
            res = min(res, dfs(''.join(n_state))+1)
    memo[state] = res
    return res
ans = dfs(A)
if ans > 10**5:
    print -1
else:
    print ans


Fixed submission:
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] == 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

Buggy python submission (verdict: Runtime Error):
n = input()
A = ''.join(raw_input() for i in xrange(n))

if n > 5:
    exit(1)

ALL = "#"*(n*n)

memo = {}
def dfs(state):
    if state in memo:
        return memo[state]
    if state == ALL:
        return 0
    s = list(state)
    memo[state] = res = 10**9
    for i in xrange(n):
        C = [state[i*n+k] for k in xrange(n)]
        for j in xrange(n):
            n_state = s[:]
            for k in xrange(n):
                n_state[k*n+j] = C[k]
            res = min(res, dfs(''.join(n_state))+1)
    memo[state] = res
    return res
ans = dfs(A)
if ans == 10**9:
    print -1
else:
    print ans


Fixed submission:
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] == 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