Piyush and Magical Park doubt

this is giving correct answer for the sample case
but showing wrong answer for all 3 test cases

You are interpreting the question wrong, basically make changes as per this :
Inside the loop write,
if(S<K)
{
break;
}
Also they have said in question that when u move to another step, strength is reduced by one unit, this thing you havent used in your code. So, make changes as
if(ar[i][j]==’.’)
{
S=S-2;
S–;
}
And also u havent used any such specific condition for ā€˜#’ , so use :
if(ar[i][j]==’#’)
{
N=N+1;
}
else
if(ar[i][j]==’#’ && i==N-1)
{
break;
}