```
public int countBinarySubstrings(String s) {
int n = s.length();
boolean[][] dp = new boolean[n + 1][n + 1];
int cnt = 0;
for (int i = n; i > 0; i--) {
for (int j = i + 1; j <= n; j++) {
if ((s.charAt(i - 1) ^ s.charAt(j - 1)) == 1 && (j - i < 2 || (dp[i + 1][j - 1] && s.charAt(i - 1) == s.charAt(i)))) {
dp[i][j] = true;
cnt++;
}
}
}
return cnt;
}
```