#include <string>
#include <string.h>
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;

int n,cnt[1005],a[1005];

int main(){
    cin >> n;
    for (int i=0;i<n;++i){
        string s; cin >> s;
        bool b[30];
        memset(b,0,sizeof(b));
        for (int j=0;j<s.length();++j) b[s[j]-97]=1;
        for (int j=0;j<25;++j) if (b[j]==1) a[i]+=pow(2,j);
    }

    sort(a,a+n);
    int cur=a[1];
    int res=1;
    for (int i=0;i<n;++i){
        if (a[i]!=cur){
            cur=a[i];
            res++;
        }
    }
    cout << res;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
	int n;
	cin>>n;
	int x[n][26] = {0};
	for(int i=0;i<n;i++)
	{
		string s;
		cin>>s;
		int count =0;

		//sort(s.begin(), s.end());
		for(int j=0;j<s.size();j++)
		{
			if(x[i][s[j]-'a'] == 0)
				x[i][s[j]-'a']++;
		}
	}
	vector<pair<int, int> > v;
	v.clear();
	int sum1 = 0, sum2 = 0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<26;j++)
		{
			if(x[i][j]>0)
			{
				sum1 += 1;
				sum2 += j;
			}
		}
		v.push_back(make_pair(sum1,sum2));
		sum1 = 0;
		sum2 = 0;
	}
	sort(v.begin(), v.end());
	int xf=0, p=0;
	for(int i=0;i<n-1;)
	{
		if(v[i].first == v[i+1].first && v[i].second == v[i+1].second)
		{
			i++;
		}
		else
		{
			xf++;
			i++;
		}
	}

	cout<<xf+1<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
	int n;
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)
	{
		string s;
		cin>>s;
		int count =0;
		sort(s.begin(), s.end());
		for(int j=0;j<s.size()-1;j++)
		{
			if(s[j]!=s[j+1])
				count++;
		}
		v[i] = count;
	}
	sort(v.begin(), v.end());
	int ans = 0;
	for(int i=0;i<n-1;i++)
	{
		if(v[i]!=v[i+1])
			ans++;
	}
	cout<<ans+1<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
	int n;
	cin>>n;
	int x[n][26] = {0};
	for(int i=0;i<n;i++)
	{
		string s;
		cin>>s;
		int count =0;

		//sort(s.begin(), s.end());
		for(int j=0;j<s.size();j++)
		{
			if(x[i][s[j]-'a'] == 0)
				x[i][s[j]-'a']++;
		}
	}
	vector<pair<int, int> > v;
	v.clear();
	int sum1 = 0, sum2 = 0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<26;j++)
		{
			if(x[i][j]>0)
			{
				sum1 += 1;
				sum2 += j;
			}
		}
		v.push_back(make_pair(sum2,sum1));
		sum1 = 0;
		sum2 = 0;
	}
	sort(v.begin(), v.end());
	int xf=0, p=0;
	for(int i=0;i<n-1;)
	{
		if(v[i].first == v[i+1].first && v[i].second == v[i+1].second)
		{
			i++;
		}
		else
		{
			xf++;
			i++;
		}
	}

	cout<<xf+1<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
	int n;
	cin>>n;

	vector<string> v;
	v.clear();
	for(int i=0;i<n;i++)
	{
		int x[26] = {0};
		string s;
		cin>>s;
		int count =0;

		//sort(s.begin(), s.end());
		for(int j=0;j<s.size();j++)
		{
			if(x[s[j]-'a'] == 0)
				x[s[j]-'a']++;
		}
		string re = "";
		char cj;
		for(int j=0;j<26;j++)
		{
			if(x[j]>0)
			{
				cj = char('a'+j);
				re = re + cj;
			}
		}
		//cout<<re<<endl;
		int flag = 0;
		for(int j=0;j<v.size();j++)
		{
			if(v[j] == re)
				flag = 1;
		}
		if(flag == 0)
			v.push_back(re);
	}
	cout<<v.size()<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
	int n;
	cin>>n;
	int x[26] = {0};
	vector<string> v;
	v.clear();
	for(int i=0;i<n;i++)
	{
		string s;
		cin>>s;
		int count =0;

		//sort(s.begin(), s.end());
		for(int j=0;j<s.size();j++)
		{
			if(x[s[j]-'a'] == 0)
				x[s[j]-'a']++;
		}
		string re = "";
		for(int j=0;j<26;j++)
		{
			if(x[j]>0)
			{
				re = re + to_string(j);
			}
		}
		int flag = 0;
		for(int j=0;j<v.size();j++)
		{
			if(v[j] == re)
				flag = 1;
		}
		if(flag == 0)
			v.push_back(re);
	}
	cout<<v.size()<<endl;
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <queue>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <map>
#define ll long long
using namespace std;
int main()
{

    //freopen("input.txt","r",stdin);
   //freopen("output.txt","w",stdout);
    ll n, m = 0, k, res = 0, a[100000] = {}, z[100000] = {};
    map<string, ll> g;
    char s[10000];
    string s1[10000],s2;
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> s;
        n = strlen(s);
        ll p = 0;
        fill(a, a + 10000, 0);
        for (int j = 0; j < n; j++)
        {
            a[s[j]]++;
            if (a[s[j]] > 1) p = 1;
        }
        sort(s, s + n);
        if (p == 0 && g[s] == 0){
                res++;
                g[s] = 1;
        }
        else
        {
            fill(a, a + 10000, 0);
            sort(s, s + n);
            s2 = "";
            for (int j = 0; j < n; j++)
            {
                a[s[j]]++;
                if(a[s[j]] == 1) s2 += s[j];
            }
            if (g[s2] == 0) res++;
        }
    }
    cout << res;

}


#include <stdio.h>
#include <algorithm>
#include <queue>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <map>
#define ll long long
using namespace std;
int main()
{

   // freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ll n, m = 0, k, res = 0, a[100000] = {}, z[100000] = {};
    map<string, ll> g;
    char s[10000];
    string s1[10000],s2;
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> s;
        n = strlen(s);
        ll p = 0;
        fill(a, a + 10000, 0);
        for (int j = 0; j < n; j++)
        {
            a[s[j]] ++;
            if (a[s[j]] > 1) p = 1;
        }
        sort(s, s + n);
        if (p == 0 && g[s] == 0)
        {
            g[s] = 1;
            res++;
        } else
        {
            fill(a, a + 10000, 0);
            s2 = "";
            for (int j = 0; j < n; j++)
            {
                a[s[j]] ++;
                if (a[s[j]] == 1) s2 += s[j];
            }
            if (g[s2] == 0) res++;
        }
    }
    cout << res;

}


#include <stdio.h>
#include <algorithm>
#include <queue>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <map>
#define ll long long
using namespace std;
int main()
{

    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ll n, m = 0, k, res = 0, a[100000] = {}, z[100000] = {};
    map<string, ll> g;
    char s[10000];
    string s1[10000],s2;
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> s;
        n = strlen(s);
        ll p = 0;
        fill(a, a + 10000, 0);
        s2 = "";
        sort(s, s + n);
        for (int j = 0; j < n; j++)
        {
            a[s[j]] ++;
            if (a[s[j]] == 1) s2 += s[j];
        }
        if (g[s2] == 0){
            res++;
            g[s2] = 1;
        }
    }
    cout << res;

}


#include <stdio.h>
#include <algorithm>
#include <queue>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <map>
#define ll long long
using namespace std;
int main()
{

    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ll n, m, k, z, res = 0, a[100000] = {};
    map<string, ll> g;
    char s[100000];
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> s;
        fill(a, a + 10000, 0);
        int p = 0;
        for (int j = 0; j < strlen(s); j++)
        {
            a[s[j]]++;
            if (a[s[j]] > 1) p = 1;
            sort(s, s + strlen(s));

        }
        if (p == 0 && g[s] == 0){
                res++;
                g[s] = 1;
        }
    }
    cout << res;

}


#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<ctime>
#include<algorithm>
#include<iterator>
#include<iostream>
#include<sstream>
#include<cctype>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<list>
#include<stack>
#include<functional>
#include<utility>
#include<numeric>
#include<bitset>
#include<iterator>
#define INF (int)1e9
#define MOD 1000000007
#define tr(c, it) for(typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define debug(x) cout<<#x" = "<<(x)<<nl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz size
#define ll long long
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define MEM(a, b) memset(a, (b), sizeof(a))
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(all(container), element) != container.end())
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define printa(a,L,R) for(int i=L;i<R;i++) cout<<a[i]<<(i==R-1?"\n":" ")
#define printv(a) printa(a,0,a.size())
#define print2d(a,r,c) for(int i=0;i<r;i++) for(int j=0;j<c;j++) cout<<a[i][j]<<(j==c-1?’n’:’ ‘)
#define PRE(i, m, n, in) for(int (i)=(m);(i)<(n);i+=in)
#define RFOR(i, n) for(int i=n ; i>=0 ; i--)
#define RPRE(i, j, k, in) for(int i=j ; i>=k ; i-=in)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define rev(v) reverse(v.begin(),v.end())
#define srt(v) sort(v.begin(),v.end())
#define grtsrt(v) sort(v.begin(),v.end(),greater<int>())
#define mnv(v) *min_element(v.begin(),v.end())
#define mxv(v) *max_element(v.begin(),v.end())
#define countv(v,a) count(v.begin(),v.end(),a)
#define toint(a) atoi(a.c_str())
using namespace std;
typedef pair<int, int> II;
typedef pair<ll, ll> LL;
typedef vector <int> VI;
typedef vector <ll> VL;
typedef vector<string> VS;
typedef vector <VS> VSS;
typedef vector <II> VII;
typedef vector <LL> VLL;
typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VII> VVII;
typedef vector <VLL> VVLL;
typedef map<int,int> MPII;
typedef map<ll,ll> MPLL;
typedef map<string, int>MPSI;
typedef map<string, ll>MPSL;
typedef map<int, string>MPIS;
typedef map<ll, string> MPLS;
typedef map<string, string> MPSS;
typedef set<int> SETI;
string tostr(int n) {stringstream rr;rr<<n;return rr.str();}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt","r",stdin);
    int n; cin >> n; VVI st(n);
    FOR(i, n) {
        string s; cin >> s; srt(s);
        //cout << s << endl;
        VI a(26, 0);
        //FOR(j, s.sz()) cout << s[j] << " ";
        FOR(j, s.sz()) {
            if(a[int(s[j]) - int('a')] == 0) {
                a[int(s[j]) - int('a')] = 1;
                int p = int(s[j]) - int('a') + 1;
                st[i].pb(p);
            }
        }
        sort(all(st[i]));
        // FOR(j, st[i].sz()) {
        //     cout << st[i][j] << " ";
        // }
        // cout << endl;
    }
    int res = n; // printv(st);
    PRE(i, 1, n, 1) {
        // st[i] & st[i - 1]
        if(st[i].sz() == st[i - 1].sz()) {
            int same = 1;
            FOR(j, st[i].sz()) {
                if(st[i][j] != st[i - 1][j]) same = 0;
                break;
            }
            if(same) {
                res--;
            }
        }
    }
    cout << res << endl;
    return 0;
}


#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<ctime>
#include<algorithm>
#include<iterator>
#include<iostream>
#include<sstream>
#include<cctype>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<list>
#include<stack>
#include<functional>
#include<utility>
#include<numeric>
#include<bitset>
#include<iterator>
#define INF (int)1e9
#define MOD 1000000007
#define tr(c, it) for(typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define debug(x) cout<<#x" = "<<(x)<<nl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz size
#define ll long long
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define MEM(a, b) memset(a, (b), sizeof(a))
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(all(container), element) != container.end())
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define printa(a,L,R) for(int i=L;i<R;i++) cout<<a[i]<<(i==R-1?"\n":" ")
#define printv(a) printa(a,0,a.size())
#define print2d(a,r,c) for(int i=0;i<r;i++) for(int j=0;j<c;j++) cout<<a[i][j]<<(j==c-1?’n’:’ ‘)
#define PRE(i, m, n, in) for(int (i)=(m);(i)<(n);i+=in)
#define RFOR(i, n) for(int i=n ; i>=0 ; i--)
#define RPRE(i, j, k, in) for(int i=j ; i>=k ; i-=in)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define rev(v) reverse(v.begin(),v.end())
#define srt(v) sort(v.begin(),v.end())
#define grtsrt(v) sort(v.begin(),v.end(),greater<int>())
#define mnv(v) *min_element(v.begin(),v.end())
#define mxv(v) *max_element(v.begin(),v.end())
#define countv(v,a) count(v.begin(),v.end(),a)
#define toint(a) atoi(a.c_str())
using namespace std;
typedef pair<int, int> II;
typedef pair<ll, ll> LL;
typedef vector <int> VI;
typedef vector <ll> VL;
typedef vector<string> VS;
typedef vector <VS> VSS;
typedef vector <II> VII;
typedef vector <LL> VLL;
typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VII> VVII;
typedef vector <VLL> VVLL;
typedef map<int,int> MPII;
typedef map<ll,ll> MPLL;
typedef map<string, int>MPSI;
typedef map<string, ll>MPSL;
typedef map<int, string>MPIS;
typedef map<ll, string> MPLS;
typedef map<string, string> MPSS;
typedef set<int> SETI;
string tostr(int n) {stringstream rr;rr<<n;return rr.str();}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt","r",stdin);
    int n; cin >> n; VVI st(n);
    FOR(i, n) {
        string s; cin >> s; srt(s);
        //cout << s << endl;
        VI a(26, 0);
        //FOR(j, s.sz()) cout << s[j] << " ";
        FOR(j, s.sz()) {
            if(a[int(s[j]) - int('a')] == 0) {
                a[int(s[j]) - int('a')] = 1;
                int p = int(s[j]) - int('a') + 1;
                st[i].pb(p);
            }
        }
        sort(all(st[i]));
        // FOR(j, st[i].sz()) {
        //     cout << st[i][j] << " ";
        // }
        // cout << endl;
    }
    srt(st); int res = n; // printv(st);
    PRE(i, 1, n, 1) {
        // st[i] & st[i - 1]
        if(st[i].sz() == st[i - 1].sz()) {
            int same = 1;
            FOR(j, st[i].sz()) {
                if(st[i][j] != st[i - 1][j]) same = 0;
                break;
            }
            if(same) {
                res--;
            }
        }
    }
    cout << res << endl;
    return 0;
}


#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<ctime>
#include<algorithm>
#include<iterator>
#include<iostream>
#include<sstream>
#include<cctype>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<list>
#include<stack>
#include<functional>
#include<utility>
#include<numeric>
#include<bitset>
#include<iterator>
#define INF (int)1e9
#define MOD 1000000007
#define tr(c, it) for(typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define debug(x) cout<<#x" = "<<(x)<<nl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz size
#define ll long long
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define MEM(a, b) memset(a, (b), sizeof(a))
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(all(container), element) != container.end())
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define printa(a,L,R) for(int i=L;i<R;i++) cout<<a[i]<<(i==R-1?"\n":" ")
#define printv(a) printa(a,0,a.size())
#define print2d(a,r,c) for(int i=0;i<r;i++) for(int j=0;j<c;j++) cout<<a[i][j]<<(j==c-1?’n’:’ ‘)
#define PRE(i, m, n, in) for(int (i)=(m);(i)<(n);i+=in)
#define RFOR(i, n) for(int i=n ; i>=0 ; i--)
#define RPRE(i, j, k, in) for(int i=j ; i>=k ; i-=in)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define rev(v) reverse(v.begin(),v.end())
#define srt(v) sort(v.begin(),v.end())
#define grtsrt(v) sort(v.begin(),v.end(),greater<int>())
#define mnv(v) *min_element(v.begin(),v.end())
#define mxv(v) *max_element(v.begin(),v.end())
#define countv(v,a) count(v.begin(),v.end(),a)
#define toint(a) atoi(a.c_str())
using namespace std;
typedef pair<int, int> II;
typedef pair<ll, ll> LL;
typedef vector <int> VI;
typedef vector <ll> VL;
typedef vector<string> VS;
typedef vector <VS> VSS;
typedef vector <II> VII;
typedef vector <LL> VLL;
typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VII> VVII;
typedef vector <VLL> VVLL;
typedef map<int,int> MPII;
typedef map<ll,ll> MPLL;
typedef map<string, int>MPSI;
typedef map<string, ll>MPSL;
typedef map<int, string>MPIS;
typedef map<ll, string> MPLS;
typedef map<string, string> MPSS;
typedef set<int> SETI;
string tostr(int n) {stringstream rr;rr<<n;return rr.str();}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt","r",stdin);
    int n; cin >> n; VS st(n);
    FOR(i, n) {
        string s; cin >> s; srt(s);
        //cout << s << endl;
        VI a(26, 0);
        //FOR(j, s.sz()) cout << s[j] << " ";
        FOR(j, s.sz()) {
            if(a[int(s[j]) - int('a')] == 0) {
                a[int(s[j]) - int('a')] = 1;
                int p = int(s[j]) - int('a') + 1;
                st[i] = st[i] + tostr(p);
            }
        }
        // FOR(j, st[i].sz()) {
        //     cout << st[i][j] << " ";
        // }
        // cout << endl;
    }
    srt(st); // printv(st);
    int res = n; // printv(st);
    PRE(i, 1, n, 1) {
        // st[i] & st[i - 1]
        if(st[i] == st[i - 1]) {
            res--;
        }
    }
    cout << res << endl;
    return 0;
}


#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<ctime>
#include<algorithm>
#include<iterator>
#include<iostream>
#include<sstream>
#include<cctype>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<list>
#include<stack>
#include<functional>
#include<utility>
#include<numeric>
#include<bitset>
#include<iterator>
#define INF (int)1e9
#define MOD 1000000007
#define tr(c, it) for(typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define debug(x) cout<<#x" = "<<(x)<<nl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz size
#define ll long long
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define MEM(a, b) memset(a, (b), sizeof(a))
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(all(container), element) != container.end())
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define printa(a,L,R) for(int i=L;i<R;i++) cout<<a[i]<<(i==R-1?"\n":" ")
#define printv(a) printa(a,0,a.size())
#define print2d(a,r,c) for(int i=0;i<r;i++) for(int j=0;j<c;j++) cout<<a[i][j]<<(j==c-1?’n’:’ ‘)
#define PRE(i, m, n, in) for(int (i)=(m);(i)<(n);i+=in)
#define RFOR(i, n) for(int i=n ; i>=0 ; i--)
#define RPRE(i, j, k, in) for(int i=j ; i>=k ; i-=in)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define rev(v) reverse(v.begin(),v.end())
#define srt(v) sort(v.begin(),v.end())
#define grtsrt(v) sort(v.begin(),v.end(),greater<int>())
#define mnv(v) *min_element(v.begin(),v.end())
#define mxv(v) *max_element(v.begin(),v.end())
#define countv(v,a) count(v.begin(),v.end(),a)
#define toint(a) atoi(a.c_str())
using namespace std;
typedef pair<int, int> II;
typedef pair<ll, ll> LL;
typedef vector <int> VI;
typedef vector <ll> VL;
typedef vector<string> VS;
typedef vector <VS> VSS;
typedef vector <II> VII;
typedef vector <LL> VLL;
typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VII> VVII;
typedef vector <VLL> VVLL;
typedef map<int,int> MPII;
typedef map<ll,ll> MPLL;
typedef map<string, int>MPSI;
typedef map<string, ll>MPSL;
typedef map<int, string>MPIS;
typedef map<ll, string> MPLS;
typedef map<string, string> MPSS;
typedef set<int> SETI;
string tostr(int n) {stringstream rr;rr<<n;return rr.str();}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt","r",stdin);
    int n; cin >> n; VI st(n, 0);
    FOR(i, n) {
        string s; cin >> s;
        VI a(26, 0);
        FOR(j, s.sz()) {
            if(a[int(s[j]) - int('a')] == 0) {
                a[int(s[j]) - int('a')] = 1;
            }
        }
        FOR(j, 26) {
            if(a[j] == 1) {
                st[i] += j + 1;
            }
        }
    }
    srt(st); int res = 0;
    //printv(st);
    int i = 0;
    while(i < n) {
        while(st[i] == st[i + 1] && i < n) {
            i++;
            // cout << i << endl;
        }
        // cout << "i = " << i << " " << st[i] << endl;
        res++;
        i++;
    }
    cout << res << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
      ios :: sync_with_stdio( false );
      cin.tie( nullptr ) ;
      cout.tie( nullptr ) ;

      int n ;
      cin >> n ;

      set<char> s ;
      s.clear() ;
      int count = 0 ;
      for( int i = 0 ; i < n ; i++ ) {
            string name ;
            cin >> name ;
            vector<char> temp ;
            temp.clear() ;
            for( int i = 0 ; i < name.length() ; i++ ) {
                  temp.push_back(name.at(i)) ;
            }
            bool sets = false ;
            for( int i = 0 ; i < temp.size() ; i++ ) {
                  if( find( s.begin() , s.end() , temp.at(i) ) == s.end() ) {
                        s.insert(name.at(i)) ;
                        sets = true ;
                  }
            }
            if(sets) count++ ;
      }

      cout << count << endl ;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
      ios :: sync_with_stdio( false );
      cin.tie( nullptr ) ;
      cout.tie( nullptr ) ;

      int n ;
      cin >> n ;

      set<char> s;
      s.clear() ;
      int count = 0 ;
      for( int i = 0 ; i < n ; i++ ) {
            string name ;
            cin >> name ;
            set<char> temp ;
            temp.clear() ;
            for( int i = 0 ; i < name.length() ; i++ ) {
                  temp.insert(name.at(i)) ;
            }
            if( s != temp ) {
                  count++ ;
                  s = temp ;
            }
      }
      cout << count << endl ;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
      ios :: sync_with_stdio( false );
      cin.tie( nullptr ) ;
      cout.tie( nullptr ) ;

      int n ;
      cin >> n ;

      set<char> s ;
      int count = 0 ;
      for( int i = 0 ; i < n ; i++ ) {
            string name ;
            cin >> name ;
            unordered_map<char,int> umap ;
            for( int i = 0 ; i < name.length() ; i++ ) {
                  umap[name.at(i)]++ ;
            }
            if( umap.size() != name.length() ) {
                  continue ;
            }
            bool nahi = false ;
            for( auto it : umap ) {
                  if( s.find(it.first) == s.end() ) {
                        nahi = true ;
                  }
            }
            for( auto it : umap ) {
                  s.insert(it.first) ;
            }
            if(nahi) count++ ;
      }
      cout << count << endl ;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	set<set<char>> setofset;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string str;
		cin >> str;
		set<char> setchar;
		for (char a : str) {
			setchar.insert(a);
		}
		setofset.insert(setchar);
	}
	cout << setofset.size() << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define sf              scanf
#define pf              printf
#define cases           ll T;cin>>T;for(int TC=1;TC<=T;TC++)
#define pi              3.1415926535897932384626433832795l
#define ignore          cin.ignore();
#define mem(n,i)        memset(n,i,sizeof n);
#define precision(n)    cout<<fixed<<setprecision(n);
#define bug()           cerr<<"**********\n";
#define pb              push_back
#define MOD             1000000007
#define cout(x)         cout<<"Case "<<TC<<": "<<x<<endl
#define EPS             1e-9
#define mp              make_pair
typedef unsigned long long ull;
typedef long long ll;
bool store[1000000];
int main()
{

    int n,count=0;
    cin>>n;
    bool check[200];
    while(n--)
    {
        string x;
        cin>>x;
        mem(check,0);
        int sum=0;
        for(int i=0;i<x.length();i++)
        {
            if(check[x[i]-96]==0)
            {
                check[x[i]-96]=1;
                sum=sum+x[i]-96;
            }
        }
        if(store[sum]==0){
            count++;
            store[sum]=1;
        }
    }
    cout<<count<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define sf              scanf
#define pf              printf
#define cases           ll T;cin>>T;for(int TC=1;TC<=T;TC++)
#define pi              3.1415926535897932384626433832795l
#define ignore          cin.ignore();
#define mem(n,i)        memset(n,i,sizeof n);
#define precision(n)    cout<<fixed<<setprecision(n);
#define bug()           cerr<<"**********\n";
#define pb              push_back
#define MOD             1000000007
#define cout(x)         cout<<"Case "<<TC<<": "<<x<<endl
#define EPS             1e-9
#define mp              make_pair
typedef unsigned long long ull;
typedef long long ll;
bool store[10000000];
int main()
{

    int n,count=0;
    cin>>n;
    bool check[200];
    while(n--)
    {
        string x;
        cin>>x;
        mem(check,0);
        int sum=0;
        for(int i=0;i<x.length();i++)
        {
            if(check[x[i]]==false)
            {
                check[x[i]]=true;
                sum=sum+x[i];
            }
        }
        //cout<<sum<<endl;
        if(store[sum]==false){
            count++;
            store[sum]=true;
        }
    }
    cout<<count<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define sf              scanf
#define pf              printf
#define cases           ll T;cin>>T;for(int TC=1;TC<=T;TC++)
#define pi              3.1415926535897932384626433832795l
#define ignore          cin.ignore();
#define mem(n,i)        memset(n,i,sizeof n);
#define precision(n)    cout<<fixed<<setprecision(n);
#define bug()           cerr<<"**********\n";
#define pb              push_back
#define MOD             1000000007
#define cout(x)         cout<<"Case "<<TC<<": "<<x<<endl
#define EPS             1e-9
#define mp              make_pair
typedef unsigned long long ull;
typedef long long ll;
bool store[1000000];
int main()
{

    int n,count=0;
    cin>>n;
    bool check[200];
    while(n--)
    {
        string x;
        cin>>x;
        mem(check,0);
        int sum=0;
        for(int i=0;i<x.length();i++)
        {
            if(check[x[i]]==0)
            {
                check[x[i]]=1;
                sum+=x[i]-96;
            }
        }
        if(store[sum]==0){
            count++;
            store[sum]=1;
        }
    }
    cout<<count<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define sf              scanf
#define pf              printf
#define cases           ll T;cin>>T;for(int TC=1;TC<=T;TC++)
#define pi              3.1415926535897932384626433832795l
#define ignore          cin.ignore();
#define mem(n,i)        memset(n,i,sizeof n);
#define precision(n)    cout<<fixed<<setprecision(n);
#define bug()           cerr<<"**********\n";
#define pb              push_back
#define MOD             1000000007
#define cout(x)         cout<<"Case "<<TC<<": "<<x<<endl
#define EPS             1e-9
#define mp              make_pair
typedef unsigned long long ull;
typedef long long ll;
int main()
{

    int n;
    cin>>n;
    set < set <char> > s;
    while(n--)
    {
        string x;
        cin>>x;
        set <char> c;
        for(int i=0;i<x.length();i++)
        {
            c.insert(x[i]);
        }
        s.insert(c);
    }
    cout<<s.size()<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define sf              scanf
#define pf              printf
#define cases           ll T;cin>>T;for(int TC=1;TC<=T;TC++)
#define pi              3.1415926535897932384626433832795l
#define ignore          cin.ignore();
#define mem(n,i)        memset(n,i,sizeof n);
#define precision(n)    cout<<fixed<<setprecision(n);
#define bug()           cerr<<"**********\n";
#define pb              push_back
#define MOD             1000000007
#define cout(x)         cout<<"Case "<<TC<<": "<<x<<endl
#define EPS             1e-9
#define mp              make_pair
typedef unsigned long long ull;
typedef long long ll;
int main()
{

    int n,count=0;
    cin>>n;
    bool check[200];
    bool store[100000];
    mem(store,0);
    while(n--)
    {
        string x;
        cin>>x;
        mem(check,0);
        int sum=0;
        for(int i=0;i<x.length();i++)
        {
            if(check[x[i]]==false)
            {
                check[x[i]]=true;
                sum=sum+x[i];
            }
        }
        //cout<<sum<<endl;
        if(store[sum]==false){
            count++;
            store[sum]=true;
        }
    }
    cout<<count<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define sf              scanf
#define pf              printf
#define cases           ll T;cin>>T;for(int TC=1;TC<=T;TC++)
#define pi              3.1415926535897932384626433832795l
#define ignore          cin.ignore();
#define mem(n,i)        memset(n,i,sizeof n);
#define precision(n)    cout<<fixed<<setprecision(n);
#define bug()           cerr<<"**********\n";
#define pb              push_back
#define MOD             1000000007
#define cout(x)         cout<<"Case "<<TC<<": "<<x<<endl
#define EPS             1e-9
#define mp              make_pair
typedef unsigned long long ull;
typedef long long ll;
bool store[1000000];
int main()
{

    int n,count=0;
    cin>>n;
    bool check[200];
    while(n--)
    {
        string x;
        cin>>x;
        mem(check,0);
        int sum=0;
        for(int i=0;i<x.length();i++)
        {
            if(check[x[i]]==false)
            {
                check[x[i]]=true;
                sum=sum+x[i];
            }
        }
        //cout<<sum<<endl;
        if(store[sum]==false){
            count++;
            store[sum]=true;
        }
    }
    cout<<count<<endl;

}


#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
const int M = 1009;
const int N = 3e5 + 19;
char s[N][M];
set<char> q;
set<string> root;

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> s[i];
	for (int i = 0; i < n; i++)
	{
		string g;
		for (int j = 0; j < sizeof s[i]; j++)
		{
			q.insert(s[i][j]);
	    }
		g = *q.begin();
		cout << g << " ";
		for (set<char>::iterator it = ++q.begin(); it != q.end(); it++)
		{
			g +=*it;
		}
		root.insert(g);
	}
	cout<<root.size() << endl;
	return 0;
}


#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
const int M = 1009;
char s[M][M];
set<char> q;
set<string> root;

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> s[i];
	for (int i = 0; i < n; i++)
	{
		string g;
		for (int j = 0; j < sizeof s[i]; j++)
		{
			q.insert(s[i][j]);
	    }
		g = *q.begin();
		cout << g << " ";
		for (set<char>::iterator it = ++q.begin(); it != q.end(); it++)
		{
			g +=*it;
		}
		root.insert(g);
	}
	cout<<root.size() << endl;
	return 0;
}


#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
const int M = 1009;
char s[M][M];
set<string> root;

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> s[i];
	for (int i = 0; i < n; i++)
	{
		string g;set<char> q;

		for (int j = 0; j < sizeof s[i]; j++)
		{
			q.insert(s[i][j]);
	    }
		g = *q.begin();
		for (set<char>::iterator it = ++q.begin(); it != q.end(); it++)
		{
			g +=*it;
		}
		root.insert(g);
	}
	cout<<root.size() << endl;
	return 0;
}


#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
const int M = 1009;
char s[M][M];
set<char> q;
set<string> root;

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> s[i];
	for (int i = 0; i < n; i++)
	{
		string g;
		for (int j = 0; j < sizeof s[i]; j++)
		{
			q.insert(s[i][j]);
	    }
		g = *q.begin();
		for (set<char>::iterator it = ++q.begin(); it != q.end(); it++)
		{
			g +=*it;
		}
		root.insert(g);
	}
	cout<<root.size() << endl;
	return 0;
}


#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
const int M = 1009;
const int N = 3e3 + 19;
char s[N][M];
set<char> q;
set<string> root;

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> s[i];
	for (int i = 0; i < n; i++)
	{
		string g;
		for (int j = 0; j < sizeof s[i]; j++)
		{
			q.insert(s[i][j]);
	    }
		g = *q.begin();
		cout << g << " ";
		for (set<char>::iterator it = ++q.begin(); it != q.end(); it++)
		{
			g +=*it;
		}
		root.insert(g);
	}
	cout<<root.size() << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;
char all[1005][1005];
struct A
{
    int have[30]={0};
}how[1005];
bool F(A a,A b)
{
    for(int i=0;i<=26;i++)
    {
        if(a.have[i]>b.have[i]) return 0;
        if(a.have[i]<b.have[i]) return 1;
    }
    return 0;
}
int main()
{
    int N,i,ans=1,j,ok;

    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",all[i]);
        for(j=0;j<=26;j++) how[i].have[j]=0;
        for(j=0;all[i][j]!='\0';j++) how[i].have[all[i][j]-'a']=1;

    }
    sort(how,how+N,F);
    for(i=1;i<N;i++)
    {
        ok=0;
        for(j=0;j<=26;j++)
        {
            if(how[i].have[j]!=how[i-1].have[j]) ok=1;
        }
        ans+=ok;
    }
    /*for(i=0;i<N;i++)
    {
        for(j=0;j<=26;j++) if(how[i].have[j]) printf("%d %d\n",i,j);
    }*/
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;
char all[1005][1005];
struct A
{
    int have[30]={0};
}how[1005];
bool F(A a,A b)
{
    for(int i=0;i<=26;i++)
    {
        if(a.have[i]>b.have[i]) return 0;
        if(a.have[i]<b.have[i]) return 1;
    }
    return 0;
}
int main()
{
    int N,i,ans=1,j,ok;

    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",all[i]);
        for(j=0;j<=26;j++) how[i].have[j]=0;
        for(j=0;all[i][j]!='\0';j++) how[i].have[all[i][j]-'a']=1;

    }
    sort(how,how+N,F);
    /*for(i=1;i<N;i++)
    {
        ok=0;
        for(j=0;j<=26;j++)
        {
            if(how[i].have[j]!=how[i-1].have[j]) ok=1;
        }
        ans+=ok;
    }*/
    for(i=0;i<N;i++)
    {
        for(j=0;j<=26;j++) if(how[i].have[j]) printf("%d %d\n",i,j);
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;
char all[1005][1005];
struct A
{
    int have[30]={0};
}how[1005];
bool F(A a,A b)
{
    for(int i=0;i<=26;i++)
    {
        if(a.have[i]>b.have[i]) return 0;
        if(a.have[i]<b.have[i]) return 1;
    }
    return 1;
}
int main()
{
    int N,i,ans=1,j,ok;

    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",all[i]);
        for(j=0;j<=26;j++) how[i].have[j]=0;
        for(j=0;all[j]!='\0';j++) how[i].have[all[i][j]-'a']=1;

    }
    sort(how,how+N,F);
    for(i=1;i<N;i++)
    {
        ok=0;
        for(j=0;j<=26;j++)
        {
            if(how[i].have[j]!=how[i-1].have[j]) ok=1;
        }
        ans+=ok;
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;
char all[1005][1005];
struct A
{
    int have[30]={0};
}how[1005];
bool F(A a,A b)
{
    for(int i=0;i<=26;i++)
    {
        if(a.have[i]>b.have[i]) return 0;
        if(a.have[i]<b.have[i]) return 1;
    }
    return 1;
}
int main()
{
    int N,i,ans=1,j,ok;

    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",all[i]);
        for(j=0;j<=26;j++) how[i].have[j]=0;
        for(j=0;all[i][j]!='\0';j++) how[i].have[all[i][j]-'a']=1;

    }
    sort(how,how+N,F);
    for(i=1;i<N;i++)
    {
        ok=0;
        for(j=0;j<=26;j++)
        {
            if(how[i].have[j]!=how[i-1].have[j]) ok=1;
        }
        ans+=ok;
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;
char all[1005];
struct A
{
    int have[30]={0};
}how[1005];
bool F(A a,A b)
{
    for(int i=0;i<26;i++)
    {
        if(a.have[i]>b.have[i]) return 0;
        if(a.have[i]<b.have[i]) return 1;
    }
    return 1;
}
int main()
{
    int N,i,ans=1,j;
    bool ok;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",all);
        for(j=0;j<26;j++) how[i].have[j]=0;
        for(j=0;all[j]!='\0';j++) how[i].have[all[j]-'a']=1;

    }
    sort(how,how+N,F);
    for(i=1;i<N;i++)
    {
        ok=0;
        for(j=0;j<26;j++) ok=ok||(how[i].have[j]!=how[i-1].have[j]);
        ans+=ok;
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;
char all[1005];
struct A
{
    int have[30]={0};
}how[1005];
bool F(A a,A b)
{
    for(int i=0;i<26;i++)
    {
        if(a.have[i]>b.have[i]) return 0;
        if(a.have[i]<b.have[i]) return 1;
    }
    return 1;
}
int main()
{
    int N,i,ans=1,j,ok;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",all);
        for(j=0;all[j]!='\0';j++) how[i].have[all[j]-'a']=1;

    }
    sort(how,how+N,F);
    for(i=1;i<N;i++)
    {
        ok=0;
        for(j=0;j<26;j++) ok=ok||(how[i].have[j]!=how[i-1].have[j]);
        ans+=ok;
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;
char all[1005];
struct A
{
    int have[30]={0};
}how[1005];
bool F(A a,A b)
{
    for(int i=0;i<=26;i++)
    {
        if(a.have[i]>b.have[i]) return 0;
        if(a.have[i]<b.have[i]) return 1;
    }
    return 1;
}
int main()
{
    int N,i,ans=1,j;
    bool ok;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",all);
        for(j=0;j<=26;j++) how[i].have[j]=0;
        for(j=0;all[j]!='\0';j++) how[i].have[all[j]-'a']=1;

    }
    sort(how,how+N,F);
    for(i=1;i<N;i++)
    {
        ok=0;
        for(j=0;j<=26;j++) ok=ok||(how[i].have[j]!=how[i-1].have[j]);
        ans+=ok;
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;
char all[1005];
struct A
{
    bool have[30]={0};
}how[1005];
bool F(A a,A b)
{
    for(int i=0;i<26;i++)
    {
        if(a.have[i]>b.have[i]) return 0;
        if(a.have[i]<b.have[i]) return 1;
    }
    return 1;
}
int main()
{
    int N,i,ans=1,j,ok;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",all);
        //printf("%d\n",i);
        for(j=0;all[j]!='\0';j++)
        {
            //printf("%d %d\n",i,j);
            how[i].have[all[j]-'a']=1;
        }
    }
    sort(how,how+N,F);
    for(i=1;i<N;i++)
    {
        ok=0;

        for(j=0;j<26;j++) ok=ok||how[i].have[j]!=how[i-1].have[j];
        ans+=ok;
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;
char all[1005];
struct A
{
    int have[30]={0};
}how[1005];
bool F(A a,A b)
{
    for(int i=0;i<=26;i++)
    {
        if(a.have[i]>b.have[i]) return 0;
        if(a.have[i]<b.have[i]) return 1;
    }
    return 1;
}
int main()
{
    int N,i,ans=1,j;
    int ok;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",all);
        for(j=0;j<=26;j++) how[i].have[j]=0;
        for(j=0;all[j]!='\0';j++) how[i].have[all[j]-'a']=1;

    }
    sort(how,how+N,F);
    for(i=1;i<N;i++)
    {
        ok=0;
        for(j=0;j<=26;j++)
        {
            if(how[i].have[j]!=how[i-1].have[j]) ok=1;
        }
        ans+=ok;
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;
char all[1005];
struct A
{
    int have[30]={0};
}how[1005];
bool F(A a,A b)
{
    for(int i=0;i<26;i++)
    {
        if(a.have[i]>b.have[i]) return 0;
        if(a.have[i]<b.have[i]) return 1;
    }
    return 1;
}
int main()
{
    int N,i,ans=1,j,ok;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",all);
        //printf("%d\n",i);
        for(j=0;all[j]!='\0';j++)
        {
            //printf("%d %d\n",i,j);
            how[i].have[all[j]-'a']=1;
        }
    }
    sort(how,how+N,F);
    for(i=1;i<N;i++)
    {
        ok=0;

        for(j=0;j<26;j++) ok=ok||how[i].have[j]!=how[i-1].have[j];
        ans+=ok;
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;
char all[1005];
struct A
{
    int have[30]={0};
}how[1005];
bool F(A a,A b)
{
    for(int i=0;i<26;i++)
    {
        if(a.have[i]>b.have[i]) return 0;
        if(a.have[i]<b.have[i]) return 1;
    }
    return 1;
}
int main()
{
    int N,i,ans=1,j,ok;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",all);
        for(j=0;j<26;j++) how[i].have[j]=0;
        for(j=0;all[j]!='\0';j++) how[i].have[all[j]-'a']=1;

    }
    sort(how,how+N,F);
    for(i=1;i<N;i++)
    {
        ok=0;
        for(j=0;j<26;j++) ok=ok||(how[i].have[j]!=how[i-1].have[j]);
        ans+=ok;
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define lli long long int
#define MOD 1000000009

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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;cin>>n;
    set <string> st;
    for(int i=0;i<n;i++)
    {
        string s,root="";cin>>s;
        sort(s.begin(),s.end());
        root+=s[0];
        for(int j=1;j<s.size();j++)
        {
            if(s[j]!=s[s[j-1]])
                root+=s[j];
        }
        //cout<<root<<endl;
        st.insert(root);
    }
    cout<<st.size()<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define lli long long int
#define MOD 1000000009

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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;cin>>n;
    set <string> st;
    for(int i=0;i<n;i++)
    {
        string s,root="";cin>>s;

        int f[26];
        for(int j=0;j<26;j++)
        {
            f[j]=0;
        }
        for(int j=0;j<s.size();j++)
        {
            f[s[j]-'a']++;
        }
        for(int j=0;j<s.size();j++)
        {
            if(f[j]>0)
                root+=(char)(j+'a');
        }
        //cout<<root<<endl;
        st.insert(root);
    }
    cout<<st.size()<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define lli long long int
#define MOD 1000000009

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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;cin>>n;
    set <string> st;
    for(int i=0;i<n;i++)
    {
        string s,root="";cin>>s;
        sort(s.begin(),s.end());
        root+=s[0];
        for(int j=1;j<s.size();j++)
        {
            if(s[j]!=s[j-1])
                root+=s[j];
        }
        //cout<<root<<endl;
        st.insert(root);
    }
    cout<<st.size()<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define lli long long int
#define MOD 1000000009

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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;cin>>n;
    set <string> st;
    for(int i=0;i<n;i++)
    {
        string s,root="";cin>>s;

        ll f[26];
        for(int j=0;j<26;j++)
        {
            f[j]=0;
        }
        for(int j=0;j<s.size();j++)
        {
            f[s[j]-'a']++;
        }
        for(int j=0;j<s.size();j++)
        {
            if(f[j]>0)
                root+=(char)(j+'a');
        }
        //cout<<root<<endl;
        st.insert(root);
    }
    cout<<st.size()<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;cin>>n;
	set<set<char> >d;
	for(int i=0;i<n;i++) {
		string s;cin>>s;
		set<char>cur;for(int j=0;j<s.size();j++)cur.insert(s[j]);
		d.insert(cur);
	}
	cout<<d.si


#include<stdio.h>
int n,ans,s,sum,t,i,S[100];
int main()
{
	scanf("%d",&n);i=n>81?n-81:1;
	while(i<=n)
	{
		t=i;sum=0;
		while(t)sum+=t%10,t/=10;
		if(sum+i==n)S[++s]=i;i++;
	}
	printf("%d\n",s);
	i=1;while(i<=s)printf("%d ",S[i]),i++;
	return 0;
}//Paul_Guderian


#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;cin>>n;
	set<set<char> >d;
	for(int i=0;i<n;i++) {
		string s;cin>>s;
		set<char>cur;for(int j=0;j<s.size();j++)cur.insert(s[j]);
		d.insert(cur);
	}
	cout<<d.size()<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	set<set<int> > st;
	while(n--) {
		string t;
		cin>>t;
		set<int> temp;
		for(char c : t){
			temp.insert(c-'a');
		}
		st.insert(temp);
	}
	cout<<st.size();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, n, g=0;
char k;
set <char> s;
int main()
{
  cin>>n;
  while(cin<<k)
  {
    cin>>k;
    s.insert(k);
  }
  cout<<s.size()-c;

}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;cin>>n;
	set<set<char> >d;
	for(int i=0;i<n;i++) {
		string s;cin>>s;
		set<char>cur;for(int j=0;j<s.size();j++)cur.insert(s[j]);
		d.insert(cur);
	}
	cout<<d.si
}


#include <bits/stdc++.h>

#define ma_asl_a7a_ya3ny ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using ll = long long;

vector<vector<bool> > vis;
string arr[1000060];
int n,m;
bool valid(int i, int j){
        return i >= 0 && i < n && j >= 0 && j < m && !vis[i][j] && arr[i][j] != '#';
}
bool dfs(int i, int j){
    if(!valid(i,j))
            return false;
    if(i == n - 1 && j == m - 1)
            return true;
    vis[i][j] = true;
    bool p,q;
    p = dfs(i + 1 , j);
    if(p)return p;
    q = dfs(i , j + 1);
    if(q)return q;
    return false;

}
int main() {

    ma_asl_a7a_ya3ny
    vis = vector<vector<bool> >(n + 6, vector<bool> (m + 6, false));
    cin >> n >> m;
    for(int i = 0; i < n; ++i)
            cin >> arr[i];
    int ans = 0;
    while(dfs(0,0)){
            ans++;
            vis[0][0] = false;
    }
    cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>

#define ma_asl_a7a_ya3ny ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using ll = long long;

vector<vector<bool> > vis;
string arr[1000060];
int n,m;
bool valid(int i, int j){
        return i >= 0 && i < n && j >= 0 && j < m && !vis[i][j] && arr[i][j] != '#';
}
bool dfs(int i, int j){
    if(!valid(i,j))
            return false;
    if(i == n - 1 && j == m - 1)
            return true;
    vis[i][j] = true;
    bool p,q;
    p = dfs(i + 1 , j);
    if(p)return p;
    q = dfs(i , j + 1);
    if(q)return q;
    return false;

}
int main() {

    ma_asl_a7a_ya3ny

    cin >> n >> m;
    vis = vector<vector<bool> >(n + 6, vector<bool> (m + 6, false));
    for(int i = 0; i < n; ++i)
            cin >> arr[i];
    int ans = 0;
    while(dfs(0,0)){
            ans++;
            vis[0][0] = false;
    }
    cout << ans << "\n";

	return 0;
}


/*input
3 3
abc

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
typedef long long int ll;
#define ld long double
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=100005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}
int main(){
	int n,m;
	cin>>n>>m;
	char g[n][m];
	REP(i,n){
		REP(j,m){
			cin>>g[i][j];
		}
	}
	for(int i=1;i<n-1;i++){
		int ans=0;
		REP(j,m){
			ans+=(g[i][j]=='#' or g[i-1][j]=='#' or g[i+1][j]=='#');
		}
		if(ans==m){
			cout<<0;
			return 0;
		}
		if(ans==m-1){
			cout<<1;
			return 0;
		}
	}
	for(int i=1;i<m-1;i++){
		int ans=0;
		REP(j,n){
			ans+=(g[j][i-1]=='#' or g[j][i]=='#' or g[j][i+1]=='#');
		}
		if(ans==n){
			cout<<0;
			return 0;
		}
		if(ans==n-1){
			cout<<1;
			return 0;
		}
	}
	for(int i=1;i<min(n,m);i++){
		int ans=0;
		REP(j,i+1){
			ans+=(g[j][i-j]=='#');
		}
		if(ans==i+1){
			cout<<0;
			return 0;
		}
		if(ans==i){
			cout<<1;
			return 0;
		}
		ans=0;
		REP(j,i+1){
			ans+=(g[n-j-1][m-(i-j)-1]=='#');
		}
		if(ans==i+1){
			cout<<0;
			return 0;
		}
		if(ans==i){
			cout<<1;
			return 0;
		}

	}
	cout<<2;
}


/*input
3 3
abc

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
typedef long long int ll;
#define ld long double
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=100005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}
int main(){
	int n,m;
	cin>>n>>m;
	char g[n][m];
	REP(i,n){
		REP(j,m){
			cin>>g[i][j];
		}
	}
	for(int i=1;i<n-1;i++){
		int ans=0;
		REP(j,m){
			ans+=(g[i][j]=='#' or g[i-1][j]=='#' or g[i+1][j]=='#');
		}
		if(ans==m){
			cout<<0;
			return 0;
		}
		if(ans==m-1){
			cout<<1;
			return 0;
		}
	}
	for(int i=1;i<m-1;i++){
		int ans=0;
		REP(j,n){
			ans+=(g[j][i-1]=='#' or g[j][i]=='#' or g[j][i+1]=='#');
		}
		if(ans==n){
			cout<<0;
			return 0;
		}
		if(ans==n-1){
			cout<<1;
			return 0;
		}
	}
	for(int i=1;i<=min(n,m);i++){
		int ans=0;
		REP(j,i+1){
			ans+=(g[j][i-j]=='#');
		}
		if(ans==i+1){
			cout<<0;
			return 0;
		}
		if(ans==i){
			cout<<1;
			return 0;
		}
		REP(j,i+1){
			ans+=(g[n-j-1][m-(i-j)-1]=='#');
		}
		if(ans==i+1){
			cout<<0;
			return 0;
		}
		if(ans==i){
			cout<<1;
			return 0;
		}
	}
	cout<<2;
}


/*input
3 3
abc

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
typedef long long int ll;
#define ld long double
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=100005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}
int main(){
	int n,m;
	cin>>n>>m;
	char g[n][m];
	REP(i,n){
		REP(j,m){
			cin>>g[i][j];
		}
	}
	for(int i=1;i<n-1;i++){
		int ans=0;
		REP(j,m){
			ans+=(g[i][j]=='#' or g[i-1][j]=='#' or g[i+1][j]=='#');
		}
		if(ans==m){
			cout<<0;
			return 0;
		}
		if(ans==m-1){
			cout<<1;
			return 0;
		}
	}
	for(int i=1;i<m-1;i++){
		int ans=0;
		REP(j,n){
			ans+=(g[j][i-1]=='#' or g[j][i]=='#' or g[j][i+1]=='#');
		}
		if(ans==n){
			cout<<0;
			return 0;
		}
		if(ans==n-1){
			cout<<1;
			return 0;
		}
	}
	cout<<min(((g[0][1]!='#')+(g[1][0]!='#')),((g[n-1][m-2]!='#')+(g[n-2][m-1]!='#')));
}


/*input
3 3
abc

*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
typedef long long int ll;
#define ld long double
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=100005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}
int main(){
	int n,m;
	cin>>n>>m;
	char g[n][m];
	REP(i,n){
		REP(j,m){
			cin>>g[i][j];
		}
	}
	ll ways[n][m];
	ways[0][0]=1;
	for(int i=1;i<n;i++){
		if(g[i][0]=='#') ways[i][0]=0;
		else ways[i][0]=ways[i-1][0];
	}
	for(int i=1;i<m;i++){
		if(g[0][i]=='#') ways[0][i]=0;
		else ways[0][i]=ways[0][i-1];
	}
	for(int i=1;i<n;i++){
		for(int j=1;j<m;j++){
			if(g[i][j]=='#') ways[i][j]=0;
			else ways[i][j]=(ways[i][j-1]+ways[i-1][j])%MOD;
		}
	}
	if(ways[n-1][m-1]==0){
		cout<<0;
		return 0;
	}
	for(int i=1;i<n-1;i++){
		int ans=0;
		REP(j,m){
			ans+=(g[i][j]=='#' or g[i-1][j]=='#' or g[i+1][j]=='#');
		}
		if(ans==m){
			cout<<0;
			return 0;
		}
		if(ans==m-1){
			cout<<1;
			return 0;
		}
	}
	for(int i=1;i<m-1;i++){
		int ans=0;
		REP(j,n){
			ans+=(g[j][i-1]=='#' or g[j][i]=='#' or g[j][i+1]=='#');
		}
		if(ans==n){
			cout<<0;
			return 0;
		}
		if(ans==n-1){
			cout<<1;
			return 0;
		}
	}
	for(int i=1;i<min(n,m);i++){
		int ans=0;
		REP(j,i+1){
			ans+=(g[j][i-j]=='#');
		}
		if(ans==i+1){
			cout<<0;
			return 0;
		}
		if(ans==i){
			cout<<1;
			return 0;
		}
		ans=0;
		REP(j,i+1){
			ans+=(g[n-j-1][m-(i-j)-1]=='#');
		}
		if(ans==i+1){
			cout<<0;
			return 0;
		}
		if(ans==i){
			cout<<1;
			return 0;
		}

	}
	cout<<2;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
typedef long long int ll;
#define ld long double
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=100005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;

ll mypow(ll a,ll b){
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}
string s[1000010];
int n,m;
int vis[1000010];
bool dfs(int x,int y)
{
    if(x<0||y<0||x==n||y==m||s[x][y]=='#'||vis[x*m+y])return 0;
    if(x==n-1&&y==m-1)return 1;
    if(x||y)vis[x*m+y]=1;
    return dfs(x+1,y)||dfs(x,y+1);
}
int main()
{
    ios::sync_with_stdio(false);

    cin>>n>>m;
    for(int i=0;i<n;i++)cin>>s[i];
    if(!dfs(0,0)){cout<<0;return 0;}
    if(!dfs(0,0)){cout<<1;return 0;}
    cout<<2;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 2e6 + 5;
int n, m, ans;
char mz[maxn];
queue<pii>q;
int getid(int x, int y) {
	return x * m + y;
}
bool vis[maxn];
bool dfs(int x,int y) {
	if (x == n - 1 && y == m - 1)return true;
	if (x + 1 < n&&mz[getid(x+1,y)]!='#') {
		if (dfs(x + 1, y)) {
			mz[getid(x, y)] = '#';
			return true;
		}
	}
	if (y + 1 < m&&mz[getid(x, y + 1)] != '#') {
		if (dfs(x, y + 1)) {
			mz[getid(x, y)] = '#';
			return true;
		}
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	/*cout << getid(0,1)<<' '<<getid(1,0);
	cout << getid(1, 0);*/
	for (int i = 0; i < n*m; i++) {
		cin >> mz[i];
	}
	if (!dfs(0, 0))cout << 0;
	else {
		mz[0] = '.';
		if (!dfs(0, 0))cout << 1;
		else cout << 2;
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 1e7 + 5;
int n, m, ans;
char mz[maxn];
queue<pii>q;
int getid(int x, int y) {
	return x * m + y;
}
bool vis[maxn];
bool ok() {
	memset(vis, 0, sizeof(vis));
	while (!q.empty())q.pop();
	q.push(pii(0, 0));
	vis[0] = 1;
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		if ((x == n - 1) && (y == m - 1))return true;
		//int x1 = x + 1, y1 = y, x2 = x, y2 = y + 1;
		int id1 = getid(x + 1, y), id2 = getid(x, y + 1);
		if ((!vis[id1]) && (mz[id1] != '#')&&((x+1)<n)) {
			q.push(pii(x + 1, y));
			vis[id1] = 1;
		}
		if ((!vis[id2]) && (mz[id2] != '#')&&(y+1<m)) {
			q.push(pii(x, y + 1));
			vis[id2] = 1;
		}
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	/*cout << getid(0,1)<<' '<<getid(1,0);
	cout << getid(1, 0);*/
	for (int i = 0; i < n*m; i++) {
		cin >> mz[i];
	}
	if (n == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else if (m == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else {
		if (!ok()) {
			cout << 0;
			return 0;
		}
		if (mz[1] == '#' || mz[m] == '#') {
			cout << 1;
			return 0;
		}
		mz[m] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		mz[m] = '.';
		//cout << getid(1, 0);
		mz[1] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		cout << 2;

	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 1e7 + 5;
int n, m, ans;
char mz[maxn];
queue<pii>q;
int getid(int x, int y) {
	return x * m + y;
}
bool vis[maxn];
bool ok() {
	memset(vis, 0, sizeof(vis));
	while (!q.empty())q.pop();
	q.push(pii(0, 0));
	vis[0] = 1;
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		if (x == n - 1 && y == m - 1)return true;
		//int x1 = x + 1, y1 = y, x2 = x, y2 = y + 1;
		int id1 = getid(x + 1, y), id2 = getid(x, y + 1);
		if ((!vis[id1]) && (mz[id1] != '#')&&((x+1)<n)) {
			q.push(pii(x + 1, y));
			vis[id1] = 1;
		}
		if ((!vis[id2]) && (mz[id2] != '#')&&(y+1<m)) {
			q.push(pii(x, y + 1));
			vis[id2] = 1;
		}
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 0; i < n*m; i++) {
		cin >> mz[i];
	}
	if (n == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else if (m == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else {
		if (!ok()) {
			cout << 0;
			return 0;
		}
		if (mz[1] == '#' || mz[getid(1, 0)] == '#') {
			cout << 1;
			return 0;
		}
		mz[1] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		mz[1] = '.';
		//cout << getid(1, 0);
		mz[getid(1, 0)] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		cout << 2;

	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 1e7 + 5;
int n, m, ans;
char mz[maxn];
queue<pii>q;
int getid(int x, int y) {
	return x * m + y;
}
bool vis[maxn];
bool ok() {
	memset(vis, 0, sizeof(vis));
	while (!q.empty())q.pop();
	q.push(pii(0, 0));
	vis[0] = 1;
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		if ((x == n - 1) && (y == m - 1))return true;
		//int x1 = x + 1, y1 = y, x2 = x, y2 = y + 1;
		int id1 = getid(x + 1, y), id2 = getid(x, y + 1);
		if ((!vis[id1]) && (mz[id1] != '#')&&(x+1<n)) {
			q.push(pii(x + 1, y));
			vis[id1] = 1;
		}
		if ((!vis[id2]) && (mz[id2] != '#')&&(y+1<m)) {
			q.push(pii(x, y + 1));
			vis[id2] = 1;
		}
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	/*cout << getid(0,1)<<' '<<getid(1,0);
	cout << getid(1, 0);*/
	for (int i = 0; i < n*m; i++) {
		cin >> mz[i];
	}
	if (n == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else if (m == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else {
		if (!ok()) {
			cout << 0;
			return 0;
		}
		if (mz[1] == '#' || mz[m] == '#') {
			cout << 1;
			return 0;
		}
		mz[m] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		mz[m] = '.';
		//cout << getid(1, 0);
		mz[1] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		cout << 2;

	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
int n, m, ans;
string mz[maxn];
queue<pii>q;
bool vis[maxn];
bool dfs(int x, int y) {
	if (x >= n || y >= m || mz[x][y] == '#'||vis[x*m+y])return false;
	if (x == n - 1 && y == m-1)return true;
	if (x || y) vis[x*m + y] = 1;
	return dfs(x + 1, y) || dfs(x, y + 1);
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	/*cout << getid(0,1)<<' '<<getid(1,0);
	cout << getid(1, 0);*/
	for (int i = 0; i < n; i++) {
		cin >> mz[i];
	}
	if (!dfs(0, 0))cout << 0;
	else {
		vis[0] = 0;
		if (!dfs(0, 0))cout << 1;
		else cout << 2;
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
int n, m, ans;
char mz[maxn];
queue<pii>q;
int getid(int x, int y) {
	return x * m + y;
}
bool vis[maxn];
bool ok() {
	memset(vis, 0, sizeof(vis));
	while (!q.empty())q.pop();
	q.push(pii(0, 0));
	vis[0] = 1;
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		if (x == n - 1 && y == m - 1)return true;
		//int x1 = x + 1, y1 = y, x2 = x, y2 = y + 1;
		int id1 = getid(x + 1, y), id2 = getid(x, y + 1);
		if ((!vis[id1]) && (mz[id1] != '#')) {
			q.push(pii(x + 1, y));
			vis[id1] = 1;
		}
		if ((!vis[id2]) && (mz[id2] != '#')) {
			q.push(pii(x, y + 1));
			vis[id2] = 1;
		}
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 0; i < n*m; i++) {
		cin >> mz[i];
	}
	if (n == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else if (m == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else {
		if (!ok()) {
			cout << 0;
			return 0;
		}
		if (mz[1] == '#' || mz[getid(1, 0)] == '#') {
			cout << 1;
			return 0;
		}
		mz[1] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		mz[1] = '.';
		mz[getid(1, 0)] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		cout << 2;

	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
int n, m, ans;
string mz[maxn];
queue<pii>q;
bool vis[maxn];
bool dfs(int x, int y) {
	if (x >= n || y >= m || mz[x][y] == '#')return false;
	if (x == n - 1 && y == m-1)return true;
	if (dfs(x + 1, y)) {
		mz[x][y] = '#';
		return true;
	}
	if (dfs(x, y + 1)) {
		mz[x][y] = '#';
		return true;
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	/*cout << getid(0,1)<<' '<<getid(1,0);
	cout << getid(1, 0);*/
	for (int i = 0; i < n; i++) {
		cin >> mz[i];
	}
	if (!dfs(0, 0))cout << 0;
	else {
		mz[0][0] = '.';
		if (!dfs(0, 0))cout << 1;
		else cout << 2;
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 2e6 + 5;
int n, m, ans;
char mz[maxn];
queue<pii>q;
int getid(int x, int y) {
	return x * m + y;
}
bool vis[maxn];
bool ok() {
	memset(vis, 0, sizeof(vis));
	while (!q.empty())q.pop();
	q.push(pii(0, 0));
	vis[0] = 1;
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		if ((x == n - 1) && (y == m - 1))return true;
		//int x1 = x + 1, y1 = y, x2 = x, y2 = y + 1;
		int id1 = getid(x + 1, y), id2 = getid(x, y + 1);
		if ((!vis[id1]) && (mz[id1] != '#')&&(x+1<n)) {
			q.push(pii(x + 1, y));
			vis[id1] = 1;
		}
		if ((!vis[id2]) && (mz[id2] != '#')&&(y+1<m)) {
			q.push(pii(x, y + 1));
			vis[id2] = 1;
		}
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	/*cout << getid(0,1)<<' '<<getid(1,0);
	cout << getid(1, 0);*/
	for (int i = 0; i < n*m; i++) {
		cin >> mz[i];
	}
	if (n == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else if (m == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else {
		if (!ok()) {
			cout << 0;
			return 0;
		}
		if (mz[1] == '#' || mz[m] == '#') {
			cout << 1;
			return 0;
		}
		mz[m] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		mz[m] = '.';
		//cout << getid(1, 0);
		mz[1] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		cout << 2;

	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
int n, m, ans;
string mz[maxn];
queue<pii>q;
//bool vis[maxn];
bool dfs(int x, int y) {
	if (x >= n || y >= m || mz[x][y] == '#')return false;
	if (x == n - 1 && y == m-1)return true;
	if (x || y) mz[x][y] = '#';
	return dfs(x + 1, y) || dfs(x, y + 1);
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	/*cout << getid(0,1)<<' '<<getid(1,0);
	cout << getid(1, 0);*/
	for (int i = 0; i < n; i++) {
		cin >> mz[i];
	}
	if (!dfs(0, 0))cout << 0;
	else {
		if (!dfs(0, 0))cout << 1;
		else cout << 2;
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 2e6 + 5;
int n, m, ans;
char mz[maxn];
queue<pii>q;
bool vis[maxn];
int getid(int x, int y) {
	return x * m + y;
}
bool dfs(int x,int y) {
	if (x == n - 1 && y == m - 1)return true;
	if (x + 1 < n&&mz[getid(x+1,y)]!='#') {
		if (dfs(x + 1, y)) {
			mz[getid(x, y)] = '#';
			return true;
		}
	}
	if (y + 1 < m&&mz[getid(x, y + 1)] != '#') {
		if (dfs(x, y + 1)) {
			mz[getid(x, y)] = '#';
			return true;
		}
	}
	return false;
}
bool bfs() {
	q.push(pii(0, 0));
	vis[0] = 1;
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		if (x == n - 1 && y == m - 1)return true;
		if ((x + 1 < n) && (!vis[getid(x + 1, y)])&&(mz[getid(x+1,y)]!='#')) {
			q.push(pii(x + 1, y));
			vis[getid(x + 1, y)] = 1;
		}
		if ((y + 1 < m) && (!vis[getid(x, y + 1)]) && (mz[getid(x, y+1)] != '#')) {
			q.push(pii(x, y + 1));
			vis[getid(x, y + 1)] = 1;
		}
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	/*cout << getid(0,1)<<' '<<getid(1,0);
	cout << getid(1, 0);*/
	for (int i = 0; i < n*m; i++) {
		cin >> mz[i];
	}
	if (!dfs(0, 0))cout << 0;
	else {
		mz[0] = '.';
		if (bfs())cout << 2;
		else cout << 1;
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 2e6 + 5;
int n, m, ans;
char mz[maxn];
queue<pii>q;
int getid(int x, int y) {
	return x * m + y;
}
bool vis[maxn];
bool ok() {
	memset(vis, 0, sizeof(vis));
	while (!q.empty())q.pop();
	q.push(pii(0, 0));
	vis[0] = 1;
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		if ((x == n - 1) && (y == m - 1))return true;
		//int x1 = x + 1, y1 = y, x2 = x, y2 = y + 1;
		int id1 = getid(x + 1, y), id2 = getid(x, y + 1);
		if ((!vis[id1]) && (mz[id1] != '#')&&(x+1<n)) {
			q.push(pii(x + 1, y));
			vis[id1] = 1;
		}
		if ((!vis[id2]) && (mz[id2] != '#')&&(y+1<m)) {
			q.push(pii(x, y + 1));
			vis[id2] = 1;
		}
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	/*cout << getid(0,1)<<' '<<getid(1,0);
	cout << getid(1, 0);*/
	for (int i = 0; i < n*m; i++) {
		cin >> mz[i];
	}
	if (n == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else if (m == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else {
		if (!ok()) {
			cout << 0;
			return 0;
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (i == 0 && j == 0)continue;
				if (i == n - 1 && j == m - 1)continue;
				if (mz[getid(i, j)] == '#')continue;
				mz[getid(i, j)] = '#';
				if (!ok()) {
					cout << 1;
					return 0;
				}
				mz[getid(i, j)] = '.';
			}
		}
		cout << 2;

	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
int n, m, ans;
char mz[maxn];
queue<pii>q;
int getid(int x, int y) {
	return x * m + y;
}
bool vis[maxn];
bool ok() {
	memset(vis, 0, sizeof(vis));
	while (!q.empty())q.pop();
	q.push(pii(0, 0));
	vis[0] = 1;
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		if (x == n - 1 && y == m - 1)return true;
		//int x1 = x + 1, y1 = y, x2 = x, y2 = y + 1;
		int id1 = getid(x + 1, y), id2 = getid(x, y + 1);
		if ((!vis[id1]) && (mz[id1] != '#')) {
			q.push(pii(x + 1, y));
			vis[id1] = 1;
		}
		if ((!vis[id2]) && (mz[id2] != '#')) {
			q.push(pii(x, y + 1));
			vis[id2] = 1;
		}
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 0; i < n*m; i++) {
		cin >> mz[i];
	}
	if (n == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else if (m == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else {
		if (!ok()) {
			cout << 0;
			return 0;
		}
		if (mz[0] == '#' || mz[getid(1, 0)] == '#') {
			cout << 1;
			return 0;
		}
		mz[1] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		mz[1] = '.';
		mz[getid(1, 0)] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		cout << 2;

	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
int n, m, ans;
char mz[maxn];
queue<pii>q;
int getid(int x, int y) {
	return x * m + y;
}
bool vis[maxn];
bool ok() {
	memset(vis, 0, sizeof(vis));
	while (!q.empty())q.pop();
	q.push(pii(0, 0));
	vis[0] = 1;
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		if (x == n - 1 && y == m - 1)return true;
		//int x1 = x + 1, y1 = y, x2 = x, y2 = y + 1;
		int id1 = getid(x + 1, y), id2 = getid(x, y + 1);
		if ((!vis[id1]) && (mz[id1] != '#')&&((x+1)<n)) {
			q.push(pii(x + 1, y));
			vis[id1] = 1;
		}
		if ((!vis[id2]) && (mz[id2] != '#')&&(y+1<m)) {
			q.push(pii(x, y + 1));
			vis[id2] = 1;
		}
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 0; i < n*m; i++) {
		cin >> mz[i];
	}
	if (n == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else if (m == 1) {
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
		cout << 1;
		return 0;
	}
	else {
		if (!ok()) {
			cout << 0;
			return 0;
		}
		if (mz[1] == '#' || mz[getid(1, 0)] == '#') {
			cout << 1;
			return 0;
		}
		mz[1] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		mz[1] = '.';
		//cout << getid(1, 0);
		mz[getid(1, 0)] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		cout << 2;

	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
int n, m, ans;
char mz[maxn];
queue<pii>q;
int getid(int x, int y) {
	return x * m + y;
}
bool vis[maxn];
bool ok() {
	memset(vis, 0, sizeof(vis));
	while (!q.empty())q.pop();
	q.push(pii(0, 0));
	vis[0] = 1;
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		if (x == n - 1 && y == m - 1)return true;
		//int x1 = x + 1, y1 = y, x2 = x, y2 = y + 1;
		int id1 = getid(x + 1, y), id2 = getid(x, y + 1);
		if ((!vis[id1]) && (mz[id1] != '#')) {
			q.push(pii(x + 1, y));
			vis[id1] = 1;
		}
		if ((!vis[id2]) && (mz[id2] != '#')) {
			q.push(pii(x, y + 1));
			vis[id2] = 1;
		}
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 0; i < n*m; i++) {
		cin >> mz[i];
	}
	if (n == 1)
		for (int i = 0; i < n*m; i++) {
		if (mz[i] == '#') {
			cout << 0;
			return 0;
		}
	}
	else if(m==1)
		for (int i = 0; i < n*m; i++) {
			if (mz[i] == '#') {
				cout << 0;
				return 0;
			}
		}
	else {
		if (!ok()) {
			cout << 0;
			return 0;
		}
		if (mz[0] == '#' || mz[getid(1, 0)] == '#') {
			if (!ok())cout << 0;
			else cout << 1;
			return 0;
		}
		mz[0] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		mz[0] = '.';
		mz[getid(1, 0)] = '#';
		if (!ok()) {
			cout << 1;
			return 0;
		}
		cout << 2;

	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
int n, m, ans;
string mz[maxn];
queue<pii>q;
bool vis[maxn];
bool dfs(int x, int y) {
	if (x >= n || y >= m || mz[x][y] == '#'||vis[x*m+y])return false;
	if (x == n - 1 && y == m-1)return true;
	if (dfs(x + 1, y)) {
		vis[x*m + y] = 1;
		return true;
	}
	if (dfs(x, y + 1)) {
		vis[x*m + y] = 1;
		return true;
	}
	return false;
}
int main() {
	cin.sync_with_stdio(false);
	cin >> n >> m;
	/*cout << getid(0,1)<<' '<<getid(1,0);
	cout << getid(1, 0);*/
	for (int i = 0; i < n; i++) {
		cin >> mz[i];
	}
	if (!dfs(0, 0))cout << 0;
	else {
		vis[0] = 0;
		if (!dfs(0, 0))cout << 1;
		else cout << 2;
	}
	return 0;
}


#include <math.h>
#include <assert.h>

#include <set>
#include <queue>
#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <functional>

template <typename T1, typename T2>
std::istream& operator>>(std::istream& is, std::pair<T1, T2>& v) {
    return is >> v.first >> v.second;
}

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
    for (T& i: v) is >> i;
    return is;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& v) {
    return os << v.first << " " << v.second;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    int n = (int) v.size();
    for (int i = 0; i < n; ++i) os << v[i] << (i == n - 1 ? "" : " ");
    return os;
}

template <typename T>
void in(T& x) {
    std::cin >> x;
}

template <typename T>
void out(const T H) {
    std::cout << (H);
}

template <typename T1, typename... T2>
void out(const T1 H, const T2... T) {
    std::cout << (H) << " "; out(T...);
}

template <typename T>
void eout(const char *s, const T H) {
    std::cerr << s << "=" << H << "\n";
}

template <typename T1, typename... T2>
void eout(const char *s, const T1 H, const T2... T) {
    while (*s != ',') std::cerr << *s++;
    std::cerr << "=" << H << " | "; eout(++s, T...);
}

#define nl                std::cout << "\n"
#define err(args...)      eout(#args, args)

#define rep(i, n)         for (int i =  0 ; i < (n); ++i)
#define per(i, n)         for (int i = (n) - 1; i > -1; --i)
#define repr(i, l, r)     for (int i = (l); i < (r); ++i)

#define sz(v)             ((int) v.size())
#define all(v)            (v).begin(), (v).end()
#define lla(v)            (v).rbegin(), (v).rend()

#define no                std::cout << "NO\n"
#define yes               std::cout << "YES\n"

const int INF           = 2147483647;
const long long int LNF = 9223372036854775807LL;

using ll  = long long int;
using vi  = std::vector<int>;
using vb  = std::vector<bool>;
using ii  = std::pair<int, int>;
using iii = std::pair< ii, int>;
using vii = std::vector<ii>;

const int N  = 505050;
const int MD = 1000*1000*1000 + 7;

int solve();
int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(0);
    //std::cout.tie(0);

    //int t; in(t); repr(tc, 1, t + 1)
    solve();

    return 0;
}

int solve() {
    int n; in(n);
    int m; in(m);
    auto id = [](int i, int j, int m) { return i*m + j; };

    std::set<int> adj[n*m];
    std::string s[n]; rep(i, n) in(s[i]);
    rep(i, n) rep(j, m) if (s[i][j] == '.') {
        if (i and s[i - 1][j] == '.') {
            int x = id(i, j, m);
            int y = id(i - 1, j, m);
            adj[x].insert(y);
            adj[y].insert(x);
        }
        if (j and s[i][j - 1] == '.') {
            int x = id(i, j, m);
            int y = id(i, j - 1, m);
            adj[x].insert(y);
            adj[y].insert(x);
        }
        if (i < n - 1 and s[i + 1][j] == '.') {
            int x = id(i, j, m);
            int y = id(i + 1, j, m);
            adj[x].insert(y);
            adj[y].insert(x);
        }
        if (j < n - 1 and s[i][j + 1] == '.') {
            int x = id(i, j, m);
            int y = id(i, j + 1, m);
            adj[x].insert(y);
            adj[y].insert(x);
        }
    }

    auto bfs = [&](int s, int nv) {
        vi parent(nv, -1);
        bool visited[nv] = {0};

        parent[s] = 0;
        visited[s] = 1;
        std::queue<int> q; q.push(s);

        while (!q.empty()) {
            int u = q.front(); q.pop();
            for (int v: adj[u]) {
                if (!visited[v]) {
                    q.push(v);
                    parent[v] = u;
                    visited[v] = 1;
                }
            }
        }
        if (visited[nv - 1]) {
            int x = nv - 1, y = parent[nv - 1];
            do {
                adj[x].erase(adj[x].find(y));
                adj[y].erase(adj[y].find(x));
                x = parent[y]; std::swap(x, y);
            } while (x);
        }
        return visited[nv - 1];
    };

    out(bfs(0, n*m) + bfs(0, n*m));
    return 0;
}


#include <math.h>
#include <assert.h>

#include <bitset>
#include <queue>
#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <functional>

template <typename T1, typename T2>
std::istream& operator>>(std::istream& is, std::pair<T1, T2>& v) {
    return is >> v.first >> v.second;
}

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
    for (T& i: v) is >> i;
    return is;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& v) {
    return os << v.first << " " << v.second;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    int n = (int) v.size();
    for (int i = 0; i < n; ++i) os << v[i] << (i == n - 1 ? "" : " ");
    return os;
}

template <typename T>
void in(T& x) {
    std::cin >> x;
}

template <typename T>
void out(const T H) {
    std::cout << (H);
}

template <typename T1, typename... T2>
void out(const T1 H, const T2... T) {
    std::cout << (H) << " "; out(T...);
}

template <typename T>
void eout(const char *s, const T H) {
    std::cerr << s << "=" << H << "\n";
}

template <typename T1, typename... T2>
void eout(const char *s, const T1 H, const T2... T) {
    while (*s != ',') std::cerr << *s++;
    std::cerr << "=" << H << " | "; eout(++s, T...);
}

#define nl                std::cout << "\n"
#define err(args...)      eout(#args, args)

#define rep(i, n)         for (int i =  0 ; i < (n); ++i)
#define per(i, n)         for (int i = (n) - 1; i > -1; --i)
#define repr(i, l, r)     for (int i = (l); i < (r); ++i)

#define sz(v)             ((int) v.size())
#define all(v)            (v).begin(), (v).end()
#define lla(v)            (v).rbegin(), (v).rend()

#define no                std::cout << "NO\n"
#define yes               std::cout << "YES\n"

const int INF           = 2147483647;
const long long int LNF = 9223372036854775807LL;

using ll  = long long int;
using vi  = std::vector<int>;
using vb  = std::vector<bool>;
using ii  = std::pair<int, int>;
using iii = std::pair< ii, int>;
using vii = std::vector<ii>;

const int N  = 1100101;
const int MD = 1000*1000*1000 + 7;

int solve();
int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(0);
    //std::cout.tie(0);

    //int t; in(t); repr(tc, 1, t + 1)
    solve();

    return 0;
}

bool a[N];
bool bfs(int n, int m, int i, int j) {
    bool visited[n + 2][m + 2] = {{0}};
    ii parent[n + 2][m + 2];

    visited[i][j] = 1;
    std::queue<ii> q; q.push({i, j});

    auto adj = [](ii u) {
        int x = u.first;
        int y = u.second;
        return vii{{x - 1, y}, {x, y - 1}, {x + 1, y}, {x, y + 1}};
    };

    while (!q.empty()) {
        auto u = q.front(); q.pop();
        for (auto v: adj(u)) {
            int x = v.first;
            int y = v.second;
            if (a[x*(m + 2) + y] and !visited[x][y]) {
                q.push(v);
                visited[x][y] = 1;
                parent[x][y] = u;
            }
        }
    }

    if (visited[n][m]) {
        int x = n, y = m;
        do {
            a[x*(m + 2) + y] = 0;
            std::tie(x, y) = parent[x][y];
        } while (x != 1 or y != 1);
        a[n*(m + 2) + m] = 1;
    }

    return visited[n][m];
}

int solve() {
    int n; in(n);
    int m; in(m);

    rep(i, n) {
        std::string s; in(s);
        rep(j, m) a[(i + 1)*(m + 2) + j + 1] = (s[j] == '.');
    }

    out(bfs(n, m, 1, 1) + bfs(n, m, 1, 1));
    return 0;
}


#include <math.h>
#include <assert.h>

#include <set>
#include <queue>
#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <functional>

template <typename T1, typename T2>
std::istream& operator>>(std::istream& is, std::pair<T1, T2>& v) {
    return is >> v.first >> v.second;
}

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
    for (T& i: v) is >> i;
    return is;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& v) {
    return os << v.first << " " << v.second;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    int n = (int) v.size();
    for (int i = 0; i < n; ++i) os << v[i] << (i == n - 1 ? "" : " ");
    return os;
}

template <typename T>
void in(T& x) {
    std::cin >> x;
}

template <typename T>
void out(const T H) {
    std::cout << (H);
}

template <typename T1, typename... T2>
void out(const T1 H, const T2... T) {
    std::cout << (H) << " "; out(T...);
}

template <typename T>
void eout(const char *s, const T H) {
    std::cerr << s << "=" << H << "\n";
}

template <typename T1, typename... T2>
void eout(const char *s, const T1 H, const T2... T) {
    while (*s != ',') std::cerr << *s++;
    std::cerr << "=" << H << " | "; eout(++s, T...);
}

#define nl                std::cout << "\n"
#define err(args...)      eout(#args, args)

#define rep(i, n)         for (int i =  0 ; i < (n); ++i)
#define per(i, n)         for (int i = (n) - 1; i > -1; --i)
#define repr(i, l, r)     for (int i = (l); i < (r); ++i)

#define sz(v)             ((int) v.size())
#define all(v)            (v).begin(), (v).end()
#define lla(v)            (v).rbegin(), (v).rend()

#define no                std::cout << "NO\n"
#define yes               std::cout << "YES\n"

const int INF           = 2147483647;
const long long int LNF = 9223372036854775807LL;

using ll  = long long int;
using vi  = std::vector<int>;
using vb  = std::vector<bool>;
using ii  = std::pair<int, int>;
using iii = std::pair< ii, int>;
using vii = std::vector<ii>;

const int N  = 505050;
const int MD = 1000*1000*1000 + 7;

int solve();
int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(0);
    //std::cout.tie(0);

    //int t; in(t); repr(tc, 1, t + 1)
    solve();

    return 0;
}

int solve() {
    int n; in(n);
    int m; in(m);
    std::string s[n]; rep(i, n) in(s[i]);

    std::vector <vb> visited(n, vb(m));
    std::function<bool(int, int)> dfs = [&](int i, int j) {
        visited[i][j] = 1;
        if (i == n - 1 and j == m - 1) {
            return s[i][j] = '#', true;
        }
        if (i > 0 and s[i - 1][j] == '.' and !visited[i - 1][j]) {
            if (dfs(i - 1, j)) return s[i][j] = '#', true;
        }
        if (j > 0 and s[i][j - 1] == '.' and !visited[i][j - 1]) {
            if (dfs(i, j - 1)) return s[i][j] = '#', true;
        }
        if (i < n - 1 and s[i + 1][j] == '.' and !visited[i + 1][j]) {
            if (dfs(i + 1, j)) return s[i][j] = '#', true;
        }
        if (j < m - 1 and s[i][j + 1] == '.' and !visited[i][j + 1]) {
            if (dfs(i, j + 1)) return s[i][j] = '#', true;
        }
        return false;
    };
    auto x = dfs(0, 0);
    s[0][0] = s[n - 1][m - 1] = '.';
    rep(i, n) std::fill(all(visited[i]), false);
    auto y = dfs(0, 0);
    out(x + y);

    return 0;
}


#include <math.h>
#include <assert.h>

#include <set>
#include <queue>
#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <functional>

template <typename T1, typename T2>
std::istream& operator>>(std::istream& is, std::pair<T1, T2>& v) {
    return is >> v.first >> v.second;
}

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
    for (T& i: v) is >> i;
    return is;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& v) {
    return os << v.first << " " << v.second;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    int n = (int) v.size();
    for (int i = 0; i < n; ++i) os << v[i] << (i == n - 1 ? "" : " ");
    return os;
}

template <typename T>
void in(T& x) {
    std::cin >> x;
}

template <typename T>
void out(const T H) {
    std::cout << (H);
}

template <typename T1, typename... T2>
void out(const T1 H, const T2... T) {
    std::cout << (H) << " "; out(T...);
}

template <typename T>
void eout(const char *s, const T H) {
    std::cerr << s << "=" << H << "\n";
}

template <typename T1, typename... T2>
void eout(const char *s, const T1 H, const T2... T) {
    while (*s != ',') std::cerr << *s++;
    std::cerr << "=" << H << " | "; eout(++s, T...);
}

#define nl                std::cout << "\n"
#define err(args...)      eout(#args, args)

#define rep(i, n)         for (int i =  0 ; i < (n); ++i)
#define per(i, n)         for (int i = (n) - 1; i > -1; --i)
#define repr(i, l, r)     for (int i = (l); i < (r); ++i)

#define sz(v)             ((int) v.size())
#define all(v)            (v).begin(), (v).end()
#define lla(v)            (v).rbegin(), (v).rend()

#define no                std::cout << "NO\n"
#define yes               std::cout << "YES\n"

const int INF           = 2147483647;
const long long int LNF = 9223372036854775807LL;

using ll  = long long int;
using vi  = std::vector<int>;
using vb  = std::vector<bool>;
using ii  = std::pair<int, int>;
using iii = std::pair< ii, int>;
using vii = std::vector<ii>;

const int N  = 505050;
const int MD = 1000*1000*1000 + 7;

int solve();
int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(0);
    //std::cout.tie(0);

    //int t; in(t); repr(tc, 1, t + 1)
    solve();

    return 0;
}

int solve() {
    int n; in(n);
    int m; in(m);
    std::string s[n]; rep(i, n) in(s[i]);

    auto adj = [&](ii u) {
        int i, j; std::tie(i, j) = u;
        vii v;
        if (i) v.push_back({i - 1, j});
        if (j) v.push_back({i, j - 1});
        if (i < n - 1) v.push_back({i + 1, j});
        if (j < m - 1) v.push_back({i, j + 1});
        return v;
    };

    auto bfs = [&](int i, int j) {
        ii parent[n][m];
        bool visited[n][m] = {{0}};

        visited[i][j] = 1;
        parent[i][j] = {i, j};

        std::queue<ii> q; q.push({i, j});
        while (!q.empty()) {
            auto u = q.front(); q.pop();
            for (auto v: adj(u)) {
                int x = v.first;
                int y = v.second;
                if (s[x][y] == '.' and !visited[x][y]) {
                    q.push(v);
                    visited[x][y] = 1;
                    parent[x][y] = u;
                }
            }
        }

        if (visited[n - 1][m - 1]) {
            int x = n - 1, y = m - 1;
            while (parent[x][y] != ii{0, 0}) {
                s[x][y] = '#';
                std::tie(x, y) = parent[x][y];
            }
            s[n - 1][m - 1] = '.';
        }

        return visited[n - 1][m - 1];
    };

    out(bfs(0, 0) + bfs(0, 0));
    return 0;
}


#include <math.h>
#include <assert.h>

#include <set>
#include <queue>
#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <functional>

template <typename T1, typename T2>
std::istream& operator>>(std::istream& is, std::pair<T1, T2>& v) {
    return is >> v.first >> v.second;
}

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
    for (T& i: v) is >> i;
    return is;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& v) {
    return os << v.first << " " << v.second;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    int n = (int) v.size();
    for (int i = 0; i < n; ++i) os << v[i] << (i == n - 1 ? "" : " ");
    return os;
}

template <typename T>
void in(T& x) {
    std::cin >> x;
}

template <typename T>
void out(const T H) {
    std::cout << (H);
}

template <typename T1, typename... T2>
void out(const T1 H, const T2... T) {
    std::cout << (H) << " "; out(T...);
}

template <typename T>
void eout(const char *s, const T H) {
    std::cerr << s << "=" << H << "\n";
}

template <typename T1, typename... T2>
void eout(const char *s, const T1 H, const T2... T) {
    while (*s != ',') std::cerr << *s++;
    std::cerr << "=" << H << " | "; eout(++s, T...);
}

#define nl                std::cout << "\n"
#define err(args...)      eout(#args, args)

#define rep(i, n)         for (int i =  0 ; i < (n); ++i)
#define per(i, n)         for (int i = (n) - 1; i > -1; --i)
#define repr(i, l, r)     for (int i = (l); i < (r); ++i)

#define sz(v)             ((int) v.size())
#define all(v)            (v).begin(), (v).end()
#define lla(v)            (v).rbegin(), (v).rend()

#define no                std::cout << "NO\n"
#define yes               std::cout << "YES\n"

const int INF           = 2147483647;
const long long int LNF = 9223372036854775807LL;

using ll  = long long int;
using vi  = std::vector<int>;
using vb  = std::vector<bool>;
using ii  = std::pair<int, int>;
using iii = std::pair< ii, int>;
using vii = std::vector<ii>;

const int N  = 505050;
const int MD = 1000*1000*1000 + 7;

int solve();
int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(0);
    //std::cout.tie(0);

    //int t; in(t); repr(tc, 1, t + 1)
    solve();

    return 0;
}

int solve() {
    int n; in(n);
    int m; in(m);
    std::string s[n]; rep(i, n) in(s[i]);

    auto adj = [&](ii u) {
        int i, j; std::tie(i, j) = u;
        vii v;
        if (i) v.push_back({i - 1, j});
        if (j) v.push_back({i, j - 1});
        if (i < n - 1) v.push_back({i + 1, j});
        if (j < m - 1) v.push_back({i, j + 1});
        return v;
    };

    auto bfs = [&](int i, int j) {
        ii parent[n][m];
        bool visited[n][m]; rep(i, n) rep(j, m) visited[i][j] = 0;

        visited[i][j] = 1;
        parent[i][j] = {i, j};

        std::queue<ii> q; q.push({i, j});
        while (!q.empty()) {
            auto u = q.front(); q.pop();
            for (auto v: adj(u)) {
                int x = v.first;
                int y = v.second;
                if (s[x][y] == '.' and !visited[x][y]) {

                    q.push(v);
                    visited[x][y] = 1;
                    parent[x][y] = u;
                }
            }
        }

        if (visited[n - 1][m - 1]) {
            int x = n - 1, y = m - 1;
            while (x or y) {
                s[x][y] = '#';
                std::tie(x, y) = parent[x][y];
            }
            s[n - 1][m - 1] = '.';
        }

        return visited[n - 1][m - 1];
    };

    auto x = bfs(0, 0);
    auto y = bfs(0, 0);
    out(x + y);
    return 0;
}


#include <time.h>
#include <math.h>
#include <assert.h>

#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <functional>

template <typename T1, typename T2>
std::istream& operator>>(std::istream& is, std::pair<T1, T2>& v) {
    return is >> v.first >> v.second;
}

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
    for (T& i: v) is >> i;
    return is;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& v) {
    return os << v.first << " " << v.second;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    int n = (int) v.size();
    for (int i = 0; i < n; ++i) os << v[i] << (i == n - 1 ? "" : " ");
    return os;
}

template <typename T>
void in(T& x) {
    std::cin >> x;
}

template <typename T>
void out(const T H) {
    std::cout << (H);
}

template <typename T1, typename... T2>
void out(const T1 H, const T2... T) {
    std::cout << (H) << " "; out(T...);
}

template <typename T>
void eout(const char *s, const T H) {
    std::cerr << s << "=" << H << "\n";
}

template <typename T1, typename... T2>
void eout(const char *s, const T1 H, const T2... T) {
    while (*s != ',') std::cerr << *s++;
    std::cerr << "=" << H << " | "; eout(++s, T...);
}

#define nl                std::cout << "\n"
#define err(args...)      eout(#args, args)

#define rep(i, n)         for (int i =  0 ; i < (n); ++i)
#define per(i, n)         for (int i = (n) - 1; i > -1; --i)
#define repr(i, l, r)     for (int i = (l); i < (r); ++i)

#define sz(v)             ((int) v.size())
#define all(v)            (v).begin(), (v).end()
#define lla(v)            (v).rbegin(), (v).rend()

#define no                std::cout << "NO\n"
#define yes               std::cout << "YES\n"

const int INF           = 2147483647;
const long long int LNF = 9223372036854775807LL;

using ll  = long long int;
using vi  = std::vector<int>;
using vb  = std::vector<bool>;
using ii  = std::pair<int, int>;
using iii = std::pair< ii, int>;
using vii = std::vector<ii>;

const int N  = 505050;
const int MD = 1000*1000*1000 + 7;

int solve();
int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(0);
    //std::cout.tie(0);

    //int t; in(t); repr(tc, 1, t + 1)
    solve();

    return 0;
}

int solve() {
    int n; in(n);
    int m; in(m);
    std::string s[n]; rep(i, n) in(s[i]);

    std::srand(time(nullptr));
    std::function<bool(int, int)> dfs = [&](int i, int j) {
        if (std::rand()&1) {
            if (i < n - 1 and s[i + 1][j] == '.' and dfs(i + 1, j)) {
                return s[i][j] = '#', true;
            }
            if (j < m - 1 and s[i][j + 1] == '.' and dfs(i, j + 1)) {
                return s[i][j] = '#', true;
            }
        } else {
            if (j < m - 1 and s[i][j + 1] == '.' and dfs(i, j + 1)) {
                return s[i][j] = '#', true;
            }
            if (i < n - 1 and s[i + 1][j] == '.' and dfs(i + 1, j)) {
                return s[i][j] = '#', true;
            }
        }
        return (i == n - 1 and j == m - 1);
    };

    auto x = dfs(0, 0); s[0][0] = '.';
    out(x + dfs(0, 0));
    return 0;
}