Problem p02825 (python)

Fixed submission:
import sys
readline = sys.stdin.readline

imp = set([1, 2])

N = int(readline())
Five = set([5, 10, 14, 19, 23])
dot = ord('.')
if N in imp:
    print(-1)
else:
    G = [[dot]*N for _ in range(N)]
    if N == 7:
        G[0][0] = 97
        G[0][1] = 97
        G[0][2] = 98
        G[0][3] = 98
        G[1][1] = 99
        G[1][2] = 99
        G[2][2] = 100
        G[2][3] = 100
        G[0][6] = 101
        G[1][6] = 101
        G[1][5] = 102
        G[2][5] = 102
        G[2][6] = 103
        G[3][6] = 103
        G[3][4] = 104
        G[4][4] = 104
        G[3][0] = 105
        G[4][0] = 105
        G[5][0] = 106
        G[6][0] = 106
        G[4][1] = 107
        G[5][1] = 107
        G[5][4] = 108
        G[5][5] = 108
        G[6][3] = 109
        G[6][4] = 109
        G[6][5] = 110
        G[6][6] = 110
    elif N == 11:
        G[0][0] = 97
        G[0][1] = 97
        G[0][2] = 98
        G[0][3] = 98
        G[1][1] = 99
        G[1][2] = 99
        G[2][2] = 100
        G[2][3] = 100
        G[0][6] = 101
        G[1][6] = 101
        G[1][5] = 102
        G[2][5] = 102
        G[2][6] = 103
        G[3][6] = 103
        G[3][4] = 104
        G[4][4] = 104
        G[3][0] = 105
        G[4][0] = 105
        G[5][0] = 106
        G[6][0] = 106
        G[4][1] = 107
        G[5][1] = 107
        G[5][4] = 108
        G[5][5] = 108
        G[6][3] = 109
        G[6][4] = 109
        G[6][5] = 110
        G[6][6] = 110
        st = 7
        G[st][st] = 97
        G[st][st+1] = 97
        G[st+1][st] = 98
        G[st+1][st+1] = 98
        G[st+2][st+2] = 99
        G[st+2][st+3] = 99
        G[st+3][st+2] = 100
        G[st+3][st+3] = 100
        G[st+2][st] = 101
        G[st+3][st] = 101
        G[st+2][st+1] = 102
        G[st+3][st+1] = 102
        G[st][st+2] = 103
        G[st+1][st+2] = 103
        G[st][st+3] = 104
        G[st+1][st+3] = 104

    elif N in Five:
        for five in range(N):
            if (N - 5*five) % 9 == 0:
                nine = (N - 5*five) // 9
                break
        for i in range(five):
            st = i*5
            for j in range(3):
                G[st+j][st] = 97+j
                G[st+j][st+1] = 97+j
            G[st][st+2] = 100
            G[st][st+3] = 100
            G[st][st+4] = 101
            G[st+1][st+4] = 101
            G[st+2][st+4] = 102
            G[st+2][st+3] = 102
            G[st+2][st+2] = 103
            G[st+1][st+2] = 103
            for j in range(3):
                G[st+3][st+2+j] = 104+j
                G[st+4][st+2+j] = 104+j
        geta = 5*five
        for i in range(nine):
            st = geta + 9*i
            for j in range(3):
                for k in range(3):
                    G[st+3*j][st+3*k] = 97
                    G[st+3*j][st+3*k+1] = 97
                    G[st+3*j+1][st+3*k+2] = 97
                    G[st+3*j+2][st+3*k+2] = 97
    elif N % 3 == 0:
        for i in range(0, N, 3):
            G[i][i] = 97
            G[i][i+1] = 97
            G[i+1][i+2] = 97
            G[i+2][i+2] = 97
    else:
        for nine in range(N):
            if (N-9*nine) % 4 == 0:
                four = (N-9*nine) // 4
                break
        for i in range(nine):
            st = i*9
            for j in range(3):
                for k in range(3):
                    G[st+3*j][st+3*k] = 97
                    G[st+3*j][st+3*k+1] = 97
                    G[st+3*j+1][st+3*k+2] = 97
                    G[st+3*j+2][st+3*k+2] = 97
        geta = 9*nine
        for i in range(four):
            st = geta + i*4
            G[st][st] = 97
            G[st][st+1] = 97
            G[st+1][st] = 98
            G[st+1][st+1] = 98
            G[st+2][st+2] = 99
            G[st+2][st+3] = 99
            G[st+3][st+2] = 100
            G[st+3][st+3] = 100
            G[st+2][st] = 101
            G[st+3][st] = 101
            G[st+2][st+1] = 102
            G[st+3][st+1] = 102
            G[st][st+2] = 103
            G[st+1][st+2] = 103
            G[st][st+3] = 104
            G[st+1][st+3] = 104
    print('\n'.join([''.join(map(chr, g)) for g in G]))

Buggy submission (verdict: Runtime Error):
        
import sys
readline = sys.stdin.readline

imp = set([1, 2, 5, 7, 10, 11, 14, 19, 23])

N = int(readline())
assert N != 5, ''
dot = ord('.')
if N in imp:
    print(-1)
else:
    G = [[dot]*N for _ in range(N)]
    if N % 3 == 0:
        for i in range(0, N, 3):
            G[i][i] = 97
            G[i][i+1] = 97
            G[i+1][i+2] = 97
            G[i+2][i+2] = 97
    else:
        for nine in range(N):
            if (N-9*nine) % 4 == 0:
                four = (N-9*nine) // 4
                break
        for i in range(nine):
            st = i*9
            for j in range(3):
                for k in range(3):
                    G[st+3*j][st+3*k] = 97
                    G[st+3*j][st+3*k+1] = 97
                    G[st+3*j+1][st+3*k+2] = 97
                    G[st+3*j+2][st+3*k+2] = 97
        geta = 9*nine
        for i in range(four):
            st = geta + i*4
            G[st][st] = 97
            G[st][st+1] = 97
            G[st+1][st] = 98
            G[st+1][st+1] = 98
            G[st+2][st+2] = 99
            G[st+2][st+3] = 99
            G[st+3][st+2] = 100
            G[st+3][st+3] = 100
            G[st+2][st] = 101
            G[st+3][st] = 101
            G[st+2][st+1] = 102
            G[st+3][st+1] = 102
            G[st][st+2] = 103
            G[st+1][st+2] = 103
            G[st][st+3] = 104
            G[st+1][st+3] = 104
    print('\n'.join([''.join(map(chr, g)) for g in G]))