#include<bits/stdc++.h>
using namespace std;
const int maxn=2020;
int n,m;
int fa[maxn];
int fnd(int x)
{
	if(fa[x]==x)
	{
		return x;
	}
	return fa[x]=fnd(fa[x]);
}
void merge(int x,int y)
{
	fa[fnd(x)]=fnd(y);
}
set<int> s1,s2;
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n+m;i++)
	{
		fa[i]=i;
	}
	merge(1,n);
	merge(1,n+1);
	merge(1,n+m);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			char c;
			cin>>c;
			if(c=='#')
			{
				merge(i,j+n);
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		s1.insert(fnd(i));
	}
	for(int i=n+1;i<=n+m;i++)
	{
		s2.insert(fnd(i));
	}
	cout<<min((int)s1.size(),(int)s2.size())-1<<endl;
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
namespace IO
{
    const int buffer_size=1e5+5;
    char buf[buffer_size],*S,*T;
    bool flag_EOF;
    inline char read_char()
    {
        if(S==T)
            T=(S=buf)+fread(buf,1,buffer_size,stdin);
        return S!=T?*(S++):EOF;
    }
    inline int read_int()
    {
        int flag=1;
        char c=read_char();
        while(c<'0'||c>'9')
        {
            if(c==EOF)
            {
                flag_EOF=true;
                return 0;
            }
            flag=(c=='-'?-1:1);
            c=read_char();
        }
        int x=0;
        while(c>='0'&&c<='9')
        {
            x=x*10+(c^48);
            c=read_char();
        }
        return x*flag;
    }
    inline void read_string(char *s)
    {
        char x;
        do
        {
            x=read_char();
            if(x==EOF)
			{
				flag_EOF=true;
            	return;
            }
        }while(x<'a'||x>'z');
        int len=0;
        while(x>='a'&&x<='z')
        {
            s[len++]=x;
            x=read_char();
        }
        s[len]='\0';
    }
    char fw[buffer_size];
    int pw;
    inline void flush()
    {
        fwrite(fw,1,pw,stdout);
        pw=0;
    }
    inline void write_char(char x)
    {
        if(pw==buffer_size)
            flush();
        fw[pw++]=x;
    }
    char st[13];
    int top;
    inline void write_int(int x)
    {
        if(!x)
        {
            write_char('0');
            return;
        }
        if(x<0)
        {
            write_char('-');
            x=-x;
        }
        while(x)
        {
            st[++top]=x%10+'0';
            x/=10;
        }
        while(top>0)
            write_char(st[top--]);
    }
    inline void write_string(const char *s)
    {
        for(int i=0;s[i];++i)
            write_char(s[i]);
    }
}
char s[10],t[10];
int cnt[15];
const int power[10]={1,10,100,1000,10000,100000};
inline bool check()
{
	static int c[15];
	for(int i=1;i<=9;++i)
		c[i]=0;
	int s1=0;
	for(int i=1;i<=5;++i)
		++c[s[i]-'0'];
	for(int i=1;i<=9;++i)
		s1+=i*power[c[i]];
	for(int i=1;i<=9;++i)
		c[i]=0;
	int s2=0;
	for(int i=1;i<=5;++i)
		++c[t[i]-'0'];
	for(int i=1;i<=9;++i)
		s2+=i*power[c[i]];
	return s1>s2;
}
int main()
{
	int k;
	scanf("%d%s%s",&k,s+1,t+1);
	for(int i=1;i<=9;++i)
		cnt[i]=k;
	for(int i=1;i<=4;++i)
	{
		--cnt[s[i]-'0'];
		--cnt[t[i]-'0'];
	}
	long long cnt_win=0,cnt_all=0;
	for(int i=1;i<=9;++i)
		for(int j=1;j<=9;++j)
		{
			s[5]=i+'0',t[5]=j+'0';
			if(i==j&&cnt[i]>=2)
			{
				cnt_all+=cnt[i]*(cnt[i]-1ll);
				if(check())
					cnt_win+=cnt[i]*(cnt[i]-1ll);
			}
			else if(i!=j&&cnt[i]>=1&&cnt[j]>=1)
			{
				cnt_all+=1ll*cnt[i]*cnt[j];
				if(check())
					cnt_win+=1ll*cnt[i]*cnt[j];
			}
		}
	printf("%.10lf\n",1.0*cnt_win/cnt_all);
    IO::flush();
    return 0;
}
