Buggy python submission (verdict: Time Limit Exceeded):
# TLE

import numpy as np
import sys
sys.setrecursionlimit(2000)



def solve(M, num1):

    Mr = np.copy(M)
    Mc = np.copy(M)


    H = M.shape[0]
    W = M.shape[1]

    ret = 0

    # if M.sum() == 0:
    if num1 == 0:
        return ret
    elif H == W == 1:
        return M[0][0]


    del_row = 100000
    del_col = 100000
    num1r = num1
    num1c = num1
    retr = 0
    retc = 0
    if H > 1:
        if Mr[0][0] == 1:
            h = 1
            w = 1
            # while h < H and M[:h + 1, :w].sum() == (h + 1) * w:
            while h < H and Mr[h][0] == 1:
                h += 1
            # while w < W and Mr[:h, :w + 1].sum() == h * (w + 1):
            while w < W and Mr[:h, w:w + 1].sum() == h:
                w += 1

            for i in range(h):
                for j in range(w):
                    Mr[i][j] = 0
                    num1r -= 1
            retr += 1

        del_row = solve(Mr[1:, :], num1r)


    if W > 1:

        if Mc[0][0] == 1:

            h = 1
            w = 1

            # while w < W and Mc[:h, :w + 1].sum() == h * (w + 1):
            while w < W and Mc[0][w] == 1:
                w += 1
            # while h < H and Mc[:h + 1, :w].sum() == (h + 1) * w:
            while h < H and Mc[h:h + 1, :w].sum() == w:
                h += 1


            for i in range(h):
                for j in range(w):
                    Mc[i][j] = 0
                    num1c -= 1
            retc += 1

        del_col = solve(Mc[:, 1:], num1c)

    return min(retr + del_row, retc + del_col)


H, W = [int(x) for x in input().split()]
s = [list(input()) for i in range(H)]
M = np.array([[ (0 if s[i][j] == '.' else 1) for j in range(W) ] for i in range(H)])

print(solve(M, M.sum()) )


Fixed submission:
import numpy as np

H, W = [int(x) for x in input().split()]
s = [list(input()) for i in range(H)]

M = np.array([[ (0 if s[i][j] == '.' else 1) for j in range(W) ] for i in range(H)])
INF = 10 ** 9
dp = np.array([[INF for j in range(W)] for i in range(H)])

for i in range(H - 1, -1, -1):
    for j in range(W - 1, -1, -1):
        ans_r = INF
        ans_d = INF
        if i == H - 1 and j == W - 1:
            dp[i][j] = M[i][j]
            continue
        if j < W - 1:  # 右に動ける
            if M[i][j] == 1 and M[i][j + 1] == 0:
                ans_r = dp[i][j + 1] + 1
            else:
                ans_r = dp[i][j + 1]
        if i < H - 1:  # 下に動ける
            if M[i][j] == 1 and M[i + 1][j] == 0:
                ans_d = dp[i + 1][j] + 1
            else:
                ans_d = dp[i + 1][j]
        dp[i][j] = min(ans_r, ans_d)

print(dp[0][0])

Buggy python submission (verdict: Time Limit Exceeded):
# TLE

import numpy as np
import sys
sys.setrecursionlimit(2000)

H, W = [int(x) for x in input().split()]
s = [list(input()) for i in range(H)]
M = np.array([[ (0 if s[i][j] == '.' else 1) for j in range(W) ] for i in range(H)])

def solve(M):

    Mr = np.copy(M)
    Mc = np.copy(M)


    H = M.shape[0]
    W = M.shape[1]

    ret = 0

    if M.sum() == 0:
        return ret
    elif H == W == 1:
        return M[0][0]


    del_row = 100000
    del_col = 100000
    retr = 0
    retc = 0
    if H > 1:
        if Mr[0][0] == 1:
            h = 1
            w = 1
            # while h < H and M[:h + 1, :w].sum() == (h + 1) * w:
            while h < H and Mr[h][0] == 1:
                h += 1
            # while w < W and Mr[:h, :w + 1].sum() == h * (w + 1):
            while w < W and Mr[:h, w:w + 1].sum() == h:
                w += 1

            for i in range(h):
                for j in range(w):
                    Mr[i][j] = 0
            retr += 1

        del_row = solve(Mr[1:, :])


    if W > 1:

        if Mc[0][0] == 1:

            h = 1
            w = 1

            # while w < W and Mc[:h, :w + 1].sum() == h * (w + 1):
            while w < W and Mc[0][w] == 1:
                w += 1
            # while h < H and Mc[:h + 1, :w].sum() == (h + 1) * w:
            while h < H and Mc[h:h + 1, :w].sum() == w:
                h += 1


            for i in range(h):
                for j in range(w):
                    Mc[i][j] = 0
            retc += 1

        del_col = solve(Mc[:, 1:])

    return min(retr + del_row, retc + del_col)


print(solve(M))


Fixed submission:
import numpy as np

H, W = [int(x) for x in input().split()]
s = [list(input()) for i in range(H)]

M = np.array([[ (0 if s[i][j] == '.' else 1) for j in range(W) ] for i in range(H)])
INF = 10 ** 9
dp = np.array([[INF for j in range(W)] for i in range(H)])

for i in range(H - 1, -1, -1):
    for j in range(W - 1, -1, -1):
        ans_r = INF
        ans_d = INF
        if i == H - 1 and j == W - 1:
            dp[i][j] = M[i][j]
            continue
        if j < W - 1:  # 右に動ける
            if M[i][j] == 1 and M[i][j + 1] == 0:
                ans_r = dp[i][j + 1] + 1
            else:
                ans_r = dp[i][j + 1]
        if i < H - 1:  # 下に動ける
            if M[i][j] == 1 and M[i + 1][j] == 0:
                ans_d = dp[i + 1][j] + 1
            else:
                ans_d = dp[i + 1][j]
        dp[i][j] = min(ans_r, ans_d)

print(dp[0][0])

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import sys
sys.setrecursionlimit(2000)

H, W = [int(x) for x in input().split()]
s = [list(input()) for i in range(H)]
M = np.array([[ (0 if s[i][j] == '.' else 1) for j in range(W) ] for i in range(H)])

def solve(M):

    Mr = np.copy(M)
    Mc = np.copy(M)


    H = M.shape[0]
    W = M.shape[1]

    ret = 0

    if M.sum() == 0:
        return ret
    elif H == W == 1:
        return M[0][0]


    del_row = 100000
    del_col = 100000
    retr = 0
    retc = 0
    if H > 1:
        if Mr[0][0] == 1:
            h = 1
            w = 1
            while h < H and M[:h + 1, :w].sum() == (h + 1) * w:
                h += 1
            while w < W and M[:h, :w + 1].sum() == h * (w + 1):
                w += 1

            for i in range(h):
                for j in range(w):
                    Mr[i][j] = 0
            retr += 1

        del_row = solve(Mr[1:, :])


    if W > 1:

        if Mc[0][0] == 1:

            h = 1
            w = 1

            while w < W and Mc[:h, :w + 1].sum() == h * (w + 1):
                w += 1
            while h < H and Mc[:h + 1, :w].sum() == (h + 1) * w:
                h += 1


            for i in range(h):
                for j in range(w):
                    Mc[i][j] = 0
            retc += 1

        del_col = solve(Mc[:, 1:])

    return min(retr + del_row, retc + del_col)


print(solve(M))


Fixed submission:
import numpy as np

H, W = [int(x) for x in input().split()]
s = [list(input()) for i in range(H)]

M = np.array([[ (0 if s[i][j] == '.' else 1) for j in range(W) ] for i in range(H)])
INF = 10 ** 9
dp = np.array([[INF for j in range(W)] for i in range(H)])

for i in range(H - 1, -1, -1):
    for j in range(W - 1, -1, -1):
        ans_r = INF
        ans_d = INF
        if i == H - 1 and j == W - 1:
            dp[i][j] = M[i][j]
            continue
        if j < W - 1:  # 右に動ける
            if M[i][j] == 1 and M[i][j + 1] == 0:
                ans_r = dp[i][j + 1] + 1
            else:
                ans_r = dp[i][j + 1]
        if i < H - 1:  # 下に動ける
            if M[i][j] == 1 and M[i + 1][j] == 0:
                ans_d = dp[i + 1][j] + 1
            else:
                ans_d = dp[i + 1][j]
        dp[i][j] = min(ans_r, ans_d)

print(dp[0][0])