#include <bits/stdc++.h>
using namespace std;
#define int long long
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int comp[4001][4001] = {};
char arr[4001][4001];
int n, m;
bool check (int i, int j) {
return (i >= 1 && i <= n && j >= 1 && j <= m && arr[i][j] != '.');
}
void dfs (int i, int j) {
for (int l = 0; l < 4; l++) {
if (check(i + dx[l], j + dy[l]) && comp[i + dx[l]][j + dy[l]] == 0 && arr[i + dx[l]][j + dy[l]] == arr[i][j]) {
comp[i + dx[l]][j + dy[l]] = comp[i][j];
dfs(i + dx[l], j + dy[l]);
}
}
}
vector <int> adj[160000001];
bool vis[160000001];
signed main () {
memset(vis, false, sizeof(vis));
cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> arr[i][j];
int c = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (comp[i][j] == 0 && arr[i][j] != '.') {
c++;
comp[i][j] = c;
dfs(i, j);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (arr[i][j] == '.') continue;
for (int l = 0; l < 4; l++) {
if (check(i + dx[l], j + dy[l])) {
if (comp[i + dx[l]][j + dy[l]] != comp[i][j]) {
adj[comp[i][j]].push_back(comp[i + dx[l]][j + dy[l]]);
adj[comp[i + dx[l]][j + dy[l]]].push_back(comp[i][j]);
}
}
}
}
}
queue <int> cur;
cur.push(comp[1][1]); vis[comp[1][1]] = 1;
int cnt = 1;
int ans = 0;
while (!cur.empty()) {
auto u = cur.size(); while (u--) {
int k = cur.front(); cur.pop();
ans = cnt;
for (auto j : adj[k]) if (!vis[j]) {
vis[j] = 1; cur.push(j);
}
}
cnt++;
}
cout << ans << '\n';
}
Compilation message
/tmp/ccePa49d.o: in function `check(long long, long long)':
tracks.cpp:(.text+0x5e): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccePa49d.o
tracks.cpp:(.text+0x75): relocation truncated to fit: R_X86_64_PC32 against symbol `m' defined in .bss section in /tmp/ccePa49d.o
tracks.cpp:(.text+0x85): relocation truncated to fit: R_X86_64_PC32 against symbol `arr' defined in .bss section in /tmp/ccePa49d.o
/tmp/ccePa49d.o: in function `dfs(long long, long long)':
tracks.cpp:(.text+0xb0): relocation truncated to fit: R_X86_64_PC32 against symbol `arr' defined in .bss section in /tmp/ccePa49d.o
tracks.cpp:(.text+0xf4): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccePa49d.o
tracks.cpp:(.text+0x123): relocation truncated to fit: R_X86_64_PC32 against symbol `m' defined in .bss section in /tmp/ccePa49d.o
tracks.cpp:(.text+0x142): relocation truncated to fit: R_X86_64_PC32 against symbol `comp' defined in .bss section in /tmp/ccePa49d.o
tracks.cpp:(.text+0x173): relocation truncated to fit: R_X86_64_PC32 against symbol `arr' defined in .bss section in /tmp/ccePa49d.o
/tmp/ccePa49d.o: in function `main':
tracks.cpp:(.text.startup+0x3b): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/ccePa49d.o
tracks.cpp:(.text.startup+0x42): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
tracks.cpp:(.text.startup+0x4e): additional relocation overflows omitted from the output
/usr/bin/ld: failed to convert GOTPCREL relocation; relink with --no-relax
collect2: error: ld returned 1 exit status