C++ 代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 55,
dx[5] = {0, 0, -1, 0, 1},
dy[5] = {0, -1, 0, 1, 0};
int n, m, a[N][N];
PII q[N * N];
bool b[N][N];
int bfs(int h1, int h2)
{
int l = 0, r = 0;
q[r] = {h1, h2};
b[h1][h2] = 1;
while (l <= r)
{
PII hhh = q[l++];
for (int i = 1; i <= 4; i++)
{
int xx = hhh.x + dx[i], yy = hhh.y + dy[i];
if (xx < 1 || yy < 1 || xx > n || yy > m || b[xx][yy] == 1)
continue;
if (a[hhh.x][hhh.y] >> (i - 1) & 1)
continue;
q[++r] = {xx, yy};
b[xx][yy] = 1;
}
}
return r + 1;
}
int main()
{
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%d", &a[i][j]);
memset(b, 0, sizeof(b));
int cnt = 0, area = -1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (b[i][j] == 0)
area = max(area, bfs(i, j)), cnt++;
printf("%d\n%d\n", cnt, area);
return 0;
}