문제
접근 방식
바깥 테두리는 무조건 공기이므로 0,0 좌표로부터 치즈가 아닌곳이 외부 공기이다.
외부공기와 2변 이상 접촉한 치즈만 1시간 후에 사라진다.
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main_2638 {
	static int N,M;
	static int[][] cheezes;
	public static void main(String[] args) 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());
		
		cheezes = new int[N][M];
		
		for(int i=0;i<N;i++) {
			st = new StringTokenizer(br.readLine());
			for(int j=0;j<M;j++) {
				cheezes[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		
		System.out.println(go());
		
		
		
	}
	
	public static int go() {
		
		int t = 0;
		
		while(cheezeCnt()!=0) {
			int[][] contactAir = bfs();
			meltCheeze(contactAir);
			t++;
		}
		
		return t;
	}
	
	public static void meltCheeze(int[][] contactAir) {
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				if(contactAir[i][j] >= 2)
					cheezes[i][j] = 0;
			}
		}
	}
	
	public static int cheezeCnt() {
		int cnt = 0;
		
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				if(cheezes[i][j] == 1)
					cnt++;
			}
		}
		
		return cnt;
	}
	
	public static class Node{
		int r;
		int c;
		
		Node(int r, int c){
			this.r = r;
			this.c = c;
			
		}
	}
	
	static int[] dr = {0,1,0,-1};
	static int[] dc = {1,0,-1,0};
	public static int[][] bfs() {
		
		// 치즈가 외부와 접촉한 변의 개수
		int[][] temp = new int[N][M];
		
		Queue<Node> q = new LinkedList<>();
		q.add(new Node(0,0));
		
		boolean[][] visited = new boolean[N][M];
		visited[0][0] = true;
		
		while(!q.isEmpty()) {
			Node node = q.poll();
			
			for(int i=0;i<4;i++) {
				int nr = node.r + dr[i];
				int nc = node.c + dc[i];
				
				if(nr<0 || nr>=N || nc<0 || nc>=M || visited[nr][nc]) continue;
				
				// 다음 위치가 치즈이면
				if(cheezes[nr][nc] == 1) 
					temp[nr][nc]++;
				else {
					visited[nr][nc] = true;
					q.add(new Node(nr,nc));
				}
			}
		}
		
		return temp;
	}
}