Iterate over the direction vector
for it in directions : NEW_LINE INDENT I = i + it [ 0 ] NEW_LINE J = j + it [ 1 ] NEW_LINE if ( isInside ( I , J , N , M ) ) : NEW_LINE INDENT if ( mat [ I ] [ J ] == 0 ) : NEW_LINE DEDENT DEDENT