public double expectedTime(int N, int M, int goalX, int goalY) {
        double[][] current = new double[N][M];

        current[0][0] = 1.0;
        double goal = 0.0;
        for (int day = 1; day < 100000; day++) {
            double[][] next = new double[N][M];
            for (int x = 0; x < N; x++) {
                for (int y = 0; y < M; y++) {
                    double e = 0.5 * current[(x + N - 1) % N][(y + M - 1) % M] +
                                0.5 * current[(x + 1) % N][(y + 1) % M];

                    if (x == goalX && y == goalY) {
                        goal += day * e;
                    } else {
                        next[x][y] += e;
                    }

                }
            }
            current = next;
        }
        if (goal == 0.0) {
            return -1;
        }
        return goal;

    }