human
A monopole magnet is a magnet that only has one pole, either north or south. They don't actually exist since real magnets have two poles, but this is a programming contest problem, so we don't care.

There is an n× m grid. Initially, you may place some north magnets and some south magnets into the cells. You are allowed to place as many magnets as you like, even multiple in the same cell.

An operation is performed as follows. Choose a north magnet and a south magnet to activate. If they are in the same row or the same column and they occupy different cells, then the north magnet moves one unit closer to the south magnet. Otherwise, if they occupy the same cell or do not share a row or column, then nothing changes. Note that the south magnets are immovable.

Each cell of the grid is colored black or white. Let's consider ways to place magnets in the cells so that the following conditions are met.

  1. There is at least one south magnet in every row and every column. 
  2. If a cell is colored black, then it is possible for a north magnet to occupy this cell after some sequence of operations from the initial placement. 
  3. If a cell is colored white, then it is impossible for a north magnet to occupy this cell after some sequence of operations from the initial placement. 


Determine if it is possible to place magnets such that these conditions are met. If it is possible, find the minimum number of north magnets required (there are no requirements on the number of south magnets).

Input

The first line contains two integers n and m (1≤ n,m≤ 1000) — the number of rows and the number of columns, respectively.

The next n lines describe the coloring. The i-th of these lines contains a string of length m, where the j-th character denotes the color of the cell in row i and column j. The characters "#" and "." represent black and white, respectively. It is guaranteed, that the string will not contain any other characters.

Output

Output a single integer, the minimum possible number of north magnets...
The above is tricky. Write me a correct solution in PYTHON3.
 ... Give me a Python code to find the minimum number of north magnets required (there are no requirements on the number of south magnets)?
assistant
#Here is Python code to find the minimum number of north magnets required (there are no requirements on the number of south magnets).

import sys
from collections import deque

lines = sys.stdin.readlines()
(n, m) = map(int, lines[0].strip().split(" "))

grid = []
for i in range(1, n+1):
    grid.append(list(lines[i].strip()))

def numIslands(m ,n, grid):
    def neighbour(i, j):
        res = []
        if i != 0:
            res.append([i-1, j])
        if i != m-1:
            res.append([i+1, j])
        if j != 0:
            res.append([i, j-1])
        if j != n-1:
            res.append([i, j+1])
        return res
    count = 0
    for i in range(m):
        for j in range(n):
            if grid[i][j] == '#':
                Q = deque()
                Q.append([i, j])
                grid[i][j] = '.'
                while Q:
                    coor = Q.pop()
                    for pos in neighbour(coor[0], coor[1]):
                        if grid[pos[0]][pos[1]] == '#':
                            Q.append(pos)
                            grid[pos[0]][pos[1]] = '.'
                count += 1
    return count

def checkRC(n, m,grid):
    rows = 0
    cols = 0
    for i in range(n):
        if len(set(grid[i])) == 1 and grid[i][0] == ".":
            rows += 1; break

    for i in range(m):
        need = True
        for j in range(n):
            if grid[j][i] == "#":
                need = False; break
        if need: cols += 1; break
    if min(rows, cols) == 0 and max(rows, cols) != 0: return False
    else: return True

def solve(n, m, grid):
    if not checkRC(n, m, grid):
        print(-1)
        return 0
    for i in range(n):
        for j in range(m):
            if grid[i][j] == ".":
                if j == 0 or grid[i][j-1] == "B":
                    grid[i][j] = "B"
                else:
                    grid[i][j] = "W"
    error = False
    for i in range(n):
        for j in range(m-1, -1, -1):
            if grid[i][j] == "W":
                if j == m-1 or grid[i][j+1] == "B":
                    grid[i][j] = "B"
                else:
                    error = True
    if error: print(-1); return 0
   ...