#include<bits/stdc++.h>
#define INF 1073741824
#define ll long long
#define PI (2*acos(0.0))
#define p1(n) printf("showing %d\n",n)
#define p2(m,n) printf("showing %d %d\n",m,n)
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define x first
#define y second
#define on(val,pos) val|(1<<pos)
#define off(val,pos) val&(~(1<<pos))
#define check(val,pos) (val&(1<<pos))
#define all(n) n.begin(),n.end()
//first four adjacent,second four corner
int row[8]={0,-1,0,1,1,-1,-1,1};
int col[8]={-1,0,1,0,-1,-1,1,1};
using namespace std;
string s[1001];
int n,m;
bool southpole(bool row[],bool col[]){
int i,j;
for(i=0;i<n;i++){
if(row[i]==false){
for(j=0;j<m;j++) if(col[j]==false)break;
if(j==m)break;
}
}
if(i<n)return true;
for(i=0;i<m;i++){
if(col[i]==false){
for(j=0;j<n;j++) if(row[j]==false)break;
if(j==n)break;
}
}
if(i<m)return true;
else return false;
}
bool northpole(){
for(int i=0;i<n;i++){
int p=0,q=m-1;
while(s[i][p]!='#' and p<m)p++;
while(s[i][q]!='#' and q>=0)q--;
if(p<q){
while(p<q and s[i][p]=='#')p++;
if(p<q)return true;
}
}
for(int i=0;i<m;i++){
int p=0,q=n-1;
while(p<n and s[p][i]!='#')p++;
while(q>=0 and s[q][i]!='#')q--;
if(p<q){
while(p<q and s[p][i]=='#')p++;
if(p<q)return true;
}
}
return false;
}
map<pii,bool> checked;
queue<pii>que;
void BFS(pii node){
checked[node]=true;
que.push(node);
while(!que.empty()){
pii cur=que.front();
que.pop();
for(int i=0;i<4;i++){
int j=cur.x+row[i],k=cur.y+col[i];
if(j>=0 and k>=0 and j<n and k<m and s[j][k]=='#' and checked[{j,k}]==false){
checked[{j,k}]=true;
que.push({j,k});
}
}
}
}
int main(){
int i,j,k,val,t=0,test;
//freopen("000input.txt","r",stdin);
cin>>n>>m;
for(i=0;i<n;i++)cin>>s[i];
bool row[n],col[m];
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(s[i][j]=='#')row[i]=true,col[j]=true;
}
}
///South pole placement check &
///north pole not enter forbidden cell check
if(southpole(row,col) or northpole()){
printf("-1\n");
}
else{
int cnt=0;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(s[i][j]=='#' and checked[{i,j}]==false){
cnt++;
BFS({i,j});
}
}
}
printf("%d\n",cnt);
}
//free(); //if pointer array
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBJTkYgMTA3Mzc0MTgyNAojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIFBJICgyKmFjb3MoMC4wKSkKI2RlZmluZSBwMShuKSBwcmludGYoInNob3dpbmcgJWRcbiIsbikKI2RlZmluZSBwMihtLG4pIHByaW50Zigic2hvd2luZyAlZCAlZFxuIixtLG4pCiNkZWZpbmUgZmFzdElPIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOyBjaW4udGllKE5VTEwpOwojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIHBpaSBwYWlyPGludCxpbnQ+CiNkZWZpbmUgcGxsIHBhaXI8bGwsbGw+CiNkZWZpbmUgeCBmaXJzdAojZGVmaW5lIHkgc2Vjb25kCiNkZWZpbmUgb24odmFsLHBvcykgdmFsfCgxPDxwb3MpCiNkZWZpbmUgb2ZmKHZhbCxwb3MpIHZhbCYofigxPDxwb3MpKQojZGVmaW5lIGNoZWNrKHZhbCxwb3MpICh2YWwmKDE8PHBvcykpCiNkZWZpbmUgYWxsKG4pIG4uYmVnaW4oKSxuLmVuZCgpCi8vZmlyc3QgZm91ciBhZGphY2VudCxzZWNvbmQgZm91ciBjb3JuZXIKaW50IHJvd1s4XT17MCwtMSwwLDEsMSwtMSwtMSwxfTsKaW50IGNvbFs4XT17LTEsMCwxLDAsLTEsLTEsMSwxfTsKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RyaW5nIHNbMTAwMV07CmludCBuLG07CmJvb2wgc291dGhwb2xlKGJvb2wgcm93W10sYm9vbCBjb2xbXSl7CiAgICBpbnQgaSxqOwogICAgZm9yKGk9MDtpPG47aSsrKXsKICAgICAgICBpZihyb3dbaV09PWZhbHNlKXsKICAgICAgICAgICAgZm9yKGo9MDtqPG07aisrKSBpZihjb2xbal09PWZhbHNlKWJyZWFrOwogICAgICAgICAgICBpZihqPT1tKWJyZWFrOwogICAgICAgIH0KICAgIH0KICAgIGlmKGk8bilyZXR1cm4gdHJ1ZTsKICAgIGZvcihpPTA7aTxtO2krKyl7CiAgICAgICAgaWYoY29sW2ldPT1mYWxzZSl7CiAgICAgICAgICAgIGZvcihqPTA7ajxuO2orKykgaWYocm93W2pdPT1mYWxzZSlicmVhazsKICAgICAgICAgICAgaWYoaj09bilicmVhazsKICAgICAgICB9CiAgICB9CiAgICBpZihpPG0pcmV0dXJuIHRydWU7CiAgICBlbHNlIHJldHVybiBmYWxzZTsKfQpib29sIG5vcnRocG9sZSgpewogICAgZm9yKGludCBpPTA7aTxuO2krKyl7CiAgICAgICAgaW50IHA9MCxxPW0tMTsKICAgICAgICB3aGlsZShzW2ldW3BdIT0nIycgYW5kIHA8bSlwKys7CiAgICAgICAgd2hpbGUoc1tpXVtxXSE9JyMnIGFuZCBxPj0wKXEtLTsKICAgICAgICBpZihwPHEpewogICAgICAgICAgICB3aGlsZShwPHEgYW5kIHNbaV1bcF09PScjJylwKys7CiAgICAgICAgICAgIGlmKHA8cSlyZXR1cm4gdHJ1ZTsKICAgICAgICB9CiAgICB9CiAgICBmb3IoaW50IGk9MDtpPG07aSsrKXsKICAgICAgICBpbnQgcD0wLHE9bi0xOwogICAgICAgIHdoaWxlKHA8biBhbmQgc1twXVtpXSE9JyMnKXArKzsKICAgICAgICB3aGlsZShxPj0wIGFuZCBzW3FdW2ldIT0nIycpcS0tOwoKICAgICAgICBpZihwPHEpewogICAgICAgICAgICB3aGlsZShwPHEgYW5kIHNbcF1baV09PScjJylwKys7CiAgICAgICAgICAgIGlmKHA8cSlyZXR1cm4gdHJ1ZTsKICAgICAgICB9CiAgICB9CgogICAgcmV0dXJuIGZhbHNlOwp9Cm1hcDxwaWksYm9vbD4gY2hlY2tlZDsKCnF1ZXVlPHBpaT5xdWU7CgoKdm9pZCBCRlMocGlpIG5vZGUpewogICAgY2hlY2tlZFtub2RlXT10cnVlOwoKICAgIHF1ZS5wdXNoKG5vZGUpOwogICAgd2hpbGUoIXF1ZS5lbXB0eSgpKXsKICAgICAgICBwaWkgY3VyPXF1ZS5mcm9udCgpOwogICAgICAgIHF1ZS5wb3AoKTsKCiAgICAgICAgZm9yKGludCBpPTA7aTw0O2krKyl7CiAgICAgICAgICAgIGludCBqPWN1ci54K3Jvd1tpXSxrPWN1ci55K2NvbFtpXTsKICAgICAgICAgICAgaWYoaj49MCBhbmQgaz49MCBhbmQgajxuIGFuZCBrPG0gYW5kIHNbal1ba109PScjJyBhbmQgY2hlY2tlZFt7aixrfV09PWZhbHNlKXsKICAgICAgICAgICAgICAgIGNoZWNrZWRbe2osa31dPXRydWU7CgogICAgICAgICAgICAgICAgcXVlLnB1c2goe2osa30pOwoKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKaW50IG1haW4oKXsKICAgIGludCBpLGosayx2YWwsdD0wLHRlc3Q7CiAgICAvL2ZyZW9wZW4oIjAwMGlucHV0LnR4dCIsInIiLHN0ZGluKTsKCiAgICAgICAgY2luPj5uPj5tOwogICAgICAgIGZvcihpPTA7aTxuO2krKyljaW4+PnNbaV07CiAgICAgICAgYm9vbCByb3dbbl0sY29sW21dOwogICAgICAgIGZvcihpPTA7aTxuO2krKyl7CiAgICAgICAgICAgIGZvcihqPTA7ajxtO2orKyl7CiAgICAgICAgICAgICAgICBpZihzW2ldW2pdPT0nIycpcm93W2ldPXRydWUsY29sW2pdPXRydWU7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgLy8vU291dGggcG9sZSBwbGFjZW1lbnQgY2hlY2sgJgogICAgICAgIC8vL25vcnRoIHBvbGUgbm90IGVudGVyIGZvcmJpZGRlbiBjZWxsIGNoZWNrCiAgICAgICAgaWYoc291dGhwb2xlKHJvdyxjb2wpIG9yIG5vcnRocG9sZSgpKXsKICAgICAgICAgICAgcHJpbnRmKCItMVxuIik7CiAgICAgICAgfQoKICAgICAgICBlbHNlewogICAgICAgICAgICBpbnQgY250PTA7CiAgICAgICAgICAgIGZvcihpPTA7aTxuO2krKyl7CiAgICAgICAgICAgICAgICBmb3Ioaj0wO2o8bTtqKyspewogICAgICAgICAgICAgICAgICAgIGlmKHNbaV1bal09PScjJyBhbmQgY2hlY2tlZFt7aSxqfV09PWZhbHNlKXsKICAgICAgICAgICAgICAgICAgICAgICAgY250Kys7CiAgICAgICAgICAgICAgICAgICAgICAgIEJGUyh7aSxqfSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHByaW50ZigiJWRcbiIsY250KTsKICAgICAgICB9CgogICAgLy9mcmVlKCk7ICAgLy9pZiBwb2ludGVyIGFycmF5CiAgICByZXR1cm4gMDsKfQo=