SPOJ BITMAP. NZEC in JAVA.

EDIT: THIS ( http://ideone.com/Jyjgvi ) IS A MODIFIED CODE,BETTER AND CLEANER THAN THE PREVIOUS ONE. PLEASE HAVE A LOOK. THE OLDER ONE IS GIVEN BELOW. EVEN THIS MODIFIED CODE GETS NZEC. PLEASE LET ME KNOW WHAT IS GOING WRONG.

here is the problem:

I’m not using any graph traversal techniques to solve this one like bfs/dfs.


My procedure:
I’m using a separate matrix that stores the distances and that will be printed as the final answer.
In the separate matrix,the places of the 1’s(from the input matrix) will be filled with 0’s. then all the cells adjacent to the 0’s in the separate matrix will be filled with 1’s. similarly all the cells adjacent to the 1’s in the separate matrix will be filled with 2’s and so on…


here is my attempt: http://ideone.com/9wWGHX . This gets an NZEC :frowning: Can any one tell me what is wrong and how it can be fixed?

In case the above link does not work,here I post my code:

			import java.util.*;
			import java.io.*;
			class spoj_BITMAP
			{
				static int ans_arr[][];
				static ArrayList<Integer> x=new ArrayList<Integer>();
				static ArrayList<Integer> y=new ArrayList<Integer>();
				
				public static void main(String args[])
				throws IOException
				{
					BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
					PrintWriter pw=new PrintWriter(System.out);
					int t=Integer.parseInt(br.readLine());
					while(t--!=0)
					{
						String in[]=br.readLine().split(" ");
						int n=Integer.parseInt(in[0]);
						int m=Integer.parseInt(in[1]);
						char arr[][]=new char[n][];
						for(int i=0;i<n;i++)
							arr[i]=br.readLine().trim().toCharArray();
						
						ans_arr=new int[n][m];
						for (int[] row: ans_arr)
						    Arrays.fill(row, -1);
						
						
						for(int i=0;i<n;i++)
						{
							for(int j=0;j<m;j++)
							{
								if(arr[i][j]=='1'){
									ans_arr[i][j]=0;
									x.add(i);
									y.add(j);
								}
							}
						}
						arrange();
						
						for(int i=0;i<n;i++)
						{
							for(int j=0;j<m;j++)
								System.out.print(ans_arr[i][j]+" ");
							System.out.println();
						}
							
						
					}
				}
				
				
				static void arrange()
				{
					while(!x.isEmpty() && !y.isEmpty())
					{
						int i=x.get(0);
						int j=y.get(0);
						
						x.remove(0);
						y.remove(0);
						//

						try{
							if(ans_arr[i-1][j]==-1){
							ans_arr[i-1][j]=ans_arr[i][j]+1;
							x.add(i-1);
							y.add(j);
							}
						}
						catch(ArrayIndexOutOfBoundsException ex) {}
                        
						try{
							if(ans_arr[i+1][j]==-1){
							ans_arr[i+1][j]=ans_arr[i][j]+1;
							x.add(i+1);
							y.add(j);
							}
						}
						catch(ArrayIndexOutOfBoundsException ex){}
						try{
							if(ans_arr[i][j-1]==-1){
							ans_arr[i][j-1]=ans_arr[i][j]+1;
							x.add(i);
							y.add(j-1);
							}
						}
						catch(ArrayIndexOutOfBoundsException ex){}
                        
						try{
							if(ans_arr[i][j+1]==-1){
							ans_arr[i][j+1]=ans_arr[i][j]+1;
							x.add(i);
							y.add(j+1);
							}
						}
						
						catch(ArrayIndexOutOfBoundsException ex){}
                                                 
						//
					}
					return;
				}
			}