import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
/**
* 0,0 에서 BFS 탐색 시작(방문하지 않고, 0인 지점에서 시작)
* 0이면 큐에 넣고 계속 탐색, 방문처리
* 1이면 해당 위치를 0으로 바꾸고 방문처리, 큐에 넣지 않음
* 방문배열 초기화
* 완료 체크
*/
public class Main {
static class Pos {
int r;
int c;
public Pos(int r, int c) {
this.r = r;
this.c = c;
}
}
static int[] dr = {-1, 1, 0, 0};
static int[] dc = {0, 0, -1, 1};
static int N, M, time, cnt;
static int[][] map;
static boolean[][] used;
public static void main(String[] args) throws IOException {
init();
process();
print();
}
private static void init() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new int[N][M];
time = 1;
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
}
private static void process() {
while (true) {
used = new boolean[N][M];
bfs();
if (checkIsDone()) break;
cnt = 0;
time++;
}
}
private static void print() {
System.out.println(time);
System.out.println(cnt);
}
private static boolean checkIsDone() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j] != 0) return false;
}
}
return true;
}
private static void bfs() {
Queue<Pos> q = new LinkedList<>();
q.add(new Pos(0, 0));
used[0][0] = true;
while (!q.isEmpty()) {
Pos now = q.poll();
for (int i = 0; i < 4; i++) {
int nr = now.r + dr[i];
int nc = now.c + dc[i];
if (isOutOfRange(nr, nc) || used[nr][nc]) continue;
if (map[nr][nc] == 1) {
map[nr][nc] = 0;
used[nr][nc] = true;
cnt++;
continue;
}
used[nr][nc] = true;
q.add(new Pos(nr, nc));
}
}
}
private static boolean isOutOfRange(int nr, int nc) {
return nr < 0 || nc < 0 || nr >= N || nc >= M;
}
}
0, 0
지점에서부터 BFS 탐색을 수행해야함0
은 공기, 1
은 치즈로 표현time
, 다 녹기 전의 치즈의 개수는 cnt
에 담아 저장while
문으로 무한 루프를 돌리고, BFS 탐색이 끝날 때마다 모든 치즈가 녹았는지를 확인cnt
값을 초기화하고, time
값을 증가시킴1
이라면 해당 위치를 0
으로 바꾸고, cnt
값 증가와 방문처리를 한다.0
이라면 해당 위치를 큐에 넣고 방문처리를 한다.