from sys import argv
from math import *
from random import *

seed('viz')

colors = [(255, 0, 0), (255, 255, 0), (0, 0, 255)]

def buildColors(n):
    res = []
    side = int(floor(n ** (1./3)))
    for i in range(side + 1):
        for j in range(side + 1):
            for k in range(side + 1):
                r = int(255. / side * i)
                g = int(255. / side * j)
                b = int(255. / side * k)
                res.append((r, g, b))
    shuffle(res)
    return res[:n]

import Image, ImageDraw

IN = open(argv[1])
SZ = 2000
n, m = map(int, IN.readline().split())
sq = int(min(SZ / n, SZ / m))
sq = 1

szX = sq * m
szY = sq * n

img = Image.new("RGB", (szY, szX))
d = ImageDraw.ImageDraw(img)

a = []
types = set()
for i in range(n):
    a.append(list(map(int, IN.readline().split())))
    for x in a[-1]:
        types.add(x)


colorsA = buildColors(len(types))
colors = {}
for (i, t) in enumerate(types):
    colors[t] = colorsA[i]

for i in range(n):
    for j in range(m):
        for x in range(sq):
            for y in range(sq):
                X = i * sq + x
                Y = j * sq + y
                d.point((Y, X), fill = colors[a[i][j]])

img.save(argv[1] + '.png')

