intbfs(vector<vector<int>>& g, int sy, int sx){ int n = g.size(), m = g[0].size(), res = 1; int dy[8] = {-1,-1,-1,0,1,1,1,0}, dx[8] = {-1,0,1,1,1,0,-1,-1}; queue<pair<int, int>> q; q.push({sy, sx}); g[sy][sx] = 0; while(!q.empty()) { auto [y, x] = q.front(); q.pop(); for(int i = 0; i < 8; i++) { auto ny = y + dy[i], nx = x + dx[i]; if(0 <= ny and ny < n and0 <= nx and nx < m and g[ny][nx]) { g[ny][nx] = 0; res++; q.push({ny, nx}); } } } return res; }
intconnectedCell(vector<vector<int>> matrix){ int n = matrix.size(), m = matrix[0].size(), res = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(matrix[i][j]) { res = max(res, bfs(matrix, i, j)); } } } return res; }