Matrix Gym Ninja has put on some extra weight during lockdown. He has decided to join a gym to shed

import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

    int N = scanner.nextInt();
    scanner.nextLine(); // Consume the newline character

    String[] grid = scanner.nextLine().split(" ");
    int calories = scanner.nextInt();

    int[][] dp = new int[N][N];
    dp[0][0] = 5;

    try {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (grid[i].charAt(j) == '#') {
                    dp[i][j] = 0;
                } else {
                    if (i > 0) {
                        dp[i][j] = Math.max(dp[i][j], dp[i - 1][j] + 5);
                    }
                    if (j > 0) {
                        dp[i][j] = Math.max(dp[i][j], dp[i][j - 1] + 5);
                    }
                }
            }
        }
    } catch (StringIndexOutOfBoundsException e) {
        System.out.println("No");
        scanner.close();
        return;
    }

    if (dp[N - 1][N - 1] >= calories) {
        int extraCalories = dp[N - 1][N - 1] - calories;
        if (extraCalories == 0) {
            System.out.println("Yes");
        } else {
            System.out.println("Yes " + extraCalories);
        }
    } else {
        System.out.println("No");
    }

    scanner.close();
}

}

some test cases are failing and some show runtime errors