let count_paths m n =
  let dp = Array.make ((m + 1) * (n + 1)) 0 in
  dp.(0) <- 1;
  for i = 0 to m do
    for j = 0 to n do
      if i > 0 || j > 0 then begin
        let above = if i > 0 then dp.((i - 1) * (n + 1) + j) else 0 in
        let left = if j > 0 then dp.(i * (n + 1) + j - 1) else 0 in
        dp.(i * (n + 1) + j) <- above + left
      end
    done
  done;
  dp.(m * (n + 1) + n)

;;

count_paths 4 6
