def Result for in range for in range print 5d end print def Rec global

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def Result(a):
for i in range(2, m + 2):
for j in range(2, n+2):
print("%5d" %a[i][j], end = '')
print()
def Rec(i,j,t,a):
global q
if a[i][j] != 0:
return
else:
a[i][j] = t
if t == m*n:
Result(a)
q = True
return
else:
for k in range(8):
Rec(i+di[k], j+dj[k], t+1, a)
a[i][j] = 0
m = int(input())
n = int(input())
print()
di = [-2,-1, 1, 2, 2, 1,-1,-2]
dj = [ 1, 2, 2, 1,-1,-2,-2,-1]
A = [[0] * (n+4) for i in range(m+4)]
for i in range(m+4):
for j in range(n + 4):
if i<2 or j < 2 or i > m+1 or j > n+1:
A[i][j] = -1
q = False
Rec(2,2,1,A)
if not q : print('Нельзя пройти все клетки')