Fork Copy package ngay6; import java.util.Scanner; public class bai3 { static int[][] map = new int[101][101]; static int[][] time = new int[101][101]; static int[] dx = {0, 0, 1, -1}; static int[] dy = {1, -1, 0, 0}; static int n, m; static void BFS(int x, int y) { int[][] queue = new int[101*101][2]; int front = 0, rear = 0; if(map[x][y] == 1) { queue[rear][0] = x; queue[rear][1] = y; time[x][y] = 0; rear++; } while(front < rear) { int cx = queue[front][0]; int cy = queue[front][1]; front++; for(int d = 0; d < 4; d++) { int nx = cx + dx[d]; int ny = cy + dy[d]; if(nx >= 0 && nx < n && ny >= 0 && ny < m) { if(map[nx][ny] == 1 && time[nx][ny] == -1){ time[nx][ny] = time[cx][cy] + 1; queue[rear][0] = nx; queue[rear][1] = ny; rear++; } } } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int tc = 1; tc <= t; tc++) { m = sc.nextInt(); n = sc.nextInt(); for(int i = 0; i < 101; i++) { for(int j = 0; j < 101; j++) { time[i][j] = -1; } } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { map[i][j] = sc.nextInt(); } } int y = sc.nextInt(); int x = sc.nextInt(); BFS(x, y); int maxTime = 0; boolean fl = true; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(map[i][j] == 1) { if(time[i][j] == -1) fl = false; else maxTime = Math.max(maxTime, time[i][j]); } } } System.out.println(maxTime); } } }