You are given an empty grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). You should fill this grid with integers in a way that satisfies the following rules:
- For any three cells $c_1$, $c_2$ and $c_3$ such that $c_1$ shares a side with $c_2$ and another side with $c_3$, the integers written in cells $c_2$ and $c_3$ are distinct.
- Let's denote the number of different integers in the grid by $K$; then, each of these integers should lie between $1$ and $K$ inclusive.
- $K$ should be minimum possible.
Find the minimum value of $K$ and a resulting (filled) grid. If there are multiple solutions, you may find any one.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $N$ and $M$.

-----Output-----
- For each test case, print $N+1$ lines.
- The first line should contain a single integer — the minimum $K$.
- Each of the following $N$ lines should contain $M$ space-separated integers between $1$ and $K$ inclusive. For each valid $i, j$, the $j$-th integer on the $i$-th line should denote the number in the $i$-th row and $j$-th column of the grid.

-----Constraints-----
- $1 \le T \le 500$
- $1 \le N, M \le 50$
- the sum of $N \cdot M$ over all test cases does not exceed $7 \cdot 10^5$

-----Subtasks-----
Subtask #1 (100 points): original constraints

-----Example Input-----
2
1 1
2 3

-----Example Output-----
1
1
3
1 1 2
2 3 3

-----Explanation-----
Example case 1: There is only one cell in the grid, so the only valid way to fill it is to write $1$ in this cell. Note that we cannot use any other integer than $1$.
Example case 2: For example, the integers written in the neighbours of cell $(2, 2)$ are $1$, $2$ and $3$; all these numbers are pairwise distinct and the integer written inside the cell $(2, 2)$ does not matter. Note that there are pairs of neighbouring cells with the same integer written in them, but this is OK.

Starter code:

Solutions:
# cook your dish here
t = int(input())
for _ in range(t):
 m,n = [int(d) for d in input().split()]

 if m == 1:
  arr = []
  if n%4 == 0:
   print(2)
   arr = [[1,1,2,2]*(n//4)]
  elif n%4 == 1:
   if n == 1:
    print(1)
    arr = [[1]]
   else:
    print(2)
    arr = [[1,1,2,2]*(n//4) + [1]]
  elif n%4 == 2:
   if n == 2:
    print(1)
    arr = [[1,1]]
   else:
    print(2)
    arr = [[1,1,2,2]*(n//4) + [1,1]]
  elif n%4 == 3:
   print(2)
   arr = [[1,1,2,2]*(n//4) + [1,1,2]]

 elif m == 2:
  if n%3 == 0:
   print(3)
   a1 = [1,2,3]*(n//3)
   arr = [a1,a1]

  elif n%3 == 1:
   if n == 1:
    print(1)
    arr = [[1],[1]]
   else:
    print(3)
    a1 = [1,2,3]*(n//3) + [1]
    arr = [a1,a1]

  elif n%3 == 2:
   if n == 2:
    print(2)
    arr = [[1,2],[1,2]]
   else:
    print(3)
    a1 = [1,2,3]*(n//3) + [1,2]
    arr = [a1,a1]

 elif m == 3:
  if n == 1:
   print(2)
   arr = [[1],[1],[2]]
  elif n == 2:
   print(3)
   arr = [[1,1],[2,2],[3,3]]
  elif n == 3:
   print(4)
   arr = [[1,3,4],[4,2,1],[4,2,1]]
  elif n == 4:
   print(4)
   arr = [[1,3,4,2],[4,2,1,3],[4,2,1,3]]
  else:
   if n%4 == 0:
    print(4)
    a1 = [1,3,4,2]*(n//4)
    a2 = [4,2,1,3]*(n//4)
    arr = [a1,a2,a2]
 
   elif n%4 == 1:
    print(4)
    a1 = [1,3,4,2]*(n//4) + [1]
    a2 = [4,2,1,3]*(n//4) + [4]
    arr = [a1,a2,a2]

   elif n%4 == 2:
    print(4)
    a1 = [1,3,4,2]*(n//4) + [1,3]
    a2 = [4,2,1,3]*(n//4) + [4,2]
    arr = [a1,a2,a2]

   elif n%4 == 3:
    print(4)
    a1 = [1,3,4,2]*(n//4) + [1,3,4]
    a2 = [4,2,1,3]*(n//4) + [4,2,1]
    arr = [a1,a2,a2]

 else:
  if n == 1:
   print(2)
   a1 = [1,3,4,2]*(n//4) + [1]
   a2 = [4,2,1,3]*(n//4) + [2]
   arr = []
   i = 0
   j = 0
   c = 0
   c1 = 0
   for i in range(m):
    if j == 0 and c < 3:
     arr.append(a1)
     c = c + 1
     if c == 2:
      j = 1
      c = 0
    else:
     arr.append(a2)
     c1 = c1 + 1
     if c1 == 2:
      j = 0
      c1 = 0
  
  elif n == 2:
   print(3)
   arr = []
   a1 = [1,1]
   a2 = [2,2]
   a3 = [3,3]

   if m%3 == 1:
    arr = [a1,a2,a3]*(m//3) + [a1]
   elif m%3 == 2:
    arr = [a1,a2,a3]*(m//3) + [a1,a2]
   elif m%3 == 0:
    arr = [a1,a2,a3]*(m//3)

  else:
   print(4)
   if n%4 == 0:
    a1 = [1,3,4,2]*(n//4)
    a2 = [4,2,1,3]*(n//4)
    arr = []
    i = 0
    j = 0
    c = 0
    c1 = 0
    for i in range(m):
     if j == 0 and c < 3:
      arr.append(a1)
      c = c + 1
      if c == 2:
       j = 1
       c = 0
     else:
      arr.append(a2)
      c1 = c1 + 1
      if c1 == 2:
       j = 0
       c1 = 0

   elif n%4 == 1:
    a1 = [1,3,4,2]*(n//4) + [1]
    a2 = [4,2,1,3]*(n//4) + [4]
    arr = []
    i = 0
    j = 0
    c = 0
    c1 = 0
    for i in range(m):
     if j == 0 and c < 3:
      arr.append(a1)
      c = c + 1
      if c == 2:
       j = 1
       c = 0
     else:
      arr.append(a2)
      c1 = c1 + 1
      if c1 == 2:
       j = 0
       c1 = 0

   elif n%4 == 2:

    a1 = [1,3,4,2]*(n//4) + [1,3]
    a2 = [4,2,1,3]*(n//4) + [4,2]
    arr = []
    i = 0
    j = 0
    c = 0
    c1 = 0
    for i in range(m):
     if j == 0 and c < 3:
      arr.append(a1)
      c = c + 1
      if c == 2:
       j = 1
       c = 0
     else:
      arr.append(a2)
      c1 = c1 + 1
      if c1 == 2:
       j = 0
       c1 = 0

   elif n%4 == 3:

    a1 = [1,3,4,2]*(n//4) + [1,3,4]
    a2 = [4,2,1,3]*(n//4) + [4,2,1]
    arr = []
    i = 0
    j = 0
    c = 0
    c1 = 0
    for i in range(m):
     if j == 0 and c < 3:
      arr.append(a1)
      c = c + 1
      if c == 2:
       j = 1
       c = 0
     else:
      arr.append(a2)
      c1 = c1 + 1
      if c1 == 2:
       j = 0
       c1 = 0



 for i in range(m):
  for j in range(n):
   print(arr[i][j],end = " ")
  print()




    
    















t=int(input())
for w in range(t):
 inp = list(map(int,input().split()))
 row=inp[0]
 col=inp[1]
 flag=0
 if(min(row,col)==1):
  if(max(row,col)>=3):
   print(2)
  else:
   print(1)
  if(1):
   a=[0 for i in range(max(col,row))]
   count=0
   it=1
   for i in range(max(col,row)):
    if(count==2):
     if(it==1):
      it=2
     else:
      it=1
     a[i]=it
     count=1
     continue
    else:
     a[i]=it
     count+=1
   if(row==1):
    for item in a:
     print(item,end=" ")
    print(" ")
   
   else:
    for item in a:
     print(item)
 elif(min(row,col)==2):
  if(max(row,col)==2):
   print(2)
   print(2,end=" ")
   print(2)
   print(1,end=" ")
   print(1)
  else:
   a=[[0 for i in range(max(row,col))]for j in range(2)]
   print(3)
   it=3
   count=1
   for i in range(max(row,col)):
    if(count==1):
     a[0][i]=it
     count+=1
     continue
    else:
     if(count==2):
      it-=1
      if(it==0):
       it=3
      a[0][i]=it
      count=1
      continue
   it=2
   count=2
   for i in range(max(row,col)):
    if(count==1):
     a[1][i]=it
     count+=1
     continue
    else:
     if(count==2):
      it-=1
      if(it==0):
       it=3
      a[1][i]=it
      count=1
      continue
   if(row==2):
    for i in range(max(row,col)):
     print(a[0][i],end=" ")
    print(" ")
    for i in range(max(row,col)):
     print(a[1][i],end=" ")
    print(' ')
   else:
    for i in range(max(row,col)):
     print(a[0][i],end=" ")
     print(a[1][i])

 else:
  print(4)
  a=[[0 for i in range(col)]for j in range(row)]
  a[0][0]=2
  a[1][0]=2
  a[0][1]=3
  a[1][1]=3
  for i in range(2,col,2):
   if(a[0][i-2]==2):
    a[0][i]=1
    a[1][i]=1
   else:
    a[0][i]=2
    a[1][i]=2
  for i in range(3,col,2):
   if(a[0][i-2]==3):
    a[0][i]=4
    a[1][i]=4
   else:
    a[0][i]=3
    a[1][i]=3
  for i in range(col):
   for j in range(2,row):
    if(a[j-2][i]==2):
     a[j][i]=1
    elif(a[j-2][i]==1):
     a[j][i]=2
    elif(a[j-2][i]==3):
     a[j][i]=4
    elif(a[j-2][i]==4):
     a[j][i]=3
  for i in range(row):
   for j in range(col):
    print(a[i][j],end=' ')
   print(" ")

def fill_grid_max():
 A = [[0 for j in range(50)] for i in range(50)]
 B = [[1,1,2,2],[3,3,4,4],[2,2,1,1],[4,4,3,3]]
 for i in range(50):
  for j in range(50):
   A[i][j] = B[i%4][j%4]
 return A
 
def calculate_k(A):
 k = 0
 for row in A:
  k = max(k,max(row))
 return k
 
def print_output(A,k,n,m):
 print(k)
 for i in range(n):
  for j in range(m):
   print(A[i][j], end = ' ')
  print()

t = int(input())
A = fill_grid_max()
for test in range(t):
 n,m = map(int,input().split())
 if n==1 and m==1:
  print(1)
  print(1)
 elif n==1:
  B = A[0][:m]
  k = max(B)
  print(k)
  for x in B:
   print(x,end = ' ')
  print()
 elif m==1:
  B = [[] for x in range(n)]
  for i in range(n):
   B[i].append(A[0][i])
  k = max(A[0][:n])
  print_output(B,k,n,m)
 elif n==2 and m==2:
  k = 2
  B = [[1,1],[2,2]]
  print_output(B,k,n,m)
 elif n==2:
  k = 3
  B = [[0 for x in range(m)] for y in range(n)]
  C = [[1,2,3],[1,2,3]]
  for i in range(n):
   for j in range(m):
    B[i][j] = C[i][j%3]
  print_output(B,k,n,m)
 elif m==2:
  k = 3
  B = [[0 for x in range(m)] for y in range(n)]
  C = [[1,1],[2,2],[3,3]]
  for i in range(n):
   for j in range(m):
    B[i][j] = C[i%3][j]
  print_output(B,k,n,m)
 if n>=3 and m>=3:
  print_output(A,4,n,m)

t = int(input())

p2 = [1, 1, 2, 2]

p3 = [1, 2, 3]

p4 = [
 [1, 1, 2, 2],
 [3, 3, 4, 4],
 [2, 2, 1, 1],
 [4, 4, 3, 3],
]

for ti in range(t):
 n, m = map(int, input().split())

 if n == 1:
  print(2 if m > 2 else 1)
  for j in range(m):
   print(p2[j % 4], end=' ')
  print()

 elif m == 1:
  print(2 if n > 2 else 1)
  for i in range(n):
   print(p2[i % 4])

 elif n == 2:
  print(3 if m > 2 else 2)
  for j in range(m):
   print(p3[j % 3], end=' ')
  print()
  for j in range(m):
   print(p3[j % 3], end=' ')
  print()

 elif m == 2:
  print(3 if n > 2 else 2)
  for i in range(n):
   print(p3[i % 3], end=' ')
   print(p3[i % 3])

 else:
  print(4)
  for i in range(n):
   for j in range(m):
    print(p4[i % 4][j % 4], end=' ')
   print()


import sys
f=sys.stdin
#f=open("test.txt","r")
t=int(f.readline())
for r in range(t):
 n, m = map(int, f.readline().rstrip().split())
 if n == 1 and m == 1:
  print("1")
  print("1")
 elif n == 2 and m == 2:
  print("2")
  print("1 2")
  print("1 2")
 elif n==2 and m==1:
  print("1")
  print("1")
  print("1")
 elif n==1 and m==2:
  print("1")
  print("1 1")
 elif n==1 and m>2:
  print("2")
  #
  for i in range(1,m+1):
   if (i % 4 == 1 or i % 4 == 2):
    print("1",end=" ")
   else:
    print("2",end=" ")
 elif n>2 and m==1:
  print("\n2")
  for i in range(1, n + 1):
   if (i % 4 == 1 or i % 4 == 2):
    print("1")
   else:
    print("2")
 elif n==2 and m>2:
  print("3")
  #
  for i in range(0,n):
   for j in range(1,m+1):
    if j%3==1:
     print("1",end=" ")
    elif j%3==2:
     print("2", end=" ")
    else:
     print("3", end=" ")
   print()
 elif n>2 and m==2:
  print("3")
  #
  for i in range(1,n+1):
   for j in range(1,m+1):
    if i%3==1:
     print("1",end=" ")
    elif i%3==2:
     print("2", end=" ")
    else:
     print("3", end=" ")
   print()
 else:
  print("4")
  l = [[0 for x in range(m)] for x in range(n)]
  for q in range(0,m):
   if (q+1)%4!=0:
    l[0][q] =(q+1)%4
    l[1][q] =(q+1)%4
   else:
    l[0][q] =4
    l[1][q] =4
  for i in range(2,n):
   for j in range(1,m-1):
    l[i][j]=10-(l[i-1][j-1]+l[i-2][j]+l[i-1][j+1])
   if(l[i][1]!=1):
    l[i][0]=l[i][1]-1
   else:
    l[i][0] = 4
   if(l[i][m-2]!=4):
    l[i][m-1]=l[i][m-2]+1
   else:
    l[i][m-1]=1
  for i in range(0,n):
   for j in range(0,m):
    print(l[i][j],end=" ")
   print()







def update(grid, i, j, k):
 used = [0]*6
 # 0 -2 -2 0 -1 1 -1 -1
 if j-2 >= 0:
  used[grid[i][j-2]] = 1
 if i-2 >= 0:
  used[grid[i-2][j]] = 1 
 if i-1 >= 0 and j-1 >= 0:
  used[grid[i-1][j-1]] = 1
 if i-1 >= 0 and j+1 < len(grid[0]):
  used[grid[i-1][j+1]] = 1

##    print("%d %d" % (i, j))
##    print(used)
##    print(grid)
 #return the first available value
 #print("k: %d" % k)
 for fill in range(1,5):
  if used[fill] == 0:
   if fill > k:
    k = fill
   grid[i][j] = fill
   return k
 #This shouldn't happen
 grid[i][j] = 5
 return 5
  

grid5050 = [[0]*50 for row in range(50)]
k=1
for i in range(50):
 for j in range(50):
  k = update(grid5050, i, j, k)
noOfCases = int(input())
for case in range(noOfCases):
 #Read in data for each case
 n,m = list(map(int, input().split()))
 #print("N and M %d %d" % (n,m))
 k = 1
 grid = [[0]*m for row in range(n)]

 if n>=3 and m>=3:
  k= 4
  for i in range(n):
   for j in range(m):
    grid[i][j] = grid5050[i][j] 
 elif n == 2 and m > 2:
  #this case i missed??
  k = 3
  for j in range(m):
   next = j % 3 +1
   grid[0][j] = next
   grid[1][j] = next
 else:
  for i in range(n):
   for j in range(m):
    k = update(grid, i, j, k)
 
 print(k) 
 print(('\n'.join([''.join([(str(item)+' ') for item in row]) 
  for row in grid]))) 
        
  


for _ in range(int(input())):
 N,M = map(int,input().split())
 a,b = M//4,M%4
 c,d = M//3,M%3 
 strdn1,strdn2,ans='1 1 2 2 ','1 2 3 ',''
 if N==1 and M==1:
  print(1,1,sep="\n")
 elif N==1:
  if M==2:
   print(1)
  else:
   print(2)
  for i in range(1,a+1):
   ans = ans + strdn1
  if b!=0:
   ans = ans+ strdn1[:2*b-1]
  print(ans)
 elif M==1:
  if N==2:
   print(1)
  else:
   print(2)
  for i in range(1,N+1):
   if i%4==1 or i%4==2:
    print(1)
   else:
    print(2)
 elif N==2 and M==2:
  print('2','1 1','2 2',sep="\n")
 elif N==2:
  print(3)
  for i in range(1,c+1):
   ans = ans + strdn2
  if d!=0:
   ans = ans+ strdn2[:2*d-1]
  print(ans)
  print(ans)
 elif M==2:
  print(3)
  for i in range(1,N+1):
   if i%3==0:
    print('3 3')
   else:
    print(str(i%3)+' '+str(i%3))
 else:
  print(4)
  str1,str2='1 2 3 4 ','3 4 1 2 '
  for i in range(1,N+1):
   ans=''
   if i%4==1 or i%4==2:
    for i in range(1,a+1):
     ans = ans + str1
    if b!=0:
     ans = ans+ str1[:2*b-1]
    print(ans)
   else:
    for i in range(1,a+1):
     ans = ans + str2
    if b!=0:
     ans = ans+ str2[:2*b-1]
    print(ans)
    
    
  
  
   
  

def valid(x,y,m,n):
 if 0<=x<m and 0<=y<n:
  return 1
 return 0
def neigh(A,x,y,m,n):
 B=[]
 if valid(x,y-2,m,n):
  B.append(A[x][y-2])
 if valid(x-2,y,m,n):
  B.append(A[x-2][y])
 if valid(x-1,y-1,m,n):
  B.append(A[x-1][y-1])
 if valid(x-1,y+1,m,n):
  B.append(A[x-1][y+1])
 return set(B)
 
AA=[[1,1,2,2],[1,1,2,3],[1,2,2,3],[1,2,3,3],[1,2,2,1],[1,2,3,1],[1,2,3,4]]
t=int(input())
for i in range(t):
 m,n=map(int,input().split())
 if m>=3 and n>=3:
  P=set(range(1,5))
 elif m==1 and n==1:
  P=set(range(1,2))
 elif m+n==3:
  P=set(range(1,2))
 elif m==1 or n==1:
  P=set(range(1,3))
 elif m==2 and n==2:
  P=set(range(1,3))
 elif m==2 or n==2:
  P=set(range(1,4))
 A=[[0 for j in range(n)] for k in range(m)]
 pk=0
 if m==1:
  A[0][0]=1
  for j in range(1,n):
   B=neigh(A,0,j,m,n)
   A[0][j]=min(P-B)
  print(max(P))
  for j in A[0]:
   print(j,end=" ")
  print()
  continue
 while True:
  trig=0
  if max(P)==4:
   for j in range(n):
    A[0][j]=AA[pk][j%4]
  else:
   for j in range(n):
    A[0][j]=(j%max(P))+1
  for j in range(1,m):
   for k in range(n):
    B=neigh(A,j,k,m,n)
    if P-B==set():
     trig=1
     break
    A[j][k]=min(P-B)
   if trig==1:
    pk+=1
    A=[[0 for j in range(n)] for k in range(m)]
    break
  else:
   break
 print(max(P))
 for j in A:
  for k in j:
   print(k,end=" ")
  print()
   
   
 
 
  


n=int(input())
for i in range(0,n):
 r,c=map(int,input().split())
 if(r==1 and c==1):
  print(1)
  print(1)
 elif(r==1 and c>1):
  l=[1,1,2,2]
  if(c==2):
   print(1)
  else:
   print(2)
  for i in range(c):
   print(l[i%4],end=" ")
  print()

 elif(c==1 and r>1):
  l=[1,1,2,2]
  if(r==2):
   print(1)
  else:
   print(2)
  for i in range(r):
   print(l[i%4])
 elif(c==2 and r>1):
  l=[1,2,3]
  if(r==2):
   print(2)
  elif(r>2):
   print(3) 
  for i in range(r):
   print(l[i%3],l[i%3])

 elif(r==2 and c>2):
  print(3)
  l=[1,2,3]
  for i in range(0,c):
   print(l[i%3],end=" ")
  print() 
  for i in range(0,c):
   print(l[i%3],end=" ")
  print() 

 elif(r>2 and c>2):
  print(4)
  l=[[1,1,3,3],[2,2,4,4],[3,3,1,1],[4,4,2,2]]
  for i in range(0,r):
   for j in range(0,c):
    print(l[i%4][j%4],end=" ")
   print() 







lst=[1,2,3,4]
for _ in range(int(input())):
 m,n = map(int, input().split())
 ans = [[0 for i in range(m)] for i in range(n)]
 k = 0
 if n == 1:
  if m > 2:
   print(2)
  else:
   print(1)
  for i in range(m):
   print((i // 2) % 2 + 1)
 elif m == 1:
  if n > 2:
   print(2)
  else:
   print(1)
  for i in range(n):
   print((i // 2) % 2 + 1, end=" ")
 elif n == 2:
  if m > 2:
   print(3)
  else:
   print(2)
  for i in range(m):
   print(i % 3 + 1, i % 3 + 1)
 elif m == 2:
  if n > 2:
   print(3)
  else:
   print(2)
  for i in range(n):
   print(i % 3 + 1, end=" ")
  print()
  for i in range(n):
   print(i % 3 + 1, end=" ")
  
 else:
  print(4)
  lst = [[1,1,3,3],[2,2,4,4],[3,3,1,1,],[4,4,2,2]]
  for i in range(m) :
   for j in range(n) :
    print( lst[i%4][j%4] , end=" ")


li = [1,2,3,4]
for _ in range( int(input())) :
 m , n = map(int,input().split())
 if m == 1 :
  if n>2 :
   print(2)
  else :
   print(1)
  for i in range( n ) :
   print( (i//2)%2 +1  , end = " ")
  print()
 elif n == 1 :
  if m > 2 :
   print(2)
  else :
   print(1) 
  for i in range(m) :
   print( (i//2)%2 +1 )
 elif m == 2 :
  if n > 2 :
   print(3)
  else :
   print(2)
  for i in range(n) :
   print( i%3 +1  , end = " ")
  print()
  for i in range(n) :
   print( i%3 +1  , end = " ")
  print()
 elif n == 2 :
  if m > 2 :
   print(3)
  else :
   print(2)
  for i in range(m) :
   print( i%3 +1  , i%3 + 1)
 else :
  print(4)
  li = [[1,1,3,3],[2,2,4,4],[3,3,1,1,],[4,4,2,2]]
  for i in range(m) :
   for j in range(n) :
    print( li[i%4][j%4] , end=" ")
   print()

for _ in range(int(input())):
 n, m = list(map(int, input().split()))
 ans = [[0 for i in range(m)] for i in range(n)]
 k = 0
 if n == 1:
  for i in range(m):
   if i%4 == 0 or i%4 == 1:
    t = 1
   else:
    t = 2
   ans[0][i] = t
   k = max(k, ans[0][i])
 elif m == 1:
  for i in range(n):
   if i%4 == 0 or i%4 == 1:
    t = 1
   else:
    t = 2
   ans[i][0] = t
   k = max(k, ans[i][0])
 elif n == 2:
  t = 1
  for i in range(m):
   ans[0][i] = t
   ans[1][i] = t
   t = (t)%3 + 1
   k = max(k, ans[0][i])
 elif m == 2:
  t = 1
  for i in range(n):
   ans[i][0] = t
   ans[i][1] = t
   t = t%3 + 1
   k = max(k, ans[i][0])
 else:
  for i in range(n):
   if i%4 == 0 or i%4 == 1:
    t = 0
   else:
    t = 2
   for j in range(m):
    ans[i][j] = (t%4) + 1
    t += 1
    k = max(k, ans[i][j])
      

 print(k)
 for i in range(n):
  print(*ans[i])


for _ in range(int(input())):
 row, col = map(int, input().split())
 if row == 1 and col == 1:
  print("1\n1")

 elif row == 1:
  if col > 2:
   print(2)
  else:
   print(1)
  ls = [1, 1, 2, 2]
  for i in range(col):
   print(ls[i%4], end=' ')
  print()

 elif col == 1:
  if row > 2:
   print(2)
  else:
   print(1)
  ls = [1, 1, 2, 2]
  for i in range(row):
   print(ls[i%4])

 elif col == 2:
  if row == 2:
   print("2\n1 2\n1 2")
  else:
   print(3)
   for j in range(row):
    for i in range(col):
     print((j%3)+1, end=' ')
    print()

 elif row == 2:
  if col == 2:
   print("2\n1 2\n1 2")

  else:
   print(3)
   for j in range(2):
    for i in range(col):
     print((i%3)+1, end=' ')
    print() 

 else:
  first = [1, 1, 2, 2]
  second = [2, 2, 1, 1]
  third = [3, 3, 4, 4]
  fourth = [4, 4, 3, 3]
  first_found = 0
  second_found = 0
  temp = 0
  print(4)
  for i in range(row):
   for j in range(col):
    x = j%4
    if temp%2 == 0:
     if first_found == 0:
      print(first[x], end=' ')
     else:
      print(second[x], end=' ')
    else:
     if(second_found == 0):
      print(third[x], end=' ')
     else:
      print(fourth[x], end=' ')

   if first_found == 0 and temp%2 == 0:
    first_found = 1
   elif first_found == 1 and temp%2 == 0:
    first_found = 0
   if second_found == 0 and temp%2 == 1:
    second_found = 1
   elif second_found == 1 and temp%2 == 1:
    second_found = 0

   temp += 1
   print()