Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, so if there is no queen and we do nothing. Only when we meet the queen position we take sum. So, for each direction we only take n sum function, even though we refresh sum to 0 at the start of each line.