n,m=map(int,input().split())
grid=[0]*n
possible=1
bl=0
bc=0
for i in range(n):
    s=input()
    l=[0]*m
    bs=0
    be=0
    for j in range(m):
        if s[j]=='.':
            l[j]=1
            if bs==1:
                be=1
        else:
            bs=1
            if be==1:
                possible=0
    grid[i]=l
    if bs==0:
        bl=1
if possible==1:
    for j in range(m):
        bs=0
        be=0
        for i in range(n):
            if grid[i][j]==1:
                if bs==1:
                    be=1
            else:
                bs=1
                if be==1:
                    possible=0
        if bs==0:
            bc=1
if bl+bc==1:
    possible=0
if possible==0:
    print(-1)
else:
    sol=0
    grid2=[0]*n
    for i in range(n):
        l=[0]*m
        grid2[i]=l
    for i in range(n):
        for j in range(m):
            if grid[i][j]==0 and grid2[i][j]==0:
                sol+=1
                tod=[[i,j]]
                while len(tod)>0:
                    x=tod.pop()
                    i1=x[0]
                    j1=x[1]
                    if i1>=0 and i1<n and j1>=0 and j1<m:
                        if grid[i1][j1]==0 and grid2[i1][j1]==0:
                            grid2[i1][j1]=1
                            tod.append([i1,j1+1])
                            tod.append([i1,j1-1])
                            tod.append([i1+1,j1])
                            tod.append([i1-1,j1])
    print(sol)
