B.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MXAN = 200 + 10;
  4. int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  5. int a[MXAN][MXAN];
  6. int sum[MXAN][MXAN];
  7. int n, m;
  8. int Bfs(int x, int y)
  9. {
  10. int res = 0;
  11. queue<pair<int, int> > que;
  12. que.push(make_pair(x, y));
  13. a[x][y] = 0;
  14. while (!que.empty())
  15. {
  16. res++;
  17. int x = que.front().first;
  18. int y = que.front().second;
  19. que.pop();
  20. for (int i = 0;i < 4;i++)
  21. {
  22. int tx = x + Next[i][0];
  23. int ty = y + Next[i][1];
  24. if (a[tx][ty] == 1)
  25. {
  26. a[tx][ty] = 0;
  27. que.push(make_pair(tx, ty));
  28. }
  29. }
  30. }
  31. return res;
  32. }
  33. int main()
  34. {
  35. int t;
  36. cin >> t;
  37. while (t--)
  38. {
  39. cin >> n >> m;
  40. for (int i = 1;i <= n;i++)
  41. for (int j = 1;j <= m;j++)
  42. cin >> a[i][j];
  43. int res = 0;
  44. for (int i = 1;i <= n;i++)
  45. for (int j = 1;j <= m;j++)
  46. {
  47. if (a[i][j] == 1)
  48. {
  49. res = max(res, Bfs(i, j));
  50. }
  51. }
  52. cout << res << endl;
  53. }
  54. return 0;
  55. }