fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void dfs(int x,int y,int c, int w[][201], int g[][201], int *dx, int *dy){
  4. w[x][y] = c;
  5. for(int i=0; i<8;i++){
  6. int nx = x+dx[i], ny = y+dy[i];
  7. if(g[nx][ny] && !w[nx][ny]) dfs(nx,ny,c,w,g,dx,dy);
  8. }
  9. }
  10.  
  11. int main(){
  12. int row, col, set = 1,t;
  13. cin >> t;
  14. while(t--)
  15. {
  16. int g[201][201] = {0};
  17. int w[201][201] = {0};
  18. int dx[8] = {-1,0,1,1,1,0,-1,-1};
  19. int dy[8] = {1,1,1,0,-1,-1,-1,0};
  20. scanf("%d%d", &row, &col);
  21.  
  22. for(int i=1; i<=row; i++)
  23. for(int j=1; j<=col; j++)
  24. scanf("%d", &g[i][j]);
  25.  
  26. for(int i=1; i<=row;i++)
  27. for(int j=1; j<=col; j++)
  28. if(g[i][j] && !w[i][j])
  29. dfs(i,j,set++,w,g,dx,dy);
  30.  
  31. unordered_map<int,int> mp;
  32. for(int i=1; i<=row;i++){
  33. for(int j=1; j<=col; j++){
  34. if (w[i][j]) mp[w[i][j]]++;
  35. }
  36. }
  37. int ans = 0;
  38. for(auto it=mp.begin();it!=mp.end();it++){
  39. ans = max(ans,it->second);
  40. }
  41. printf("%d\n",ans);
  42. }
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 15432KB
stdin
1
5 5
0 0 1 1 0
0 1 0 0 0
1 1 1 0 0
0 0 0 0 1
1 0 0 0 0
stdout
6