import operator
import itertools


def grid_operation_in_dir(arr, k, x_dir, y_dir, operation=operator.mul, initial_value=1):
    """
    Find max product in given direction.
    :param arr: square matrix with numbers
    :param k: product length
    :param x_dir: -1, 0, 1
    :param y_dir: -1, 0, 1
    :param operation
    :param initial_value for operator
    :return: max value for given operation with k elements in given direction
    """
    max_p = 0
    x_size = abs((k - 1) * x_dir)
    y_size = abs((k - 1) * y_dir)
    for x, y in itertools.product(range(len(arr)-x_size), range(len(arr)-y_size)):
        p = initial_value
        for i in range(k):
            p = operation(p, arr[(x + i * x_dir) % len(arr)][(y + i * y_dir) % len(arr)])
        if p > max_p:
            max_p = p
    return max_p