// Parasparopagraho Jīvānām
 
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
 
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
 
using namespace  __gnu_pbds ;
using namespace __gnu_cxx ;
using namespace std ; 
 
typedef long long ll ;
typedef long double ldb ;
 
typedef pair<int, int> pii ;
typedef pair<int, ll> pil ;
typedef pair<ll,ll> pll ;
 
typedef vector<int> vi ;
typedef vector<ll> vll ;
typedef vector<pii> vpi ;
typedef vector<pll> vpl ;
typedef vector<bool> vb ; 
typedef vector<pair<ll,int> > vpli ; 
typedef vector<int,pil> edges ;
typedef vector<vi> matrix ; 
typedef priority_queue<int> pqu ; 
 
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ;
 
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define per(i,b,a) for (int i = (b); i >= (a); i--)
 
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pob pop_back 
#define fi first
#define se second
#define ins insert 
#define bk back
#define con continue 
#define lbd lower_bound 
#define ubd upper_bound 
 
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
 
#define pr cout << // I don't like this but my laptop's 'O' key is not functioning well
 
/* Careful when using long long
__builtin_clz(int x) {} //: the number of zeros at the beginning of the number
__builtin_ctz(int x) {} //: the number of zeros at the end of the number
__builtin_popcount(int x) {} //: the number of ones in the number
__builtin_parity(int x) {} //: the parity (even or odd) of the number of ones */
 
const int mod1 = 1000000007 ;
const int mod2 = 998244353 ;
const ll  infl = 4e18 ;
const int infi = 2e9 ;
const int maxn = 1e6 + 5 ;
const int block = 500 ;
const int logn = 60 ;
const int alpha = 27 ;
const ldb pi = acos(-1) ;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) ;
int rng_lr(int a, int b)
{
    int ret ;
    ret = uniform_int_distribution<int>(a, b)(rng) ;
    return ret ;
    // For shuffling use shuffle(all, rng) ; 
}
 
ll modpow(ll a, ll b, int MOD) { ll res = 1 ; while(b) { if(b&1) { res = res*a ; res %= MOD ; } a *= a ; a %= MOD ; b >>= 1 ; } return res%MOD ; }
void upmin(int &a, int b) { if(a < b) { a = b ; } }
void relax(int &a, int b) { if(a > b) { a = b ; } }
ll add(ll a, ll b, int MOD) { a += b ; if(a >= MOD) { a -= MOD ; } return  a ; }
ll sub(ll a, ll b, int MOD) { a -= b ; if(a < 0) { a += MOD ; } return a ; }
ll mul(ll a, ll b, int MOD) { b %= MOD ; a *= b ; a %= MOD ; return a ; }
ll inverse(ll a, ll MOD) { a = modpow(a, MOD - 2, MOD) ; return a ; }
ll lcm(ll a, ll b) { ll ret ; ll g = __gcd(a, b) ; ret = a/g ; ret = ret*b ; return ret ; }
int icast(char ch) { return int(ch-'a') ; }
void hyn(int a, int b) { if(a == b) cout << "YES\n" ; else cout << "NO\n" ; } 
void pyd(int a, int b) { if(a == b) cout << "First\n" ; else cout << "Second\n" ; } 

// Check constraints + overflows + types in typedef 

int main() 
{
    ios::sync_with_stdio(false) ;
    cin.tie(NULL) ;  
    int n ; 
    string s ; 
    cin >> n >> s ; 
    if(s[0] != s[n-1]) {
    	pr "1\n" ; 
    	return 0 ; 
    }
    rep(i,1,n-2) {
    	if(s[i] != s[0] && s[i + 1] != s[0]) {
    		pr "2\n" ; 
    		return 0 ; 
    	}
    }
    pr "-1\n" ; 
    return 0 ; 
}

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define pb push_back
#define mp make_pair
#define ll long long
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define testcase ll T; cin >> T; for (ll tc = 1; tc <= T; tc++)
#define M 1000000007
#define MM 998244353
#define eps 1e-8
#define eq(x,y) (fabs((x)-(y)) < eps)
#define r2 1.41421356237
ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

int main()
{
  //ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  //testcase {
  ll ans=0;
  int n,m;
  cin>>n>>m;
  char g[n+2][m+2]={};
  for(int i=0;i<n+2;i++) {
  	for(int j=0;j<m+2;j++) {
  		g[i][j]='#';
  	}
  }
  for(int i=1;i<=n;i++) {
  	for(int j=1;j<=m;j++) {
  		char q;
  		cin>>q;
  		g[i][j]=q;
  	}
  }
  for(int i=1;i<=n;i++) {
  	int c=1;
  	for(int j=1;j<=m+1;j++) {
  		if(g[i][j]=='.' && g[i][j-1]=='.') { c++; }
  		else { ans+=c-1; c=1; }
  	}
  	//cout<<ans<<'\n';
  	//ans+=((c*(c-1))/2); c=1;
  }
  for(int i=1;i<=m;i++) {
  	int c=1;
  	for(int j=1;j<=n+1;j++) {
  		if(g[j][i]=='.' && g[j-1][i]=='.') { c++; }
  		else { ans+=c-1; c=1; }
  	}
  	//cout<<ans<<'\n';
  	//ans+=((c*(c-1))/2); c=1;	
  }
  cout<<ans;
  //}
  return 0;
}
