move left , up , right , down from current location and if new location is valid , then get number count of length ( k - 1 ) from that new digit and add in count we found so far
for move in range ( 5 ) : NEW_LINE INDENT ro = i + row [ move ] NEW_LINE co = j + col [ move ] NEW_LINE if ( ro >= 0 and ro <= 3 and co >= 0 and co <= 2 and keypad [ ro ] [ co ] != ' * ' and keypad [ ro ] [ co ] != ' # ' ) : NEW_LINE INDENT nextNum = ord ( keypad [ ro ] [ co ] ) - 48 NEW_LINE count [ num ] [ k ] += count [ nextNum ] [ k - 1 ] NEW_LINE DEDENT DEDENT