h,w=map(int,input().split()) if min(h,w)==0: print(0) for i in range(h): print(*[0]*w) exit() if h%2==0 and w%2==0: print(h*w) for i in range(h): print(*[1]*w) exit() if h%2==0: print(h*(w-1)) for i in range(h): print(*[1]*(w-1)+[0]) exit() if w%2==0: print((h-1)*w) for i in range(h-1): print(*[1]*w) print(*[0]*w) exit() ans=[[0]*w for i in range(h)] for i in range(h-1): for j in range(w-1): ans[i][j]=1 ans[h-2][w-2]=0 ans[h-2][w-1]=1 ans[h-1][w-2]=1 ans[h-1][w-1]=1 print((h-1)*(w-1)+2) for i in ans: print(*i)