#include <bits/stdc++.h>

using namespace std;

struct DSU {
    vector<int> ps;
    DSU(int n): ps(n) {
        iota(ps.begin(), ps.end(), 0);
    }
    int find(int i) {
        if (ps[i] == i) {
            return i;
        }
        return ps[i] = find(ps[i]);
    }
    bool merge(int i, int j) {
        i = find(i);
        j = find(j);
        if (i == j) {
            return false;
        }
        ps[i] = j;
        return true;
    }
};


void solve() {
    int h, w;
    cin >> h >> w;
    DSU dsu(h + w);
    for (auto [i, j] : vector<pair<int, int>>{{0, w}, {0, w+h-1}, {w-1, w}, {w-1, w+h-1}}) {
        dsu.merge(i, j);
    }
    for (int i = 0; i < h; ++i) {
        string s;
        cin >> s;
        for (int j = 0; j < w; ++j) {
            if (s[j] == '#') {
                dsu.merge(w + i, j);
            }
        }
    }
    set<int> a, b;
    for (int i = 0; i < w; ++i) {
        a.insert(dsu.find(i));
    }
    for (int i = 0; i < h; ++i) {
        b.insert(dsu.find(w + i));
    }
    int answer = min(a.size(), b.size()) - 1;
    cout << answer << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
    return 0;
}
#include <bits/stdc++.h>
#define Ri register int
#define ll long long
#define db double
#define FOR(i, j, k) for(Ri i = j; i <= k; i++)
#define DEC(i, j, k) for(Ri i = j; i >= k; i--)
using namespace std;

const int maxn = 2e5 + 100;
int n;
vector<int> g[maxn];
int mx, xm;

int d[maxn];
inline void dfs(int u, int fa) {
	d[u] = d[fa] + 1;
	if(d[u] > mx) mx = d[u], xm = u;
	for(Ri i = 0; i < g[u].size(); i++) {
		if(g[u][i] == fa) continue;
		dfs(g[u][i], u);
	}
}

bool f[maxn];
void tagi(int u) {
	if(!u) return;
	f[u] = 1;
	for(Ri i = 0; i < g[u].size(); i++) {
		if(d[g[u][i]] < d[u]) {
			tagi(g[u][i]);
			break;
		}
	}
}

int out[maxn];
int sol(int u, int fa, int w) {
	out[u] = ++w;
	int h = 0;
	for(Ri i = 0; i < g[u].size(); i++) {
		if(g[u][i] == fa) continue;
		if(f[g[u][i]]) {h = g[u][i]; continue;}
		w = sol(g[u][i], u, w) + 1;
	}
	if(h) w = sol(h, u, w);
	return w;
}

int main() {
	scanf("%d", &n);
	FOR(i, 2, n) {
		int x, y;
		scanf("%d%d", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	mx = 0, xm = 0;
	dfs(1, 0);
	int w = xm;
	mx = 0, xm = 0;
	dfs(w, 0);
	tagi(xm);
	sol(w, 0, 0);
	FOR(i, 1, n) printf("%d ", out[i]);
	return 0;
}

