/*** author: yuji9511 ***/
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
using vll = vector<ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
struct UnionFind {
private:
    ll N;
    vector<ll> parent;
    vector<ll> num;
    vector<ll> diff_weight;

public:
    UnionFind(ll n){
        N = n;
        parent.resize(N);
        iota(parent.begin(), parent.end(), 0);
        num.assign(N, 1);
        diff_weight.assign(N, 0);
    }

    ll root(ll x){
        if(x == parent[x]){
            return x;
        }else{
            ll r = root(parent[x]);
            diff_weight[x] += diff_weight[parent[x]];
            return parent[x] = r;
        }
    }

    void unite(ll a, ll b, ll w = 0){
        if(a > b) swap(a,b);
        w += weight(a); w -= weight(b);
        a = root(a); b = root(b);
        if(a == b) return;
        if(sz(a) < sz(b)){
            parent[a] = b;
        }else{
            parent[b] = a;
        }
        ll sum = num[a] + num[b];
        num[a] = sum;
        num[b] = sum;
        diff_weight[b] = w;
    }

    bool same(ll a, ll b){ return root(a) == root(b);}
    
    ll sz(ll x){ return num[root(x)];}

    ll weight(ll x){
        root(x);
        return diff_weight[x];
    }

    ll diff(ll a, ll b){
        return weight(b) - weight(a);
    }
};

void solve(){
    ll N,Q;
    cin >> N >> Q;
    vll C(N);
    rep(i,0,N) cin >> C[i];
    
    map<int,int> mp[200010];
    UnionFind uf(N+1);
    rep(i,0,N){
        mp[i][C[i]] = 1;
    }

    while(Q--){
        ll t,a,b;
        cin >> t >> a >> b;
        if(t == 1){
            a--; b--;
            if(a > b) swap(a,b);
            if(uf.same(a,b)) continue;
            ll ra = uf.root(a), rb = uf.root(b);
            if(uf.sz(a) < uf.sz(b)){
                uf.unite(a,b);
                if(uf.root(a) != rb) exit(1);
                for(auto &e: mp[ra]){
                    mp[rb][e.first] += e.second;
                }
                mp[ra].clear();
            }else{
                uf.unite(a,b);
                if(uf.root(a) != ra) exit(1);
                for(auto &e: mp[rb]){
                    mp[ra][e.first] += e.second;
                }
                mp[rb].clear();
            }

        }else{
            a--;
            ll res = mp[uf.root(a)][b];
            print(res);
        }

    }
    

}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
}
#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
#define int long long
using namespace std;
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}
#define fi first
#define se second
#define mkp make_pair
#define pb push_back
typedef pair<int,int>pii;
#define maxn 1000005
#define N 1003
int n,m,a[N][N];
char str[N][N];
queue<pii>q;
bool vis[N][2];
int ff[N<<1];
inline int getf(int x){
	while(x!=ff[x])x=ff[x]=ff[ff[x]];
	return x;
}
inline void merge(int x,int y){
	ff[getf(x)]=getf(y);
}
set<int>s;
signed main()
{
	n=read(),m=read();
	For(i,1,n)cin>>(str[i]+1);
	For(i,1,n)For(j,1,m)a[i][j]=(str[i][j]=='#');
	a[1][1]=a[1][m]=a[n][1]=a[n][m]=1;
	For(i,1,n+m)ff[i]=i;
	For(i,1,n)For(j,1,m)if(a[i][j])merge(i,j+n);
	int res=2e9;
	For(i,1,n)s.insert(getf(i));//cout<<getf(i)<<" ";puts("");
	res=min(res,(int)s.size());//cout<<s.size()<<endl;
	s.clear();
	For(i,1,m)s.insert(getf(i+n));//,cout<<getf(i+n)<<' ';puts("");
	res=min(res,(int)s.size());//cout<<s.size()<<endl;
	cout<<res-1; 
	return 0;
}
/*
10 10
,#,,,,,,,, ok
#,,,#,,,,, ok
,,..,...,,
,,..,...,,
,,..,...,, 
,,,,#,,,,, ok
,#,,,,,,#,
,,..,...,,
,,,,,,,,,,

max(hang lie)

1
..........

*/