The code runs in time not more than 1 sec for largest constraints. I used CodeChef editor. Why am I getting tle? Although there exists solution with lesser time complexity but my solution runs within time. Help please.

The time complexity of your algorithm is O(n*m) per test case and there were 1000 test cases which makes it 10^9 in worst case which is slow for time constraints of the problem.

I thought given time limit is for individual test case. But got to know that time limit should cover all the test cases.