Arrays-Wave print column wise

When i run the code, the custom input works fine, but upon submission it shows runtime error.
What went wrong?

Code:

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int m = scn.nextInt();
int n = scn.nextInt();
int[][] arr = new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
arr[i][j]=scn.nextInt();
}
System.out.println();
}

	for(int j = 0;j<n;j++){
		if(j%2==0){
			for(int i =0;i<m;i++){
				System.out.print(arr[i][j]+", ");
			}
		}else{
			for(int i=n-1;i>=0;i--){
				System.out.print(arr[i][j]+", ");
			}
		}
	}
	System.out.print("END");
}

}

@Raunak-Agrawal-1537545869717573,
https://ide.codingblocks.com/s/279555 corrected code.

Error:
You have written: for(int i=n-1;i>=0;i--){

instead of: for (int i = m - 1; i >= 0; i--) {

Hence the runtime error.

Thanks got it.
But it was still giving correct output while entering the custom input mentioned in the question.

@Raunak-Agrawal-1537545869717573,
That’s because the test input is a 4x4 matrix. Hence m and n are equal. Your code was giving a Run time error in case m!=n

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.