TLE in Jan long challenge Doremon (CHFDORA)

I tried solving CHFDORA from Jan long challenge but I am getting TLE in the last two test cases. I am using Python3. If anyone can point my error that would be great. Thanks.

    # cook your dish here
for _ in range(int(input())):
    n,m = map(int,input().split())
    matrix = []
    for _ in range(n):
        a = list(map(int,input().split()))
    ans = n*m
    for i in range(1,n-1):
        for j in range(1,m-1):
            if matrix[i-1][j] == matrix[i+1][j] and matrix[i][j-1]==matrix[i][j+1]:
                ans += 1
                k = 2
                while i-k >= 0 and i+k <n and j-k >= 0 and j+k <m and (matrix[i-k][j] == matrix[i+k][j]) and (matrix[i][j-k]==matrix[i][j+k]):
                    ans += 1
                    k += 1

See this: Same logic, one AC other TLE

This does not solve my problem.

I have tried using fast input like stdin in python and unfortunately according to me, you cannot pass the last two test cases in python 3.6 . If you want to get the python code AC, here is a solution though. PyPy3 is a faster implementation of python3 that speeds up python programs. You can just copy your existing python3 code and paste it and submit it. You will surely get AC. Link to the AC code.