Is testdata correct?
frustrating runtime errors
yes,please use while(scanf(\"%d%d\", &n, &m) != EOF) to read the datas, 
Testdata CONTAINS such N,M that N <= 0 || M <= 0 !!

while(scanf(\"%d%d\",&N,&M) != EOF)
{
	if(N <= 0 || M <= 0)
	{
		while(true);
	}
got a TLE!
© 2015 HUST ACMICPC TEAM. All Right Reserved.