Runtime error (UVA)

Então galera, é o seguinte. O programa tem que resolver uma sequência de conversão de moedas especificadas, quando eu rodo no prompt, ele retorna certinho, porém quando eu envio pro uva judge (104), ele retorna runtime error. Algúem pode me ajudar? Segue meu codigo a baixo:

import java.util.Scanner;

public class Main {

    static int MAX = 20;
    static double LUCRO_MIN = 1.01;
        	
    public static void main(String[] args) {
        int n, i, j;
        double v;
        Scanner sc = new Scanner(System.in);
        
        while(sc.hasNext()) {
            double[][] W = new double [MAX][MAX];
            n = sc.nextInt();
            sc.nextLine();
            for(i = 0; i < n; i++) {
                int pos = 0;
                String linha = sc.nextLine();
                String vertice[] = linha.split(" ");
                for(j = 0; j < n; j++) {
                    if(i == j) {
                        W[i][i] = 0;
                        continue;
                    }
                    v = Double.parseDouble(vertice[pos]);
                    W[i][j] = v;
                    pos++;
            }
          }
        Converte(n, W);
      }
    }

    static public void Imprime (int i, int j, int l, int P[][][]) {
      if(l == 0)
        System.out.printf ("%d\n", i + 1);
      else {
        System.out.printf ("%d ", i + 1);
        int x = P[l][i][j];
        Imprime (x, j, l - 1, P);
      }
    }

    static void IniZero (int n, double m[][][], int l) {
      int i, j;
      for(i = 0; i < n; i++)
        for(j = 0; j < n; j++)
          m[l][i][j] = 0;
    }

    static void Converte(int n, double W[][]) {
      double[][][] B = new double [MAX][MAX][MAX];
      int [][][]P = new int [MAX][MAX][MAX];
      int l, i, j, k;
      for(i = 0; i < n; i++)
        for(j = 0; j < n; j++) {
          B[1][i][j] = W[i][j];
          if(W[i][j] > 0)
            P[1][i][j] = j;
        }
      for(l = 2; l <= n; l++) {
        IniZero(n, B, l);
        for(i = 0; i < n; i++)
          for(j = 0; j < n; j++)
            for(k = 0; k < n; k++) {
              if(B[l][i][j] < W[i][k] * B[l-1][k][j]) {
                B[l][i][j] = W[i][k] * B[l-1][k][j];
                P[l][i][j] = k;
                if(B[l][i][i] >= LUCRO_MIN) {
                  Imprime(i, i, l, P);
                  return;
                }
              }
            }
      }
      System.out.printf("no arbitrage sequence exists\n");
    }
}