For this Question: Given a binary matrix of size N x M. The task is to find the distance of nearest 1 in the matrix for each cell. The distance is calculated as |i1 – i2| + |j1 – j2|, where i1, j1 are the row number and column number of the current cell and i2, j2 are the row number and column number of the nearest cell having value 1.The first line contains two integers N and M, denoting the number of rows and columns of the matrix. Next N lines contain M integers either 0 or 1.

1 <= T <= 20
1 <= N, M <= 500
Each element of matrix is either 0 or 1
It is guaranteed that there is at least one 1 present in the input.Print N lines. Each line containing M integers denoting the distance from nearest 1.Sample Input:
3 3
1 0 0
0 1 0
0 0 0

Sample Output:
0 1 2 
1 0 1 
2 1 2, I have written this Solution Code: import java.io.*;
import java.util.*;




class Pair {
	int i;
	int j;
	Pair(int i, int j) {
		this.i = i;
		this.j = j;
	}
}

class Main {
	static boolean isValid(Pair p, int n, int m) {
		int i = p.i;
		int j = p.j;
		if(i>=0 && i<n && j>=0 && j<m){
			return true;
		}
		return false;
	}
	public static void main (String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().trim().split("\\s+");
		int N = Integer.parseInt(s[0]);
		int M = Integer.parseInt(s[1]);

		int arr[][] = new int[N][M];
		int res[][] = new int[N][M];

		Queue<Pair> q = new LinkedList<>();
		for(int i=0; i<N; i++) {
			String[] nums = br.readLine().trim().split("\\s+");
			for(int j=0; j<M; j++) {
				arr[i][j] = Integer.parseInt(nums[j]);
				if(arr[i][j] == 1) {
					res[i][j] = 0;
					q.add(new Pair(i,j));
				}
				else {
					res[i][j] = -1;
				}
			}
		}
		while(!q.isEmpty()){
			Pair p = q.remove();

			Pair[] check = {
				new Pair(p.i - 1, p.j - 0),
				new Pair(p.i - 0, p.j - 1),
				new Pair(p.i + 1, p.j + 0),
				new Pair(p.i + 0, p.j + 1)
			};
			for(int i=0; i<4; i++) {
				Pair c = check[i];
				if(isValid(c, N, M) && res[c.i][c.j] == -1){
					res[c.i][c.j] = res[p.i][p.j] + 1;
					q.add(c);
				}
			}
		}
		for(int i=0; i<N; i++) {
				for(int j=0; j<M; j++) {
					System.out.print(res[i][j] + " ");
				}
				System.out.println();
		}
	}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not?