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()))
matrix.append(a)
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
print(ans)
```