Problem:


Score : 400 points

Problem Statement

We have a grid with H rows and W columns. At first, all cells were painted white.

Snuke painted N of these cells. The i-th ( 1 \leq i \leq N ) cell he painted is the cell at the a_i-th row and b_i-th column.

Compute the following:

For each integer j ( 0 \leq j \leq 9 ), how many subrectangles of size 3×3 of the grid contains exactly j black cells, after Snuke painted N cells?

Constraints

3 \leq H \leq 10^9

3 \leq W \leq 10^9

0 \leq N \leq min(10^5,H×W)

1 \leq a_i \leq H (1 \leq i \leq N)

1 \leq b_i \leq W (1 \leq i \leq N)

(a_i, b_i) \neq (a_j, b_j) (i \neq j)

Input

The input is given from Standard Input in the following format:

H W N
a_1 b_1
:
a_N b_N

Output

Print 10 lines.
The (j+1)-th ( 0 \leq j \leq 9 ) line should contain the number of the subrectangles of size 3×3 of the grid that contains exactly j black cells.

Sample Input 1

4 5 8
1 1
1 4
1 5
2 3
3 1
3 2
3 4
4 4

Sample Output 1

0
0
0
2
4
0
0
0
0
0

There are six subrectangles of size 3×3. Two of them contain three black cells each, and the remaining four contain four black cells each.

Sample Input 2

10 10 20
1 1
1 4
1 9
2 5
3 10
4 2
4 7
5 9
6 4
6 6
6 7
7 1
7 3
7 7
8 1
8 5
8 10
9 2
10 4
10 9

Sample Output 2

4
26
22
10
2
0
0
0
0
0

Sample Input 3

1000000000 1000000000 0

Sample Output 3

999999996000000004
0
0
0
0
0
0
0
0
0



Sample input:
4 5 8
1 1
1 4
1 5
2 3
3 1
3 2
3 4
4 4


Sample output:
0
0
0
2
4
0
0
0
0
0

Submission status: Time Limit Exceeded

Solution code:
H,W,N=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]
rows=[]
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b.append(i)
    rows.append(ab[i-1][0])

if N>0:
  rows.append(ab[N-1][0])
  r_b.append(N)

cols=[[ab[j][1] for j in range(r_b[i],r_b[i+1])] for i in range(len(r_b)-1)]

rN=len(cols)


ched_box=[[[[False]*3 for k in range(3)] for j in range((len(cols[i]) if i<rN else 0))] for i in range(3)]

area_n=[0]*9

for rn in range(rN):
  cN=len(cols[rn])
  row=rows[rn]

  if rn!=0:
    ched_box[0]=ched_box[1]
    ched_box[1]=ched_box[2]
    if rn<rN-2:
      ched_box[2]=[[[False]*3 for k in range(3)] for j in range(len(cols[rn+2]))]
  
  dic_cn=[dict(zip(cols[rn],list(range(cN))))]
  if (rows[rn+1]==row+1 if rn<rN-1 else False):
    row1=row+1
    cN1=len(cols[rn+1])
    dic_cn.append(dict(zip(cols[rn+1],list(range(cN1)))))
    if (rows[rn+2]==row+2 if rn<rN-2 else False):
      row2=row+2
      cN2=len(cols[rn+2])
      dic_cn.append(dict(zip(cols[rn+2],list(range(cN2)))))
    else:
      row2=None
  elif (rows[rn+1]==row+2 if rn<rN-1 else False):
    row1=row+2
    cN1=len(cols[rn+1])
    dic_cn.append(dict(zip(cols[rn+1],list(range(cN1)))))
    row2=None
  else:
    row1=None
    row2=None
  
  for cn in range(cN):
    col=cols[rn][cn]
       
    for boxr in range(3):
      for boxc in range(3):
        if col-2+boxc<1 or col+boxc>W or row-2+boxr<1 or row+boxr>H:
          ched_box[0][cn][boxr][boxc]=True
    find_rowcol=[]
    for ch_row in range(row,min(row+3,H+1)):
      if ch_row==row:
        for ch_col in range(col+1,min(col+3,W+1)):
          if ch_col in dic_cn[0]:
            find_rowcol.append([row,ch_col])
      elif ch_row==row1:
        for ch_col in range(max(col-2,1),min(col+3,W+1)):
          if ch_col in dic_cn[1]:
            find_rowcol.append([row1,ch_col])
      elif ch_row==row2:
        for ch_col in range(max(col-2,1),min(col+3,W+1)):
          if ch_col in dic_cn[2]:
            find_rowcol.append([row2,ch_col])
            
    count=[[1]*3 for i in range(3)]

    for [find_row,find_col] in find_rowcol:

      if find_row==row:
        ch_rn=0
        ch_cn=dic_cn[0][find_col]
      elif find_row==row1:
        ch_rn=1
        ch_cn=dic_cn[1][find_col]
      elif find_row==row2:
        ch_rn=2
        ch_cn=dic_cn[2][find_col]
      for box_row in range(find_row-row,3):
        for box_col in range(max(find_col-col,0),min(3,3+find_col-col)):
          if not ched_box[0][cn][box_row][box_col]:
            count[box_row][box_col]+=1
            ched_box[ch_rn][ch_cn][box_row-find_row+row][box_col-find_col+col]=True
    for box_row in range(3):
      for box_col in range(3):
        if not ched_box[0][cn][box_row][box_col]:
          area_n[count[box_row][box_col]-1]+=1



print((W-2)*(H-2)-sum(area_n))
for i in range(9):
  print(area_n[i])

Submission status: Time Limit Exceeded

Solution code:
H,W,N=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]
rows=[]
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b.append(i)
    rows.append(ab[i-1][0])

if N>0:
  rows.append(ab[N-1][0])
  r_b.append(N)

cols=[[ab[j][1] for j in range(r_b[i],r_b[i+1])] for i in range(len(r_b)-1)]

rN=len(cols)


ched_box=[[[[False]*3 for k in range(3)] for j in range((len(cols[i]) if i<rN else 0))] for i in range(3)]

area_n=[0]*9

for rn in range(rN):
  cN=len(cols[rn])
  row=rows[rn]

  if rn!=0:
    ched_box[0]=ched_box[1]
    ched_box[1]=ched_box[2]
    if rn<rN-2:
      ched_box[2]=[[[False]*3 for k in range(3)] for j in range(len(cols[rn+2]))]
  
  diccol=[dict(zip(cols[rn],list(range(cN))))]
  if (rows[rn+1]==row+1 if rn<rN-1 else False):
    row1=row+1
    cN1=len(cols[rn+1])
    diccol.append(dict(zip(cols[rn+1],list(range(cN1)))))
    if (rows[rn+2]==row+2 if rn<rN-2 else False):
      row2=row+2
      cN2=len(cols[rn+2])
      diccol.append(dict(zip(cols[rn+2],list(range(cN2)))))
    else:
      row2=None
  elif (rows[rn+1]==row+2 if rn<rN-1 else False):
    row1=row+2
    cN1=len(cols[rn+1])
    diccol.append(dict(zip(cols[rn+1],list(range(cN1)))))
    row2=None
  else:
    row1=None
    row2=None
  
  for cn in range(cN):
    col=cols[rn][cn]
       
    for boxr in range(3):
      for boxc in range(3):
        if col-2+boxc<1 or col+boxc>W or row-2+boxr<1 or row+boxr>H:
          ched_box[0][cn][boxr][boxc]=True
    find_ab=[set([]) for i in range(3)]
    for chr in range(row,min(row+3,H+1)):
      if chr==row:
        for chc in range(col+1,min(col+3,W+1)):
          if chc in diccol[0]:
            find_ab[0].add(chc)
      elif chr==row1:
        for chc in range(max(col-2,1),min(col+3,W+1)):
          if chc in diccol[1]:
            find_ab[1].add(chc)
      elif chr==row2:
        for chc in range(max(col-2,1),min(col+3,W+1)):
          if chc in diccol[2]:
            find_ab[2].add(chc)



    for boxr in range(3):
      for boxc in range(3):
        if not ched_box[0][cn][boxr][boxc]:
          count=1
          for i in range(3):
            for j in range(3):
              if row-2+boxr+i==row:
                if col-2+boxc+j in find_ab[0]:
                  count+=1
                  ched_box[0][diccol[0][col-2+boxc+j]][2-i][2-j]=True
              elif row-2+boxr+i==row1:
                if col-2+boxc+j in find_ab[1]:
                  count+=1
                  ched_box[1][diccol[1][col-2+boxc+j]][2-i][2-j]=True
              elif row-2+boxr+i==row2:
                if col-2+boxc+j in find_ab[2]:
                  count+=1
                  ched_box[2][diccol[2][col-2+boxc+j]][2-i][2-j]=True              

                


                

          area_n[count-1]+=1

print((W-2)*(H-2)-sum(area_n))
for i in range(9):
  print(area_n[i])

Submission status: Time Limit Exceeded

Solution code:
h,w,n=map(int,input().split())
jl=[0]*10
ab=[]
if n==0:
  print((w-2)*(h-2))
  for i in range(9):
    print(0)
  exit()
for i in range(n):
  a,b=map(int,input().split())
  a-=1
  b-=1
  for j in range(max(0,b-2),min(w-2,b+1)):
    for k in range(max(0,a-2),min(h-2,a+1)):
      ab=ab+[[k,j]]

ab.sort(key=lambda x: x[1])
ab.sort()
flag=False
count=1
now=ab[0]
for i in ab[1:]:
  if i==now:
    count+=1
  else:
    jl[count]+=1
    count=1
    now=i
jl[count]+=1
jl[0]=(h-2)*(w-2)-sum(jl[1:])
for i in range(10):
  print(jl[i])

Submission status: Time Limit Exceeded

Solution code:
H,W,N=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]
rows=[]
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b.append(i)
    rows.append(ab[i-1][0])
  if i==N-1:
    r_b.append(N)
    rows.append(ab[N-1][0])

cols=[[ab[j][1] for j in range(r_b[i],r_b[i+1])] for i in range(len(r_b)-1)]

rN=len(cols)

ched_box=[[[[False]*3 for k in range(3)] for j in range((len(cols[i]) if i<rN else 0))] for i in range(3)]

area_n=[0]*9

for rn in range(rN):
  cN=len(cols[rn])
  row=rows[rn]

  if rn!=0:
    ched_box[0]=ched_box[1]
    ched_box[1]=ched_box[2]
    if rn<rN-2:
      ched_box[2]=[[[False]*3 for k in range(3)] for j in range(len(cols[rn+2]))]
  
  setcol=[]
  if (rows[rn+1]==row+1 if rn<rN-1 else False):
    row1=row+1
    setcol.append(set(cols[rn+1]))
    if (rows[rn+2]==row+2 if rn<rN-2 else False):
      row2=row+2
      setcol.append(set(cols[rn+2]))
    else:
      row2=None
  elif (rows[rn+1]==row+2 if rn<rN-1 else False):
    row1=row+2
    setcol.append(set(cols[rn+1]))
    row2=None
  else:
    row1=None
    row2=None
  
  for cn in range(cN):
    col=cols[rn][cn]
       
    for boxr in range(3):
      for boxc in range(3):
        if col-2+boxc<1 or col+boxc>W or row-2+boxr<1 or row+boxr>H:
          ched_box[0][cn][boxr][boxc]=True
    find_ab=[]
    for chr in range(row,min(row+3,H+1)):
      if chr==row:
        ch_col=set(cols[rn][cn+1:min(cn+3,cN)])
        for chc in range(min(col+1,W),min(col+3,W+1)):
          if chc in ch_col:
            find_ab.append([row,chc])
      elif chr in [row+1,row+2]:
        if chr==row1:
          ch_col=setcol[0]
        elif chr==row2:
          ch_col=setcol[1]
        else:
          ch_col=[]
        for chc in range(max(col-2,1),min(col+3,W+1)):
          if chc in ch_col:
            find_ab.append([chr,chc])

    
    for boxr in range(3):
      for boxc in range(3):
        if not ched_box[0][cn][boxr][boxc]:
          count=1
          for i in range(3):
            for j in range(3):
              if [row-2+boxr+i,col-2+boxc+j] in find_ab:
                count+=1
                if row-2+boxr+i==row1:
                  rn_tmp=rn+1
                elif row-2+boxr+i==row2:
                  rn_tmp=rn+2
                else:
                  rn_tmp=rn
                ched_box[rn_tmp-rn][cols[rn_tmp].index(col-2+boxc+j)][2-i][2-j]=True
          area_n[count-1]+=1

print((W-2)*(H-2)-sum(area_n))
for i in range(9):
  print(area_n[i])

Submission status: Time Limit Exceeded

Solution code:
H,W,N=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b.append(i)
r_b.append(N)

ab=[ab[r_b[i]:r_b[i+1]] for i in range(len(r_b)-1)]

if N is 0:
  ab=[]

checked=[[[[False]*3 for k in range(3)]  for j in range(len(ab[i]))] for i in range(len(ab))]

area_n=[0 for i in range(1,10)]  

for r_n in range(len(ab)):
  row=ab[r_n][0][0]
  
  ch_list_min_add=[0,0]
  
  for c_n in range(len(ab[r_n])):
    col=ab[r_n][c_n][1]

    if row<=2:
      for box_r in range(3-row):
        for box_c in range(3):
          checked[r_n][c_n][box_r][box_c]=True
    if row>=H-1:
      for box_r in range(H+1-row,3):
        for box_c in range(3):
          checked[r_n][c_n][box_r][box_c]=True
    if col<=2:
      for box_r in range(3):
        for box_c in range(3-col):
          checked[r_n][c_n][box_r][box_c]=True
    if col>=W-1:
      for box_r in range(3):
        for box_c in range(W+1-col,3):
          checked[r_n][c_n][box_r][box_c]=True
    ch_p_rc=[[],[]]
    min_flag=[True,True]
    ch_start_add=ch_list_min_add[:]
    for ch_r in range(row,min(row+3,H+1)):
      if ch_r==row:
        for ch_c in range(min(col+1,W),min(col+3,W+1)):
          for j in range(min(c_n+1,len(ab[r_n])),min(c_n+3,len(ab[r_n]))):
            if ch_c<ab[r_n][j][1]:
              break
            if ab[r_n][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n,j])
              break
      elif ch_r==row+1 and (ab[r_n+1][0][0]==row+1 if r_n<len(ab)-1 else False):
        ch_c=max(col-2,1)
        while(ch_c<min(col+3,W+1)):
          for j in range(ch_start_add[0],len(ab[r_n+1])):
            if ch_c<ab[r_n+1][j][1]:
              ch_c=ab[r_n+1][j][1]
              ch_start_add[0]=j
              if min_flag[0]:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
            if ab[r_n+1][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+1,j])
              ch_c+=1
              ch_start_add[0]=min(j+1,len(ab[r_n+1])-1)
              if min_flag[0] and ch_c>col-2:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
            if j==len(ab[r_n+1])-1:
              ch_c=min(col+3,W+1)
      elif ch_r==row+2 and (ab[r_n+1][0][0]==row+2 if r_n<len(ab)-1 else False):
        ch_c=max(col-2,1)
        while(ch_c<min(col+3,W+1)):
           for j in range(ch_start_add[0],len(ab[r_n+1])):
            if ch_c<ab[r_n+1][j][1]:
              ch_c=ab[r_n+1][j][1]
              ch_start_add[0]=j
              if min_flag[0]:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
            if ab[r_n+1][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+1,j])
              ch_c+=1
              ch_start_add[0]=min(j+1,len(ab[r_n+1])-1)
              if min_flag[0] and ch_c>col-2:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
            if j==len(ab[r_n+1])-1:
              ch_c=min(col+3,W+1)
      elif ch_r==row+2 and (ab[r_n+2][0][0]==row+2 if r_n<len(ab)-2 else False):
        ch_c=max(col-2,1)
        while(ch_c<min(col+3,W+1)):
          for j in range(ch_start_add[1],len(ab[r_n+2])):
            if ch_c<ab[r_n+2][j][1]:
              ch_c=ab[r_n+2][j][1]
              ch_start_add[1]=j
              if min_flag[1]:
                min_flag[1]=False
                ch_list_min_add[1]=j
              break
            if ab[r_n+2][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+2,j])
              ch_c+=1
              ch_start_add[1]=min(j+1,len(ab[r_n+2])-1)
              if min_flag[1] and ch_c>col-2:
                min_flag[1]=False
                ch_list_min_add[1]=j
              break
            if j==len(ab[r_n+2])-1:
              ch_c=min(col+3,W+1)

    for box_r in range(3):
      for box_c in range(3):
        if not checked[r_n][c_n][box_r][box_c]:
          checked[r_n][c_n][box_r][box_c]=True
          count=1
          for ch_r in range(row,row+box_r+1):
            for ch_c in range(col+box_c-2 if ch_r!=row else col+1,col+box_c+1):
              if [ch_r,ch_c] in ch_p_rc[0]:
                add=ch_p_rc[1][ch_p_rc[0].index([ch_r,ch_c])]

                checked[add[0]][add[1]][box_r+row-ch_r][box_c+col-ch_c]=True
                count+=1
       
                
          area_n[count-1]+=1
          
print((H-2)*(W-2)-sum(area_n))
for i in range(9):
  print(area_n[i])

Submission status: Time Limit Exceeded

Solution code:
H,W,N=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b.append(i)
r_b.append(N)

ab=[ab[r_b[i]:r_b[i+1]] for i in range(len(r_b)-1)]

if N is 0:
  ab=[]

checked=[[[[False]*3 for k in range(3)]  for j in range(len(ab[i]))] for i in range(len(ab))]

area_n=[0 for i in range(1,10)]  

for r_n in range(len(ab)):
  row=ab[r_n][0][0]
  
  ch_list_min_add=[0,0]
  
  for c_n in range(len(ab[r_n])):
    col=ab[r_n][c_n][1]

    if row<=2:
      for box_r in range(3-row):
        for box_c in range(3):
          checked[r_n][c_n][box_r][box_c]=True
    if row>=H-1:
      for box_r in range(H+1-row,3):
        for box_c in range(3):
          checked[r_n][c_n][box_r][box_c]=True
    if col<=2:
      for box_r in range(3):
        for box_c in range(3-col):
          checked[r_n][c_n][box_r][box_c]=True
    if col>=W-1:
      for box_r in range(3):
        for box_c in range(W+1-col,3):
          checked[r_n][c_n][box_r][box_c]=True
    ch_p_rc=[[],[]]
    min_flag=[True,True]
    for ch_r in range(row,min(row+3,H+1)):
      if ch_r==row:
        for ch_c in range(min(col+1,W),min(col+3,W+1)):
          for j in range(min(c_n+1,len(ab[r_n])),min(c_n+3,len(ab[r_n]))):
            if ch_c<ab[r_n][j][1]:
              break
            if ab[r_n][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n,j])
              break
      elif ch_r==row+1 and (ab[r_n+1][0][0]==row+1 if r_n<len(ab)-1 else False):
        ch_c=max(col-2,1)
        while(ch_c<min(col+3,W+1)):
          for j in range(ch_list_min_add[0],len(ab[r_n+1])):
            if ch_c<ab[r_n+1][j][1]:
              ch_c=ab[r_n+1][j][1]
              if min_flag[0]:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
            if ab[r_n+1][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+1,j])
              ch_c+=1
              if min_flag[0] and ch_c>col-2:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
            if j==len(ab[r_n+1])-1:
              ch_c=min(col+3,W+1)
      elif ch_r==row+2 and (ab[r_n+1][0][0]==row+2 if r_n<len(ab)-1 else False):
        ch_c=max(col-2,1)
        while(ch_c<min(col+3,W+1)):
           for j in range(ch_list_min_add[0],len(ab[r_n+1])):
            if ch_c<ab[r_n+1][j][1]:
              ch_c=ab[r_n+1][j][1]
              if min_flag[0]:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
            if ab[r_n+1][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+1,j])
              ch_c+=1
              if min_flag[0] and ch_c>col-2:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
            if j==len(ab[r_n+1])-1:
              ch_c=min(col+3,W+1)
      elif ch_r==row+2 and (ab[r_n+2][0][0]==row+2 if r_n<len(ab)-2 else False):
        ch_c=max(col-2,1)
        while(ch_c<min(col+3,W+1)):
          for j in range(ch_list_min_add[1],len(ab[r_n+2])):
            if ch_c<ab[r_n+2][j][1]:
              ch_c=ab[r_n+2][j][1]
              if min_flag[1]:
                min_flag[1]=False
                ch_list_min_add[1]=j
              break
            if ab[r_n+2][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+2,j])
              ch_c+=1
              if min_flag[1] and ch_c>col-2:
                min_flag[1]=False
                ch_list_min_add[1]=j
              break
            if j==len(ab[r_n+2])-1:
              ch_c=min(col+3,W+1)

    for box_r in range(3):
      for box_c in range(3):
        if not checked[r_n][c_n][box_r][box_c]:
          checked[r_n][c_n][box_r][box_c]=True
          count=1
          for ch_r in range(row,row+box_r+1):
            for ch_c in range(col+box_c-2 if ch_r!=row else col+1,col+box_c+1):
              if [ch_r,ch_c] in ch_p_rc[0]:
                add=ch_p_rc[1][ch_p_rc[0].index([ch_r,ch_c])]

                checked[add[0]][add[1]][box_r+row-ch_r][box_c+col-ch_c]=True
                count+=1
       
                
          area_n[count-1]+=1
          
print((H-2)*(W-2)-sum(area_n))
for i in range(9):
  print(area_n[i])

Submission status: Time Limit Exceeded

Solution code:
H,W,N=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b.append(i)
r_b.append(N)

ab=[ab[r_b[i]:r_b[i+1]] for i in range(len(r_b)-1)]

if N is 0:
  ab=[]

checked=[[[[False]*3 for k in range(3)]  for j in range(len(ab[i]))] for i in range(len(ab))]

area_n=[0 for i in range(1,10)]  

for r_n in range(len(ab)):
  row=ab[r_n][0][0]
  
  ch_list_min_add=[0,0]
  
  for c_n in range(len(ab[r_n])):
    col=ab[r_n][c_n][1]

    if row<=2:
      for box_r in range(3-row):
        for box_c in range(3):
          checked[r_n][c_n][box_r][box_c]=True
    if row>=H-1:
      for box_r in range(H+1-row,3):
        for box_c in range(3):
          checked[r_n][c_n][box_r][box_c]=True
    if col<=2:
      for box_r in range(3):
        for box_c in range(3-col):
          checked[r_n][c_n][box_r][box_c]=True
    if col>=W-1:
      for box_r in range(3):
        for box_c in range(W+1-col,3):
          checked[r_n][c_n][box_r][box_c]=True
    ch_p_rc=[[],[]]
    min_flag=[True,True]
    for ch_r in range(row,min(row+3,H+1)):
      if ch_r==row:
        for ch_c in range(min(col+1,W),min(col+3,W+1)):
          for j in range(min(c_n+1,len(ab[r_n])),min(c_n+3,len(ab[r_n]))):
            if ch_c<ab[r_n][j][1]:
              break
            if ab[r_n][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n,j])
              break
      elif ch_r==row+1 and (ab[r_n+1][0][0]==row+1 if r_n<len(ab)-1 else False): 
        for ch_c in range(max(col-2,1),min(col+3,W+1)):
          for j in range(ch_list_min_add[0],len(ab[r_n+1])):
            if ch_c<ab[r_n+1][j][1]:
              if min_flag[0]:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
            if ab[r_n+1][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+1,j])
              if min_flag[0] and ch_c>col-2:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
      elif ch_r==row+2 and (ab[r_n+1][0][0]==row+2 if r_n<len(ab)-1 else False):
        for ch_c in range(max(col-2,1),min(col+3,W+1)):
           for j in range(ch_list_min_add[0],len(ab[r_n+1])):
            if ch_c<ab[r_n+1][j][1]:
              if min_flag[0]:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
            if ab[r_n+1][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+1,j])
              if min_flag[0] and ch_c>col-2:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
      elif ch_r==row+2 and (ab[r_n+2][0][0]==row+2 if r_n<len(ab)-2 else False):
        for ch_c in range(max(col-2,1),min(col+3,W+1)):
          for j in range(ch_list_min_add[1],len(ab[r_n+2])):
            if ch_c<ab[r_n+2][j][1]:
              if min_flag[1]:
                min_flag[1]=False
                ch_list_min_add[1]=j
              break
            if ab[r_n+2][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+2,j])
              if min_flag[1] and ch_c>col-2:
                min_flag[1]=False
                ch_list_min_add[1]=j
              break

    for box_r in range(3):
      for box_c in range(3):
        if not checked[r_n][c_n][box_r][box_c]:
          checked[r_n][c_n][box_r][box_c]=True
          count=1
          for ch_r in range(row,row+box_r+1):
            for ch_c in range(col+box_c-2 if ch_r!=row else col+1,col+box_c+1):
              if [ch_r,ch_c] in ch_p_rc[0]:
                add=ch_p_rc[1][ch_p_rc[0].index([ch_r,ch_c])]

                checked[add[0]][add[1]][box_r+row-ch_r][box_c+col-ch_c]=True
                count+=1
       
                
          area_n[count-1]+=1
          
print((H-2)*(W-2)-sum(area_n))
for i in range(9):
  print(area_n[i])

Submission status: Time Limit Exceeded

Solution code:
H,W,N=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b.append(i)
r_b.append(N)

ab=[ab[r_b[i]:r_b[i+1]] for i in range(len(r_b)-1)]

if N is 0:
  ab=[]

checked=[[[[False]*3 for k in range(3)]  for j in range(len(ab[i]))] for i in range(len(ab))]

area_n=[0 for i in range(1,10)]  

for r_n in range(len(ab)):
  row=ab[r_n][0][0]
  
  ch_list_min_add=[0,0]
  
  for c_n in range(len(ab[r_n])):
    col=ab[r_n][c_n][1]

    if row<=2:
      for box_r in range(3-row):
        for box_c in range(3):
          checked[r_n][c_n][box_r][box_c]=True
    if row>=H-1:
      for box_r in range(H+1-row,3):
        for box_c in range(3):
          checked[r_n][c_n][box_r][box_c]=True
    if col<=2:
      for box_r in range(3):
        for box_c in range(3-col):
          checked[r_n][c_n][box_r][box_c]=True
    if col>=W-1:
      for box_r in range(3):
        for box_c in range(W+1-col,3):
          checked[r_n][c_n][box_r][box_c]=True
    ch_p_rc=[[],[]]
    min_flag=[True,True]
    for ch_r in range(row,min(row+3,H+1)):
      if ch_r==row:
        for ch_c in range(min(col+1,W),min(col+3,W+1)):
          for j in range(min(c_n+1,len(ab[r_n])),min(c_n+3,len(ab[r_n]))):
            if ch_c<ab[r_n][j][1]:
              break
            if ab[r_n][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n,j])
              break
      elif ch_r==row+1 and (ab[r_n+1][0][0]==row+1 if r_n<len(ab)-1 else False): 
        for ch_c in range(max(col-2,1),min(col+3,W+1)):
          for j in range(ch_list_min_add[0],len(ab[r_n+1])):
            if ch_c<ab[r_n+1][j][1]:
              break
            if ab[r_n+1][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+1,j])
              if min_flag[0]:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
      elif ch_r==row+2 and (ab[r_n+1][0][0]==row+2 if r_n<len(ab)-1 else False):
        for ch_c in range(max(col-2,1),min(col+3,W+1)):
           for j in range(ch_list_min_add[0],len(ab[r_n+1])):
            if ch_c<ab[r_n+1][j][1]:
              break
            if ab[r_n+1][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+1,j])
              if min_flag[0]:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
      elif ch_r==row+2 and (ab[r_n+2][0][0]==row+2 if r_n<len(ab)-2 else False):
        for ch_c in range(max(col-2,1),min(col+3,W+1)):
          for j in range(ch_list_min_add[1],len(ab[r_n+2])):
            if ch_c<ab[r_n+2][j][1]:
              break
            if ab[r_n+2][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+2,j])
              if min_flag[1]:
                min_flag[1]=False
                ch_list_min_add[1]=j
              break

    for box_r in range(3):
      for box_c in range(3):
        if not checked[r_n][c_n][box_r][box_c]:
          checked[r_n][c_n][box_r][box_c]=True
          count=1
          for ch_r in range(row,row+box_r+1):
            for ch_c in range(col+box_c-2 if ch_r!=row else col+1,col+box_c+1):
              if [ch_r,ch_c] in ch_p_rc[0]:
                add=ch_p_rc[1][ch_p_rc[0].index([ch_r,ch_c])]

                checked[add[0]][add[1]][box_r+row-ch_r][box_c+col-ch_c]=True
                count+=1
       
                
          area_n[count-1]+=1
          
print((H-2)*(W-2)-sum(area_n))
for i in range(9):
  print(area_n[i])

Submission status: Time Limit Exceeded

Solution code:
H,W,N=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b.append(i)
r_b.append(N)

ab=[ab[r_b[i]:r_b[i+1]] for i in range(len(r_b)-1)]

if N is 0:
  ab=[]

checked=[[[[False]*3 for k in range(3)]  for j in range(len(ab[i]))] for i in range(len(ab))]

area_n=[0 for i in range(1,10)]  

for r_n in range(len(ab)):
  row=ab[r_n][0][0]
  
  ch_list_min_add=[0,0]
  
  for c_n in range(len(ab[r_n])):
    col=ab[r_n][c_n][1]

    if row<=2:
      for box_r in range(3-row):
        for box_c in range(3):
          checked[r_n][c_n][box_r][box_c]=True
    if row>=H-1:
      for box_r in range(H+1-row,3):
        for box_c in range(3):
          checked[r_n][c_n][box_r][box_c]=True
    if col<=2:
      for box_r in range(3):
        for box_c in range(3-col):
          checked[r_n][c_n][box_r][box_c]=True
    if col>=W-1:
      for box_r in range(3):
        for box_c in range(W+1-col,3):
          checked[r_n][c_n][box_r][box_c]=True
    ch_p_rc=[[],[]]
    min_flag=[True,True]
    for ch_r in range(row,min(row+3,H+1)):
      if ch_r==row:
        for ch_c in range(min(col+1,W),min(col+3,W+1)):
          for j in range(min(c_n+1,len(ab[r_n])),min(c_n+3,len(ab[r_n]))):
            if ch_c<ab[r_n][j][1]:
              break
            if ab[r_n][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n,j])
              break
      elif ch_r==row+1 and (ab[r_n+1][0][0]==row+1 if r_n<len(ab)-1 else False): 
        for ch_c in range(max(col-2,1),min(col+3,W+1)):
          for j in range(0,len(ab[r_n+1])):
            if ch_c<ab[r_n+1][j][1]:
              break
            if ab[r_n+1][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+1,j])
              if min_flag[0]:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
      elif ch_r==row+2 and (ab[r_n+1][0][0]==row+2 if r_n<len(ab)-1 else False):
        for ch_c in range(max(col-2,1),min(col+3,W+1)):
           for j in range(0,len(ab[r_n+1])):
            if ch_c<ab[r_n+1][j][1]:
              break
            if ab[r_n+1][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+1,j])
              if min_flag[0]:
                min_flag[0]=False
                ch_list_min_add[0]=j
              break
      elif ch_r==row+2 and (ab[r_n+2][0][0]==row+2 if r_n<len(ab)-2 else False):
        for ch_c in range(max(col-2,1),min(col+3,W+1)):
          for j in range(0,len(ab[r_n+2])):
            if ch_c<ab[r_n+2][j][1]:
              break
            if ab[r_n+2][j]==[ch_r,ch_c]:
              ch_p_rc[0].append([ch_r,ch_c])
              ch_p_rc[1].append([r_n+2,j])
              if min_flag[1]:
                min_flag[1]=False
                ch_list_min_add[1]=j
              break

    for box_r in range(3):
      for box_c in range(3):
        if not checked[r_n][c_n][box_r][box_c]:
          checked[r_n][c_n][box_r][box_c]=True
          count=1
          for ch_r in range(row,row+box_r+1):
            for ch_c in range(col+box_c-2 if ch_r!=row else col+1,col+box_c+1):
              if [ch_r,ch_c] in ch_p_rc[0]:
                add=ch_p_rc[1][ch_p_rc[0].index([ch_r,ch_c])]

                checked[add[0]][add[1]][box_r+row-ch_r][box_c+col-ch_c]=True
                count+=1
       
                
          area_n[count-1]+=1
          
print((H-2)*(W-2)-sum(area_n))
for i in range(9):
  print(area_n[i])