#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define ip pair<ll,ll>
#define vii vector<ll>
#define vpp vector<ip>
#define vdd vector<ld>
#define vb vector<bool>
#define pi  pair<ll,ip>
#define sc(x) scanf("%lli",&x)
#define hu 200010
#define pq priotity_queue<ll>
#define pqs priority_queue<ll,vii,greater<ll>>
#define ppq priority_queue<ip,vpp,greater<ip>>
#define INF INT_MAX
#define ff first
#define ss second
#define fo(i,k,n) for(ll i=k;i<n;i++) 
using namespace std;
const ll M = 1000000000+7;
//Grid Movement
ll dx[] = {-1, 0, 1, 0};
ll dy[] = {0, -1, 0, 1};

ld Pi= acos(-1);

ll lcm(ll a,ll b)
{
    return (a*b)/(__gcd(a,b));
}

ll mex(ll x, ll y) 
{ 
	ll res = 1;	  
	x = x % M; 
	if (x == 0) return 0; 
	while (y > 0) 
	{ 
		if (y & 1) 
		res = (res*x) % M; 
		y = y>>1; 
		x = (x*x) % M; 
	} 
	return res; 
} 

bool comp(ll comp1,ll comp2)
{
    return comp1>comp2;
}

void yes()
{
    printf("YES\n");
}

void no()
{
    printf("NO\n");
}


void solve(ll t)
{
    ll n;
    sc(n);
    
    ll dp[n][2];
    vector<string>vec(n);
    fo(i,0,n) cin >> vec[i];
    
    dp[0][1] = (vec[0]=="OR")?3:1;
    dp[0][0] = (vec[0]=="OR")?1:3;
    
    fo(i,1,n)
    {
        if(vec[i]=="OR")
        {
            dp[i][0] = dp[i-1][0];
            dp[i][1] = dp[i-1][1]*2 + dp[i-1][0];
        }
        else
        {
            dp[i][1] = dp[i-1][1];
            dp[i][0] = dp[i-1][0]*2 + dp[i-1][1];
        }
    }
    ll ans = dp[n-1][1];
    cout << ans << endl;
    
}

int main()
{
	//ios_base::sync_with_stdio(0); cin.tie(0);
    ll t=1;
    //sc(t);
    fo(i,1,t+1) solve(i);
}



#pragma GCC optimize("-Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
// #include <atcoder/all>
// #include <bits/stdc++.h> 
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>

using namespace std;
// using namespace atcoder;
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<p64,p64> pp64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef pair<ll,p64> tp;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln '\n'
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define zero ll(0)
#define set_bits(x) __builtin_popcountll(x)
// #define mint modint998244353
 
ll mpow(ll a, ll b){
	if(a==0) return 0;
	if(b==0) return 1;
	ll t1 = mpow(a,b/2);
	t1 *= t1;
	t1 %= MOD;
	if(b%2) t1 *= a;
	t1 %= MOD;
	return t1;
}
 
ll mpow(ll a, ll b, ll p){
	if(a==0) return 0;
	if(b==0) return 1;
	ll t1 = mpow(a,b/2,p);
	t1 *= t1;
	t1 %= p;
	if(b%2) t1 *= a;
	t1 %= p;
	return t1;
}
 
ll modinverse(ll a, ll m){
	ll m0 = m; 
    ll y = 0, x = 1; 
    if (m == 1) return 0; 
    while (a > 1){
        ll q = a / m; 
        ll t = m; 
        m = a % m, a = t; 
        t = y; 
        y = x - q * y; 
        x = t; 
    }
    if (x < 0) x += m0; 
    return x; 
}

mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
 
ll range(ll l, ll r){
	return l + mt()%(r-l+1);
}
 
ll rev(ll v){
	return mpow(v,MOD-2);
}

ll nc2(ll n){
	return (n*(n-1))/2;
}

vv64 a;
v64 r,c;
ll h,w;

void set_column(ll, ll);

void set_row(ll index, ll v){
	if(r[index]) return;
	r[index]=v;
	forn(i,w) if(a[index][i]) set_column(i,v);
}

void set_column(ll index, ll v){
	if(c[index]) return;
	c[index]=v;
	forn(i,h) if(a[i][index]) set_row(i,v);
}

void solve(){
	cin >> h >> w;
	a.resize(h,v64(w,0));
	r.resize(h,0);
	c.resize(w,0);
	forn(i,h){
		forn(j,w){
			char c;
			cin >> c;
			if(c=='#') a[i][j]=1;
		}
	}
	set_row(0,1);
	set_row(h-1,1);
	set_column(0,1);
	set_column(w-1,1);
	ll val = 2;
	forn(i,h){
		forn(j,w) if(a[i][j]){
			set_row(i,val);
			set_column(j,val);
			val++;
		}
	}
	ll a1=0,a2=0;
	set<ll> s1,s2;
	for(auto it : r) {
		if(it) s1.insert(it);
		else a1++;
	}
	for(auto it : c) {
		if(it) s2.insert(it);
		else a2++;
	}
	cout << min(a1+sz(s1)-1,a2+sz(s2)-1) << ln;
}

int main()
{
	fast_cin();
	ll t=1;
	// cin >> t;
    forn(i,t) {
    	// cout << "Case #" << i+1 << ": ";
    	solve();
    }
    return 0;
}