枚举k * k 的位置, 然后接上它周围白色连通块的数量, 再统计完全在k * k范围里的连通块, 这个只要某个连通块全部的方格
在k * k里面就好, 并且k * k是一行一行移的, 所以可以优化到n ^ 3。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, k, idx, now, id[N][N], sum[N][N];
bool vis[N * N];
char Map[N][N]; int fa[N * N], cnt[N * N], num[N * N]; int getRoot(int x) {
return fa[x] == x ? x : fa[x] = getRoot(fa[x]);
} void change(int x, int op) {
x = getRoot(x);
if(op == ) {
num[x]--;
if(!num[x]) now += cnt[x];
} else {
if(!num[x]) now -= cnt[x];
num[x]++;
}
}
int main() {
scanf("%d%d", &n, &k);
for(int i = ; i <= n * n; i++) fa[i] = i;
for(int i = ; i <= n; i++)
scanf("%s", Map[i] + );
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(Map[i][j] == '.') id[i][j] = ++idx, cnt[idx] = ;
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
if(Map[i][j] != '.') continue;
if(Map[i - ][j] == '.') {
int x = getRoot(id[i][j]);
int y = getRoot(id[i - ][j]);
if(x != y) fa[y] = x, cnt[x] += cnt[y];
}
if(Map[i][j - ] == '.') {
int x = getRoot(id[i][j]);
int y = getRoot(id[i][j - ]);
if(x != y) fa[y] = x, cnt[x] += cnt[y];
}
}
}
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
sum[i][j] = sum[i - ][j] + sum[i][j - ] - sum[i - ][j - ] + (Map[i][j] == 'X');
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(Map[i][j] == '.') num[getRoot(id[i][j])]++; int ans = ;
queue<int> que; for(int i = ; i + k - <= n; i++) {
for(int j = ; j + k - <= n; j++) {
if(j == ) {
for(int u = i; u < i + k; u++)
for(int v = j; v < j + k; v++)
change(id[u][v], );
}
int ret = ;
if(i - > ) {
for(int z = j; z <= j + k - ; z++) {
if(Map[i - ][z] != '.') continue;
int x = getRoot(id[i - ][z]);
if(!vis[x]) {
vis[x] = true;
que.push(x);
ret += cnt[x];
}
}
}
if(i + k <= n) {
for(int z = j; z <= j + k - ; z++) {
if(Map[i + k][z] != '.') continue;
int x = getRoot(id[i + k][z]);
if(!vis[x]) {
vis[x] = true;
que.push(x);
ret += cnt[x];
}
}
} if(j - > ) {
for(int z = i; z <= i + k - ; z++) {
if(Map[z][j - ] != '.') continue;
int x = getRoot(id[z][j - ]);
if(!vis[x]) {
vis[x] = true;
que.push(x);
ret += cnt[x];
}
}
}
if(j + k <= n) {
for(int z = i; z <= i + k - ; z++) {
if(Map[z][j + k] != '.') continue;
int x = getRoot(id[z][j + k]);
if(!vis[x]) {
vis[x] = true;
que.push(x);
ret += cnt[x];
}
}
}
while(!que.empty()) {
vis[que.front()] = false;
que.pop();
}
ans = max(ans, now + ret + sum[i + k - ][j + k - ] - sum[i - ][j + k - ] - sum[i + k - ][j - ] + sum[i - ][j - ]);
if(j + k <= n) {
for(int z = i; z < i + k; z++) change(id[z][j], -);
for(int z = i; z < i + k; z++) change(id[z][j + k], );
} else {
for(int u = i; u < i + k; u++)
for(int v = j; v < j + k; v++)
change(id[u][v], -);
}
}
}
printf("%d\n", ans);
return ;
} /*
*/