# Arup Guha
# 7/12/2025
# Solution to 2025 SI@UCF CP Camp Contest #1 Problem: Bandit

nC = int(input())

# Process cases.
for loop in range(nC):

    toks = input().split()
    r = int(toks[0])
    c = int(toks[1])

    # Will store my r+c parity.
    myparity = -1

    # Will store the number of guards at both parities.
    guardparity = [0,0]

    # Read the grid.
    for i in range(r):

        # Get this line.
        s = input().strip()

        # Go through the characters.
        for j in range(c):

            # It's me, log my parity.
            if s[j] == 'X':
                myparity = (i+j)%2

            # Update guard parity.
            if s[j] == '#':
                guardparity[(i+j)%2] += 1


    # Print accordingly. You can get away from 1 guard on your "parity" at most.
    if guardparity[myparity] < 2:
        print(1)
    else:
        print(0)
