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 required.

If there is no placement of magnets that satisfies all conditions, print a single integer -1.

Examples

Input


3 3
.#.
###
##.


Output


1


Input


4 2
##
.#
.#
##


Output


-1


Input


4 5
....#
####.
.###.
.#...


Output


2


Input


2 1
.
#


Output


-1


Input


3 5
.....
.....
.....


Output


0

Note

In the first test, here is an example placement of magnets:

<image>

In the second test, we can show that no required placement of magnets exists. Here are three example placements that fail to meet the requirements. The first example violates rule 3 since we can move the north magnet down onto a white square. The second example violates rule 2 since we cannot move the north magnet to the bottom-left black square by any sequence of operations. The third example violates rule 1 since there is no south magnet in the first column.

<image>

In the third test, here is an example placement of magnets. We can show that there is no required placement of magnets with fewer north magnets.

<image>

In the fourth test, we can show that no required placement of magnets exists. Here are two example placements that fail to meet the requirements. The first example violates rule 1 since there is no south magnet in the first row. The second example violates rules 1 and 3 since there is no south magnet in the second row and we can move the north magnet up one unit onto a white square.

<image>

In the fifth test, we can put the south magnet in each cell and no north magnets. Because there are no black cells, it will be a correct placement.
import java.io.*;
import java.util.*;
import static java.lang.Math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class MonopoleMagnets {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }
    static class TaskA {
        long mod = (long)(998244353);
        long fact[];
        int depth[];
        int parentTable[][];
        int degree[];
        ArrayList<Integer> leaves;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
    	int r[] = new int[]{1 , -1 , 0 , 0};
    	int c[] = new int[]{0 , 0 , 1 , -1};
        public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
            while(testNumber-->0){
                int n = in.nextInt();
                int m = in.nextInt();
                int a[][] = new int[n][m];
                for(int i=0;i<n;i++){
                	String s = in.next();
                	for(int j=0;j<m;j++){
                		if(s.charAt(j) == '#')
                			a[i][j] = 1;
                	}
                }
                boolean rowFound = false;
                boolean colFound = false;
                int row[] = new int[n];
                int col[] = new int[m];
                int rowSum = 0;
                int colSum = 0;
                for(int i=0;i<n;i++){
                	// int x = 0;
                	for(int j=0;j<m;j++)
                		row[i] += a[i][j];
                	if(row[i] == 0)
                		rowFound = true;
                	rowSum += row[i];
                }
                for(int j=0;j<m;j++){
                	// int x = 0;
                	for(int i=0;i<n;i++)
                		col[j] += a[i][j];
                	if(col[j] == 0)
                		colFound = true;
                	colSum += col[j];
                }
                boolean check = false;
                if(rowFound || colFound){
                	if(rowFound && colFound && rowSum == 0 && rowSum == colSum){
                		out.println(0);
                		continue;
                	}
                	check = true;
                }
                int count = 0;
                for(int i=0;i<n;i++)
                	for(int j=0;j<m;j++){
                		if(a[i][j] == 1){
                			dfs(a , i , j);
                			count++;
                		}
                	}
                boolean ans = true;
                for(int i=0;i<n;i++){
                	int x = (a[i][0] == 0)?2:1;
                	for(int j=0;j<m-1;j++){
                		if(a[i][j]!=a[i][j+1])
                			x--;
                	}
                	if(x<0)
                		ans = false;
                }
                for(int j=0;j<m;j++){
                	int x = (a[0][j] == 0)?2:1;
                	for(int i=0;i<n-1;i++)
                		if(a[i][j] != a[i+1][j])
                			x--;
                	if(x<0)
                		ans = false;
                }
                // print2d(a , out);
                if(ans && !check){
                	out.println(count);
                	continue;
                }
                if(!ans){
                	out.println(-1);
                	continue;
                }
                boolean possible = false;
                for(int j=0;j<m;j++){
                	if(col[j] != 0)
                		continue;
                	int i = 0;
                	for(i=0;i<n;i++){
                		if(row[i]!=0)
                			continue;
                		// row[i] = 1;
                		// col[j] = 1;
                		possible = true;
                		break;
                	}
                }
                
                // for(int i=0;i<n;i++)
                // 	if(row[i] == 0)
                // 		possible = false;
                // for(int j=0;j<m;j++)
                // 	if(col[j] == 0)
                		// possible = false;
                if(possible)
                	out.println(count);
                else
                	out.println(-1);
            }
        }

        public void dfs(int a[][] , int x , int y){
        	a[x][y] = 2;
        	for(int i=0;i<4;i++){
        		int row = x+r[i];
        		int col = y+c[i];
        		if(row<0 || col<0 || row == a.length || col == a[row].length || a[row][col] == 0 || a[row][col] == 2)
        			continue;
        		dfs(a , row , col);
        	}
        }

        public int distance(ArrayList<ArrayList<Integer>> a , int u , int v){
            return depth[u]+depth[v] - 2*depth[lca(a , u , v)];
        }

        // public void dfs(ArrayList<ArrayList<Integer>> a , int index , int parent){
        //     parentTable[index][0] = parent;
        //     depth[index] = depth[parent]+1;
            
        //     if(a.get(index).size()==1)
        //         leaves.add(index);

        //     for(int i=1;i<parentTable[index].length;i++)
        //         parentTable[index][i] = parentTable[parentTable[index][i-1]][i-1];
        //     for(int i=0;i<a.get(index).size();i++){
        //         if(a.get(index).get(i)==parent)
        //             continue;
        //         dfs(a , a.get(index).get(i) , index);
        //     }
        // }

        public int lca(ArrayList<ArrayList<Integer>> a , int u , int v){
            if(depth[v]<depth[u]){
                int x = u;
                u = v;
                v = x;
            }
            int diff = depth[v] - depth[u];
            for(int i=0;i<parentTable[v].length;i++){
                // checking whether the ith bit is set in the diff variable
                if(((diff>>i)&1) == 1)
                    v = parentTable[v][i];
            }
            if(v == u)
                return v;
            for(int i=parentTable[v].length-1;i>=0;i--){
                if(parentTable[u][i] != parentTable[v][i]){
                    v = parentTable[v][i];
                    u = parentTable[u][i];
                }
            }
            return parentTable[u][0];
        }

        // for the min max problems
        public void build(int lookup[][] , int arr[], int n) { 
            for (int i = 0; i < n; i++) 
                lookup[i][0] = arr[i]; 
            for (int j = 1; (1 << j) <= n; j++) { 
                for (int i = 0; (i + (1 << j) - 1) < n; i++) { 
                    if (lookup[i][j - 1] > lookup[i + (1 << (j - 1))][j - 1]) 
                        lookup[i][j] = lookup[i][j - 1]; 
                    else
                        lookup[i][j] = lookup[i + (1 << (j - 1))][j - 1]; 
                } 
            } 
        }
        public int query(int lookup[][] , int L, int R) { 
            int j = (int)(Math.log(R - L + 1)/Math.log(2)); 
            if (lookup[L][j] >= lookup[R - (1 << j) + 1][j]) 
                return lookup[L][j]; 
            else
                return lookup[R - (1 << j) + 1][j]; 
        }

        // for printing purposes
        public void print1d(int a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
        }
        public void print2d(int a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            out.println();
        }
        public void sieve(int a[]){
            a[0] = a[1] = 1;
            int i;
            for(i=2;i*i<=a.length;i++){
                if(a[i] != 0)
                    continue;
                a[i] = i;
                for(int k = (i)*(i);k<a.length;k+=i){
                    if(a[k] != 0)
                        continue;
                    a[k] = i;
                }
            }
        }
        public int [][] matrixExpo(int c[][] , int n){
            int a[][] = new int[c.length][c[0].length];
            int b[][] = new int[a.length][a[0].length];
            for(int i=0;i<c.length;i++)
                for(int j=0;j<c[0].length;j++)
                    a[i][j] = c[i][j];
            for(int i=0;i<a.length;i++)
                b[i][i] = 1;
            while(n!=1){
                if(n%2 == 1){
                    b = matrixMultiply(a , a);
                    n--;
                }
                a = matrixMultiply(a , a);
                n/=2;
            }
            return matrixMultiply(a , b);
        }
        public int [][] matrixMultiply(int a[][] , int b[][]){
            int r1 = a.length;
            int c1 = a[0].length;
            int c2 = b[0].length;
            int c[][] = new int[r1][c2];
            for(int i=0;i<r1;i++){
                for(int j=0;j<c2;j++){
                    for(int k=0;k<c1;k++)
                        c[i][j] += a[i][k]*b[k][j];
                }
            }
            return c;
        }
        public long nCrPFermet(int n , int r , long p){
            if(r==0)
                return 1l;
            // long fact[] = new long[n+1];
            // fact[0] = 1;
            // for(int i=1;i<=n;i++)
            //     fact[i] = (i*fact[i-1])%p;
            long modInverseR = pow(fact[r] , p-2 , p);
            long modInverseNR = pow(fact[n-r] , p-2 , p);
            long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p;
            return w;
        }
        public long pow(long a, long b, long m) {
            a %= m;
            long res = 1;
            while (b > 0) {
                long x = b&1;
                if (x == 1)
                    res = res * a % m;
                a = a * a % m;
                b >>= 1;
            }
            return res;
        }
        public long pow(long a, long b) {
            long res = 1;
            while (b > 0) {
                long x = b&1;
                if (x == 1)
                    res = res * a;
                a = a * a;
                b >>= 1;
            }
            return res;
        }
        public void swap(int a[] , int p1 , int p2){
            int x = a[p1];
            a[p1] = a[p2];
            a[p2] = x;
        }
        public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) { 
            if (start > end) {
                return;
            }
            int mid = (start + end) / 2;
            a.add(mid);
            sortedArrayToBST(a, start, mid - 1);
            sortedArrayToBST(a, mid + 1, end); 
        }
        class Combine{
            int value;
            int delete;
            Combine(int val , int delete){
                this.value = val;
                this.delete = delete;
            }
        }
        class Sort2 implements Comparator<Combine>{
            public int compare(Combine a , Combine b){
                if(a.value > b.value)
                    return 1;
                else if(a.value == b.value && a.delete>b.delete)
                    return 1;
                else if(a.value == b.value && a.delete == b.delete)
                    return 0;
                return -1;
            }
        }
        public int lowerLastBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>=x)
                return -1;
            if(a.get(r)<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid-1)<x)
                    return mid-1;
                else if(a.get(mid)>=x)
                    r = mid-1;
                else if(a.get(mid)<x && a.get(mid+1)>=x)
                    return mid;
                else if(a.get(mid)<x && a.get(mid+1)<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(ArrayList<Integer> a , Integer x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>x)
                return l;
            if(a.get(r)<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid+1)>x)
                    return mid+1;
                else if(a.get(mid)<=x)
                    l = mid+1;
                else if(a.get(mid)>x && a.get(mid-1)<=x)
                    return mid;
                else if(a.get(mid)>x && a.get(mid-1)>x)
                    r = mid-1;
            }
            return mid;
        }
        public int lowerLastBound(int a[] , int x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>=x)
                return -1;
            if(a[r]<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid-1]<x)
                    return mid-1;
                else if(a[mid]>=x)
                    r = mid-1;
                else if(a[mid]<x && a[mid+1]>=x)
                    return mid;
                else if(a[mid]<x && a[mid+1]<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(long a[] , long x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>x)
                return l;
            if(a[r]<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid+1]>x)
                    return mid+1;
                else if(a[mid]<=x)
                    l = mid+1;
                else if(a[mid]>x && a[mid-1]<=x)
                    return mid;
                else if(a[mid]>x && a[mid-1]>x)
                    r = mid-1;
            }
            return mid;
        }
        public long log(float number , int base){
            return (long) Math.ceil((Math.log(number) / Math.log(base)) + 1e-9);
        }
        public long gcd(long a , long b){
            if(a<b){
                long c = b;
                b = a;
                a = c;
            }
            while(b!=0){
                long c = a;
                a = b;
                b = c%a;
            }
            return a;
        }
        public long[] gcdEx(long p, long q) {
            if (q == 0)
                return new long[] { p, 1, 0 };
            long[] vals = gcdEx(q, p % q);
            long d = vals[0];
            long a = vals[2];
            long b = vals[1] - (p / q) * vals[2];
            // 0->gcd 1->xValue 2->yValue
            return new long[] { d, a, b };
        }
        public void sievePhi(int a[]){
            a[0] = 0;
            a[1] = 1;
            for(int i=2;i<a.length;i++)
                a[i] = i-1;
            for(int i=2;i<a.length;i++)
                for(int j = 2*i;j<a.length;j+=i)
                    a[j] -= a[i];
        }
        public void lcmSum(long a[]){
            int sievePhi[] = new int[(int)1e6 + 1];
            sievePhi(sievePhi);
            a[0] = 0;
            for(int i=1;i<a.length;i++)
                for(int j = i;j<a.length;j+=i)
                    a[j] += (long)i*sievePhi[i];
        }
        static class AVLTree{
            Node root;
            public AVLTree(){
                this.root = null;
            }
            public int height(Node n){
                return (n == null ? 0 : n.height);
            }
            public int getBalance(Node n){
                return (n == null ? 0 : height(n.left) - height(n.right));
            }
            public Node rotateRight(Node a){
                Node b = a.left;
                Node br = b.right;

                a.lSum -= b.lSum;
                a.lCount -= b.lCount;
                
                b.rSum += a.rSum;
                b.rCount += a.rCount;
                
                b.right = a;
                a.left = br;
                
                a.height = 1 + Math.max(height(a.left) , height(a.right));
                b.height = 1 + Math.max(height(b.left) , height(b.right));
                return b;
            }
            public Node rotateLeft(Node a){
                Node b = a.right;
                Node bl = b.left;
                
                a.rSum -= b.rSum;
                a.rCount -= b.rCount;
                
                b.lSum += a.lSum;
                b.lCount += a.lCount;
                
                b.left = a;
                a.right = bl;
                
                a.height = 1 + Math.max(height(a.left) , height(a.right));
                b.height = 1 + Math.max(height(b.left) , height(b.right));
                return b;
            }
            public Node insert(Node root , long value){
                if(root == null){
                    return new Node(value);
                }
                if(value<=root.value){
                    root.lCount++;
                    root.lSum += value;
                    root.left =  insert(root.left , value);
                }
                if(value>root.value){
                    root.rCount++;
                    root.rSum += value;
                    root.right = insert(root.right , value);
                }
                // updating the height of the root
                root.height = 1 + Math.max(height(root.left) , height(root.right));
                int balance = getBalance(root);

                //ll
                if(balance>1 && value<=root.left.value)
                    return rotateRight(root);
                //rr
                if(balance<-1 && value>root.right.value)
                    return rotateLeft(root);
                //lr
                if(balance>1 && value>root.left.value){
                    root.left = rotateLeft(root.left);
                    return rotateRight(root);
                }
                //rl
                if(balance<-1 && value<=root.right.value){
                    root.right = rotateRight(root.right);
                    return rotateLeft(root);
                }
                return root;
            }
            public void insertElement(long value){
                this.root = insert(root , value);
            }
            public int getElementLessThanK(long k){
                int count = 0;
                Node temp = root;
                while(temp!=null){
                    if(temp.value == k){
                        if(temp.left == null || temp.left.value<k){
                            count += temp.lCount;
                            return count-1;
                        }
                        else
                            temp = temp.left;
                    }
                    else if(temp.value>k){
                        temp = temp.left;
                    }
                    else{
                        count += temp.lCount;
                        temp = temp.right;
                    }
                }
                return count;
            }
            public void inorder(Node root , PrintWriter out){
                Node temp = root;
                if(temp!=null){
                    inorder(temp.left , out);
                    out.println(temp.value + " " + temp.lCount + " " + temp.rCount);
                    inorder(temp.right , out);
                }
            }
        }
        static class Node{
            long value;
            long lCount , rCount;
            long lSum , rSum;
            Node left , right;
            int height;
            public Node(long value){
                this.value = value;
                left = null;
                right = null;
                lCount = 1;
                rCount = 1;
                lSum = value;
                rSum = value;
                height = 1;
            }
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}
