#include //sort,二分探索,など #include //popcount #include //固定長bit集合 #include //pow,logなど #include //複素数 #include //両端アクセスのキュー #include //ファイルストリーム(標準入力変更用) #include //sortのgreater #include //setprecision(浮動小数点の出力の誤差) #include //入出力 #include //集合演算(積集合,和集合,差集合など) #include //map(辞書) #include //iota(整数列の生成),gcdとlcm(c++17) #include //キュー #include //集合 #include //スタック #include //文字列 #include //イテレータあるけど順序保持しないmap #include //イテレータあるけど順序保持しないset #include //pair #include //可変長配列 //#include //using namespace atcoder; //名前 using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef map msi; typedef map msll; typedef pair pii; typedef pair pllll; typedef vector vi; typedef vector vll; typedef vector vs; typedef vector vb; typedef vector> vvi; typedef vector> vvll; typedef vector> vvs; typedef vector> vvb; //定数 const ll MOD = 1000000007; const ll INF = 1000000000000000000; const int MAXR = 100000; //10^5:配列の最大のrange //マクロ #define rep(i,n) for(int i=0;i=0;i--) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define in1(x1) cin >> x1 #define in2(x1, x2) cin >> x1 >> x2 #define in3(x1, x2, x3) cin >> x1 >> x2 >> x3 #define in4(x1, x2, x3, x4) cin >> x1 >> x2 >> x3 >> x4 #define in5(x1, x2, x3, x4, x5) cin >> x1 >> x2 >> x3 >> x4 >> x5 #define in6(x1, x2, x3, x4, x5, x6) cin >> x1 >> x2 >> x3 >> x4 >> x5 >> x6 #define inN(x, N) rep(i, N) in1(x[i]) #define outl(x) cout << x << endl #define out2l(x, y) cout << x << " " << y << endl #define out3l(x1, x2, x3) cout << x1 << " " << x2 << " " << x3 << endl #define out4l(x1, x2, x3, x4) cout << x1 << " " << x2 << " " << x3 << " " << x4 << endl #define outList(x) for(auto y : x) cout << y << " ";cout << endl //よく使う関数 template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } inline ll div_ceil(ll a, ll b) { return (a + (b - 1)) / b; } inline string zeropad(int n, int m) { ostringstream sout; sout << setfill('0') << setw(m) << n; return sout.str(); } class UnionFind { //par[i]:ノードiの親 //count[i]:iが親の時、iが属するグループのノード数 vector par; vector count; int m_groupCount = 0; public: UnionFind(int N) { //1始まりのときに必要 //par.push_back(0); //count.push_back(0); m_groupCount = N; rep(i, N) { //最初は全てが根であるとして初期化 par.push_back(i); count.push_back(1); } } //データxが属する木の根を再帰で得る:root(x) = {xの木の根} int root(int x) { if (par[x] == x) return x; //ついでに親を張り替え return par[x] = root(par[x]); } //xとyの木を併合 void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける par[rx] = ry; //要素数の変更 count[ry] += count[rx]; count[rx] = 0; //木の併合によってグループが1つ減る m_groupCount--; } // 2つのデータx, yが属する木が同じならtrueを返す bool same(int x, int y) { return root(x) == root(y); } //指定したデータxが属するグループの要素数を返す int size(int x) { return count[root(x)]; } //UnionFindのグループの個数を返す int groupCount() { return m_groupCount; } //UnionFindのコピー処理 UnionFind copy() { UnionFind uf(par.size()); //元のUnionFindの親情報を再現 rep(i, par.size()) { uf.unite(i, this->root(i)); } return uf; } }; int H, W; int rctoi(int h, int w) { return h * W + w; } int dx[4] = { -1, 0, 1, 0 }; int dy[4] = { 0, 1, 0, -1 }; int calc(int h, int w, vs &C, UnionFind &uf) { queue q; q.emplace(h, w); vvi d(H, vi(W, -1)); d[h][w] = 0; while (!q.empty()) { auto p = q.front(); q.pop(); rep(i, 4) { int x = p.first + dx[i]; int y = p.second + dy[i]; if (!(x >= 0 && x < H && y >= 0 && y < W)) continue; if (d[x][y] != -1) continue; if(uf.same(rctoi(h, w), rctoi(x, y))) continue; q.emplace(x, y); d[x][y] = d[p.first][p.second]; if (C[x][y] == '.') { return d[x][y]; } else { d[x][y]++; } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); //標準入力をファイルに変更 //std::ifstream in("input.txt"); //std::cin.rdbuf(in.rdbuf()); in2(W, H); vs C(H); inN(C, H); UnionFind uf(H * W); rep(i, H) rep(j, W) rep(k, 4) { int x = i + dx[k]; int y = j + dy[k]; if (!(x >= 0 && x < H && y >= 0 && y < W)) continue; if (C[i][j] == C[x][y]) uf.unite(rctoi(i, j), rctoi(x, y)); } int ans = numeric_limits().max(); rep(i, H) rep(j, W) { if (C[i][j] == '.') { chmin(ans, calc(i, j, C, uf)); } } outl(ans); return 0; }