Piyush and magical park 2 test cases wrong

import java.util.Scanner;

public class PiyushAndPark {
static Scanner scn = new Scanner(System.in);

public static void main(String[] args) {
	int N = scn.nextInt();
	int M = scn.nextInt();
	int K = scn.nextInt();
	int S = scn.nextInt();

	char[][] park = null;
	park = takeInput(N, M);

	int energyLeft = findEnergyLeft(park, N, M, S);
	if (K <= energyLeft) {
		System.out.println("Yes");
	} else {
		System.out.println("No");
	}
	System.out.println(energyLeft);
}

public static char[][] takeInput(int N, int M) {
	char[][] park = new char[N][M];
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			park[i][j] = scn.next().charAt(0);
		}
	}
	return park;
}

public static int findEnergyLeft(char[][] park, int N, int M, int S) {

	int i = 0, j = 0;
	while (i < N) {
		while (j < M) {
			//System.out.print(i + "*" + j + "= ");
			if (j == 0) {
				if (park[i][j] == '.') {
					S = S - 2;
				} else if (park[i][j] == '*') {
					S = S + 5;
				} else if (park[i][j] == '#') {
					//System.out.println(S);
					break;
				}
			} else {
				S = S - 1;
				if (park[i][j] == '.') {
					S = S - 2;
				} else if (park[i][j] == '*') {
					S = S + 5;
				} else if (park[i][j] == '#') {
					//System.out.println(S);
					break;
				}
			}
			j++;
			//System.out.println(S);
		}
		i++;
		j=0;
	}
	return S;
}

}

Hey @Abhishek-Verma-1947662505352705
Your code does not work for the cases when Piyush can’t escape.

Eg. when the input is:

4 4 5 10
. . . .
. # . .
* * * *
* # * *

Your output is:
Yes
17

But the correct answer is
No

Try to attempt the question again.

Hi Abhishek, as you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.