def format_grays(grays, size=8):
    """Format grays list as matrix of gray values.

    >>> out = format_grays([0,0,1,1,1, 0,1,1,3,4, 0,1,6,6,7, 7,7,7,7,9, 8,7,7,8,9], size=4)
    >>> print('\\n'.join(line.strip() for line in out.splitlines()))
    0   0   1   1   1
    0   1   1   3   4
    0   1   6   6   7
    7   7   7   7   9
    8   7   7   8   9
    """
    width = size + 1
    lines = []
    for y in range(width):
        line = []
        for x in range(width):
            gray = grays[y * width + x]
            line.append(format(gray, '4'))
        lines.append(''.join(line))
    return '\n'.join(lines)
