想用有限状态机解的,但是算法循环多了,导致超时

row,col = map(int,input().split())
minetb = [[0 for _ in range (col)] for _ in range(row) ]
state = 0
x,y=0,0
allstate = [[0,1],[1,0],[1,-1],[-1,1],[-1,0],[0,-1],[-1,-1],[1,1]]
for i in range(row):
    minetb[i] = list(str1 for str1 in input())
for i in range(row):
    for j in range(col):
        if(minetb[i][j] == '*' ):
            continue
        if(minetb[i][j]=='.'):
            minetb[i][j]=0
        count = 0
        for h in allstate:
            x= 0
            y= 0
            x=h[0]+i
            y=h[1]+j
            if(row>x>=0 and col>y>=0 ):
                if(minetb[x][y]=='*'):
                    minetb[i][j]+=1
for i in range(row):
    for j in range(col):
        print(minetb[i][j],end="")
    print()