#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i = (a);i<=(b);i++)
#define ROF(i,a,b) for(int i = (a);i>=(b);i--)
#define MST(a,x) memset(a,x,sizeof(a))
#define ll long long
#define PB push_back
#define PH push
#define MP make_pair
#define FT first
#define SD second
#define N 105
#define M 51
#define INF 500
#define MOD 924844033
#define MOD2 1000000009
#define eps 1e-14
using namespace std;
inline int Max(int a,int b)
{
    return (((b-a)>>(32-1))&(a^b))^b;
}
inline int Min(int a,int b)
{
    return (((b-a)>>(32-1))&(a^b))^a;
}
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,1,-1};
struct node
{
    int to,w;
}edge[60*N*N];
vector<int>e[60*N*N];
int a[N][N],b[N][N],q[2*N*N],d[2*N*N],pt[2*N*N];
char s[N];
int bg,ed,cnt;

bool bfs(int s)
{
       int front=0,rear=0;
       memset(d,-1,sizeof(d));
       q[rear++]=s;
       d[s]=0;
       while(front<rear)
       {
               int k=q[front++];
               for(int v:e[k])
               {
                   node &e1 = edge[v];
                   if(e1.w>0&&d[e1.to]==-1)
                   {
                       d[e1.to]=d[k]+1;
                       q[rear++]=e1.to;
                   }
               }
       }
       if(d[ed]>=0)
             return true;
       return false;
}

int dinic(int k,int sum)  //k is the sourse
{
      if (k==ed)return sum;
      for (int &i = pt[k]; i < e[k].size(); ++i)
      {
          if(sum<=0)break;
          int v = e[k][i];

          node &e1 = edge[v];
          node &e2 = edge[v^1];
          if(d[e1.to]==d[k]+1&&e1.w>0)
          {
                if(int tmp = dinic(e1.to,min(sum,e1.w)))
                {
                    e1.w -= tmp;
                    e2.w += tmp;
                    return tmp;
                }
          }
      }
      return 0;
}

void addedge(int from,int to,int w)
{
    edge[cnt].to = to;
    edge[cnt].w = w;
    e[from].PB(cnt++);

    edge[cnt].to = from;
    edge[cnt].w = 0;
    e[to].PB(cnt++);
}
int main()
{
    //freopen("out.txt","r",stdin);
    //freopen("1.txt","w",stdout);
    //cin.tie(0);
    //ios::sync_with_stdio(false);
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        cnt = 0;
        bg = 1,ed = n*m*2+1;
        FOR(i,1,n)
        {
            FOR(j,1,m)
            {
                a[i][j] = i*m+j;
                b[i][j] = i*m+j+n*m;
            }
        }
        int f = 1;
        FOR(i,1,n)
        {
            scanf("%s",s);
            FOR(j,1,m)
            {
                if(s[j-1] == 'X')
                {
                    if(i == 1||i == n||j == 1||j == m)f = 0;
                    addedge(a[i][j],b[i][j],INF);
                    addedge(1,a[i][j],INF);
                }
                else addedge(a[i][j],b[i][j],1);
            }
        }
        if(!f)
        {
            printf("-1\n");
            continue;
        }

        FOR(i,1,n)
        {
            addedge(b[i][1],ed,INF);
            addedge(b[i][m],ed,INF);
            FOR(j,1,m-1)addedge(b[i][j],a[i][j+1],INF);;
            FOR(j,2,m)addedge(b[i][j],a[i][j-1],INF);
        }
        FOR(j,1,m)
        {
            addedge(b[1][j],ed,INF);
            addedge(b[n][j],ed,INF);
            FOR(i,1,n-1)addedge(b[i][j],a[i+1][j],INF);
            FOR(i,2,n)addedge(b[i][j],a[i-1][j],INF);
        }
        int ret = 0;
        while(bfs(bg))
        {
            MST(pt,0);
            while(int flow = dinic(bg,INF))
                ret += flow;
        }
        printf("%d\n",ret);
    }
    return 0;
}


Measured memory_bytes: 17792