You seem to be creating an InputStreamReader on System.in twice - I presume that is causing the problem.
You seem to be doing a few other strange things in your code as well - eg the if condition in main is completely unnecessary as you are guaranteed the input will lie within those bounds. And there is no need to store all of the input in an array before processing it - that will suffice here but in some problems that will cause you to exceed the memory limit, so it's not advisable.
Anyway, if you've tested your code on the largest possible input file (10 cases of the largest size), you'll know this approach won't fit inside the time limit anyway, so you'll need to think of an improved algorithm.