|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static boolean visited[][]; |
| 7 | + static int arr[][]; |
| 8 | + static int N,M,answer; |
| 9 | + static int dx[] = {-1,1,0,0}; |
| 10 | + static int dy[] = {0,0,-1,1}; |
| 11 | + |
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + |
| 15 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 16 | + |
| 17 | + N = Integer.parseInt(st.nextToken()); |
| 18 | + M = Integer.parseInt(st.nextToken()); |
| 19 | + |
| 20 | + arr = new int[N][M]; |
| 21 | + |
| 22 | + for(int i =0; i<N; i++) { |
| 23 | + st = new StringTokenizer(br.readLine()); |
| 24 | + for(int j = 0; j<M; j++) { |
| 25 | + arr[i][j] = Integer.parseInt(st.nextToken()); |
| 26 | + } |
| 27 | + } |
| 28 | + visited = new boolean[N][M]; |
| 29 | + int countAnswer = 0; |
| 30 | + answer = 0; |
| 31 | + for(int i =0; i<N; i++) { |
| 32 | + for(int j = 0; j<M; j++) { |
| 33 | + if(arr[i][j] == 1 && visited[i][j] == false) { |
| 34 | + countAnswer++; |
| 35 | + bfs(i,j); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + System.out.println(countAnswer); |
| 40 | + System.out.println(answer); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + static void bfs(int x, int y) { |
| 45 | + |
| 46 | + Queue<int[] > queue = new LinkedList<>(); |
| 47 | + queue.add(new int[] {x,y}); |
| 48 | + visited[x][y] = true; |
| 49 | + int count =1; |
| 50 | + |
| 51 | + while(!queue.isEmpty()) { |
| 52 | + |
| 53 | + int now[] = queue.poll(); |
| 54 | + int nowx = now[0]; |
| 55 | + int nowy = now[1]; |
| 56 | + for(int i =0; i<4; i++) { |
| 57 | + int nx = nowx + dx[i]; |
| 58 | + int ny = nowy + dy[i]; |
| 59 | + |
| 60 | + if(nx >=0 && nx<N && ny >=0 && ny <M) { |
| 61 | + if(visited[nx][ny]==false && arr[nx][ny]== 1) { |
| 62 | + count++; |
| 63 | + queue.add(new int[] { nx,ny}); |
| 64 | + visited[nx][ny] = true; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + answer = Math.max(answer,count); |
| 72 | + } |
| 73 | +} |
| 74 | + |
0 commit comments