Problem:
 Score : 900 points    Problem Statement Let us consider a grid of squares with N rows and N columns. You want to put some domino pieces on this grid.
Each domino piece covers two squares that have a common side. Each square can be covered by at most one piece.  For each row of the grid, let's define its quality as the number of domino pieces that cover at least one square in this row.
We define the quality of each column similarly.  Find a way to put at least one domino piece on the grid so that the quality of every row is equal to the quality of every column,
or determine that such a placement doesn't exist.      Constraints  2 \le N \le 1000         Input Input is given from Standard Input in the following format:  N       Output If the required domino placement doesn't exist, print a single integer -1 .  Otherwise, output your placement as N strings of N characters each.
If a square is not covered, the corresponding character must be . (a dot).
Otherwise, it must contain a lowercase English letter.
Squares covered by the same domino piece must contain the same letter.
If two squares have a common side but belong to different pieces, they must contain different letters.        Sample Input 1 6      Sample Output 1 aabb..
b..zz.
ba....
.a..aa
..a..b
..a..b  The quality of every row and every column is 2.       Sample Input 2 2      Sample Output 2 -1

Solution (Python):
n=int(input())

if n==2:
    print(-1)
else:
    x=[]
    if n%3==0:
        for i in range(n//3):
            x.append("."*(3*i)+"a"+"."*(n-3*i-1))
            x.append("."*(3*i)+"a"+"."*(n-3*i-1))
            x.append("."*(3*i)+".aa"+"."*(n-3*i-3))
    elif n%6==1:
        for i in range(n//6-1):
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+"ddg.ll"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.g.kk"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.hhj."+"."*(n-6*i-6))
            x.append("."*(6*i)+"ffiij."+"."*(n-6*i-6))
        x.append("."*(n-7)+".aab.c.")
        x.append("."*(n-7)+"d..b.c.")
        x.append("."*(n-7)+"d..eeff")
        x.append("."*(n-7)+"g..mm.l")
        x.append("."*(n-7)+"gnn...l")
        x.append("."*(n-7)+"h...kkj")
        x.append("."*(n-7)+"hii...j")
    elif n%6==2:
        for i in range(n//6-1):
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+"ddg.ll"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.g.kk"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.hhj."+"."*(n-6*i-6))
            x.append("."*(6*i)+"ffiij."+"."*(n-6*i-6))
        x.append("."*(n-8)+".a.bb.cc")
        x.append("."*(n-8)+".a...m.j")
        x.append("."*(n-8)+"..pp.m.j")
        x.append("."*(n-8)+"hh..i.o.")
        x.append("."*(n-8)+"gg..i.o.")
        x.append("."*(n-8)+"..n.ll.k")
        x.append("."*(n-8)+"f.n....k")
        x.append("."*(n-8)+"f.dd.ee.")
    elif n%6==4:
        for i in range(n//6):
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+"ddg.ll"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.g.kk"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.hhj."+"."*(n-6*i-6))
            x.append("."*(6*i)+"ffiij."+"."*(n-6*i-6))
        x.append("."*(n-4)+"aacb")
        x.append("."*(n-4)+"ffcb")
        x.append("."*(n-4)+"hgdd")
        x.append("."*(n-4)+"hgee")
    else:
        for i in range(n//6):
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+".a.b.c"+"."*(n-6*i-6))
            x.append("."*(6*i)+"ddg.ll"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.g.kk"+"."*(n-6*i-6))
            x.append("."*(6*i)+"e.hhj."+"."*(n-6*i-6))
            x.append("."*(6*i)+"ffiij."+"."*(n-6*i-6))
        x.append("."*(n-5)+"aabbc")
        x.append("."*(n-5)+"g.h.c")
        x.append("."*(n-5)+"gjh..")
        x.append("."*(n-5)+"dj.ii")
        x.append("."*(n-5)+"deeff")
    for i in range(n):
        print("".join(x[i]))

Solution (Python):
n = int(input())

s3=["a..","a..",".aa"]
s = [["abcc", "abdd", "ddba", "ccba"],
["dccdd", "daa.c", "c..bc", "c..bd", "ddccd"],
["abbc..", "a.ac..", "bba.cc", "a..aab", "a..b.b", ".aabaa"],
["aba....","aba....","bab....","bab....","a..bbaa","a..aabb",".aabbaa"]]

if n == 2:
    print(-1)
elif n == 3:
    [print(x) for x in s3]
else:
    d, m = divmod(n, 4)
    d -= 1
    m += 4
    for i in range(d):
        [print("." * 4 * i + x + "." * (4 * (d - i - 1) + m)) for x in s[0]]
    [print("." * 4 * d + x) for x in s[m - 4]]

Solution (Python):
ans = {
    3: [
        'aa.',
        '..a',
        '..a'
    ],
    4: [
        'aacd',
        'bbcd',
        'cdaa',
        'cdbb'
    ],
    5: [
        'aabbc',
        'bcc.c',
        'b..ba',
        'c..ba',
        'cddee'
    ],
    6: [
        'aabbcc',
        'bbccdd',
        'a.a.a.',
        'a.a.a.',
        '.a.a.a',
        '.a.a.a'
    ],
    7: [
        'aabbaa.',
        'ccaa..b',
        'aacc..b',
        '....bca',
        '....bca',
        '....cbc',
        '....cbc'
    ]
}
N = int(input())
if N < 8:
    if N in ans:
        for line in ans[N]:
            print(line)
    else:
        print(-1)
else:
    div = N // 4
    for i in range(div-1):
        for line in ans[4]:
            print('.'*(i*4) + line + '.'*(N-(i*4)-4))
    m = (N % 4) + 4
    for line in ans[m]:
        print('.'*(N-m) + line)

Solution (Python):
N = int(input())

if N == 2:
    print(-1)

elif N == 3:
    ptn = ['abb','a.c','ddc']
    print('\n'.join(ptn))

else:
    ptns = {
        4: ['abcc','abdd','eegh','ffgh'],
        5: ['abc..','abc..','ghhdd','g.iee','jjiff'],
        6: ['abc...','abc...','ddeeff','gghhii','...jkl','...jkl'],
        7: ['abc....','abc....','def....','def....','m..gghh','m..iijj','.nnkkll']
    }
    x = N//4 - 1
    y = N - 4*x
    ansss = [['.']*(N) for _ in range(N)]
    for k in range(x):
        for i in range(4):
            for j in range(4):
                ansss[4*k+i][4*k+j] = ptns[4][i][j]
    for i in range(y):
        for j in range(y):
            ansss[4*x+i][4*x+j] = ptns[y][i][j]
    print('\n'.join([''.join(anss) for anss in ansss]))

Solution (Python):
n = int(input())


def concat_board(board1, board2):
    
    
    
    
    x, y = len(board1), len(board2)
    board = []
    sr = '.' * y
    board += [board1[i] + sr for i in range(x)]
    sl = '.' * x
    board += [sl + board2[i] for i in range(y)]
    return board


def make3board(x):
    
    
    
    
    base = ["aab", "d.b", "dcc"]
    if x == 3:
        return base
    else:
        return concat_board(base, make3board(x - 3))


def make4board(x):
    
    
    
    
    
    base = ["aagh", "bbgh", "cdee", "cdff"]
    if x == 4:
        return base
    else:
        return concat_board(base, make4board(x - 4))


def make5board(x):
    
    
    
    
    
    
    base = ["aadde", "bbg.e", "ccgff", "..abc", "..abc"]
    if x == 5:
        return base
    else:
        return concat_board(base, make5board(x - 5))


board = []
if n % 3 == 0:
    board = make3board(n)
elif n % 4 == 0:
    board = make4board(n)
elif n % 5 == 0:
    board = make5board(n)
elif n == 7 or n == 11:
    board = [
        "aabbcc.", "dd.dd.c", "..d..dc", "..d..db", "dd.dd.b", "..d..da",
        "..d..da"
    ]
    if n == 11:
        board = concat_board(board, make4board(4))
else:
    for x in range(4, n, 4):
        y = n - x
        if y % 5 == 0:
            board = concat_board(make4board(x), make5board(y))
            break

if len(board) != 0:
    print(*board, sep='\n')
else:
    print(-1)

Solution (Python):
n = int(input())


def make3board(x):
    
    
    
    
    m = x // 3
    base = ["aab", "d.b", "dcc"]
    board = []
    for i in range(m):
        sl = "..." * i
        sr = "..." * (m - 1 - i)
        for bj in base:
            board.append(sl + bj + sr)
    return board


def make4board(x):
    
    
    
    
    
    m = x // 4
    base = ["aagh", "bbgh", "cdee", "cdff"]
    board = []
    for i in range(m):
        sl = "...." * i
        sr = "...." * (m - 1 - i)
        for bj in base:
            board.append(sl + bj + sr)
    return board


def make5board(x):
    
    
    
    
    
    
    m = x // 5
    base = ["aadde", "bbg.e", "ccgff", "..abc", "..abc"]
    board = []
    for i in range(m):
        sl = "....." * i
        sr = "....." * (m - 1 - i)
        for bj in base:
            board.append(sl + bj + sr)
    return board


def make7board(x):
    
    
    
    
    
    
    
    
    m = x // 7
    base = [
        "aabbcc.", "dd.dd.c", "..d..dc", "..d..db", "dd.dd.b", "..d..da",
        "..d..da"
    ]
    return board


def concat_board(board1, board2):
    
    
    
    
    x, y = len(board1), len(board2)
    board = []
    sr = '.' * y
    board += [board1[i] + sr for i in range(x)]
    sl = '.' * x
    board += [sl + board2[i] for i in range(y)]
    return board


board = []
if n % 3 == 0:
    board = make3board(n)
elif n % 4 == 0:
    board = make4board(n)
elif n % 5 == 0:
    board = make5board(n)
elif n == 7 or n == 11:
    board7 = [
        "aabbcc.", "dd.dd.c", "..d..dc", "..d..db", "dd.dd.b", "..d..da",
        "..d..da"
    ]
    board = concat_board(board7, make4board(n - 7))
else:
    for x in range(4, n, 4):
        y = n - x
        if y % 5 == 0:
            board = concat_board(make4board(x), make5board(y))
            break

if len(board) != 0:
    print(*board, sep='\n')
else:
    print(-1)

Solution (Python):
n = int(input())

threeT = [
    ['a', 'a', '.'],
    ['.', '.', 'a'],
    ['.', '.', 'a']
]
fourT = [
    ['a', 'a', 'c', 'd'],
    ['b', 'b', 'c', 'd'],
    ['e', 'f', 'g', 'g'],
    ['e', 'f', 'h', 'h']
]
fiveT = [
    ['a', 'a', '.', 'c', 'd'],
    ['.', 'b', 'b', 'c', 'd'],
    ['e', '.', 'f', 'f', 'g'],
    ['e', '.', 'h', '.', 'g'],
    ['i', 'i', 'h', 'j', 'j']
]
sevenT = [
    ['a', 'a', 'b', 'b', 'c', 'c', '.'],
    ['d', 'd', 'e', 'e', '.', '.', 'f'],
    ['g', '.', '.', 'h', 'h', '.', 'f'],
    ['g', 'i', '.', '.', '.', '.', 'j'],
    ['.', 'i', '.', '.', 'k', 'k', 'j'],
    ['.', '.', 'l', '.', '.', 'm', 'n'],
    ['.', '.', 'l', '.', '.', 'm', 'n']
]

if n == 2:
    print(-1)
    exit()

if n == 7:
    for i in range(n):
        print(''.join(sevenT[i]))
    exit()

if n == 11:
    for i in range(n):
        br = ['.'] * n
        if i < 7 :
            for j in range(7):
                br[j] = sevenT[i][j]
        else:
            for j in range(4):
                br[7+j] = fourT[i-7][j]
        print(''.join(br))
    exit()

if n % 3 == 0:
    for i in range(n):
        br = ['.'] * n
        ind = i % 3
        k = i // 3
        for j in range(3):
            br[3*k+j] = threeT[ind][j]
        print(''.join(br))
    exit()
ac = n % 4
afive = ac
afour = (n - afive * 5) // 4
for i in range(n):
    br = ['.'] * n
    if i // 4 < afour:
        ind = i % 4
        k = i // 4
        for j in range(4):
            br[4*k+j] = fourT[ind][j]
    else:
        off = 4 * afour
        ind = (i-off) % 5
        k = (i-off) // 5
        for j in range(5):
            br[off+5*k+j] = fiveT[ind][j]
    print(''.join( br))

Solution (Python):
import sys
n=int(input())
if n==2:
  print(-1)
  sys.exit()
if n==3:
  print('aa.')
  print('..a')
  print('..a')
  sys.exit()

x=(n//4)-1
y=(n%4)+4

l=[[['a','a','b','c'],['d','d','b','c'],['b','c','a','a'],['b','c','d','d']],[['a','a','b','b','a'],['b','c','c','.','a'],['b','.','.','c','b'],['a','.','.','c','b'],['a','b','b','a','a']],[['a','a','b','c','.','.'],['d','d','b','c','.','.'],['.','.','a','a','b','c'],['.','.','d','d','b','c'],['b','c','.','.','a','a'],['b','c','.','.','d','d']],[['a','a','b','b','c','c','.'],['d','d','.','d','d','.','a'],['.','.','d','.','.','d','a'],['.','.','d','.','.','d','b'],['d','d','.','d','d','.','b'],['.','.','d','.','.','d','c'],['.','.','d','.','.','d','c']]]

if n<8:
  for i in range(n):
    print(''.join(l[n-4][i]))
  sys.exit()

ans=[]
for i in range(n):
  ans1=['.']*n
  ans.append(ans1)

for i in range(x):
  for j in range(4):
    for k in range(4):
      ans[i*4+j][i*4+k]=l[0][j][k]

for j in range(y):
  for k in range(y):
    ans[x*4+j][x*4+k]=l[y-4][j][k]

for i in range(n):
  print(''.join(ans[i]))

Solution (Python):
N = int(input())
if N==2:
    print(-1)
    exit()
if N==3:
    print('.aa')
    print('b..')
    print('b..')
    exit()

ans = [['.']*N for i in range(N)]
a4 = [
    'aacd',
    'bbcd',
    'cdaa',
    'cdbb'
]
a5 = [
    'aabbc',
    'h.iic',
    'hj..d',
    'gj..d',
    'gffee'
]
a6 = [
    'aacd..',
    'bbcd..',
    'cd..aa',
    'cd..bb',
    '..aacd',
    '..bbcd'
]
a7 = [
    'aa.bb.c',
    '.ii..jc',
    'h..mmj.',
    'h.n...d',
    '.ln...d',
    'gl..kk.',
    'g.ff.ee'
]
k = 0
if N%4==1:
    for i,row in enumerate(a5):
        for j,c in enumerate(row):
            ans[i][j] = c
    k += 5
elif N%4==2:
    for i,row in enumerate(a6):
        for j,c in enumerate(row):
            ans[i][j] = c
    k += 6
elif N%4==3:
    for i,row in enumerate(a7):
        for j,c in enumerate(row):
            ans[i][j] = c
    k += 7
while k < N:
    for i,row in enumerate(a4):
        for j,c in enumerate(row):
            ans[k+i][k+j] = c
    k += 4
assert k==N

for row in ans:
    print(''.join(row))

Solution (Python):
N = int(input())

lst = [['.'] * N for _ in range(N)]

def calc3(x, y): 
    lst[x][y] = 'a'
    lst[x][y + 1] = 'a'

    lst[x][y + 2] = 'b'
    lst[x + 1][y + 2] = 'b'

    lst[x + 2][y + 2] = 'c'
    lst[x + 2][y + 1] = 'c'

    lst[x + 2][y] = 'd'
    lst[x + 1][y] = 'd'

if N % 3 == 0:
    for h in range(0, N, 3):
        for w in range(0, N, 3):
            calc3(h, w)

    for i in lst:
        print (*i, sep = '')
    exit()

def calc4(x, y):
    lst[x][y] = 'a'
    lst[x][y + 1] = 'a'

    lst[x + 1][y] = 'b'
    lst[x + 1][y + 1] = 'b'

    lst[x + 2][y + 2] = 'c'
    lst[x + 2][y + 3] = 'c'


    lst[x + 3][y + 2] = 'd'
    lst[x + 3][y + 3] = 'd'

    lst[x + 2][y] = 'e'
    lst[x + 3][y] = 'e'

    lst[x + 2][y + 1] = 'f'
    lst[x + 3][y + 1] = 'f'

    lst[x][y + 2] = 'g'
    lst[x + 1][y + 2] = 'g'

    lst[x][y + 3] = 'h'
    lst[x + 1][y + 3] = 'h'

if N % 4 == 0:
    for h in range(0, N, 4):
        for w in range(0, N, 4):
            calc4(h, w)

    for i in lst:
        print (*i, sep = '')
    exit()

def calc5(x, y):
    lst[x][y] = 'a'
    lst[x][y + 1] = 'a'

    lst[x][y + 2] = 'b'
    lst[x][y + 3] = 'b'

    lst[x][y + 4] = 'c'
    lst[x + 1][y + 4] = 'c'


    lst[x + 1][y] = 'd'
    lst[x + 2][y] = 'd'

    lst[x + 1][y + 1] = 'e'
    lst[x + 2][y + 1] = 'e'

    lst[x + 2][y + 4] = 'f'
    lst[x + 3][y + 4] = 'f'

    lst[x + 3][y] = 'g'
    lst[x + 4][y] = 'g'

    lst[x + 3][y + 2] = 'h'
    lst[x + 3][y + 3] = 'h'

    lst[x + 4][y + 1] = 'i'
    lst[x + 4][y + 2] = 'i'

    lst[x + 4][y + 3] = 'j'
    lst[x + 4][y + 4] = 'j'

if N % 5 == 0:
    for h in range(0, N, 5):
        for w in range(0, N, 5):
            calc5(h, w)

    for i in lst:
        print (*i, sep = '')
    exit()

def calc7(x, y):
    lst[x][y + 1] = 'a'
    lst[x][y + 2] = 'a'

    lst[x][y + 3] = 'b'
    lst[x][y + 4] = 'b'

    lst[x][y + 5] = 'c'
    lst[x][y + 6] = 'c'

    lst[x + 1][y] = 'd'
    lst[x + 2][y] = 'd'

    lst[x + 1][y + 3] = 'e'
    lst[x + 1][y + 4] = 'e'

    lst[x + 1][y + 5] = 'f'
    lst[x + 1][y + 6] = 'f'

    lst[x + 3][y] = 'g'
    lst[x + 4][y] = 'g'

    lst[x + 3][y + 1] = 'h'
    lst[x + 4][y + 1] = 'h'

    lst[x + 3][y + 2] = 'i'
    lst[x + 4][y + 2] = 'i'

    lst[x + 5][y] = 'j'
    lst[x + 6][y] = 'j'

    lst[x + 5][y + 1] = 'k'
    lst[x + 6][y + 1] = 'k'

    lst[x + 5][y + 2] = 'l'
    lst[x + 6][y + 2] = 'l'
    
    lst[x + 2][y + 3] = 'm'
    lst[x + 2][y + 4] = 'm'

    lst[x + 2][y + 5] = 'n'
    lst[x + 2][y + 6] = 'n'

if N % 7 == 0:
    for h in range(0, N, 7):
        for w in range(0, N, 7):
            calc7(h, w)

    for i in lst:
        print (*i, sep = '')
    exit()

if N <= 2:
    print (-1)
    exit()

count = 0
n = N
while True:
    if n % 5 == 0:
        flag = True
        break
    if n % 7 == 0:
        flag = False
        break
    n -= 4
    count += 1

for i in range(count):
    calc4(4 * i, 4 * i)


if flag:
    for i in range(4 * count, N, 5):
        calc5(i, i)
else:
    for i in range(4 * count, N, 7):
        calc7(i, i)


for i in lst:
        print (*i, sep = '')

Solution (Python):
def join(xs, N):
    n = len(xs)
    r = ""
    s = 0
    for i, x in enumerate(xs):
        x = x.strip()
        t = len(x.split())
        S,E="."*s,"."*(N-t-s)
        for l in x.split():
            r+=S+l+E+"\n"
        s += t
    return r
s7="""
.bc.a..
.bc.a..
..aabba
aab...a
x.baa..
x..b.aa
aa.b.xx
"""
s6="""
..aabx
d...bx
dla...
.lab..
xx.bxx
ddaall"""
s5="""
aa.bx
d..bx
dee.a
i.rra
ijjxx
"""
s4="""
xyzz
xyrr
zzxy
rrxy"""
s3="""
jj.
..j
..j"""

s=[s4,s5,s6,s7]
N=int(input())
if N<3:
    print(-1)
elif N==3:
    print(s3.strip())
elif N<=7:
    print(s[N-4].strip())
else:
    print(join([s[0]]*(N//4-1)+[s[N%4]],N))

Solution (Python):


N = int(input())
precomputed = {
    4: ["abcc", "abdd", "eefg", "hhfg"],
    5: ["abc..", "abc..", "ddeff", "g.ehh", "giijj"],
    6: ["abc...", "abc...", "d.eeff", "dghh..", ".g.ijj", "kk.ill"],
    7: ["abc....", "abc....", "def....", "def....", "g..hhii", "g..jjkk", ".llmmnn"],
}
if N == 2:
    print(-1)
elif N == 3:
    print("aa.")
    print("..b")
    print("..b")
else:
    K = N // 4
    M = N % 4
    assert 4 * K + M == N
    K -= 1
    M += 4
    assert K >= 0
    assert M in [4, 5, 6, 7]
    assert 4 * K + M == N
    grid = [['.' for j in range(N)] for i in range(N)]

    for k in range(K):
        for i in range(4):
            for j in range(4):
                grid[4 * k + i][4 * k + j] = precomputed[4][i][j]
    for i in range(M):
        for j in range(M):
            grid[4 * K + i][4 * K + j] = precomputed[M][i][j]
    for row in grid:
        print("".join(row))

Solution (Python):
def search(field, xs, ys, n, q, c, r):
    if r == 0:
        return field
    for i in range(n):
        if xs[i] >= q:
            continue
        can_vertical = i < n - 1 and xs[i + 1] < q
        for j in range(n):
            if ys[j] >= q:
                continue
            if field[i][j] != 0:
                continue
            if can_vertical and field[i + 1][j] == 0:
                field[i][j] = field[i + 1][j] = c
                xs[i] += 1
                xs[i + 1] += 1
                ys[j] += 1
                result = search(field, xs, ys, n, q, c + 1, r - 1)
                if result is not None:
                    return result
                field[i][j] = field[i + 1][j] = 0
                xs[i] -= 1
                xs[i + 1] -= 1
                ys[j] -= 1
            if j < n - 1 and ys[j + 1] < q and field[i][j + 1] == 0:
                field[i][j] = field[i][j + 1] = c
                xs[i] += 1
                ys[j] += 1
                ys[j + 1] += 1
                result = search(field, xs, ys, n, q, c + 1, r - 1)
                if result is not None:
                    return result
                field[i][j] = field[i][j + 1] = 0
                xs[i] -= 1
                ys[j] -= 1
                ys[j + 1] -= 1
    return None


def search_main(n, q=3):
    print(n, q)
    field = [[0] * n for _ in range(n)]
    xs = [0] * n
    ys = [0] * n
    remain = n * 2 * q // 3
    result = search(field, xs, ys, n, q, 1, remain)
    if result is None:
        print(None)
    else:
        for row in result:
            print(' '.join('{:02d}'.format(x) for x in row))


def solve(n):
    if n <= 2:
        print(-1)
        return
    if n == 3:
        print('aab\nc.b\ncdd')
        return
    field = [['.'] * n for _ in range(n)]
    r = n % 4
    repeat = [n, n - 5, n - 6, n - 7]
    for i in range(0, repeat[r], 4):
        field[i + 0][i:i + 4] = list('aacd')
        field[i + 1][i:i + 4] = list('bbcd')
        field[i + 2][i:i + 4] = list('efgg')
        field[i + 3][i:i + 4] = list('efhh')

    if r == 1:
        field[n - 5][n - 5:n] = list('abc..')
        field[n - 4][n - 5:n] = list('abc..')
        field[n - 3][n - 5:n] = list('deeff')
        field[n - 2][n - 5:n] = list('d.ghh')
        field[n - 1][n - 5:n] = list('iigjj')
    elif r == 2:
        field[n - 6][n - 6:n] = list('abc...')
        field[n - 5][n - 6:n] = list('abc...')
        field[n - 4][n - 6:n] = list('d.eeff')
        field[n - 3][n - 6:n] = list('dghh..')
        field[n - 2][n - 6:n] = list('.g.ijj')
        field[n - 1][n - 6:n] = list('kk.ill')
    elif r == 3:
        field[n - 7][n - 7:n] = list('abc....')
        field[n - 6][n - 7:n] = list('abc....')
        field[n - 5][n - 7:n] = list('def....')
        field[n - 4][n - 7:n] = list('def....')
        field[n - 3][n - 7:n] = list('g..hhii')
        field[n - 2][n - 7:n] = list('g..jjkk')
        field[n - 1][n - 7:n] = list('.llmmnn')

    print('\n'.join(map(''.join, field)))






n = int(input())
solve(n)

Solution (Python):
n=int(input());f=["aabbc","hi..c","hi..d","g.jjd","gffee"];x=list("cdcd");a=[n*["."]for _ in range(n)];z={2:[-1],3:["abb","a.c","ddc"],5:["aabbc","hi..c","hi..d","g.jjd","gffee"],7:["..abc..","..abc..","aax..aa","bbx..bb","cc.yycc","..abc..","..abc.."]}
if n in z:print(*z[n]);exit()
if n%2:
  for i in range(5):
    for j in range(5):a[-i-1][-j-1]=f[i][j]
  n-=5
for i in range(0,n,2):a[i][i],a[i][i+1],a[i+1][i],a[i+1][i+1]=list("aabb")
if n%4:
  for i in range(0,n,2):a[i][(i+2)%n],a[i][(i+3)%n],a[i+1][(i+2)%n],a[i+1][(i+3)%n]=x
else:
  for i in range(0,n,2):a[n-i-2][i],a[n-i-2][i+1],a[n-i-1][i],a[n-i-1][i+1]=x
for i in a:print("".join(i))

Solution (Python):
n=int(input());f=["aabbc","hi..c","hi..d","g.jjd","gffee"];x=["c","d"]
if n==2:print(-1);exit()
if n==3:print("abb","a.c","ddc");exit()
if n==5:print("aabbc","hi..c","hi..d","g.jjd","gffee");exit()
if n==7:print("..abc..","..abc..","aax..aa","bbx..bb","cc.yycc","..abc..","..abc..");exit()
a=[n*["."]for _ in range(n)]
if n%2:
  for i in range(5):
    for j in range(5):a[-i-1][-j-1]=f[i][j]
  n-=5
for i in range(0,n,2):
  a[i][i],a[i][i+1]="a","a"
  a[i+1][i],a[i+1][i+1]="b","b"
if n%4==0:
  for i in range(0,n,2):
    a[n-i-2][i],a[n-i-2][i+1]=x
    a[n-i-1][i],a[n-i-1][i+1]=x
else:
  for i in range(0,n,2):
    a[i][(i+2)%n],a[i][(i+3)%n]=x
    a[i+1][(i+2)%n],a[i+1][(i+3)%n]=x
for i in a:print("".join(i))

Solution (Python):
n=int(input())
f=["aabbc","hi..c","hi..d","g.jjd","gffee"]
if n==2:print(-1);exit()
if n==3:print("abb","a.c","ddc");exit()
if n==5:print("aabbc","hi..c","hi..d","g.jjd","gffee");exit()
if n==7:print("..abc..","..abc..","aax..aa","bbx..bb","cc.yycc","..abc..","..abc..");exit()
a=[n*["."]for _ in range(n)]
if n%2:
  for i in range(5):
    for j in range(5):a[-i-1][-j-1]=f[i][j]
  n-=5
for i in range(0,n,2):
  a[i][i],a[i][i+1]="a","a"
  a[i+1][i],a[i+1][i+1]="b","b"
if n%4==0:
  for i in range(0,n,2):
    a[n-i-2][i],a[n-i-2][i+1]="c","d"
    a[n-i-1][i],a[n-i-1][i+1]="c","d"
else:
  for i in range(0,n-2,2):
    a[i][i+2],a[i][i+3]="c","d"
    a[i+1][i+2],a[i+1][i+3]="c","d"
  a[n-2][0],a[n-2][1]="c","d"
  a[n-1][0],a[n-1][1]="c","d"
for i in a:print("".join(i))

Solution (Python):
n=int(input())
if n==2:
  print(-1)
  exit()
if n==3:
  print("abb")
  print("a.c")
  print("ddc")
  exit()
if n==5:
  print("aabbc","hi..c","hi..d","g.jjd","gffee")
  exit()
if n==7:
  print("..abc..")
  print("..abc..")
  print("aax..aa")
  print("bbx..bb")
  print("cc.yycc")
  print("..abc..")
  print("..abc..")
  exit()
a=[n*["."]for _ in range(n)]
if n%2:
  a[-5][-5],a[-5][-4],a[-5][-3],a[-5][-2],a[-5][-1]=list("aabbc")
  a[-4][-5],a[-4][-4],a[-4][-3],a[-4][-2],a[-4][-1]=list("hi..c")
  a[-3][-5],a[-3][-4],a[-3][-3],a[-3][-2],a[-3][-1]=list("hi..d")
  a[-2][-5],a[-2][-4],a[-2][-3],a[-2][-2],a[-2][-1]=list("g.jjd")
  a[-1][-5],a[-1][-4],a[-1][-3],a[-1][-2],a[-1][-1]=list("gffee")
  n-=5
for i in range(0,n,2):
  a[i][i],a[i][i+1]="a","a"
  a[i+1][i],a[i+1][i+1]="b","b"
if n%4==0:
  for i in range(0,n,2):
    a[n-i-2][i],a[n-i-2][i+1]="c","d"
    a[n-i-1][i],a[n-i-1][i+1]="c","d"
else:
  for i in range(0,n-2,2):
    a[i][i+2],a[i][i+3]="c","d"
    a[i+1][i+2],a[i+1][i+3]="c","d"
  a[n-2][0],a[n-2][1]="c","d"
  a[n-1][0],a[n-1][1]="c","d"
for i in a:print("".join(i))

Solution (Python):

N = int(input())


if N == 3:

    ans = [".aa","b..","b.."]

    for i in ans:
        print (i)

elif N >= 4:

    four = ["abcc","abdd","hhfg","eefg"]
    five = ["ahhgg","ai..f","bi..f","b.jje","ccdde"]
    six = ["add.ff","a.ce..","bbce..",".ffadd","e..a.c","e..bbc"]
    seven = [".iijjkk","fadd...","fa.c...","gbbc...","g...add","h...a.c","h...bbc"]

    ans = [ ["."] * N for i in range(N) ]

    now = 0
    while N-now > 7:

        for i in range(4):
            for j in range(4):

                ans[i+now][j+now] = four[i][j]

        now += 4

    if N - now == 4:
        for i in range(4):
            for j in range(4):

                ans[i+now][j+now] = four[i][j]

    elif N - now == 5:
        for i in range(5):
            for j in range(5):

                ans[i+now][j+now] = five[i][j]

    elif N - now == 6:
        for i in range(6):
            for j in range(6):

                ans[i+now][j+now] = six[i][j]

    else:
        for i in range(7):
            for j in range(7):

                ans[i+now][j+now] = seven[i][j]

    for i in range(N):
        print ("".join(ans[i]))

else:
    print (-1)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

n = int(input())
pata = ["abb", 
        "a.c", 
        "ddc"]

patb = ["abcc",
        "abdd",
        "eefg",
        "hhfg"]

patc = ["abbcc",
        "ad..e",
        "fd..e",
        "f.ggh",
        "iijjh"]

patd = [".aabbcc",
        "a.ddee.",
        "ad....d",
        "bd....d",
        "be....e",
        "ce....e",
        "c.ddee."]

pate = [".aabbcc....",
        "a.ddee.....",
        "ad....d....",
        "bd....d....",
        "be....e....",
        "ce....e....",
        "c.ddee.....",
        ".......abcc",
        ".......abdd",
        ".......ccab",
        ".......ddab"]

cnt = 0

def create_ab(w):
    val = [["."] * w for _ in range(w)]
    ok = False
    for fives in range(200):
        if (w - fives * 5) % 4 == 0:
            fours = (w - fives * 5) // 4
            if fours >= 0:
                ok = True
                break
    if not ok:
        return None
    t = 0
    for i in range(fives):
        for i, line in enumerate(patc):
            for j, ch in enumerate(line):
                val[t+i][t+j] = ch
        t += 5
    for i in range(fours):
        for i, line in enumerate(patb):
            for j, ch in enumerate(line):
                val[t+i][t+j] = ch
        t += 4 
    ret = []
    for line in val:
        ret.append("".join([str(item) for item in line]))
    return ret

def solve(n):
    global cnt
    if n % 3 == 0:
        repeat = n // 3
        for i in range(repeat):
            for line in pata:
                print(line * repeat)
    elif n % 4 == 0:
        repeat = n // 4
        for i in range(repeat):
            for line in patb:
                print(line * repeat)
    elif n % 5 == 0:
        repeat = n // 5
        for i in range(repeat):
            for line in patc:
                print(line * repeat)
    elif n % 7 == 0:
        repeat = n // 7
        for i in range(repeat):
            for line in patd:
                print(line * repeat)
    elif n % 11 == 0:
        repeat = n // 11
        for i in range(repeat):
            for line in pate:
                print(line * repeat)
    else:
        ret = create_ab(n)
        if ret:
            for line in ret:
                print(line)
        else:
            cnt += 1
            print(-1)

solve(n)

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
if n<20:
    if n==2:
        print(-1)
    elif n==7:
        print('aabbcc.')
        print('dd.dd.a')
        print('..d..da')
        print('..d..db')
        print('dd.dd.b')
        print('..d..dc')
        print('..d..dc')
    elif n%3==0:
        x=n//3
        for i in range(x):
            print('...'*i+'a..'+'...'*(x-1-i))
            print('...'*i+'a..'+'...'*(x-1-i))
            print('...'*i+'.bb'+'...'*(x-1-i))
    elif n%4==0:
        x=n//4
        for i in range(x):
            print('....'*i+'abcc'+'....'*(x-1-i))
            print('....'*i+'abdd'+'....'*(x-1-i))
            print('....'*i+'eegh'+'....'*(x-1-i))
            print('....'*i+'ffgh'+'....'*(x-1-i))
    elif n%5==0:
        x=n//5
        for i in range(x):
            print('.....'*i+'aabcc'+'.....'*(x-1-i))
            print('.....'*i+'d.b.f'+'.....'*(x-1-i))
            print('.....'*i+'d.eef'+'.....'*(x-1-i))
            print('.....'*i+'gg.ij'+'.....'*(x-1-i))
            print('.....'*i+'.hhij'+'.....'*(x-1-i))
    elif n==11:
        print('a..cee'+'.....')
        print('a..c.f'+'.....')
        print('.bbddf'+'.....')
        print('ceea..'+'.....')
        print('c.fa..'+'.....')
        print('ddf.bb'+'.....')
        print('......'+'aabcc')
        print('......'+'d.b.f')
        print('......'+'d.eef')
        print('......'+'gg.ij')
        print('......'+'.hhij')
    else:
        if n%5==0:
            a=0
            b=n//5
        elif n%5==1:
            a=4
            b=(n-16)//5
        elif n%5==2:
            a=3
            b=(n-12)//5
        elif n%5==3:
            a=2
            b=(n-8)//5
        elif n%5==4:
            a=1
            b=(n-4)//5
        x=a
        for i in range(x):
            print('....'*i+'abcc'+'....'*(x-1-i)+'.....'*b)
            print('....'*i+'abdd'+'....'*(x-1-i)+'.....'*b)
            print('....'*i+'eegh'+'....'*(x-1-i)+'.....'*b)
            print('....'*i+'ffgh'+'....'*(x-1-i)+'.....'*b)
        x=b
        for i in range(x):
            print('....'*a+'.....'*i+'aabcc'+'.....'*(x-1-i))
            print('....'*a+'.....'*i+'d.b.f'+'.....'*(x-1-i))
            print('....'*a+'.....'*i+'d.eef'+'.....'*(x-1-i))
            print('....'*a+'.....'*i+'gg.ij'+'.....'*(x-1-i))
            print('....'*a+'.....'*i+'.hhij'+'.....'*(x-1-i))
else:
    if n%5==0:
        a=0
        b=n//5
    elif n%5==1:
        a=4
        b=(n-16)//5
    elif n%5==2:
        a=3
        b=(n-12)//5
    elif n%5==3:
        a=2
        b=(n-8)//5
    elif n%5==4:
        a=1
        b=(n-4)//5
    x=a
    for i in range(x):
        print('....'*i+'abcc'+'....'*(x-1-i)+'.....'*b)
        print('....'*i+'abdd'+'....'*(x-1-i)+'.....'*b)
        print('....'*i+'eegh'+'....'*(x-1-i)+'.....'*b)
        print('....'*i+'ffgh'+'....'*(x-1-i)+'.....'*b)
    x=b
    for i in range(x):
        print('....'*a+'.....'*i+'aabcc'+'.....'*(x-1-i))
        print('....'*a+'.....'*i+'d.b.f'+'.....'*(x-1-i))
        print('....'*a+'.....'*i+'d.eef'+'.....'*(x-1-i))
        print('....'*a+'.....'*i+'gg.ij'+'.....'*(x-1-i))
        print('....'*a+'.....'*i+'.hhij'+'.....'*(x-1-i))