Problem:
Score : 1000 points

Problem Statement

We will host a rock-paper-scissors tournament with N people. The participants are called Person 1, Person 2, \ldots, Person N.
For any two participants, the result of the match between them is determined in advance.
This information is represented by positive integers A_{i,j} ( 1 \leq j < i \leq N ) as follows:

If A_{i,j} = 0, Person j defeats Person i.

If A_{i,j} = 1, Person i defeats Person j.

The tournament proceeds as follows:

We will arrange the N participants in a row, in the order Person 1, Person 2, \ldots, Person N from left to right.

We will randomly choose two consecutive persons in the row. They will play a match against each other, and we will remove the loser from the row.
We will repeat this process N-1 times, and the last person remaining will be declared the champion.

Find the number of persons with the possibility of becoming the champion.

Constraints

1 \leq N \leq 2000

A_{i,j} is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
A_{2,1}
A_{3,1}A_{3,2}
:
A_{N,1}\ldotsA_{N,N-1}

Output

Print the number of persons with the possibility of becoming the champion.

Sample Input 1

3
0
10

Sample Output 1

2

Person 1 defeats Person 2, Person 2 defeats Person 3 and Person 3 defeats Person 1.
If Person 1 and Person 2 play the first match, Person 3 will become the champion.
If Person 2 and Person 3 play the first match, Person 1 will become the champion.

Sample Input 2

6
0
11
111
1111
11001

Sample Output 2

3

Solution in Rust (1.15.1) (judge verdict: Accepted)

// https://atcoder.jp/contests/m-solutions2019/tasks/m_solutions2019_f
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

use std::ops::{Index,IndexMut};
use std::ops::{Shl,Shr,BitOrAssign};


#[derive(Clone, Debug)]
struct BitVector {
    n: usize,
    size: usize,
    data: Vec<u64>
}

impl BitVector {
    fn new(n: usize) -> Self {
        let size = (n+63)/64;
        BitVector {
            n: n,
            size: size,
            data: vec![0; size]
        }
    }

    fn get(&self, i: usize) -> bool {
        self.data[i/64]>>(i%64) & 1 != 0
    }

    fn set(&mut self, i: usize) {
        self.data[i/64] |= 1<<(i%64);
    }
}

impl<'a> Shl<usize> for &'a BitVector {
    type Output = BitVector;

    fn shl(self, amount: usize) -> Self::Output {
        let mut new_vector = BitVector::new(self.n);

        let diff = amount / 64;
        let diff_mod = amount % 64;
        if diff_mod == 0 {
            for i in 0..self.size-diff {
                new_vector.data[i+diff] = self.data[i];
            }
        } else {
            let lower_mask = (1<<(64-diff_mod))-1;
            let upper_mask = ((1<<diff_mod)-1)<<(64-diff_mod);
            for i in 0..self.size-diff {
                new_vector.data[i+diff] |= (self.data[i] & lower_mask)<<diff_mod;
                if i+diff+1 < self.size {
                    new_vector.data[i+diff+1] |= (self.data[i] & upper_mask)>>(64-diff_mod);
                }
            }
        }
        new_vector
    }
}

impl BitOrAssign for BitVector {
    fn bitor_assign(&mut self, rhs: Self) {
        for i in 0..self.size {
            self.data[i] |= rhs.data[i];
        }
    }
}

// ====

fn check(from: usize, to: usize, win_vector: &Vec<BitVector>, dp_left: &Vec<BitVector>, dp_left0: &Vec<BitVector>, dp_left_row: &Vec<BitVector>, dp_right: &Vec<BitVector>) -> bool {
    let v = win_vector[0].data.len();
    for i in 0..v {
        if dp_left0[from].data[i] & win_vector[from].data[i] & dp_left_row[to].data[i] >= 1 {
            // debug!("found-l", from, to);
            return true;
        }
        if dp_left0[from].data[i] & dp_right[to].data[i] >= 1 && win_vector[from].get(to) {
            // debug!("found-r", from, to);
            return true;
        }
        if dp_left[from].data[i] & dp_left_row[to].data[i] >= 1 {
            return true;
        }
    }
    false
}

fn main() {
    input! {
        n: usize,
        a: [chars; n-1]
    };

    let mut table = dvec!(false; n, n);
    for i in 0..n {
        for j in 0..i {
            table[i][j] = ifv!(a[i-1][j] == '0', false, true);
            table[j][i] = table[i][j] ^ true;
        }
        table[i][i] = true;
    }

    let mut win_vector = vec![BitVector::new(n); n];
    for i in 0..n {
        for j in 0..n {
            if table[i][j] {
                win_vector[i].set(j);
            }
        }
    }

    let mut dp_left = vec![BitVector::new(n); n];
    let mut dp_right = vec![BitVector::new(n); n];
    let mut dp_left_row = vec![BitVector::new(n); n];
    let mut dp_right_row = vec![BitVector::new(n); n];
    let mut dp_left0 = vec![BitVector::new(n); n];
    let mut dp_right0 = vec![BitVector::new(n); n];
    for i in 0..n {
        dp_left[i].set(i);
        dp_right[i].set(i);
        if i >= 1 {
            dp_left0[i].set(i-1);
        }
        if i+1 < n {
            dp_right0[i].set(i+1);
        }
        dp_left_row[i].set(i);
        dp_right_row[i].set(i);
    }

    for d in 1..n {
        for i in 0..n {
            if i >= d {
                if check(i, i-d, &win_vector, &dp_left, &dp_left0, &dp_left_row, &dp_right) {
                    dp_left[i].set(i-d);
                    if i-d >= 1 {
                        dp_left0[i].set(i-d-1);
                    }
                    dp_left_row[i-d].set(i);
                }
            }
            if i+d < n {
                if check(i, i+d, &win_vector, &dp_right, &dp_right0, &dp_right_row, &dp_left) {
                    dp_right[i].set(i+d);
                    if i+d+1 < n {
                        dp_right0[i].set(i+d+1);
                    }
                    dp_right_row[i+d].set(i);
                }
            }
        }
    }

    let mut count = 0;
    // for i in 0..n {
    //     for j in 0..n {
    //         let b = if i < j {
    //             dp_right[i].get(j)
    //         } else {
    //             dp_left[i].get(j)
    //         };
    //         debug!(i, j, b);
    //     }
    // }

    for i in 0..n {
        if dp_left[i].get(0) && dp_right[i].get(n-1) {
            count += 1;
        }
    }
    println!("{}", count);

}