import java.util.*;
import java.io.*;
public class R639D2D {
 
	public static void main(String[] args) throws Exception{
		int num = 998244353;
		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		StringTokenizer st = new StringTokenizer(bf.readLine());
 		int sum = 0;
 		boolean emptyrow = false;
 		boolean emptycol = false;
 		int n = Integer.parseInt(st.nextToken());
 		int m = Integer.parseInt(st.nextToken());
 		//true == white, false == black
 		boolean[][] array = new boolean[n][m];
 		boolean blackstring = true; //true --> prev = white
 		boolean found = true;
 		for(int i = 0;i<n;i++){
 			String c = bf.readLine();
 			int count = 0;
 			for(int j = 0;j<m;j++){
 				if (c.charAt(j) == '.')
 					array[i][j] = true;
 				else if (c.charAt(j) == '#')
 					array[i][j] = false;
 			}
 		}
 		
 		for(int i = 0;i<n;i++){
 			int count = 0;
 			for(int j = 0;j<m;j++){
 				if (array[i][j] != blackstring){
 					count+=1;
 					blackstring = array[i][j];
 				}
 			}
 			if (array[i][m-1] == false)
 				count+=1;
 			
 			if (count != 2 && count!= 0)
 				found = false;
 			if (count == 0)
 				emptyrow= true;
 			blackstring = true;
 			sum+= count;
 		}
 		
 		for(int i = 0;i<m;i++){
 			int count = 0;
 			for(int j = 0;j<n;j++){
 				if (array[j][i] != blackstring){
 					count+=1;
 					blackstring = array[j][i];
 				}
 			}
 			if (array[n-1][i] == false)
 				count+=1;
 			
 			if (count != 2 && count!= 0)
 				found = false;
 			if (count == 0)
 				emptycol = true;
 			blackstring = true;
 			sum+= count;
 		}
 		if (found == false || emptycol^emptyrow== true)
 			out.println("-1");
 		else{
 			//hello
 			int count = 0;
 			for(int i = 0;i<n;i++){
 				for(int j =0;j<m;j++){
 					if (array[i][j]!= true){
 						array[i][j] = true;
 						Stack<List<Integer>> dfs = new Stack<List<Integer>>();
 						ArrayList<Integer> coords = new ArrayList<Integer>();
 						coords.add(i);
 						coords.add(j);
 						dfs.add(coords);
 						while(!dfs.isEmpty()){
 							List<Integer> coordsa = dfs.pop();
 							ArrayList<Integer> newcoords1 = new ArrayList<Integer>();
 							ArrayList<Integer> newcoords2 = new ArrayList<Integer>();
 							ArrayList<Integer> newcoords3 = new ArrayList<Integer>();
 							ArrayList<Integer> newcoords4 = new ArrayList<Integer>();
 							newcoords1.add(coordsa.get(0)-1);
 							newcoords1.add(coordsa.get(1));
 							newcoords2.add(coordsa.get(0)+1);
 							newcoords2.add(coordsa.get(1));
 							newcoords3.add(coordsa.get(0));
 							newcoords3.add(coordsa.get(1)-1);
 							newcoords4.add(coordsa.get(0));
 							newcoords4.add(coordsa.get(1)+1);
 							if (newcoords1.get(0) >=0 && newcoords1.get(0)< n && newcoords1.get(1) >=0 && newcoords1.get(1)< m && array[newcoords1.get(0)][newcoords1.get(1)] == false){
 								dfs.add(newcoords1);
 								array[newcoords1.get(0)][newcoords1.get(1)] = true;
 								
 							}
 							if (newcoords2.get(0) >=0 && newcoords2.get(0)< n && newcoords2.get(1) >=0 && newcoords2.get(1)< m && array[newcoords2.get(0)][newcoords2.get(1)] == false){
 								dfs.add(newcoords2);
 								array[newcoords2.get(0)][newcoords2.get(1)] = true;
 								
 							}
 							if (newcoords3.get(0) >=0 && newcoords3.get(0)< n && newcoords3.get(1) >=0 && newcoords3.get(1)< m && array[newcoords3.get(0)][newcoords3.get(1)] == false){
 								dfs.add(newcoords3);
 								array[newcoords3.get(0)][newcoords3.get(1)] = true;
 								
 							}
 							if (newcoords4.get(0) >=0 && newcoords4.get(0)< n && newcoords4.get(1) >=0 && newcoords4.get(1)< m && array[newcoords4.get(0)][newcoords4.get(1)] == false){
 								dfs.add(newcoords4);
 								array[newcoords4.get(0)][newcoords4.get(1)] = true;
 								
 							}
 							
 						}
 						count+=1;
 					}
 				}
 			}
 			out.println(count);
 		}
 		out.close();
 		
 		
 		
	}
 
}