Problem p02735 (python)

Fixed submission:
import math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans
'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)
'''


#a = list(map(int, input().split()))

h,w = list(map(int, input().split()))

s = []
for i in range(h):
    ss=[]
    a=input()
    for j in range(w):
        if(a[j]=='.'):
            ss.append(0)
        else:
            ss.append(1)
    s.append(ss)
        
dp = [[999 for j in range(w)]for i in range(h)]
dp[0][0] = s[0][0]

for i in range(h+w-2):
    for j in range(i+2):
        x = j
        y = i+1-j
        
        if(0<= x and x<=h-1 and 0<=y and y<=w-1):
            #print(x,y)
            if(s[x][y]==0):
                dp[x][y] = min(dp[max(0,x-1)][y], dp[x][max(0,y-1)])
            else:
                if(x!=0 and s[x-1][y]==1):
                    dp[x][y] = dp[x-1][y]
                elif(x!=0 and s[x-1][y]==0):
                    dp[x][y] = dp[x-1][y]+1
                if(y!=0 and s[x][y-1]==1):
                    dp[x][y] = min(dp[x][y],dp[x][y-1])
                elif(y!=0 and s[x][y-1]==0):
                    dp[x][y] = min(dp[x][y],dp[x][y-1]+1)
                   




print(dp[-1][-1])

Buggy submission (verdict: Runtime Error):
import math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans
'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)
'''


#a = list(map(int, input().split()))

h,w = list(map(int, input().split()))

s = []
for i in range(h):
    ss=[]
    a=input()
    for j in range(w):
        if(a[j]=='.'):
            ss.append(0)
        else:
            ss.append(1)
    s.append(ss)
        
dp = [[999 for j in range(w)]for i in range(h)]
dp[0][0] = s[0][0]

for i in range(h+w-2):
    for j in range(i+2):
        x = j
        y = i+1-j
        
        if(0<= x and x<=w-1 and 0<=y and y<=h-1):
            #print(x,y)
            if(s[x][y]==0):
                dp[x][y] = min(dp[max(0,x-1)][y], dp[x][max(0,y-1)])
            else:
                if(x!=0 and s[x-1][y]==1):
                    dp[x][y] = dp[x-1][y]
                elif(x!=0 and s[x-1][y]==0):
                    dp[x][y] = dp[x-1][y]+1
                if(y!=0 and s[x][y-1]==1):
                    dp[x][y] = min(dp[x][y],dp[x][y-1])
                elif(y!=0 and s[x][y-1]==0):
                    dp[x][y] = min(dp[x][y],dp[x][y-1]+1)
                   




print(dp[-1][-1])