for i, lst in enumerate(input_matrix): # raw and list
for j, value in enumerate(lst): # column and value
s = (input_matrix[(i - 1) % M][j] + input_matrix[(i + 1) % M][j] + input_matrix[i][(j - 1) % N] +
input_matrix[i][(j + 1) % N])
print(s, end=' ')
print()