public int longestCommonSubstring(String A, String B) {
        if (A.length() == 0 || B.length() == 0)
            return 0;
        int[][] matrix = new int[A.length() +1][B.length() + 1];
        int max = -1;
        for (int i =1; i <= A.length() ; ++i) {
            for (int j = 1; j <= B.length(); ++j) {
                if (A.charAt(i-1) == B.charAt(j-1)) {
                    matrix[i][j] = matrix[i-1][j-1] + 1;
                    max = Math.max(matrix[i][j], max);
                }
            }
        }
        return max;
    }