A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called $k$-balanced if every substring of size $k$ of this bitstring has an equal amount of 0 and 1 characters ($\frac{k}{2}$ of each).

You are given an integer $k$ and a string $s$ which is composed only of characters 0, 1, and ?. You need to determine whether you can make a $k$-balanced bitstring by replacing every ? characters in $s$ with either 0 or 1.

A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows.

The first line of each test case contains two integers $n$ and $k$ ($2 \le k \le n \le 3 \cdot 10^5$, $k$ is even)  — the length of the string and the parameter for a balanced bitstring.

The next line contains the string $s$ ($|s| = n$). It is given that $s$ consists of only 0, 1, and ?.

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.


-----Output-----

For each test case, print YES if we can replace every ? in $s$ with 0 or 1 such that the resulting bitstring is $k$-balanced, or NO if it is not possible.


-----Example-----
Input
9
6 4
100110
3 2
1?1
3 2
1?0
4 4
????
7 4
1?0??1?
10 10
11??11??11
4 2
1??1
4 4
?0?0
6 2
????00

Output
YES
YES
NO
YES
YES
NO
NO
YES
NO



-----Note-----

For the first test case, the string is already a $4$-balanced bitstring.

For the second test case, the string can be transformed into 101.

For the fourth test case, the string can be transformed into 0110.

For the fifth test case, the string can be transformed into 1100110.

Source: codeforces | Difficulty: MEDIUM
Tags: String algorithms

Implementation


Solutions:
import sys
input = sys.stdin.readline
ins = lambda : input().rstrip()
ini = lambda : int(input().rstrip())
inm = lambda : map(int, input().rstrip().split())
inl = lambda : list(map(int, input().split()))
out = lambda x, s='\n': print(s.join(map(str, x)))
t = ini()
for _ in range(t):
	(n, k) = inm()
	s = list(ins())
	ans = True
	count0 = count1 = 0
	for i in range(k):
		tmp = -1
		for j in range(i, n, k):
			if s[j] != '?':
				if tmp != -1 and s[j] != tmp:
					ans = False
					break
				tmp = s[j]
		if not ans:
			break
		if tmp == '0':
			count0 += 1
		elif tmp == '1':
			count1 += 1
	if count0 > k // 2 or count1 > k // 2:
		ans = False
	print('YES' if ans else 'NO')


def check(s, k):
	count = {'0': 0, '1': 0, '?': 0}
	for i in range(k):
		val = '?'
		for j in range(i, len(s), k):
			if s[j] == '?':
				continue
			if val == '?':
				val = s[j]
				continue
			if val != s[j]:
				return False
		count[val] += 1
	diff = abs(count['0'] - count['1'])
	marks = count['?'] - diff
	if marks < 0:
		return False
	return marks % 2 == 0
T = int(input())
for t in range(T):
	(n, k) = [int(i) for i in input().split(' ')]
	s = input()
	ans = check(s, k)
	if ans == True:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = list(input())
	flag = 0
	bal = 0
	ques = 0
	for i in range(k):
		s = set()
		s.add(arr[i])
		for j in range(i, n, k):
			s.add(arr[j])
		if '1' in s and '0' in s:
			flag = 1
			break
		elif '0' in s and '?' in s:
			bal -= 1
		elif '1' in s and '?' in s:
			bal += 1
		elif '1' in s:
			bal += 1
		elif '0' in s:
			bal -= 1
		elif '?' in s:
			ques += 1
	if bal == 0 and ques == 0:
		pass
	elif bal != 0 and ques == 0:
		flag = 1
	elif bal == 0 and ques != 0:
		if ques % 2 != 0:
			flag = 1
	elif ques % 2 == 0:
		if bal % 2 == 0 and abs(bal) <= ques:
			pass
		else:
			flag = 1
	elif bal % 2 != 0 and abs(bal) <= ques:
		pass
	else:
		flag = 1
	if flag:
		print('NO')
	else:
		print('YES')


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
ans = []
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	one = 0
	zerr = 0
	lst = input()
	for i in range(k):
		a = '1' in lst[i::k]
		b = '0' in lst[i::k]
		if a and b:
			one = k
			zerr = k
			break
		elif a:
			one += 1
		elif b:
			zerr += 1
	if k % 2 == 0 and 2 * one <= k and (2 * zerr <= k):
		ans.append('YES')
	else:
		ans.append('NO')
for i in ans:
	print(i)


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	(n, k) = map(int, input().split())
	S = input().strip()
	A = [-1] * k
	for i in range(n):
		if S[i] == '0':
			if A[i % k] == 1:
				print('NO')
				break
			else:
				A[i % k] = 0
		if S[i] == '1':
			if A[i % k] == 0:
				print('NO')
				break
			else:
				A[i % k] = 1
	else:
		Z = 0
		O = 0
		for i in range(k):
			if A[i] == 0:
				Z += 1
			if A[i] == 1:
				O += 1
		if Z > k // 2 or O > k // 2:
			print('NO')
		else:
			print('YES')


T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	s = list(input())
	sp = [[] for _ in range(k)]
	for (i, u) in enumerate(s):
		sp[i % k].append(u)
	(ok, zero, one) = (1, 0, 0)
	for i in range(k):
		(cc0, cc1) = (0, 0)
		for u in sp[i]:
			if u == '0':
				cc0 += 1
			elif u == '1':
				cc1 += 1
		if cc0 > 0 and cc1 > 0:
			ok = 0
			break
		elif cc0 > 0:
			zero += 1
		elif cc1 > 0:
			one += 1
	if ok and zero <= k // 2 and (one <= k // 2):
		print('YES')
	else:
		print('NO')


def solve(s, n, k):
	arr = [0, 0, 0]
	i = 0
	while i < k:
		if s[i] == '1':
			arr[1] += 1
		elif s[i] == '0':
			arr[0] += 1
		else:
			arr[2] += 1
		i += 1
	if arr[0] * 2 <= k and arr[1] * 2 <= k:
		i = k
		ini = 0
		while i < n and (arr[0] * 2 <= k and arr[1] * 2 <= k):
			if s[i] != '?' and s[ini] != '?' and (s[i] != s[ini]):
				return False
			if s[i] == '1':
				arr[1] += 1
			elif s[i] == '0':
				arr[0] += 1
			else:
				arr[2] += 1
			if s[ini] == '1':
				arr[1] -= 1
			elif s[ini] == '0':
				arr[0] -= 1
			else:
				arr[2] -= 1
			ini += 1
			i += 1
		if i == n and (arr[0] * 2 <= k and arr[1] * 2 <= k):
			return True
		return False
	else:
		return False

def make(s, n):
	s = list(s)
	ini = 0
	i = k
	while i < n:
		if s[i] == '?' and s[ini] != '?':
			s[i] = s[ini]
		elif s[i] != '?' and s[ini] == '?':
			s[ini] = s[i]
		i += 1
		ini += 1
	return s
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	s = make(s, n)
	if solve(s, n, k):
		print('YES')
	else:
		print('NO')


import sys
s = '9\n6 4\n100110\n3 2\n1?1\n3 2\n1?0\n4 4\n????\n7 4\n1?0??1?\n10 10\n11??11??11\n4 2\n1??1\n4 4\n?0?0\n6 2\n????00'

def solve(data, k):
	one = 0
	zero = 0
	for bit in data[:k]:
		if bit == '1':
			one += 1
		if bit == '0':
			zero += 1
	if one > k / 2 or zero > k / 2:
		return 'NO'
	start = 0
	end = k
	while end < len(data):
		if data[start] == '1' and data[end] == '0' or (data[start] == '0' and data[end] == '1'):
			return 'NO'
		if (data[start] == '?' or data[end] == '?') and (not (data[start] == '?' and data[end] == '?')):
			if data[start] == '?':
				data[start] = data[end]
			elif data[end] == '?':
				data[end] = data[start]
		start += 1
		end += 1
	one = 0
	zero = 0
	for bit in data[len(data) - k:]:
		if bit == '1':
			one += 1
		if bit == '0':
			zero += 1
	if one > k / 2 or zero > k / 2:
		return 'NO'
	return 'YES'
s = sys.stdin.read()
s = s.split('\n')
tests = int(s[0])
line = 1
for test in range(tests):
	case = s[line]
	case = case.split(' ')
	k = int(case[1])
	line += 1
	data = list(s[line])
	line += 1
	answer = solve(data, k)
	print(answer)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	a = [0] * (n - k + 1)
	b = [0] * (n - k + 1)
	res = True
	for (i, x) in enumerate(s):
		if i + k < n:
			tp = [s[i], s[i + k]]
			if s[i] != s[i + k]:
				if '?' in tp:
					s[i] = s[i + k] = sorted(tp)[0]
				else:
					res = False
					break
		else:
			break
	if not res:
		print('NO')
		continue
	i = 0
	while i < k:
		if s[i] == '?':
			b[0] += 1
		else:
			a[0] += 2 * int(s[i]) - 1
		i += 1
	while i < n:
		a[i - k + 1] = a[i - k]
		b[i - k + 1] = b[i - k]
		if s[i - k] != '?':
			a[i - k + 1] -= 2 * int(s[i - k]) - 1
		else:
			b[i - k + 1] -= 1
		if s[i] == '?':
			b[i - k + 1] += 1
		else:
			a[i - k + 1] += 2 * int(s[i]) - 1
		i += 1
	for (x, y) in zip(a, b):
		d = y - abs(x)
		if d < 0 or d & 1:
			print('NO')
			break
	else:
		print('YES')


import math, queue, heapq
import sys
sys.setrecursionlimit(10 ** 6)
fastinput = sys.stdin.readline
fastout = sys.stdout.write
t = int(fastinput())
while t:
	t -= 1
	(n, k) = map(int, fastinput().split())
	s = input()
	count1 = [0] * k
	count2 = [0] * k
	count0 = 0
	count4 = 0
	count3 = 0
	ans = True
	for j in range(k):
		v = 0
		for i in range(j, n, k):
			v += 1
			if s[i] == '0':
				count1[j] += 1
			elif s[i] == '?':
				count2[j] += 1
		if count2[j] == v:
			count3 += 1
		elif count1[j] > 0 and count1[j] + count2[j] == v:
			count0 += 1
		elif count1[j] > 0:
			ans = False
			break
		else:
			count4 += 1
	if not ans:
		print('NO')
	elif count0 + count3 + count4 == k and count0 <= k // 2 and (count4 <= k // 2):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(x, k) = map(int, input().split())
	w = input()
	on = [False] * k
	off = [False] * k
	for i in range(x):
		if w[i] == '1':
			on[i % k] = True
		if w[i] == '0':
			off[i % k] = True
	works = True
	for i in range(k):
		if on[i] == off[i] == True:
			works = False
			break
	if on.count(True) * 2 > k or off.count(True) * 2 > k:
		works = False
	print('YES' if works == True else 'NO')


def ans(arr):
	(n, k) = (nk[0], nk[1])
	temp = [set() for _ in range(k)]
	for (i, j) in enumerate(arr):
		temp[i % k].add(j)
	if any(('0' in y and '1' in y for y in temp)) or (sum(('0' in y for y in temp)) > k // 2 or sum(('1' in y for y in temp)) > k // 2):
		return 'NO'
	return 'YES'
t = int(input())
for _ in range(t):
	nk = list(map(int, input().strip().split()))
	s = list(input())
	print(ans(s))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(input())
	flag = False
	dic = [-1 for i in range(k)]
	for i in range(n):
		if a[i] == '1' and dic[i % k] != 0:
			dic[i % k] = 1
		elif a[i] == '0' and dic[i % k] != 1:
			dic[i % k] = 0
		elif a[i] == '?':
			if dic[i % k] != -1:
				continue
			else:
				dic[i % k] = '?'
		else:
			flag = True
			break
	if flag:
		print('NO')
	else:
		s = count = 0
		for i in dic:
			if i == '?':
				count += 1
			elif i == 1:
				s += 1
		if s > k // 2 or (s < k // 2 and count == 0):
			print('NO')
		elif s == k // 2 and count == 0:
			print('YES')
		elif count != 0:
			if s == k // 2:
				print('YES')
			elif k // 2 - s <= count:
				print('YES')
			else:
				print('NO')


for i in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(input())
	if n == k:
		t = 0
		s = 0
		for x in a:
			if x == '1':
				t += 1
			if x == '0':
				s += 1
		if 2 * s <= k and 2 * t <= k:
			print('YES')
		else:
			print('NO')
	else:
		l = 'YES'
		for j in range(k):
			h = j
			s = set()
			while h < n:
				s.add(a[h])
				h += k
			if '1' in s and '0' in s:
				l = 'NO'
				break
			if '1' in s:
				h = j
				while h < n:
					a[h] = '1'
					h += k
			if '0' in s:
				h = j
				while h < n:
					a[h] = '0'
					h += k
		t = 0
		s = 0
		for j in range(k):
			if a[j] == '1':
				t += 1
			if a[j] == '0':
				s += 1
		if 2 * t > k or 2 * s > k:
			l = 'NO'
		print(l)


import math as mt
import collections as cc
import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n, k) = I()
	s = input()
	tf = 1
	ch = [-1] * k
	le = 0
	re = 0
	for i in range(n):
		if s[i] in ['0', '1']:
			now = i % k
			if ch[now] == -1:
				if s[i] == '1':
					ch[now] = 1
					le += 1
				else:
					ch[now] = 0
					re += 1
			elif ch[now] != int(s[i]):
				tf = 0
				break
	if not tf or le > k // 2 or re > k // 2:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	x = 0
	X = 0
	A = input()
	for i in range(k):
		y = '1' in A[i::k]
		z = '0' in A[i::k]
		if y and z:
			x = k
			X = k
			break
		elif y:
			x += 1
		elif z:
			X += 1
	if k % 2 == 0 and 2 * x <= k and (2 * X <= k):
		print('YES')
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	w = ''
	for i in range(k):
		fl = 0
		for j in range(i, n, k):
			if s[j] != '?':
				w += s[j]
				fl = 1
				break
		if not fl:
			w += '?'
	one = 0
	zero = 0
	for i in range(k):
		if w[i] == '1':
			one += 1
		elif w[i] == '0':
			zero += 1
	if one * 2 > k or zero * 2 > k:
		print('NO')
	else:
		left = k // 2 - one
		w = list(w)
		for i in range(k):
			if w[i] == '?':
				if left:
					w[i] = '1'
					left -= 1
				else:
					w[i] = '0'
		wi = 0
		f = True
		for i in range(n):
			if s[i] != '?' and s[i] != w[wi]:
				print('NO')
				f = False
				break
			wi += 1
			wi = wi % k
		if f != False:
			print('YES')


def solve():
	(n, k) = [int(x) for x in input().split()]
	s = input()
	sk = ['?'] * k
	for (i, c) in enumerate(s):
		if c != '?':
			if sk[i % k] != '?' and sk[i % k] != c:
				return 'NO'
			if sk[i % k] == '?':
				sk[i % k] = c
	(c1, c0, cq) = (0, 0, 0)
	for (i, c) in enumerate(sk):
		if c == '1':
			c1 += 1
		elif c == '0':
			c0 += 1
		else:
			cq += 1
	for i in range(cq + 1):
		if c1 + i == c0 + cq - i:
			return 'YES'
	return 'NO'
T = int(input())
for case in range(T):
	print(solve())


from collections import Counter
cases = int(input())
for t in range(cases):
	(n, k) = list(map(int, input().split()))
	s = input()
	s1 = list(s[:k])
	f = 0
	for i in range(k, n):
		if s[i] != '?' and s1[i % k] != '?':
			if s[i] != s1[i % k]:
				f = 1
				break
		elif s[i] != '?':
			s1[i % k] = s[i]
	if f == 1:
		print('NO')
	else:
		s1 = Counter(s1)
		v1 = s1['1']
		v2 = s1['0']
		ch1 = k // 2 - v1
		ch2 = k // 2 - v2
		if ch1 >= 0 and ch2 >= 0:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	l = len(s)
	bad = False
	n0 = 0
	n1 = 0
	nwhat = 0
	for j in range(k):
		q = j
		e = s[q]
		while q < l:
			if e == '?':
				if s[q] != '?':
					e = s[q]
			elif s[q] not in [e, '?']:
				bad = True
				break
			q += k
		if e == '0':
			n0 += 1
		elif e == '1':
			n1 += 1
		elif e == '?':
			nwhat += 1
		if bad:
			break
	if abs(n0 - n1) > nwhat:
		bad = True
	print('NO') if bad else print('YES')


import math
t = int(input())
for q in range(t):
	(n, k) = [int(i) for i in input().split()]
	s = list(input())
	t = True
	for i in range(k):
		z = 0
		x = 0
		q = 0
		for j in range(i, n, k):
			if s[j] == '1':
				x += 1
			elif s[j] == '0':
				z += 1
			else:
				q += 1
		if z and x:
			t = False
		elif z:
			for j in range(i, n, k):
				s[j] = '0'
		elif x:
			for j in range(i, n, k):
				s[j] = '1'
	z = 0
	x = 0
	q = 0
	for i in range(k):
		if s[i] == '1':
			x += 1
		elif s[i] == '0':
			z += 1
		else:
			q += 1
	if z > x + q or x > z + q:
		t = False
	if t:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	bitstring = input()
	balanced = ['?'] * k
	answer = True
	for j in range(k):
		for i in range(j, n, k):
			if bitstring[i] != '?':
				if balanced[j] != '?':
					if balanced[j] != bitstring[i]:
						answer = False
						break
				else:
					balanced[j] = bitstring[i]
		if not answer:
			break
	if answer:
		q = balanced.count('?')
		o = balanced.count('1')
		r = k // 2 - o
		answer = r >= 0 and q >= r
	print('YES' if answer else 'NO')


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List
sys.setrecursionlimit(99999)
(t,) = map(int, input().split())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	ts = ['?'] * k
	b = True
	for i in range(n):
		if s[i] in '01':
			if ts[i % k] == '?':
				ts[i % k] = s[i]
			elif ts[i % k] != s[i]:
				b = False
				break
	if not b:
		print('NO')
	else:
		cnt = k / 2
		c1 = ts.count('1')
		c0 = ts.count('0')
		if c1 > cnt or c0 > cnt:
			print('NO')
		else:
			print('YES')


import sys, math
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	mas = []
	for i in range(k):
		mas.append(s[i])
	ans = 'YES'
	for i in range(n):
		if s[i] != mas[i % k] and s[i] != '?':
			if mas[i % k] == '?':
				mas[i % k] = s[i]
			else:
				ans = 'NO'
				break
	bal = 0
	cur = 0
	for i in range(k):
		if mas[i] == '1':
			bal += 1
		elif mas[i] == '0':
			bal -= 1
		else:
			cur += 1
	if abs(bal) > abs(cur):
		ans = 'NO'
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(str(input()))
	arr = ['?'] * k
	for i in range(k):
		arr[i] = s[i]
	temp = True
	for i in range(n - k):
		if s[i] != s[i + k]:
			if s[i] == '?':
				if arr[i % k] == '?' or arr[i % k] == s[i + k]:
					arr[i % k] = s[i + k]
				else:
					temp = False
					break
			elif s[i + k] == '?':
				if arr[i % k] == '?' or arr[i % k] == s[i]:
					arr[i % k] = s[i]
				else:
					temp = False
					break
			else:
				temp = False
				break
	one = 0
	zero = 0
	q = 0
	for i in range(k):
		if arr[i] == '0':
			zero += 1
		elif arr[i] == '1':
			one += 1
		else:
			q += 1
	if temp:
		if q < abs(one - zero):
			temp = False
		elif (q - abs(one - zero)) % 2 != 0:
			temp = False
		if temp:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	I = [int(m) for m in input().split(' ')]
	(n, k) = (I[0], I[1])
	arr = [c for c in input()]
	valid = True
	zeros = 0
	ones = 0
	for i in range(k):
		curr = '?'
		for j in range(i, n, k):
			if curr == '?':
				curr = arr[j]
			elif curr == '0' and arr[j] == '1':
				valid = False
				break
			elif curr == '1' and arr[j] == '0':
				valid = False
				break
		if curr == '0':
			zeros += 1
		elif curr == '1':
			ones += 1
	if not valid:
		print('NO')
		continue
	if zeros > k // 2 or ones > k // 2:
		print('NO')
		continue
	print('YES')


from functools import reduce
tc = int(input())
for _ in range(tc):
	(n, k) = map(int, input().split(' '))
	s = input()
	l = 0
	deq = list(s)
	ones = reduce(lambda acc, x: acc + (x == '1'), deq[:k], 0)
	zeros = reduce(lambda acc, x: acc + (x == '0'), deq[:k], 0)
	questions = reduce(lambda acc, x: acc + (x == '?'), deq[:k], 0)
	gap = abs(ones - zeros)
	if gap > questions:
		print('NO')
		continue
	rest = (questions - gap) // 2
	q0 = ones > zeros and gap + rest or rest
	q1 = zeros > ones and gap + rest or rest
	l = 0
	success = True
	for r in range(k, n):
		if deq[l] == '?':
			if deq[r] == '0':
				q0 -= 1
				if q0 < 0:
					success = False
					break
			elif deq[r] == '1':
				q1 -= 1
				if q1 < 0:
					success = False
					break
		elif deq[l] == '0':
			if deq[r] == '?':
				deq[r] = '0'
			elif deq[r] == '1':
				success = False
				break
		elif deq[r] == '?':
			deq[r] = '1'
		elif deq[r] == '0':
			success = False
			break
		l += 1
	print(success and 'YES' or 'NO')


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		(n, k) = list(map(int, input().split()))
		arry = list(input())
		if n % k:
			lent = n // k + 1
		else:
			lent = n // k
		ans = True
		for j in range(k):
			curr = None
			for i in range(lent):
				now = i * k + j
				if now <= n - 1:
					if arry[now] != '?':
						if not curr:
							curr = arry[now]
							break
			if curr:
				for i in range(lent):
					now = i * k + j
					if now <= n - 1:
						if arry[now] == '?':
							arry[now] = curr
						elif arry[now] != '?' and curr != arry[now]:
							ans = False
							break
			if not ans:
				break
		count1 = 0
		count0 = 0
		other = 0
		for i in range(k):
			if arry[i] == '0':
				count0 += 1
			elif arry[i] == '1':
				count1 += 1
			else:
				other += 1
		if count0 != count1 and min(count0, count1) + other < max(count0, count1):
			ans = False
		yield ('YES' if ans else 'NO')
t = int(input())
ans = gift()
print(*ans, sep='\n')


from collections import Counter
from collections import defaultdict
from collections import deque
import math
import heapq
import sys
import io, os
input = sys.stdin.readline
from bisect import *
rs = lambda : input().strip()
ri = lambda : int(input())
rl = lambda : list(map(int, input().strip().split()))
rls = lambda : list(map(str, input().split()))
t = int(input())
for _ in range(0, t):
	(n, k) = rl()
	a = list(input().strip())
	f = 1
	for i in range(k, n):
		if a[i] == '?' and a[i - k] != '?':
			a[i] = a[i - k]
		elif a[i - k] == '?' and a[i] != '?':
			a[i - k] = a[i]
		elif a[i] != a[i - k]:
			print('NO')
			f = 0
			break
	if f:
		ones = 0
		zeros = 0
		for i in range(0, k):
			if a[i] == '1':
				ones += 1
			elif a[i] == '0':
				zeros += 1
		if ones > k // 2 or zeros > k // 2:
			f = 0
		i = 0
		j = k
		while j < n:
			if a[i] == '1':
				ones -= 1
			elif a[i] == '0':
				zeros -= 1
			i = i + 1
			if a[j] == '1':
				ones += 1
			elif a[j] == '0':
				zeros += 1
			j = j + 1
			if ones > k // 2 or zeros > k // 2:
				f = 0
		if f:
			print('YES')
		else:
			print('NO')


def f(n, k, s):
	l = [i for i in s]
	for i in range(n - k):
		if l[i] + l[i + k] in ['01', '10']:
			return 'NO'
		if l[i + k] != '?':
			l[i] = l[i + k]
		else:
			l[i + k] = l[i]
	for i in range(n - k):
		if l[n - 1 - i - k] != '?':
			l[n - 1 - i] = l[n - 1 - i - k]
		else:
			l[n - 1 - i - k] = l[n - 1 - i]
	if l[:k].count('0') > k // 2 or l[:k].count('1') > k // 2:
		return 'NO'
	return 'YES'
r = []
for _ in range(int(input())):
	(n, k) = input().split()
	(n, k) = (int(n), int(k))
	s = input()
	r.append(f(n, k, s))
for i in r:
	print(i)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ch = True
	c = ['?'] * k
	for i in range(n):
		if s[i] != '?':
			if c[i % k] == '?':
				c[i % k] = s[i]
			elif s[i] != c[i % k]:
				ch = False
	o = c.count('1')
	z = c.count('0')
	if o * 2 > k or z * 2 > k:
		ch = False
	if ch:
		print('YES')
	else:
		print('NO')


import io, os
inp = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
inar = lambda : list(map(int, inp().split()))
inin = lambda : int(inp())
inst = lambda : inp().decode().rstrip('\n\r')
_T_ = inin()
for _t_ in range(_T_):
	(n, k) = inar()
	s = inst()
	ones = [False] * k
	zeros = [False] * k
	(c1, c0) = (0, 0)
	for i in range(n):
		if s[i] == '1':
			ones[i % k] = True
		if s[i] == '0':
			zeros[i % k] = True
	ans = 'YES'
	for i in range(k):
		if ones[i] and zeros[i]:
			ans = 'NO'
		if ones[i]:
			c1 += 1
		if zeros[i]:
			c0 += 1
	if c0 > k / 2 or c1 > k / 2:
		ans = 'NO'
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(N, K) = map(int, input().split())
	a = list(input())[:-1]
	cs = 0
	q = 0
	for i in range(K):
		t = 0
		if a[i] == '1':
			t = 1
		elif a[i] == '0':
			t = -1
		else:
			q += 1
		cs += t
	q -= abs(cs)
	if q < 0:
		print('NO')
		continue
	for i in range(N - K):
		if a[i] != '?' and a[i + K] != '?' and (a[i] != a[i + K]):
			print('NO')
			break
		elif a[i] != a[i + K]:
			if a[i] == '0':
				a[i + K] = '0'
			elif a[i] == '1':
				a[i + K] = '1'
			elif a[i + K] == '0':
				a[i] = '0'
			elif a[i + K] == '1':
				a[i] = '1'
	else:
		cs = 0
		q = 0
		for i in range(K):
			t = 0
			if a[i] == '1':
				t = 1
			elif a[i] == '0':
				t = -1
			else:
				q += 1
			cs += t
		q -= abs(cs)
		if q < 0:
			print('NO')
			continue
		for i in range(N - K):
			if a[i] != '?' and a[i + K] != '?' and (a[i] != a[i + K]):
				print('NO')
				break
			elif a[i] != a[i + K]:
				if a[i] == '0':
					a[i + K] = '0'
				elif a[i] == '1':
					a[i + K] = '1'
				elif a[i + K] == '0':
					a[i] = '0'
				elif a[i + K] == '1':
					a[i] = '1'
		else:
			cs = [0] * (N + 1)
			qs = [0] * (N + 1)
			for i in range(N):
				t = 0
				if a[i] == '1':
					t = 1
				elif a[i] == '0':
					t = -1
				else:
					qs[i + 1] += 1
				cs[i + 1] = cs[i] + t
				qs[i + 1] += qs[i]
			for i in range(N - K + 1):
				if (abs(cs[i + K] - cs[i]) - (qs[i + K] - qs[i])) % 2:
					print('NO')
					break
				elif abs(cs[i + K] - cs[i]) - (qs[i + K] - qs[i]) > 0:
					print('NO')
					break
			else:
				print('YES')


from collections import Counter

def solve(s, k):
	r = [None] * k
	for i in range(len(s)):
		c = s[i]
		if c != '?':
			if r[i % k] is None:
				r[i % k] = c
			elif r[i % k] != c:
				return False
	c = Counter(r)
	return abs(c['0'] - c['1']) <= c[None]
t = int(input())
for _ in range(t):
	(_, k) = map(int, input().split())
	s = input()
	print('YES' if solve(s, k) else 'NO')


t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split(' ')))
	st = input()
	if k % 2 == 1:
		print('NO')
		continue
	(zero, one) = (0, 0)
	flag = True
	for i in range(k):
		tmp = None
		for j in range(i, n, k):
			if st[j] != '?':
				if tmp is not None and st[j] != tmp:
					flag = False
					break
				tmp = st[j]
		if not flag:
			break
		if tmp == '0':
			zero += 1
		elif tmp == '1':
			one += 1
	if max(one, zero) > k // 2:
		flag = False
	print('YES' if flag else 'NO')


for k in range(int(input())):
	l = [int(x) for x in input('').split()]
	n = l[0]
	k = l[1]
	s = list(input(''))
	ls = []
	b = ''
	for i in range(k):
		if s[i] == '?':
			for j in range(i + k, n, k):
				if s[j] != '?':
					s[i] = s[j]
					break
	for i in range(k):
		if s[i] != '?':
			for j in range(i + k, n, k):
				if s[j] == '?':
					s[j] = s[i]
				elif s[j] != s[i]:
					print('NO')
					b = 'n'
					break
			if b == 'n':
				break
	else:
		c1 = s[:k].count('1')
		c0 = s[:k].count('0')
		if c1 <= int(k // 2) and c0 <= int(k // 2):
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	a = [-1] * k
	for i in range(n):
		if s[i] != '?':
			if a[i % k] == -1:
				a[i % k] = s[i]
			elif a[i % k] != s[i]:
				print('NO')
				break
	else:
		z = on = 0
		for i in a:
			if i == '0':
				z += 1
			elif i == '1':
				on += 1
		if z > k // 2 or on > k // 2:
			print('NO')
		else:
			print('YES')


for ad in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = list(input())
	l = []
	for i in range(1, n // k + 1):
		l.append(s[k * (i - 1):k * i])
	l.append(s[n // k * k:])
	a = l[0]
	t = 0
	for i in l:
		if t == 1:
			break
		for j in range(len(i)):
			if a[j] == i[j]:
				continue
			elif i[j] == '?':
				continue
			elif a[j] == '?':
				a[j] = i[j]
			else:
				t = 1
				break
	if t == 1:
		print('NO')
	elif abs(a.count('0') - a.count('1')) <= a.count('?'):
		print('YES')
	else:
		print('NO')


t = int(input())
for j in range(t):
	(n, k) = map(int, input().split())
	x = 0
	xxx = 0
	a = input()
	for i in range(0, k):
		y = '1' in a[i::k]
		z = '0' in a[i::k]
		if y and z:
			x = k
			xxx = k
			break
		elif y:
			x += 1
		elif z:
			xxx += 1
	if 2 * x <= k and 2 * xxx <= k:
		print('YES')
	else:
		print('NO')


import sys
import random
from fractions import Fraction
from math import *

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def finput():
	return float(input())

def tinput():
	return input().split()

def linput():
	return list(input())

def rinput():
	return map(int, tinput())

def fiinput():
	return map(float, tinput())

def flinput():
	return list(fiinput())

def rlinput():
	return list(map(int, input().split()))

def trinput():
	return tuple(rinput())

def srlinput():
	return sorted(list(map(int, input().split())))

def NOYES(fl):
	if fl:
		print('NO')
	else:
		print('YES')

def YESNO(fl):
	if fl:
		print('YES')
	else:
		print('NO')

def main():
	(n, k) = rinput()
	q = linput()
	fl = 1
	w = [0, 0, 0]
	for i in range(n - k - 1, -1, -1):
		if q[i] != '?' and q[i + k] != '?':
			if q[i] != q[i + k]:
				fl = 0
		if q[i] == '?':
			q[i] = q[i + k]
		if q[i + k] == '?':
			q[i + k] = q[i]
	for i in range(k):
		if q[i] != '?':
			w[int(q[i])] += 1
		else:
			w[2] += 1
	if (w[1] + w[2] - w[0]) % 2:
		fl = 0
	if (w[1] + w[2] - w[0]) // 2 < 0 or (w[1] + w[2] - w[0]) // 2 > w[2]:
		fl = 0
	YESNO(fl)
for TESTING in range(iinput()):
	main()


def main():
	t = int(input())
	for i in range(t):
		(n, k) = map(int, input().split())
		s = input()
		my_s = [-1 for i in range(k)]
		stop = False
		for i in range(n):
			j = i % k
			if s[i] == '?':
				pass
			elif my_s[j] != -1 and my_s[j] != int(s[i]):
				stop = True
				break
			else:
				my_s[j] = int(s[i])
		total_0 = 0
		total_1 = 0
		for i in my_s:
			if i == 0:
				total_0 += 1
			if i == 1:
				total_1 += 1
		if k - total_1 - total_0 >= abs(total_1 - total_0) and (not stop):
			print('YES')
		else:
			print('NO')
main()


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
from collections import deque
from bisect import bisect_left
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(input())
	flag = 1
	for i in range(k):
		(a, b, c) = (0, 0, 0)
		for j in range(i, n, k):
			if l[j] == '1':
				a += 1
			elif l[j] == '0':
				b += 1
			else:
				c += 1
		if a > 0 and b > 0:
			flag = 0
		elif a > 0:
			for j in range(i, n, k):
				l[j] = '1'
		elif b > 0:
			for j in range(i, n, k):
				l[j] = '0'
	(a, b, c) = (0, 0, 0)
	for j in range(k):
		if l[j] == '1':
			a += 1
		elif l[j] == '0':
			b += 1
		else:
			c += 1
	if c == 0:
		if a != b:
			flag = 0
	elif a > k // 2 or b > k // 2:
		flag = 0
	if flag == 0:
		print('NO')
	else:
		print('YES')


T = int(input())
ans = ['YES'] * T
for t in range(T):
	(n, k) = map(int, input().split())
	S = input()
	lis = ['?'] * k
	for i in range(k):
		for j in range(i, n, k):
			if lis[i] == '?':
				lis[i] = S[j]
			elif lis[i] == '0':
				if S[j] == '1':
					ans[t] = 'NO'
			elif S[j] == '0':
				ans[t] = 'NO'
	p = 0
	A = 0
	for s in lis:
		if s == '1':
			p += 1
		elif s == '?':
			A += 1
	a = k // 2 - p
	if not 0 <= a <= A:
		ans[t] = 'NO'
print(*ans, sep='\n')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(s, l) = (input(), [''] * k)
	for i in range(k):
		l[i] = s[i::k]
	(ans, c1, c2) = (True, 0, 0)
	for i in range(k):
		c1 += 1 if '1' in l[i] else -1
		c2 += -1 if '0' in l[i] else 1
		ans &= not ('0' in l[i] and '1' in l[i])
	ans &= c1 <= 0 and 0 <= c2
	print('YES' if ans else 'NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	arr = [-1] * n
	flag = True
	for i in range(n):
		if arr[i % k] == -1:
			if s[i] == '0':
				arr[i % k] = 0
			if s[i] == '1':
				arr[i % k] = 1
		elif s[i] == '0' and arr[i % k] == 1 or (s[i] == '1' and arr[i % k] == 0):
			flag = False
			break
	zero = one = 0
	for i in range(k):
		if arr[i] != -1:
			if arr[i] == 0:
				zero += 1
			else:
				one += 1
	if zero > k // 2 or one > k // 2:
		flag = False
	if flag:
		print('YES')
	else:
		print('NO')


from functools import cmp_to_key
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	is_valid = True
	for i in range(k, n):
		if s[i] != '?':
			if s[i % k] == '?':
				s[i % k] = s[i]
			elif s[i % k] != s[i]:
				is_valid = False
				break
	if not is_valid:
		print('NO')
	else:
		(cnt0, cnt1) = (0, 0)
		for i in range(k):
			if s[i] == '0':
				cnt0 += 1
			elif s[i] == '1':
				cnt1 += 1
		if cnt0 <= k // 2 and cnt1 <= k // 2:
			print('YES')
		else:
			print('NO')


def ans(x, k, n):
	for i in range(0, k):
		if x[i] == '?':
			for j in range(i + k, n, k):
				if x[j] != '?':
					x[i] = x[j]
					break
	for i in range(0, k):
		if x[i] != '?':
			for j in range(i + k, n, k):
				if x[j] == '?':
					x[j] = x[i]
				elif x[j] != x[i]:
					return 'NO'
	d = {'0': 0, '1': 0, '?': 0}
	for i in range(0, k):
		d[x[i]] += 1
	if d['0'] <= k // 2 and d['1'] <= k // 2:
		return 'YES'
	return 'NO'
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	x = list(input())
	print(ans(x, k, n))


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = input()
	x = [set() for _ in range(k)]
	for (i, c) in enumerate(s):
		x[i % k].add(c)
	if any(('0' in y and '1' in y for y in x)) or sum(('0' in y for y in x)) > k // 2 or sum(('1' in y for y in x)) > k // 2:
		print('NO')
		continue
	print('YES')


import sys
input = sys.stdin.readline

def inInt():
	return int(input())

def inStr():
	return input().strip('\n')

def inIList():
	return list(map(int, input().split()))

def inSList():
	return input().split()

def bsearch(nums, target):
	N = len(nums or [])
	l = 0
	r = N - 1
	while l <= r:
		mid = (l + r) // 2
		if nums[mid] < target:
			l = mid + 1
		elif nums[mid] > target:
			r = mid - 1
		else:
			return (None, mid, None)
	return (r if r >= 0 else None, None, l if l <= N - 1 else None)

def yesOrNo(val):
	print('YES' if val else 'NO')

def printSpacedArray(nums):
	for x in nums:
		print(x, end=' ')
	print()

def solve():
	nk = inIList()
	(n, k) = (nk[0], nk[1])
	s = inStr()
	out = [s[i:i + k] for i in range(0, len(s), k)]
	spots = []
	for i in range(k):
		count1s = 0
		count0s = 0
		for n in out:
			if len(n) > i:
				if n[i] == '0':
					count0s += 1
				elif n[i] == '1':
					count1s += 1
		if count1s == 0 and count0s == 0:
			spots.append('?')
		elif count1s > 0 and count0s > 0:
			return 'NO'
		elif count1s > 0:
			spots.append('1')
		else:
			spots.append('0')
	if spots.count('0') <= k / 2 and spots.count('1') <= k / 2:
		return 'YES'
	return 'NO'
tests = inInt()
for case in range(tests):
	print(solve())


import sys
readline = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(readline())

def read_int_n():
	return list(map(int, readline().split()))

def read_float():
	return float(readline())

def read_float_n():
	return list(map(float, readline().split()))

def read_str():
	return readline().strip()

def read_str_n():
	return readline().strip().split()

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.perf_counter()
		ret = f(*args, **kwargs)
		e = time.perf_counter()
		error_print(e - s, 'sec')
		return ret
	return wrap
from collections import Counter, defaultdict
from itertools import product

def ref(N, K, S):
	S = [c for c in S]
	n = Counter(S)['?']
	iq = [i for i in range(N) if S[i] == '?']
	for j in product('01', repeat=n):
		for (k, v) in zip(iq, j):
			S[k] = v
		ok = True
		cc = Counter(S[:K])
		if cc['0'] > K // 2 or cc['1'] > K // 2:
			ok = False
		for i in range(N - K):
			a = S[i]
			b = S[i + K]
			if a != b:
				ok = False
		if ok:
			return 'YES'
	return 'NO'

def slv(N, K, S):
	sk = defaultdict(Counter)
	for i in range(N):
		sk[i % K][S[i]] += 1
	c = Counter()
	for i in range(K):
		if '0' in sk[i] and '1' in sk[i]:
			return 'NO'
		if '0' in sk[i]:
			c['0'] += 1
		elif '1' in sk[i]:
			c['1'] += 1
	if c['0'] > K // 2 or c['1'] > K // 2:
		return 'NO'
	return 'YES'

def main():
	for _ in range(read_int()):
		(N, K) = read_int_n()
		S = read_str()
		print(slv(N, K, S))
main()


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	flag = True
	cnt = [0, 0]
	for i in range(k):
		for j in range(i + k, n, k):
			if s[i] == '?':
				s[i] = s[j]
			elif s[j] == '?':
				continue
			elif s[i] != s[j]:
				flag = False
		if s[i] != '?':
			cnt[int(s[i])] += 1
	if max(cnt) > k // 2:
		flag = False
	print('YES' if flag else 'NO')


t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	s = list(input())
	s1 = ['?'] * k
	f = 0
	i = 0
	while i < k:
		j = i
		while j < n:
			if s[j] == '0':
				if s1[j % k] == '1':
					f = 1
					break
				else:
					s1[j % k] = '0'
			elif s[j] == '1':
				if s1[j % k] == '0':
					f = 1
					break
				else:
					s1[j % k] = '1'
			j = j + k
		if f:
			break
		i = i + 1
	if f:
		print('NO')
	elif s1.count('0') <= k // 2 and s1.count('1') <= k // 2:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	a = ['?' for i in range(k)]
	for i in range(k):
		c = '?'
		for j in range(i, n, k):
			if c != '?':
				if '?' != s[j] != c:
					print('NO')
					break
			elif s[j] != '?':
				c = s[j]
				a[i] = s[j]
		else:
			continue
		break
	else:
		count_0 = 0
		count_1 = 0
		for c in a:
			if c == '1':
				count_1 += 1
			elif c == '0':
				count_0 += 1
		if count_1 * 2 > k or count_0 * 2 > k:
			print('NO')
		else:
			print('YES')


from pathlib import Path
import sys
import functools
import itertools
import re

def solve():
	for test in range(int(input())):
		(n, k) = map(int, input().split())
		s = str(input())
		ok = True
		one = 0
		zero = 0
		for i in range(0, k):
			cur = -1
			for j in range(i, n, k):
				if s[j] != '?':
					if cur != -1 and ord(s[j]) - ord('0') != cur:
						ok = False
						break
					cur = ord(s[j]) - ord('0')
			if cur != -1:
				one += 1 if cur == 1 else 0
				zero += 1 if cur == 0 else 0
		if max(one, zero) > k // 2:
			ok = False
		print('YES' if ok == True else 'NO')

def main():
	if Path('test.inp').exists():
		sys.stdin = open('test.inp', 'r')
	solve()
main()


def count(s):
	n0 = 0
	n1 = 0
	nq = 0
	for i in range(k):
		if s[i] == '1':
			n1 += 1
		elif s[i] == '0':
			n0 += 1
		else:
			nq += 1
	return (n0, n1, nq)

def soln(s):
	for i in range(k):
		if s[i] == '?':
			j = i + k
			while j < len(s):
				if s[i] != s[j]:
					if s[i] == '?':
						s[i] = s[j]
					elif s[j] == '?':
						s[j] = s[i]
					else:
						return False
				j += k
		else:
			j = i + k
			while j < len(s):
				if s[i] != s[j]:
					if s[j] == '?':
						s[j] = s[i]
					else:
						return False
				j += k
	(n0, n1, nq) = count(s)
	if n1 <= k // 2 and n0 <= k // 2:
		return True
	else:
		return False
t = int(input())
while t > 0:
	t -= 1
	(n, k) = [int(x) for x in input().split()]
	a = input()
	s = list(a)
	(n0, n1, nq) = count(s)
	if n0 <= k // 2 or n1 <= k // 2:
		if soln(s):
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for t in range(int(input())):
	(n, m) = map(int, input().split())
	a = input()
	a = '0' + a
	flag = 0
	visit = ['?' for i in range(m)]
	for i in range(1, n + 1):
		if visit[i % m] == '?':
			visit[i % m] = a[i]
		elif visit[i % m] == '0':
			if a[i] == '1':
				flag = -1
				break
		elif visit[i % m] == '1':
			if a[i] == '0':
				flag = -1
				break
	(add, count) = (0, 0)
	if flag == -1:
		print('NO')
		continue
	for i in range(m):
		if visit[i] == '1':
			add += 1
		elif visit[i] == '0':
			count += 1
	if add <= m // 2 and count <= m // 2:
		print('YES')
	else:
		print('NO')


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = ip()
for _ in range(t):
	(n, k) = inp()
	s = ips()
	flag = True
	(one, zero, ques) = (0, 0, 0)
	for i in range(k):
		temp = set()
		for j in range(i, n, k):
			temp.add(s[j])
		temp = list(temp)
		if '1' in temp and '0' in temp:
			flag = False
			break
		elif '1' in temp:
			one += 1
		elif '0' in temp:
			zero += 1
		else:
			ques += 1
	if one == zero:
		pass
	else:
		for i in range(ques):
			if one < zero:
				one += 1
			else:
				zero += 1
		if one == zero:
			pass
		else:
			flag = False
	if flag:
		print('YES')
	else:
		print('NO')


import sys
import math
input = sys.stdin.readline
T = int(input())
for t in range(T):
	(n, k) = map(int, input().split(' '))
	A = list(input().strip())
	bits = [None for i in range(k)]
	can = True
	for i in range(n):
		bit = A[i]
		if bit == '?':
			continue
		pos = i % k
		if bits[pos] is None or bits[pos] == bit:
			bits[pos] = bit
		else:
			can = False
			print('NO')
			break
	if not can:
		continue
	for i in range(n):
		bit = A[i]
		pos = i % k
		if bit == '?' and bits[pos] is not None:
			A[i] = bits[pos]
	ones = zeros = 0
	for i in range(k):
		bit = A[i]
		if bit == '0':
			zeros += 1
		elif bit == '1':
			ones += 1
	print('YES' if ones <= k // 2 and zeros <= k // 2 else 'NO')


for _ in range(int(input())):
	(n, k) = [int(x) for x in input().split()]
	s = input()
	a = ['?'] * k
	z = 0
	o = 0
	i = 0
	no = False
	for x in s:
		if x == '?':
			pass
		elif x == a[i]:
			pass
		elif a[i] == '?':
			a[i] = x
			z = z + 1 if x == '0' else z
			o = o + 1 if x == '1' else o
		elif a[i] != x:
			no = True
			break
		else:
			pass
		i = 0 if i == k - 1 else i + 1
	if no:
		print('NO')
	elif k - (z + o) >= abs(z - o):
		print('YES')
	else:
		print('NO')


def solve():
	(n, k) = map(int, input().split())
	s = input()
	check = [-1] * k
	for i in range(n):
		ind = i % k
		if check[ind] == -1:
			check[ind] = s[i]
			continue
		if s[i] == '1':
			if check[ind] == '0':
				return False
			check[ind] = '1'
		if s[i] == '0':
			if check[ind] == '1':
				return False
			check[ind] = '0'
	(s1, s0, ex) = (0, 0, 0)
	for i in range(k):
		if check[i] == '1':
			s1 += 1
		elif check[i] == '0':
			s0 += 1
		else:
			ex += 1
	return s1 <= k // 2 and s0 <= k // 2
for _ in range(int(input())):
	if solve():
		print('YES')
	else:
		print('NO')


from itertools import permutations
from collections import defaultdict
from math import *
import os
import sys
from io import BytesIO, IOBase
from heapq import nlargest
from bisect import *
import copy
import itertools
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
t = int(input())
while t:
	t -= 1
	(n, k) = map(int, input().split())
	s = input()
	s = list(s)
	for i in range(k):
		if s[i] == '?':
			j = i + k
			while j < n:
				if s[j] != '?':
					s[i] = s[j]
					break
				j += k
	isPossible = True
	for i in range(k):
		if s[i] != '?':
			j = i + k
			while j < n:
				if s[j] == '?':
					s[j] = s[i]
				elif s[j] != s[i]:
					isPossible = False
					break
				j += k
	if isPossible:
		d = dict()
		(d['0'], d['1'], d['?']) = (0, 0, 0)
		for i in range(k):
			d[s[i]] += 1
		if d['0'] <= k // 2 and d['1'] <= k // 2:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


import sys as _sys
ZERO = '0'
ONE = '1'
GAP = '?'

def main():
	t = int(input())
	for i in range(t):
		(n, k) = _read_ints()
		s = _read_line()
		if can_make_k_balanced(s, k=k):
			output = 'YES'
		else:
			output = 'NO'
		print(output)

def _read_line():
	result = _sys.stdin.readline()
	assert result[-1] == '\n'
	return result[:-1]

def _read_ints():
	return map(int, _read_line().split(' '))

def can_make_k_balanced(s, k):
	assert isinstance(s, str)
	assert k % 2 == 0
	half_k = k // 2
	slices = []
	i_slice_start = 0
	while i_slice_start < len(s):
		slices.append(s[i_slice_start:i_slice_start + k])
		i_slice_start += k
	final_slice_data = list(s[0:k])
	for slice_data in slices:
		for i in range(len(slice_data)):
			if slice_data[i] == GAP:
				continue
			if final_slice_data[i] == GAP:
				final_slice_data[i] = slice_data[i]
			elif final_slice_data[i] != slice_data[i]:
				return False
	known_sum = sum((_char_to_int(char) for char in final_slice_data if char != GAP))
	gaps_sum = half_k - known_sum
	gaps_n = final_slice_data.count(GAP)
	return 0 <= gaps_sum <= gaps_n

def _char_to_int(char):
	assert char in (ZERO, ONE)
	return int(char)
main()


def xint(x):
	if x == '?':
		return None
	else:
		return int(x)
T = int(input())
for t in range(T):
	(n, k) = list(map(lambda x: int(x), input().split(' ')))
	s = list(map(lambda x: xint(x), input()))
	d = [None] * k
	result = True
	for i in range(n):
		if d[i % k] == None:
			d[i % k] = s[i]
		elif d[i % k] != s[i] and s[i] != None:
			result = False
			break
	one = 0
	zero = 0
	for x in d:
		if x == 0:
			zero += 1
		elif x == 1:
			one += 1
	if one > k / 2 or zero > k / 2:
		result = False
	if result == True:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	t -= 1
	(n, k) = map(int, input().split())
	s = input()
	tp = ['?'] * k
	ok = True
	for i in range(n):
		ik = i % k
		if tp[ik] == '?':
			tp[ik] = s[i]
		elif s[i] != '?' and tp[ik] != s[i]:
			ok = False
			break
	n0 = 0
	n1 = 0
	for elm in tp:
		if elm == '0':
			n0 += 1
		elif elm == '1':
			n1 += 1
	if ok == False or n0 > k // 2 or n1 > k // 2:
		print('No')
	else:
		print('Yes')


from collections import Counter
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	answer = 'YES'
	for i in range(k):
		data2 = [element for element in s[i:n:k]]
		base = Counter(data2)
		if base['1'] != 0 and base['0'] != 0:
			answer = 'NO'
			break
		elif base['1'] != 0:
			for j in range(i, n, k):
				s[j] = '1'
		elif base['0'] != 0:
			for j in range(i, n, k):
				s[j] = '0'
	if answer == 'YES':
		base = Counter(s[0:k])
		if max(base['1'], base['0']) > k // 2:
			answer = 'NO'
	print(answer)


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	zeroCount = 0
	oneCount = 0
	contradiction = False
	for i in range(k):
		cnt = i
		mustBeZero = False
		mustBeOne = False
		while cnt < n:
			if s[cnt] == ord('0'):
				mustBeZero = True
			if s[cnt] == ord('1'):
				mustBeOne = True
			cnt += k
		if mustBeZero and mustBeOne:
			contradiction = True
			break
		elif mustBeOne:
			oneCount += 1
		elif mustBeZero:
			zeroCount += 1
	if contradiction:
		print('NO')
	elif oneCount <= k // 2 and zeroCount <= k // 2:
		print('Yes')
	else:
		print('NO')


def algo(n, k, arr):
	c0 = arr[:k].count('0')
	c1 = arr[:k].count('1')
	cq = arr[:k].count('?')
	if c0 > k // 2:
		return 'NO'
	if c1 > k // 2:
		return 'NO'
	res = [None] * k
	for i in range(k):
		a = 0
		t = [0, 0, 0]
		while i + a * k < n:
			idx = i + a * k
			if arr[idx] == '0':
				t[0] += 1
			elif arr[idx] == '1':
				t[1] += 1
			else:
				t[2] += 1
			a += 1
		if t[0] != 0 and t[1] != 0:
			return 'NO'
		if t[0]:
			res[i] = '0'
		elif t[1]:
			res[i] = '1'
	c0 = res.count('0')
	c1 = res.count('1')
	cq = res.count('?')
	if c0 > k // 2:
		return 'NO'
	if c1 > k // 2:
		return 'NO'
	return 'YES'
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(input())
	ans = algo(n, k, arr)
	print(ans)


from bisect import *
from collections import *
from decimal import *

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	(n, k) = ma()
	s = st()
	s = list(s)
	fl = 0
	for i in range(k, n):
		if s[i] != '?':
			if s[i % k] == '?':
				s[i % k] = s[i]
			elif s[i % k] != s[i]:
				fl = 1
				break
	(o, z) = (0, 0)
	for i in range(k):
		if s[i] == '1':
			o += 1
		elif s[i] == '0':
			z += 1
	if o > k // 2 or z > k // 2:
		fl = 1
	s = 'YES'
	if fl:
		s = 'NO'
	print(s)


def isBalanced(k, s):
	ones = 0
	zeros = 0
	for i in range(k):
		value = s[i]
		if value == '1':
			ones += 1
		elif value == '0':
			zeros += 1
		next_pos = i + k
		while next_pos < len(s):
			if value == '?':
				if value != s[next_pos]:
					value = s[next_pos]
					if value == '0':
						zeros += 1
					else:
						ones += 1
			elif s[next_pos] != value and s[next_pos] != '?':
				return False
			next_pos += k
	if ones > k // 2 or zeros > k // 2:
		return False
	return True
testcases = []
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	testcases.append((k, s))
for case in testcases:
	(k, s) = case
	if isBalanced(k, s):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	a = list(input())
	for i in range(k):
		c = set()
		for j in range(i, n, k):
			if a[j] != '?':
				c.add(a[j])
		if len(c) > 1:
			break
		if c:
			c = list(c)[0]
			for j in range(i, n, k):
				a[j] = c
	else:
		cs0 = a[:k].count('0')
		cs1 = a[:k].count('1')
		for i in range(k):
			if a[i] == '?':
				if cs0 < cs1:
					cs0 += 1
					a[i::k] = ['0'] * len(a[i::k])
				else:
					cs1 += 1
					a[i::k] = ['1'] * len(a[i::k])
		if a == (a[:k] * (n // k + 1))[:n] and cs0 == cs1:
			print('YES')
		else:
			print('NO')
		continue
	print('NO')


import math
tests = int(input())
for _ in range(tests):
	[n, k] = list(map(int, input().split()))
	vals = list(input())
	zeros = 0
	ones = 0
	qs = 0
	balance = k // 2
	template = [None for _ in range(k)]
	for i in range(k):
		if vals[i] == '1':
			ones += 1
			template[i] = 1
		elif vals[i] == '0':
			zeros += 1
			template[i] = 0
		else:
			qs += 1
	breakout = False
	if zeros > balance or ones > balance:
		breakout = True
		print('NO')
	else:
		for it in range(1, math.ceil(n / k)):
			if breakout:
				break
			ind = it * k
			while ind < n and ind < (it + 1) * k:
				actual = ind % k
				if vals[ind] == '1':
					if template[actual] == 0:
						breakout = True
						print('NO')
						break
					elif template[actual] is None:
						template[actual] = 1
						qs -= 1
						ones += 1
				elif vals[ind] == '0':
					if template[actual] == 1:
						breakout = True
						print('NO')
						break
					elif template[actual] is None:
						template[actual] = 0
						qs -= 1
						zeros += 1
				if zeros > balance or ones > balance:
					breakout = True
					print('NO')
					break
				ind += 1
	if not breakout:
		print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	l = ['?'] * k
	f = 0
	for i in range(n):
		if s[i] == '1':
			if l[i % k] == '0':
				f = 1
				break
			else:
				l[i % k] = '1'
		elif s[i] == '0':
			if l[i % k] == '1':
				f = 1
				break
			else:
				l[i % k] = '0'
	if f:
		print('NO')
	elif abs(l.count('1') - l.count('0')) > l.count('?'):
		print('NO')
	else:
		print('YES')


from collections import *
import bisect
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	ans = True
	for i in range(k):
		zero = 0
		one = 0
		for j in range(i, n, k):
			if s[j] == '0':
				zero += 1
			elif s[j] == '1':
				one += 1
		if zero and one:
			ans = False
			break
		if not ans:
			break
		if one or zero:
			for j in range(i, n, k):
				if one:
					s[j] = '1'
				else:
					s[j] = '0'
	one = 0
	zero = 0
	ques = 0
	for i in range(k):
		if s[i] == '1':
			one += 1
		elif s[i] == '0':
			zero += 1
		else:
			ques += 1
	check = False
	for i in range(ques + 1):
		if i + one == ques - i + zero:
			check = True
	if not check and ques != 0:
		ans = False
	if ques == 0 and one != zero:
		ans = False
	if ans:
		print('YES')
	else:
		print('NO')


mod = 1000000007
eps = 10 ** (-9)

def main():
	import sys
	input = sys.stdin.readline
	for _ in range(int(input())):
		(N, K) = map(int, input().split())
		S = input().rstrip('\n')
		N0 = 0
		N1 = 0
		ok = 1
		for k in range(K):
			flg = -1
			for j in range(N):
				i = j * K + k
				if i >= N:
					break
				if S[i] == '0':
					if flg == 1:
						ok = 0
					else:
						flg = 0
				elif S[i] == '1':
					if flg == 0:
						ok = 0
					else:
						flg = 1
			if flg == 0:
				N0 += 1
			elif flg == 1:
				N1 += 1
		if N0 * 2 <= K and N1 * 2 <= K and (ok == 1):
			print('YES')
		else:
			print('NO')
main()


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	t = int(input())
	for i in range(t):
		(n, k) = map(int, input().split())
		ss = str(input())
		cnt = [0] * 3
		s = list(ss)
		for j in range(0, n - k):
			if s[j] != '?' and s[j + k] == '?':
				s[j + k] = s[j]
			if s[j] == '?' and s[j + k] != '?':
				s[j] = s[j + k]
		(l, dis) = (0, 0)
		f = True
		while l <= n - k:
			while dis < k:
				if s[l + dis] == '0':
					cnt[0] += 1
				elif s[l + dis] == '1':
					cnt[1] += 1
				else:
					cnt[2] += 1
				dis += 1
			if cnt[2] < abs(cnt[1] - cnt[0]) or (l + k < n and s[l] != s[l + k] and (s[l] != '?') and (s[l + k] != '?')):
				f = False
				break
			if s[l] == '0':
				cnt[0] -= 1
			elif s[l] == '1':
				cnt[1] -= 1
			else:
				cnt[2] -= 1
			l += 1
			dis -= 1
		if f:
			print('YES')
		else:
			print('NO')
main()


test_case = int(input())
for _ in range(test_case):
	(n, k) = map(int, input().split())
	st = input()
	v = ['?' for i in range(k)]
	ans = True
	for i in range(k):
		for j in range(i, n, k):
			if st[j] == '?':
				continue
			elif v[i] == '?' or v[i] == st[j]:
				v[i] = st[j]
			elif v[i] != st[i] or st[i] != st[j]:
				ans = False
	(one, zero) = (0, 0)
	for i in range(k):
		if v[i] == '1':
			one += 1
		elif v[i] == '0':
			zero += 1
	if one > k // 2 or zero > k // 2:
		ans = False
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for j in range(0, t):
	(n, k) = map(int, input().split())
	s = list(input())
	fff = 0
	for l in range(0, k):
		uu = s[l]
		for ff in range(l, n, k):
			if s[ff] == '?':
				if uu == '?':
					uu = s[ff]
			elif s[ff] == '0':
				if uu == '?' or uu == '0':
					uu == '0'
				else:
					fff = -1
					break
				uu = s[ff]
			else:
				if uu == '?' or uu == '1':
					uu == '1'
				else:
					fff = -1
					break
				uu = s[ff]
		if fff == -1:
			break
		else:
			for ff in range(l, n, k):
				s[ff] = uu
	if fff == -1:
		print('NO')
	else:
		q = 0
		o = 0
		z = 0
		for l in range(0, k):
			if s[l] == '1':
				o += 1
			elif s[l] == '0':
				z += 1
			else:
				q += 1
		if z > k // 2 or o > k // 2:
			print('NO')
		else:
			print('YES')


t = int(input())
while t:
	(n, k) = [int(i) for i in input().split()]
	s = input()
	ans = [''] * n
	flag = False
	helper = []
	for i in range(k):
		curr = None
		ques = set()
		for j in range(i, n, k):
			if s[j] != '?':
				if curr == None:
					curr = s[j]
				elif s[j] != curr:
					flag = True
				ans[j] = s[j]
			else:
				ques.add(j)
		if flag:
			break
		if curr is not None:
			for index in ques:
				ans[index] = curr
		helper.append(curr)
	if flag:
		print('NO')
	else:
		ques = 0
		ones = 0
		zer = 0
		for i in range(k):
			if helper[i] == '1':
				ones += 1
			if helper[i] == '0':
				zer += 1
			if helper[i] == None:
				ques += 1
		ques -= max(ones, zer) - min(ones, zer)
		if ques < 0:
			print('NO')
		elif ques % 2 == 0:
			print('YES')
		else:
			print('NO')
	t -= 1


T = int(input())
while T:
	(n, k) = map(int, input().split())
	S = list(input())
	F = 'YES'
	R = S[:k]
	a = R.count('0')
	b = R.count('1')
	for i in range(k):
		if S[i] == '?':
			j = i + k
			while j < n and S[j] == '?':
				j = j + k
			if j < n:
				S[i] = S[j]
				if S[i] == '0':
					a += 1
				else:
					b += 1
	for i in range(k):
		if S[i] == '?':
			if a < b:
				a += 1
				S[i] = '0'
			else:
				b += 1
				S[i] = '1'
	if a == b:
		i = 0
		while i + k < n:
			a = S[i]
			b = S[i + k]
			if a != b:
				if b != '?':
					F = 'NO'
					break
				else:
					S[i + k] = a
			i += 1
	else:
		F = 'NO'
	print(F)
	T = T - 1


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(map(str, input()))
	flag = True
	for i in range(k):
		if s[i] == '?':
			for j in range(i + k, n, k):
				if s[j] != '?':
					s[i] = s[j]
	for i in range(k):
		if s[i] != '?':
			for j in range(i + k, n, k):
				if s[j] == '?':
					s[j] = s[i]
				elif s[i] != s[j]:
					flag = False
					break
		if not flag:
			break
	if flag:
		if s[:k].count('1') <= k // 2 and s[:k].count('0') <= k // 2:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


def ans(arr, n, k):
	for i in range(k):
		if arr[i] == '?':
			for j in range(i + k, n, k):
				if arr[j] != '?':
					arr[i] = arr[j]
					break
	(p, q) = (0, 0)
	for i in range(k):
		if arr[i] != '?':
			if arr[i] == '1':
				q += 1
			else:
				p += 1
			for j in range(i + k, n, k):
				if arr[j] == '?':
					arr[j] = arr[i]
				elif arr[j] != arr[i]:
					return 'NO'
	if p <= k // 2 and q <= k // 2:
		return 'YES'
	return 'NO'
T = int(input())
for i in range(T):
	(n, k) = map(int, input().strip().split(' '))
	arr = list(input())
	print(ans(arr, n, k))


for t in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	if '0' * (k // 2 + 1) in s or '1' * (k // 2 + 1) in s or k > n:
		print('NO')
		continue

	def check(s):
		s = list(s)
		for i in range(k):
			temp = set([])
			for j in range(i, n, k):
				if s[j] != '?':
					temp.add(s[j])
			if len(temp) == 2:
				return (False, [])
			if len(temp) == 1:
				val = temp.pop()
				for j in range(i, n, k):
					s[j] = val
		return (True, s)
	(ans, s) = check(s)
	if ans:
		o = z = 0
		for i in s[:k]:
			if i == '1':
				o += 1
			if i == '0':
				z += 1
		if o <= k // 2 and z <= k // 2:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


def solve(s, n, k):
	first_k = ''
	if k % 2:
		return 'NO'
	for i in range(k):
		num1 = 0
		num2 = 0
		num3 = 0
		for j in range(i, n, k):
			if s[j] == '0':
				num1 += 1
			elif s[j] == '1':
				num2 += 1
			else:
				num3 += 1
		if num1 * num2 > 0:
			return 'NO'
		if num1 > 0:
			first_k += '0'
		elif num2 > 0:
			first_k += '1'
		else:
			first_k += '?'
	num1 = 0
	num2 = 0
	num3 = 0
	for i in range(k):
		if first_k[i] == '0':
			num1 += 1
		elif first_k[i] == '1':
			num2 += 1
		else:
			num3 += 1
	if num1 + num3 < num2 or num2 + num3 < num1:
		return 'NO'
	else:
		return 'YES'
t = int(input())
for i in range(t):
	temp = input().split(' ')
	n = int(temp[0])
	k = int(temp[1])
	s = input()
	print(solve(s, n, k))


t = int(input())
for _ in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = input()
	ok = True
	arr = list(s)
	for l in range(1, n):
		r = l + k - 1
		if r == n:
			break
		if arr[r] == '?':
			arr[r] = arr[l - 1]
		if arr[l - 1] == '?':
			arr[l - 1] = arr[r]
		if arr[r] != arr[l - 1]:
			ok = False
			break
	for r in range(n - 1, -1, -1):
		l = r - k + 1
		if l == 0:
			break
		if arr[r] == '?':
			arr[r] = arr[l - 1]
		if arr[l - 1] == '?':
			arr[l - 1] = arr[r]
		if arr[r] != arr[l - 1]:
			ok = False
			break
	z = o = q = 0
	for i in range(k):
		if arr[i] == '0':
			z += 1
		elif arr[i] == '1':
			o += 1
		else:
			q += 1
	if q < abs(z - o):
		ok = False
	if ok:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	small = ['?'] * k
	f = 0
	for i in range(k):
		ind = i
		while ind < n:
			if s[ind] == '0':
				if small[ind % k] == '1':
					f = 1
					break
				else:
					small[ind % k] = '0'
			elif s[ind] == '1':
				if small[ind % k] == '0':
					f = 1
					break
				else:
					small[ind % k] = '1'
			ind += +k
	if f:
		print('NO')
	elif small.count('0') <= k // 2 and small.count('1') <= k // 2:
		print('YES')
	else:
		print('NO')


__author__ = '\n\n\n   ***   \t       *\n    *                  *\n    *                  *\n    *\t ****\t ****  *****    ****   *   *\n    *\t*   *   *      *   *    *  *   *   *   \n    *\t*   *   *      *   *    *  *   *   *\n*   *\t*   *   *      *   *    *  *   *****      \n*   *\t*   *   *      *   *    *  *       *  \n ***\t *** *  *      *****    *** *      * \n                                           *\n********************************************  \n'
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	p = []
	for i in range(k):
		if s[i] != '?':
			p.append(set(s[i]))
		else:
			p.append(set([]))
	lock = 0
	for i in range(n):
		if s[i] != '?':
			p[i % k].add(s[i])
			if len(p[i % k]) > 1:
				lock = 1
				print('no')
				break
	if lock == 0:
		bir = 0
		bos = 0
		for i in range(k):
			b = list(p[i])
			if b and b[0] == '1':
				bir += 1
			if b == []:
				bos += 1
		if bir <= k // 2 and min(bir + bos, k // 2) == k // 2:
			print('yes')
		else:
			print('no')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	s = list(s)
	f = 1
	one = 0
	zero = 0
	for i in range(0, k):
		temp = -1
		for j in range(i, n, k):
			if s[j] != '?':
				if temp != -1 and temp != ord(s[j]):
					f = 0
					break
				temp = ord(s[j])
		if temp != -1:
			if temp == ord('1'):
				one += 1
			elif temp == ord('0'):
				zero += 1
	if one > k // 2 or zero > k // 2:
		print('NO')
		continue
	if f == 1:
		print('YES')
	else:
		print('NO')


t = int(input())

def f():
	(n, k) = map(int, input().split())
	S = list(input())
	for i in range(k):
		b = -1
		for j in range(i, n, k):
			if S[j] == '?':
				if b != -1:
					S[j] = str(b)
			else:
				a = int(S[j])
				if b == -1:
					b = a
				elif a ^ b:
					print('NO')
					return
		if b != -1:
			for j in range(i, n, k):
				S[j] = str(b)
	T = S[:k]
	x = T.count('0')
	y = T.count('1')
	if x <= k // 2 and y <= k // 2:
		print('YES')
	else:
		print('NO')
for x in range(t):
	f()


def gt():
	return list(map(int, input().split()))

def ok(ar, n, k):
	for i in range(k, n):
		if ar[i] != '?':
			if ar[i - k] != '?' and ar[i - k] != ar[i]:
				return 0
		if ar[i] == '?':
			if ar[i - k] != '?':
				ar[i] = ar[i - k]
	for i in range(n - 1 - k, -1, -1):
		if ar[i] == '?' and ar[i + k] != '?':
			ar[i] = ar[i + k]
	one = 0
	zero = 0
	puz = 0
	for i in range(k):
		one += ar[i] == '1'
		zero += ar[i] == '0'
		puz += ar[i] == '?'
	tmp = abs(one - zero)
	return tmp <= puz and puz - tmp & 1 == 0
(t,) = gt()
while t:
	t -= 1
	(n, k) = gt()
	ar = list(input())
	print('YES' if ok(ar, n, k) else 'NO')


def solve():
	(n, k) = list(map(int, input().split()))
	s = list(input())
	for i in range(k):
		zero = False
		one = False
		for j in range(i, n, k):
			if s[j] == '1':
				one = True
			if s[j] == '0':
				zero = True
		if one and zero:
			print('NO')
			return
		if not one and (not zero):
			continue
		for j in range(i, n, k):
			s[j] = '1' if one else '0'
	one = 0
	zero = 0
	for i in range(k):
		if s[i] == '1':
			one += 1
		if s[i] == '0':
			zero += 1
	print('YES' if one <= k / 2 and zero <= k / 2 else 'NO')
ct = int(input())
while ct > 0:
	solve()
	ct -= 1


def func(n, k, s):
	zeroCount = 0
	oneCount = 0
	s = list(s)
	for i in range(k):
		if s[i] == '?':
			for j in range(i + k, n, k):
				if s[j] != '?':
					s[i] = s[j]
					break
	for i in range(k):
		if s[i] != '?':
			for j in range(i + k, n, k):
				if s[j] == '?':
					s[j] = s[i]
				elif s[j] != s[i]:
					return 'NO'
	for i in range(k):
		if s[i] == '1':
			oneCount += 1
		if s[i] == '0':
			zeroCount += 1
	if oneCount <= k / 2 and zeroCount <= k / 2:
		return 'YES'
	else:
		return 'NO'
t = int(input())
ans_arr = []
for i in range(t):
	n_klist = list(map(int, input().split()))
	n = n_klist[0]
	k = n_klist[1]
	s = str(input())
	balance_str = func(n, k, s)
	ans_arr.append(balance_str)
for j in ans_arr:
	print(j)


import math
import collections
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2 ** 20)
t = 1
t = int(stdin.readline())
for _ in range(t):
	(n, k) = list(map(int, stdin.readline().rstrip().split()))
	s = stdin.readline().strip('\n')
	l = [i for i in s]
	(cn0, cn1, c) = (0, 0, 0)
	chk = True
	for i in range(n - k):
		if s[i] == '?' or s[i + k] == '?':
			continue
		elif s[i] != s[i + k]:
			chk = False
			break
		else:
			continue
	for i in range(k):
		if s[i] == '1':
			cn1 += 1
		elif s[i] == '0':
			cn0 += 1
		else:
			c += 1
	f0 = k // 2 - cn0
	f1 = k // 2 - cn1
	if f1 + f0 != c or f0 < 0 or f1 < 0:
		chk = False
	s = list(s)
	for i in range(k, n):
		if s[i - k] == '1' and s[i] == '?':
			s[i] = '1'
		elif s[i - k] == '1' and s[i] == '0':
			chk = False
			break
		if s[i - k] == '0' and s[i] == '?':
			s[i] = '0'
		elif s[i - k] == '0' and s[i] == '1':
			chk = False
			break
	for i in range(k, n):
		if s[i - k] == '1':
			cn1 -= 1
		if s[i - k] == '0':
			cn0 -= 1
		if s[i - k] == '?':
			c -= 1
		if s[i] == '1':
			cn1 += 1
		if s[i] == '0':
			cn0 += 1
		if s[i] == '?':
			c += 1
		f0 = k // 2 - cn0
		f1 = k // 2 - cn1
		if f1 + f0 != c or f0 < 0 or f1 < 0:
			chk = False
			break
	if chk:
		print('YES')
	else:
		print('NO')


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = input().rstrip()
	flag = 'YES'
	(cnt0, cnt1) = (0, 0)
	b = []
	for x in a:
		b.append(x)
	for x in range(k):
		s = set()
		for y in range(x, n, k):
			s.add(b[y])
		if '1' in s and '0' in s:
			flag = 'NO'
			break
		if len(s) == 1:
			continue
		if '1' in s:
			for y in range(x, n, k):
				b[y] = '1'
		else:
			for y in range(x, n, k):
				b[y] = '0'
	a = ''.join(b) + ' '
	cnt0 = a[:k].count('0')
	cnt1 = a[:k].count('1')
	for x in range(k - 1, n):
		if cnt0 * 2 > k or cnt1 * 2 > k:
			flag = 'NO'
			break
		if a[x - k + 1] == '1':
			cnt1 -= 1
		if a[x - k + 1] == '0':
			cnt0 -= 1
		if a[x + 1] == '1':
			cnt1 += 1
		if a[x + 1] == '0':
			cnt0 += 1
	print(flag)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	a = list(input())
	flag = 1
	for i in range(n):
		if a[i] == '?':
			if i + k < n:
				a[i] = a[i + k]
		elif a[i] == '0':
			if i + k < n:
				if a[i] != a[i + k] and a[i + k] == '1':
					flag = 0
					break
				a[i + k] = a[i]
			if i - k >= 0:
				if a[i] != a[i - k] and a[i - k] == '1':
					flag = 0
					break
				a[i - k] = a[i]
		else:
			if i + k < n:
				if a[i] != a[i + k] and a[i + k] == '0':
					flag = 0
					break
				a[i + k] = a[i]
			if i - k >= 0:
				if a[i] != a[i - k] and a[i - k] == '1':
					flag = 0
					break
				a[i - k] = a[i]
	one = 0
	zero = 0
	doubt = 0
	for i in range(k):
		if a[i] == '1':
			one += 1
		elif a[i] == '0':
			zero += 1
		else:
			doubt = 0
	if one > k / 2 or zero > k / 2:
		print('NO')
		continue
	oneneed = k / 2 - one
	zeroneed = k / 2 - zero
	for i in range(k, n):
		if a[i - k] == '1':
			one -= 1
		if a[i - k] == '0':
			zero -= 1
		if a[i] == '1':
			one += 1
		if a[i] == '0':
			zero += 1
		if one > k / 2 or zero > k / 2:
			flag = 0
			break
	if flag == 0:
		print('NO')
	else:
		print('YES')


import sys
from collections import Counter

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7

def check(C, K):
	if C['0'] > K // 2 or C['1'] > K // 2:
		return False
	else:
		return True
for _ in range(INT()):
	(N, K) = MAP()
	S = list(input())
	ok = True
	for i in range(N - K):
		if S[i + K] == '?':
			S[i + K] = S[i]
		elif S[i] != '?' and S[i] != S[i + K]:
			ok = False
			break
	if not ok:
		NO()
		continue
	C = Counter()
	for i in range(K):
		C[S[i]] += 1
	if not check(C, K):
		NO()
		continue
	for i in range(K, N):
		C[S[i - K]] -= 1
		C[S[i]] += 1
		if not check(C, K):
			ok = False
			break
	if ok:
		YES()
	else:
		NO()


def f(x):
	if x == '?':
		return -1
	return int(x)
t = int(input())
for t in range(t):
	(n, k) = [int(x) for x in input().split(' ')]
	a = [f(x) for x in input()]
	dat = []
	i = 0
	while 1:
		if i + k <= n:
			dat.append(a[i:i + k])
		else:
			dat.append(a[i:n])
			while len(dat[-1]) != k:
				dat[-1].append(-1)
		i += k
		if n <= i:
			break
	fl = 1
	dom1 = 0
	dom0 = 0
	domany = 0
	for j in range(k):
		stl = set()
		for i in range(len(dat)):
			stl.add(dat[i][j])
		if 0 in stl and 1 in stl:
			fl = 0
			break
		if 1 in stl:
			dom1 += 1
		elif 0 in stl:
			dom0 += 1
		else:
			domany += 1
	if fl:
		if max(dom1, dom0) <= round(k / 2):
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


t = int(input())
for case in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	visited = [False] * n
	f = False
	for x in range(n):
		if s[x] != '?' and (not visited[x]):
			for y in range(x % k, n, k):
				if s[y] != s[x] and s[y] != '?':
					print('NO')
					f = True
					break
				else:
					s[y] = s[x]
				visited[y] = True
		if f:
			break
		visited[x] = True
	if not f:
		print('YES' if s[:k].count('1') <= k / 2 and s[:k].count('0') <= k / 2 else 'NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	f = 0
	s = input()
	a = [-1] * k
	for i in range(n):
		x = i % k
		if a[x] == -1 and s[i] != '?':
			a[x] = int(s[i])
		elif a[x] != -1:
			if s[i] != '?' and int(s[i]) != a[x]:
				f = 1
				break
	if f == 1:
		print('NO')
		continue
	if a.count(-1) == k:
		print('YES')
	else:
		q = a.count(-1)
		one = a.count(1)
		zero = a.count(0)
		if one == zero and q % 2 == 0:
			print('YES')
		elif one > zero and q >= one - zero:
			t = q - (one - zero)
			if t % 2 == 0:
				print('YES')
			else:
				print('NO')
		elif zero > one and q >= zero - one:
			t = q - (zero - one)
			if t % 2 == 0:
				print('YES')
			else:
				print('NO')
		else:
			print('NO')


ans = []
for _ in range(int(input().strip())):
	(n, k) = map(int, input().strip().split())
	string = input().strip()
	S = list(string)
	flag = True
	for i in range(n):
		if i + k < n:
			if S[i + k] == '?':
				S[i + k] = S[i]
			elif S[i] != '?':
				if S[i] != S[i + k]:
					flag = False
					break
	for i in range(n - 1, -1, -1):
		if i - k >= 0:
			if S[i - k] == '?':
				S[i - k] = S[i]
			elif S[i] != '?':
				if S[i] != S[i - k]:
					flag = False
					break
	first_k = S[:k]
	first_k = ''.join(first_k)
	ones = first_k.count('1')
	zeroes = first_k.count('0')
	questions = first_k.count('?')
	if ones == zeroes == k // 2 and flag:
		ans.append('YES')
	elif questions == k and flag:
		ans.append('YES')
	elif ones == questions == k // 2 and flag:
		ans.append('YES')
	elif zeroes == questions == k // 2 and flag:
		ans.append('YES')
	elif zeroes + questions == k // 2 and flag:
		ans.append('YES')
	elif ones + questions == k // 2 and flag:
		ans.append('YES')
	elif ones + questions == zeroes == k // 2 and flag:
		ans.append('YES')
	elif zeroes + questions == ones == k // 2 and flag:
		ans.append('YES')
	elif ones <= k // 2 <= ones + questions and flag:
		ans.append('YES')
	else:
		ans.append('NO')
print('\n'.join((str(res) for res in ans)))


import math
t = int(input())

def findConsecutive(n, k, arr):
	new_arr = arr.copy()
	if k >= n:
		return arr
	for i in range(n):
		change = True
		while change == True:
			change = False
			if new_arr[i] == '?':
				if i - k >= 0 and new_arr[i - k] != '?':
					new_arr[i] = new_arr[i - k]
					change = True
				elif i + k < n and new_arr[i + k] != '?':
					new_arr[i] = new_arr[i + k]
					change = True
			elif i - k >= 0 and new_arr[i - k] == '?':
				new_arr[i - k] = new_arr[i]
				change = True
			elif i + k < n and new_arr[i + k] == '?':
				new_arr[i + k] = new_arr[i]
				change = True
			elif i - k >= 0 and new_arr[i - k] != new_arr[i]:
				return False
			elif i + k < n and new_arr[i + k] != new_arr[i]:
				return False
	return new_arr

def lookSubarrays(n, k, arr):
	one_count = 0
	zero_count = 0
	for i in range(0, n - k + 1):
		if i == 0:
			one_count = arr[i:i + k].count('1')
			zero_count = arr[i:i + k].count('0')
		else:
			if arr[i - 1] == '1':
				one_count -= 1
			elif arr[i - 1] == '0':
				zero_count -= 1
			if arr[i + k - 1] == '1':
				one_count += 1
			elif arr[i + k - 1] == '0':
				zero_count += 1
		if one_count > k / 2 or zero_count > k / 2:
			return 'NO'
	return 'YES'
ns = []
ks = []
arrs = []
for _ in range(t):
	(n, k) = input().split(' ')
	ns.append(int(n))
	ks.append(int(k))
	nums = list(input())
	arrs.append(nums)
for i in range(t):
	n = ns[i]
	k = ks[i]
	moves = n - 1
	arr = arrs[i].copy()
	returned = findConsecutive(n, k, arr)
	if returned == False:
		print('NO')
		continue
	arr = returned.copy()
	print(lookSubarrays(n, k, arr))


import sys

def answer(n, k, s):
	a = ['?' for _ in range(k)]
	for i in range(n):
		if s[i] == '0' and a[i % k] == '1':
			return 'NO'
		elif s[i] == '1' and a[i % k] == '0':
			return 'NO'
		elif s[i] == '0' and a[i % k] == '?':
			a[i % k] = '0'
		elif s[i] == '1' and a[i % k] == '?':
			a[i % k] = '1'
	n0 = a.count('0')
	n1 = a.count('1')
	if n0 <= k // 2 and n1 <= k // 2:
		return 'YES'
	return 'NO'

def main():
	t = int(sys.stdin.readline())
	while t:
		(n, k) = map(int, sys.stdin.readline().split())
		s = sys.stdin.readline().rstrip()
		print(answer(n, k, s))
		t -= 1
	return
main()


from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict
from itertools import accumulate
R = lambda : map(int, input().split())
midx = {'0': 0, '1': 1, '?': 2}
for _ in range(int(input())):
	(n, k) = R()
	s = [c for c in input()]
	good = True
	for i in range(k):
		cter = Counter(s[i::k])
		sc = '?'
		if '0' in cter and '1' in cter:
			good = False
			break
		elif '0' in cter:
			sc = '0'
		elif '1' in cter:
			sc = '1'
		for j in range(i, n, k):
			s[j] = sc
	cnts = [0, 0, 0]
	for i in range(n):
		cnts[midx[s[i]]] += 1
		if sum(cnts) > k:
			cnts[midx[s[i - k]]] -= 1
		if sum(cnts) == k and abs(cnts[0] - cnts[1]) > cnts[2]:
			good = False
			break
	print('YES' if good else 'NO')


t = int(input())
while t:
	t -= 1
	(n, k) = map(int, input().split())
	s = input()
	s = list(s)
	ans = True
	for i in range(k, n):
		if s[i] != '?':
			if s[i % k] == '?':
				s[i % k] = s[i]
			elif s[i % k] != s[i]:
				ans = False
				break
	if not ans:
		print('NO')
		continue
	(one, zer) = (0, 0)
	for i in range(k):
		if s[i] == '0':
			zer += 1
		elif s[i] == '1':
			one += 1
	if one <= k // 2 and zer <= k // 2:
		print('YES')
	else:
		print('NO')


import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil, atan, pi

def input():
	return sys.stdin.readline()[:-1]
import string
from bisect import bisect_left, bisect_right
from functools import lru_cache
MOD = int(1000000000.0) + 7
INF = float('inf')

def solve():
	(n, k) = [int(x) for x in input().split()]
	s = [x for x in input()]
	z = 0
	o = 0
	for i in range(k):
		cur = -1
		for j in range(i, n, k):
			c = s[j]
			if c == '?':
				continue
			if cur == -1:
				cur = c
			elif cur != c:
				print('NO')
				return
		if cur == '0':
			z += 1
		elif cur == '1':
			o += 1
	t = k // 2
	if z > t or o > t:
		print('NO')
		return
	o = 0
	z = 0
	q = 0
	for i in range(n):
		j = i + 1
		if s[i] == '?':
			q += 1
		elif s[i] == '1':
			o += 1
		else:
			z += 1
		if i >= k:
			if s[i - k] == '?':
				q -= 1
			elif s[i - k] == '1':
				o -= 1
			else:
				z -= 1
		if j >= k:
			if z > t or o > t:
				print('NO')
				return
	print('YES')
t = 1
t = int(input())
for case in range(1, t + 1):
	ans = solve()


t = int(input())
for case in range(t):
	(n, k) = map(int, input().split())
	s = input().split()[0]
	modk = [2] * k
	flag = False
	for i in range(n):
		if s[i] == '1':
			if modk[i % k] == 0:
				flag = True
				break
			modk[i % k] = 1
		elif s[i] == '0':
			if modk[i % k] == 1:
				flag = True
				break
			modk[i % k] = 0
	if flag:
		print('NO')
		continue
	numZeros = 0
	numOnes = 0
	for i in range(k):
		if modk[i] == 0:
			numZeros += 1
		elif modk[i] == 1:
			numOnes += 1
	if numZeros <= k // 2 and numOnes <= k // 2:
		print('YES')
	else:
		print('NO')


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
from array import array
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def replaceAllEmpty(arr, n, k):
	last = 0
	for i in range(k):
		bitSet = set()
		toSet = last
		for j in range(i, n, k):
			bitSet.add(arr[j])
		if '1' in bitSet and '0' in bitSet:
			return (False, arr)
		elif '?' not in bitSet:
			continue
		elif '1' in bitSet:
			toSet = 1
		elif '0' in bitSet:
			toSet = 0
		else:
			continue
		toSet = str(toSet)
		for j in range(i, n, k):
			arr[j] = toSet
	return (True, arr)

def test_case():
	(n, k) = map(int, input().split())
	arr = [x for x in input()]
	x = [0, 0]
	for i in range(k):
		if arr[i] == '0':
			x[0] += 1
		if arr[i] == '1':
			x[1] += 1
	if x[0] > k // 2 or x[1] > k // 2:
		return False
	if x[0] == k // 2 and x[1] != k // 2 or (x[1] == k // 2 and x[0] != k // 2):
		if x[1] == k // 2:
			toSet = '0'
		else:
			toSet = '1'
		for i in range(k):
			if arr[i] == '?':
				arr[i] = toSet
	(possible, arr) = replaceAllEmpty(arr, n, k)
	if not possible:
		return False
	x = [0, 0]
	for i in range(k):
		if arr[i] == '0':
			x[0] += 1
		if arr[i] == '1':
			x[1] += 1
	if x[0] > k // 2 or x[1] > k // 2:
		return False
	return True

def main():
	t = int(input())
	for _ in range(t):
		if test_case():
			print('YES')
		else:
			print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()

def dummy_function():
	x = 10
	x *= 100
	print('This function is written by Harsh.')

def dummy_function1():
	x = 10
	x *= 100
	print('This function is written by Harsh.')

def dummy_function3():
	x = 10
	x *= 100
	print('This function is written by Harsh.')

def dummy_function4():
	x = 10
	x *= 100
	print('This function is written by Harsh.')


import math
from collections import deque
from sys import stdin, stdout
from string import ascii_letters
import sys
letters = ascii_letters
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(input())
	was = [-1] * k
	can = True
	for i in range(n):
		if arr[i] == '?':
			continue
		if was[i % k] == -1:
			was[i % k] = arr[i]
		elif was[i % k] != arr[i]:
			can = False
			break
	buff = deque(arr[:k])
	zcount = 0
	ocount = 0
	qcount = 0
	for i in range(k):
		if was[i] == -1:
			qcount += 1
		elif was[i] == '1':
			ocount += 1
		else:
			zcount += 1
	if zcount > k // 2 or ocount > k // 2 or k // 2 - zcount + (k // 2 - ocount) > qcount:
		can = False
	print('YES' if can else 'NO')


import sys
from os import path
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def solve():
	(n, k) = [int(i) for i in input().split()]
	arr = [c for c in input()]
	for i in range(n):
		if arr[i] == '?':
			arr[i] = -1
		else:
			arr[i] = int(arr[i])
	one = 0
	zero = 0
	for i in range(k):
		if arr[i] == 1:
			one += 1
		elif arr[i] == 0:
			zero += 1
	if one > k // 2 or zero > k // 2:
		print('NO')
		return
	if one == k // 2:
		for i in range(k):
			if arr[i] == -1:
				arr[i] = 0
		zero = k // 2
	if zero == k // 2:
		for i in range(k):
			if arr[i] == -1:
				arr[i] = 1
		one = k // 2
	for i in range(n - k):
		l = arr[i]
		r = arr[i + k]
		if l == 1 and r == 0 or (l == 0 and r == 1):
			print('NO')
			return
		if l == 1 and r == -1:
			arr[i + k] = 1
		if l == 0 and r == -1:
			arr[i + k] = 0
		if l == -1 and r == 1:
			one += 1
		if l == -1 and r == 0:
			zero += 1
		if zero > k // 2 or one > k // 2:
			print('NO')
			return
	print('YES')

def main():
	tc = int(input().strip())
	for case in range(tc):
		solve()
main()


for _ in range(int(input())):
	(n, k) = [int(j) for j in input().split()]
	arr = list(input())
	flag = 0
	for i in range(k):
		f0 = 0
		f1 = 0
		for j in range(i, n, k):
			if arr[j] == '0':
				f0 += 1
			if arr[j] == '1':
				f1 += 1
		if f0 > 0 and f1 > 0:
			print('NO')
			flag = 1
			continue
		else:
			a = '?'
			if f0 > 0:
				a = '0'
			if f1 > 0:
				a = '1'
			for j in range(i, n, k):
				arr[j] = a
	c0 = 0
	c1 = 0
	if flag == 0:
		for i in range(k):
			if arr[i] == '0':
				c0 += 1
			if arr[i] == '1':
				c1 += 1
		if c0 > k / 2 or c1 > k / 2:
			print('NO')
			continue
		else:
			print('YES')
			continue


import sys
input = sys.stdin.readline
read = lambda : map(int, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0, hi=None):
	if hi == None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
from itertools import permutations
import math
from bisect import bisect_left

def solve():
	(n, k) = read()
	s = list(input().rstrip())
	for i in range(k):
		if s[i] == '?':
			for j in range(i + k, n, k):
				if s[j] != '?':
					s[i] = s[j]
					break
	for i in range(k):
		if s[i] != '?':
			for j in range(i + k, n, k):
				if s[j] == '?':
					s[j] = s[i]
				elif s[j] != s[i]:
					print('NO')
					return
	if s[:k].count('1') <= k // 2 and s[:k].count('0') <= k // 2:
		print('YES')
	else:
		print('NO')
for _ in range(int(input())):
	solve()


import math
num_cases = int(input())
for _ in range(num_cases):
	(n, k) = list(map(int, input().strip().split()))
	arr = input().strip()
	arr = [i for i in arr]
	ones = 0
	zeros = 0
	for i in range(k):
		if arr[i] == '0':
			zeros += 1
		elif arr[i] == '1':
			ones += 1
	qs = k - (zeros + ones)
	if qs < abs(zeros - ones):
		print('NO')
	else:
		not_poss = 0
		start = arr[0]
		for i in range(1, n - k + 1):
			last = arr[i + k - 1]
			if last == '?' and start != '?':
				arr[i + k - 1] = start
				last = start
			elif last != '?' and start != '?' and (last != start):
				not_poss = 1
				break
			if last != start:
				if last == '?' and start == '0':
					qs += 1
					zeros -= 1
				if last == '?' and start == '1':
					qs += 1
					ones -= 1
				if last == '0' and start == '?':
					qs -= 1
					zeros += 1
				if last == '1' and start == '?':
					qs -= 1
					ones += 1
				if qs < abs(zeros - ones):
					not_poss = 1
					break
			start = arr[i]
		if not_poss == 1:
			print('NO')
		else:
			print('YES')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	bitstring = input()
	zero = 0
	one = 0
	answer = True
	for j in range(k):
		balanced = '?'
		for i in range(j, n, k):
			if bitstring[i] != '?':
				if balanced != '?':
					if balanced != bitstring[i]:
						answer = False
						break
				else:
					balanced = bitstring[i]
		if not answer:
			break
		if balanced == '1':
			one += 1
		elif balanced == '0':
			zero += 1
	if answer:
		answer = max(one, zero) <= k // 2
	print('YES' if answer else 'NO')


def ans():
	(n, k) = map(int, input().split())
	a = input()
	l = list(a)
	for i in range(k):
		if l[i] == '?':
			for j in range(i + k, n, k):
				if l[j] != '?':
					l[i] = l[j]
					break
	for i in range(k):
		if l[i] != '?':
			for j in range(i + k, n, k):
				if l[j] == '?':
					l[j] = l[i]
				elif l[j] != l[i]:
					print('NO')
					return
	one = l[:k].count('1')
	zero = l[:k].count('0')
	if one <= k // 2 and zero <= k // 2:
		print('YES')
	else:
		print('NO')

def solve():
	for _ in range(int(input())):
		ans()
solve()


for _ in range(int(input())):
	(n, k) = [int(x) for x in input().split()]
	a = input()
	x = 0
	y = 0
	z = 0
	flag = 0
	for i in range(k):
		c = 0
		d = 0
		for j in range(i, n, k):
			if a[j] == '0':
				c += 1
			elif a[j] == '1':
				d += 1
		if c > 0 and d > 0:
			flag = 1
			break
		elif c > 0:
			x += 1
		elif d > 0:
			y += 1
		else:
			z += 1
	for i in range(z):
		if x < k // 2:
			x += 1
		else:
			y += 1
	if x != y:
		flag = 1
	if flag:
		print('NO')
	else:
		print('YES')


import os, sys
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		fl2 = 0
		(n, k) = map(int, input().split())
		s = list(input())
		for i in range(k):
			x = i
			(fl, fl1) = (0, 0)
			while x < n:
				if s[x] == '1':
					fl = 1
				elif s[x] == '0':
					fl1 = 1
				x += k
			if fl and fl1:
				fl2 = 1
				print('NO')
				break
			elif fl:
				s[i] = '1'
			elif fl1:
				s[i] = '0'
		if not fl2:
			a = [0, 0, 0]
			for i in range(k):
				if s[i] == '0':
					a[0] += 1
				elif s[i] == '1':
					a[1] += 1
				else:
					a[2] += 1
			if a[0] > k // 2 or a[1] > k // 2:
				print('NO')
			else:
				print('YES')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve(case):
	(n, k) = sep()
	s = list(inp())
	f = True
	for kk in range(k):
		arr = []
		if not f:
			break
		for i in range(kk, n, k):
			arr.append(s[i])
		if arr.count('?') == len(arr):
			continue
		elif arr.count('0') > 0 and arr.count('1') > 0:
			f = False
			break
		else:
			if arr.count('0') > 0:
				chng = '0'
			else:
				chng = '1'
			for i in range(kk, n, k):
				s[i] = chng
	if f:
		sum = 0
		cnt = 0
		for i in range(k):
			if s[i] == '?':
				cnt += 1
			else:
				sum += int(s[i])
		if sum > k // 2 or sum + cnt < k // 2:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')
testcase(int(inp()))


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	arr = list(input())
	i1 = 0
	i2 = k
	f = True
	while i2 < n:
		if arr[i1] == '?' and arr[i2] != '?':
			arr[i1] = arr[i2]
		elif arr[i1] != '?' and arr[i2] == '?':
			arr[i2] = arr[i1]
		elif arr[i1] != '?' and arr[i2] != '?' and (arr[i1] != arr[i2]):
			f = False
			break
		i2 += 1
		i1 += 1
	if f == False:
		print('NO')
	else:
		i1 = n - 1 - k
		i2 = n - 1
		while i1 >= 0:
			if arr[i1] == '?' and arr[i2] != '?':
				arr[i1] = arr[i2]
			elif arr[i1] != '?' and arr[i2] == '?':
				arr[i2] = arr[i1]
			elif arr[i1] != '?' and arr[i2] != '?' and (arr[i1] != arr[i2]):
				f = False
				break
			i1 -= 1
			i2 -= 1
		if f == False:
			print('NO')
		else:
			ans = [0, 0, 0]
			for i in range(k):
				if arr[i] == '0':
					ans[0] += 1
				elif arr[i] == '1':
					ans[1] += 1
				else:
					ans[2] += 1
			d = max(ans[0], ans[1]) - min(ans[0], ans[1])
			if ans[2] >= d:
				print('YES')
			else:
				print('NO')


def isKSubstring(s, k, n):
	if s[:k].count('1') != k // 2:
		return False
	for i in range(k, n):
		if s[i] != s[i - k]:
			return False
	return True
t = int(input())
for p in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	if s == ['?'] * n:
		print('YES')
	elif s.count('?') == 0:
		if isKSubstring(s, k, n):
			print('YES')
		else:
			print('NO')
	else:
		ans = 'YES'
		if s[:k].count('?') == 0:
			if s[:k].count('1') != k // 2:
				ans = 'NO'
			else:
				for i in range(k, n):
					if s[i] == '?':
						s[i] = s[i - k]
					elif s[i] != s[i - k]:
						ans = 'NO'
						break
			print(ans)
		else:
			a = s[:k].count('1')
			b = s[:k].count('0')
			if a > k // 2 or b > k // 2:
				ans = 'NO'
			if ans == 'YES':
				if n >= 2 * k:
					for i in range(k):
						if s[i] == '?':
							for j in range(i + k, n, k):
								if s[j] != '?':
									s[i] = s[j]
				else:
					for i in range(n - k):
						if s[i] == '?':
							for j in range(i + k, n, k):
								if s[j] != '?':
									s[i] = s[j]
					a = s[:k].count('1')
					b = s[:k].count('0')
					if a > k // 2 or b > k // 2:
						ans = 'NO'
					if ans == 'YES':
						for i in range(n - k, k):
							if s[i] == '?':
								if a < k // 2:
									s[i] = '1'
									a += 1
								elif b < k // 2:
									s[i] = '0'
									b += 1
				if ans == 'YES':
					a = s[:k].count('1')
					b = s[:k].count('0')
					if a < k // 2 or b < k // 2:
						for i in range(k):
							if s[i] == '?':
								if a < k // 2:
									s[i] = '1'
									a += 1
								elif b < k // 2:
									s[i] = '0'
									b += 1
					if s[:k].count('1') != k // 2:
						ans = 'NO'
					else:
						for i in range(k, n):
							if s[i] == '?':
								s[i] = s[i - k]
							elif s[i] != s[i - k]:
								ans = 'NO'
								break
			print(ans)


test = int(input())
for t in range(0, test):
	(n, k) = [int(i) for i in input().split()]
	b = input()
	a = []
	for e in b:
		a.append(e)
	ak = []
	for i in range(0, k):
		ak.append([])
	for i in range(0, n):
		ak[i % k].append(a[i])
	f = 1
	kw = []
	for i in range(0, k):
		unique = []
		for e in ak[i]:
			if e not in unique:
				unique.append(e)
		if '1' in unique and '0' in unique:
			f = 0
			break
		else:
			if len(unique) == 2:
				unique.remove('?')
			kw.append(unique[0])
	if f == 0:
		print('NO')
		continue
	for i in range(0, k):
		j = i
		while j < n:
			a[j] = kw[i]
			j += k
	oc = 0
	zc = 0
	qc = 0
	i = 0
	while i < k:
		if a[i] == '1':
			oc += 1
		elif a[i] == '0':
			zc += 1
		else:
			qc += 1
		i += 1
	if oc > k / 2 or zc > k / 2:
		print('NO')
		continue
	while i < n:
		if a[i - k] == '1':
			oc -= 1
		elif a[i - k] == '0':
			zc -= 1
		else:
			qc -= 1
		if a[i] == '1':
			oc += 1
		elif a[i] == '0':
			zc += 1
		else:
			qc += 1
		if oc > k / 2 or zc > k / 2:
			f = 0
			break
		i += 1
	if f == 0:
		print('NO')
	else:
		print('YES')


def is_poss(n, k, s):
	first = ''
	for i in range(k):
		char = s[i]
		for j in range(i, n, k):
			if s[j] != '?' and s[j] != char and (char != '?'):
				return False
			if s[j] != '?':
				char = s[j]
		first += char
	freq = {'?': 0, '0': 0, '1': 0}
	for i in first:
		freq[i] += 1
	if freq['0'] + freq['?'] < freq['1'] or freq['1'] + freq['?'] < freq['0']:
		return False
	return True

def main():
	t = int(input())
	for _ in range(t):
		(n, k) = map(int, input().split())
		s = input()
		if is_poss(n, k, s):
			print('YES')
		else:
			print('NO')
main()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	S = input()
	s = []
	for i in S:
		s.append(i)
	ca = 0
	cb = 0
	cc = 0
	for i in range(k):
		if s[i] == '0':
			ca += 1
		elif s[i] == '1':
			cb += 1
		else:
			cc += 1
	check = True
	if ca > k // 2 or cb > k // 2:
		check = False
	ca = k // 2 - ca
	cb = k // 2 - cb
	for i in range(k, n):
		if s[i] != '?' and s[i - k] != '?' and (s[i] != s[i - k]):
			check = False
			break
		if s[i] == '?' and s[i - k] != '?':
			s[i] = s[i - k]
		elif s[i - k] == '?' and s[i] != '?':
			s[i - k] = s[i]
	ca = 0
	cb = 0
	cc = 0
	for i in range(k):
		if s[i] == '0':
			ca += 1
		elif s[i] == '1':
			cb += 1
		else:
			cc += 1
	if ca > k // 2 or cb > k // 2:
		check = False
	for i in range(k, n):
		if s[i] != '?' and s[i - k] != '?' and (s[i] != s[i - k]):
			check = False
			break
		if s[i] == '0':
			ca += 1
		elif s[i] == '1':
			cb += 1
		else:
			cc += 1
		if s[i - k] == '0':
			ca -= 1
		elif s[i - k] == '1':
			cb -= 1
		else:
			cc -= 1
		if ca > k // 2 or cb > k // 2:
			check = False
			break
	if check:
		print('YES')
	else:
		print('NO')


import sys
import math
input = sys.stdin.readline

def get_int():
	return int(input().strip())

def get_int_list():
	return list(map(int, input().strip().split()))

def get_char_list():
	return list(input().strip())

def count(ds, tv):
	result = 0
	for v in ds:
		if v == tv:
			result += 1
	return result
cases = get_int()
for _ in range(cases):
	(n, k) = get_int_list()
	chs = get_char_list()
	kstr = chs[:k]
	onecount = count(kstr, '1')
	markcount = count(kstr, '?')
	zerocount = k - onecount - markcount
	if onecount > k // 2 or onecount + markcount < k // 2:
		print('NO')
	else:
		bad = False
		remaining_one = k // 2 - onecount
		remaining_zero = k // 2 - zerocount
		for i in range(k, n, k):
			for j in range(i, min(n, i + k)):
				ti = j - i
				origin_char = kstr[ti]
				target_char = chs[j]
				if origin_char != '?' and target_char != '?' and (origin_char != target_char):
					bad = True
					break
				elif origin_char == '?' and target_char != '?':
					if target_char == '1' and remaining_one > 0:
						remaining_one -= 1
						kstr[ti] = '1'
					elif target_char == '1' and remaining_one <= 0:
						bad = True
						break
					elif target_char == '0' and remaining_zero <= 0:
						bad = True
						break
					else:
						remaining_zero -= 1
						kstr[ti] = '0'
			if bad:
				break
		if bad:
			print('NO')
		else:
			print('YES')


import sys, math, cmath, time
start_time = time.time()

def solve():
	(n, k) = invr()
	s = insr()
	q = ['?'] * k
	qw = 0
	for i in range(len(s)):
		w = i % k
		if q[w] == '?' and s[i] != '?':
			q[w] = s[i]
		if s[i] != '?' and q[w] != s[i]:
			qw = 1
			break
	if qw == 0 and 2 * q.count('1') <= k and (2 * q.count('0') <= k):
		print('YES')
	else:
		print('NO')

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
		sys.stdout.close()

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def counter_elements(a):
	q = dict()
	for i in range(len(a)):
		if a[i] not in q:
			q[a[i]] = 0
		q[a[i]] = q[a[i]] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))

def transpose(a):
	(n, m) = (len(a), len(a[0]))
	b = [[0] * n for i in range(m)]
	for i in range(m):
		for j in range(n):
			b[i][j] = a[j][i]
	return b

def factorial(n, m=1000000007):
	q = 1
	for i in range(n):
		q = q * (i + 1) % m
	return q
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())

def solve():
	(n, k) = mpint()
	half = k >> 1
	s = list(inp())
	sts = [set(s[i::k]) for i in range(k)]
	for st in sts:
		st.discard('?')
		if len(st) == 2:
			return False
	sts = [st.pop() if st else None for st in sts]
	for i in range(n):
		t = i % k
		if s[i] == '?' and sts[t]:
			s[i] = sts[t]
	zero = one = 0
	for char in s[:k]:
		if char == '0':
			zero += 1
		elif char == '1':
			one += 1
	for i in range(k, n):
		if zero > half or one > half:
			return False
		if s[i - k] == '0':
			zero -= 1
		elif s[i - k] == '1':
			one -= 1
		if s[i] == '0':
			zero += 1
		elif s[i] == '1':
			one += 1
	if zero > half or one > half:
		return False
	return True
for __ in range(itg()):
	print('YES' if solve() else 'NO')


import sys
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	a = sys.stdin.readline().strip()
	p = []
	f = 0
	z = 0
	o = 0
	for j in range(k):
		p.append(['?'])
	for j in range(n):
		p[j % k].append(a[j])
	for j in range(k):
		q = set(p[j])
		if len(q) > 2:
			f = 1
		elif '1' in q:
			o = o + 1
		elif '0' in q:
			z = z + 1
	if f == 1:
		print('NO')
	else:
		q = k // 2
		if z > q or o > q:
			print('NO')
		else:
			print('YES')


from sys import stdin
input = stdin.readline
q = int(input())
for _ in range(q):
	(n, k) = map(int, input().split())
	s = list(input())
	odp = 'YES'
	for j in range(n):
		i = n - j - 1
		if i < k:
			continue
		if s[i] != '?':
			if s[i - k] == '?':
				s[i - k] = s[i]
			elif s[i - k] != s[i]:
				odp = 'NO'
	c = s[:k]
	x = c.count('1')
	ind = 0
	if c.count('1') > k // 2:
		odp = 'NO'
	if c.count('0') > k // 2:
		odp = 'NO'
	print(odp)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(input())
	flag = 0
	for i in range(k):
		b = [0, 0]
		for j in range(i, n, k):
			if a[j] != '?':
				b[int(a[j])] += 1
		if min(b) > 0:
			flag = 1
		elif max(b) > 0:
			for j in range(i, n, k):
				if b[0] > 0:
					a[j] = '0'
				else:
					a[j] = '1'
	one = 0
	zero = 0
	q = 0
	for i in range(k):
		if a[i] == '1':
			one += 1
		elif a[i] == '0':
			zero += 1
		else:
			q += 1
	if one != zero and (zero + q < one or one + q < zero):
		flag = 1
	if flag:
		print('NO')
	else:
		print('YES')


N = int(input())
Ans = []
for i in range(N):
	(a, b) = map(int, input().split())
	P = list(input())
	t = 0
	used = [0] * a
	for j in range(a):
		if used[j] == 0:
			if j + b >= a:
				break
			for l in range(b, a - j + 1, b):
				if j + l >= a:
					break
				used[j + l] = 1
				if P[j] == '?' and P[j + l] != '?':
					P[j] = P[j + l]
				if P[j] != '?' and P[j + l] == '?':
					P[j + l] = P[j]
	for j in range(a):
		if j + b >= a:
			break
		if P[j] == '0' and P[j + b] == '1':
			t = 1
			break
		if P[j] == '1' and P[j + b] == '0':
			t = 1
			break
	c_1 = 0
	c_2 = 0
	for j in range(b):
		if P[j] == '0':
			c_1 += 1
		if P[j] == '1':
			c_2 += 1
	if c_1 > b // 2 or c_2 > b // 2:
		t = 1
	if t == 0:
		Ans.append('YES')
	else:
		Ans.append('NO')
for i in Ans:
	print(i)


for t in range(int(input())):
	(n, k) = map(int, input().split())
	wa = ['?'] * k
	pala = input().strip()
	res = 'YES'
	for i in range(n):
		if pala[i] != '?':
			if wa[i % k] == '?':
				wa[i % k] = pala[i]
			elif pala[i] != wa[i % k]:
				res = 'NO'
	else:
		(a, b) = (0, 0)
		for c in wa:
			if c == '0':
				a += 1
			elif c == '1':
				b += 1
		if 2 * a > k or 2 * b > k:
			res = 'NO'
	print(res)


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)
for xyz in range(0, int(input())):
	(n, m) = map(int, input().split())
	l = list(input())
	f = True
	for i in range(m, n):
		if l[i] != '?' and l[i % m] != '?':
			if l[i % m] != l[i]:
				f = False
				break
		elif l[i] == '?' and l[i % m] != '?':
			l[i] = l[i % m]
		elif l[i] != '?' and l[i % m] == '?':
			l[i % m] = l[i]
	c0 = c1 = 0
	for i in range(0, m):
		if l[i] == '0':
			c0 += 1
		elif l[i] == '1':
			c1 += 1
	if c1 > m // 2 or c0 > m // 2:
		f = False
	if f:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	li = input()
	must_one = [False] * n
	must_zeros = [False] * n
	f = 0
	for i in range(n):
		if li[i] == '1':
			must_one[i % k] = True
		if li[i] == '0':
			must_zeros[i % k] = True
	zero = one = 0
	for i in range(k):
		if must_one[i] and must_zeros[i]:
			print('NO')
			f = -1
			break
		one += must_one[i]
		zero += must_zeros[i]
	if f == 0:
		if one > k // 2 or zero > k // 2:
			print('NO')
		else:
			print('YES')


import sys

def solve(string, k):
	n = len(string)
	string = list(string)
	for mod in range(k):
		o = False
		z = False
		for i in range(mod, n, k):
			if string[i] == '1':
				o = True
			elif string[i] == '0':
				z = True
		if o and z:
			return False
		if o:
			for i in range(mod, n, k):
				string[i] = '1'
		elif z:
			for i in range(mod, n, k):
				string[i] = '0'
	ones = 0
	zeros = 0
	for i in range(k):
		ones += string[i] == '1'
		zeros += string[i] == '0'
	if ones <= k / 2 and zeros <= k / 2:
		return True
	return False
caseCount = int(next(sys.stdin))
for i in range(caseCount):
	(n, k) = [int(x) for x in next(sys.stdin)[:-1].split(' ')]
	s = next(sys.stdin)[:-1]
	ans = solve(s, k)
	print(['NO', 'YES'][ans])


import sys
input = sys.stdin.readline

def solve(s, k):
	pt = 0
	L = [-1] * k
	for i in range(len(s)):
		if s[i] != '?':
			t = int(s[i])
			if L[i % k] != -1 and L[i % k] != t:
				return False
			else:
				L[i % k] = t
	news = ''
	ctL = [0, 0, 0]
	for i in range(k):
		t = L[i]
		ctL[t] += 1
	if ctL[0] + ctL[2] < ctL[1] or ctL[1] + ctL[2] < ctL[0]:
		return False
	return True
for i in ' ' * int(input()):
	(n, k) = map(int, input().split())
	print(['NO', 'YES'][solve(input().strip(), k)])


def solve():
	(n, k) = map(int, input().split())
	s = list(input().strip())
	for start in range(k):
		letters = set(s[start:n:k])
		if '0' in letters and '1' in letters:
			print('NO')
			return
		if '0' in letters:
			s[start] = '0'
		if '1' in letters:
			s[start] = '1'
	nr_0 = s[:k].count('0')
	nr_1 = s[:k].count('1')
	nr_q = s[:k].count('?')
	if abs(nr_0 - nr_1) > nr_q:
		print('NO')
	else:
		print('YES')
runs = int(input())
while runs > 0:
	solve()
	runs -= 1


from sys import stdin, stdout

def input():
	return stdin.readline().strip()

def ans(N, K, A):
	template = list(A[0:K])
	for _ in range(2):
		num_0 = 0
		num_1 = 0
		num_q = 0
		for c in template:
			if c == '0':
				num_0 += 1
			elif c == '1':
				num_1 += 1
			elif c == '?':
				num_q += 1
			else:
				assert False
		gap = max(num_0, num_1) - min(num_0, num_1)
		if gap > num_q:
			return 'NO'
		for i in range(N):
			a = template[i % K]
			b = A[i]
			if a != '?' and b != '?' and (a != b):
				return 'NO'
			if a == '?' and b != '?':
				template[i % K] = b
	return 'YES'
T = int(input())
for t in range(T):
	(N, K) = input().split()
	N = int(N)
	K = int(K)
	A = input()
	print(ans(N, K, A))


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	a = ['*'] * k
	f = 0
	for i in range(n):
		if s[i] != '?':
			if s[i] == a[i % k] or a[i % k] == '*':
				a[i % k] = s[i]
			else:
				f = 1
				print('NO')
				break
	if f == 0:
		(star, zero, one) = (a.count('*'), a.count('0'), a.count('1'))
		if abs(zero - one) <= star:
			print('YES')
		else:
			print('NO')


t = int(input())
while t:
	(n, k) = map(int, input().split())
	s = input()
	on = [False for i in range(n)]
	off = [False for i in range(n)]
	for i in range(n):
		if s[i] == '1':
			on[i % k] = True
		if s[i] == '0':
			off[i % k] = True
	check = True
	for i in range(k):
		if on[i] and off[i]:
			check = False
	onCnt = offCnt = 0
	for i in range(k):
		if on[i]:
			onCnt += 1
		elif off[i]:
			offCnt += 1
	if onCnt * 2 > k or offCnt * 2 > k:
		check = False
	if check:
		print('YES')
	else:
		print('NO')
	t -= 1


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	arr = []
	imp = 0
	for i in range(k):
		if s[i] != '?':
			arr.append(s[i])
		else:
			j = 0
			while i + j * k < n:
				if s[i + j * k] != '?':
					break
				j += 1
			if i + j * k < n:
				arr.append(s[i + j * k])
			else:
				arr.append('?')
	for i in range(n):
		if s[i] == arr[i % k] or s[i] == '?':
			continue
		else:
			imp = 1
			break
	if imp == 1:
		print('NO')
	else:
		zero = 0
		one = 0
		ques = 0
		for i in arr:
			if i == '0':
				zero += 1
			elif i == '1':
				one += 1
			else:
				ques += 1
		if abs(one - zero) <= ques:
			print('YES')
		else:
			print('NO')


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc
farr = [1]
ifa = []

def fact(x, mod=0):
	if mod:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr) % mod)
	else:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr))
	return farr[x]

def ifact(x, mod):
	global ifa
	ifa.append(pow(farr[-1], mod - 2, mod))
	for i in range(x, 0, -1):
		ifa.append(ifa[-1] * i % mod)
	ifa = ifa[::-1]

def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	return farr[i] * ifa[i - j] % mod

def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j, mod) * ifa[j] % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def linc(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t > f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rinc(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t < f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def ldec(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t < f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rdec(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t > f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def binfun(x):
	c = 0
	for w in arr:
		c += ceil(w / x)
	return c

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class smt:

	def __init__(self, l, r, arr):
		self.l = l
		self.r = r
		self.value = (1 << 31) - 1 if l < r else arr[l]
		mid = (l + r) // 2
		if l < r:
			self.left = smt(l, mid, arr)
			self.right = smt(mid + 1, r, arr)
			self.value &= self.left.value & self.right.value

	def setvalue(self, x, val):
		if self.l == self.r:
			self.value = val
			return
		mid = (self.l + self.r) // 2
		if x <= mid:
			self.left.setvalue(x, val)
		else:
			self.right.setvalue(x, val)
		self.value = self.left.value & self.right.value

	def ask(self, l, r):
		if l <= self.l and r >= self.r:
			return self.value
		val = (1 << 31) - 1
		mid = (self.l + self.r) // 2
		if l <= mid:
			val &= self.left.ask(l, r)
		if r > mid:
			val &= self.right.ask(l, r)
		return val

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] < self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d

def GP(it):
	return [(ch, len(list(g))) for (ch, g) in groupby(it)]

class DLN:

	def __init__(self, val):
		self.val = val
		self.pre = None
		self.next = None
t = N()
for i in range(t):
	(n, k) = RL()
	s = list(input())
	flag = True
	for i in range(k):
		for j in range(i, n, k):
			if s[j] != s[j % k]:
				if s[j] == '?':
					continue
				if s[j % k] == '?':
					s[j % k] = s[j]
				else:
					flag = False
					break
		if not flag:
			break
	zero = one = 0
	for i in range(k):
		if s[i] == '1':
			one += 1
		elif s[i] == '0':
			zero += 1
	if zero > k // 2 or one > k // 2:
		flag = False
	ans = 'YES' if flag else 'NO'
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	l = [0] * k
	f = k
	for i in range(k):
		v = []
		c = 0
		for j in s[i::k]:
			if j != '?':
				if j not in v:
					v += [j]
					c += 1
				if c > 1:
					break
		else:
			if v:
				l[i] = (-1) ** int(v[0])
				f -= 1
			continue
		break
	else:
		print(['NO', 'YES'][abs(sum(l)) <= f])
		continue
	print('NO')


import sys
input = sys.stdin.readline
from collections import *
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input()[:-1])
	se = defaultdict(set)
	for i in range(n):
		if s[i] != '?':
			se[i % k].add(s[i])
	flag = True
	for i in range(k):
		if len(se[i]) >= 2:
			flag = False
	if not flag:
		print('NO')
		continue
	for i in range(n):
		if s[i] == '?' and len(se[i % k]) == 1:
			s[i] = list(se[i % k])[0]
	zacc = [0]
	oacc = [0]
	for i in range(n):
		zacc.append(zacc[-1] + (1 if s[i] == '0' else 0))
		oacc.append(oacc[-1] + (1 if s[i] == '1' else 0))
	for i in range(n - k + 1):
		if zacc[i + k] - zacc[i] > k // 2 or oacc[i + k] - oacc[i] > k // 2:
			flag = False
	if flag:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	ans = True
	for i in range(k):
		d = set(s[i::k])
		if '0' in d and '1' in d:
			print('NO')
			ans = False
			break
		if '?' in d and len(d) == 2:
			r = (d - {'?'}).pop()
			for p in range(i, n, k):
				s[p] = r
	if ans:
		print('YES') if max(s[:k].count('0'), s[:k].count('1')) <= k / 2 else print('NO')


import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	w = list(input())
	e = w[:k]
	l = 0
	for i in range(k, n):
		if e[i % k] == '?' and (w[i] == '1' or w[i] == '0'):
			e[i % k] = w[i]
		elif (e[i % k] == '1' or e[i % k] == '0') and (w[i] == '1' or w[i] == '0') and (e[i % k] != w[i]):
			l = 1
			break
	if abs(e.count('1') - e.count('0')) > e.count('?'):
		l = 1
	if l == 1:
		print('NO')
	else:
		print('YES')


def solve(n, k, s):
	flag = True
	zerorow = 0
	onerow = 0
	random = 0
	for i in range(k):
		one = 0
		zero = 0
		for j in range(i, n, k):
			if s[j] == '0':
				zero += 1
			if s[j] == '1':
				one += 1
		if zero and one:
			flag = False
			break
		elif zero:
			zerorow += 1
		elif one:
			onerow += 1
		else:
			random += 1
	if zerorow > k / 2 or onerow > k / 2:
		flag = False
	if flag:
		print('YES')
	else:
		print('NO')
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	solve(n, k, s)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input()[:-1])
	flag = True
	for i in range(n):
		if i + k >= n:
			break
		elif s[i] == s[i + k]:
			continue
		elif s[i] == '1' and s[i + k] == '?':
			s[i + k] = '1'
		elif s[i] == '0' and s[i + k] == '?':
			s[i + k] = '0'
		elif s[i] == '?' and s[i + k] == '1':
			s[i] = '1'
		elif s[i] == '?' and s[i + k] == '0':
			s[i] = '0'
		else:
			flag = False
	if not flag:
		print('NO')
		continue
	cnt_0 = 0
	cnt_1 = 0
	for i in range(k):
		if s[i] == '0':
			cnt_0 += 1
		elif s[i] == '1':
			cnt_1 += 1
	if 2 * max(cnt_0, cnt_1) > k:
		flag = False
	s = s[::-1]
	cnt_0 = 0
	cnt_1 = 0
	for i in range(k):
		if s[i] == '0':
			cnt_0 += 1
		elif s[i] == '1':
			cnt_1 += 1
	if 2 * max(cnt_0, cnt_1) > k:
		flag = False
	if not flag:
		print('NO')
	else:
		print('YES')


import sys

def input():
	return sys.stdin.readline()[:-1]

def getInt():
	return int(input())

def getIntIter():
	return map(int, input().split())

def getIntList():
	return list(getIntIter())
for _ in range(getInt()):
	(n, k) = getIntIter()
	s = input()
	mods = [-1] * k
	flag = False
	for (i, char) in enumerate(s):
		if char == '0':
			if mods[i % k] != 1:
				mods[i % k] = 0
			else:
				flag = True
				break
		elif char == '1':
			if mods[i % k] != 0:
				mods[i % k] = 1
			else:
				flag = True
				break
	if flag:
		print('NO')
	else:
		oneCount = 0
		zeroCount = 0
		for char in mods:
			if char == 1:
				oneCount += 1
			elif char == 0:
				zeroCount += 1
		if oneCount <= k // 2 and zeroCount <= k // 2:
			print('YES')
		else:
			print('NO')


def cone(arr):
	ans = 0
	for i in range(len(arr)):
		if arr[i] == '1':
			ans += 1
	return ans

def czero(arr):
	ans = 0
	for i in range(len(arr)):
		if arr[i] == '0':
			ans += 1
	return ans
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	flag = 1
	if n == k:
		one = 0
		zero = 0
		for i in range(n):
			if s[i] == '1':
				one += 1
			elif s[i] == '0':
				zero += 1
		if zero > n // 2 or one > n // 2:
			flag = 0
	if flag == 0:
		print('NO')
		continue
	temp = [-1] * k
	for start in range(k):
		val = s[start]
		temp[start] = val
		i = start + k
		while i < n:
			if s[i] != '?':
				if val != '?':
					if s[i] != val:
						flag = 0
						break
				else:
					val = s[i]
					temp[start] = val
			elif val != '?':
				s[i] = val
			i += k
		if flag == 0:
			print('NO')
			break
	for i in range(k):
		s[i] = temp[i % k]
	if (czero(s[0:k]) > k // 2 or cone(s[0:k]) > k // 2) and flag:
		print('NO')
		continue
	if flag:
		print('YES')


t = int(input())
for i in range(t):
	(n, k) = list(map(int, input().split()))
	s = str(input())
	lis = []
	for j in range(k):
		lis.append(s[j])
	num = []
	for j in range(n):
		num.append(s[j])
	p = 0
	for j in range(k, n):
		if num[j] != '?' and lis[j % k] == '?':
			lis[j % k] = num[j]
		elif num[j] != '?' and lis[j % k] != '?':
			if num[j] != lis[j % k]:
				p += 1
				break
		elif num[j] == '?' and lis[j % k] != '?':
			num[j] = lis[j % k]
	x = 0
	y = 0
	for j in range(k):
		if lis[j] == '1':
			y += 1
		if lis[j] == '0':
			x += 1
	if p == 1 or x > k // 2 or y > k // 2:
		print('NO')
	else:
		print('YES')


import os
from io import BytesIO, IOBase
import sys
import math
from math import ceil
from collections import Counter

def main():
	for t in range(int(input())):
		(n, k) = map(int, input().split())
		s = list(input().rstrip())
		ans = 'YES'
		o = 0
		z = 0
		for i in range(k):
			if s[i] == '1':
				o += 1
			if s[i] == '0':
				z += 1
		if o > k // 2 or z > k // 2:
			ans = 'NO'
		if ans == 'YES':
			for i in range(0, n - k):
				if s[i] == '0':
					if s[i + k] == '1':
						ans = 'NO'
						break
					elif s[i + k] == '?':
						s[i + k] = '0'
				elif s[i] == '1':
					if s[i + k] == '0':
						ans = 'NO'
						break
					elif s[i + k] == '?':
						s[i + k] = '1'
			(o, z) = (0, 0)
			for i in range(n - k, n):
				if s[i] == '1':
					o += 1
				if s[i] == '0':
					z += 1
			if o > k // 2 or z > k // 2:
				ans = 'NO'
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
	(N, K) = map(int, input().split())
	s = [c for c in input().strip()]
	for i in range(K):
		vs = set()
		for j in range(i, N, K):
			if s[j] != '?':
				vs.add(s[j])
		if '0' in vs and '1' in vs:
			print('NO')
			break
		if vs:
			c = vs.pop()
			for j in range(i, N, K):
				s[j] = c
	else:
		a = b = 0
		for i in range(K):
			if s[i] == '1':
				a += 1
			elif s[i] == '0':
				b += 1
		if 2 * a > K or 2 * b > K:
			print('NO')
		else:
			print('YES')


t = int(input())
for l in range(t):
	(n, k) = list(map(int, input().split()))
	s = input()
	cntOnes = 0
	cntZeros = 0
	isBalanced = True
	for col in range(k):
		num = -1
		for idx in range(col, n, k):
			if s[idx] == '0' and num != 1:
				num = 0
			elif s[idx] == '0' and num == 1:
				isBalanced = False
				break
			elif s[idx] == '1' and num != 0:
				num = 1
			elif s[idx] == '1' and num == 0:
				isBalanced = False
				break
		if num == 0:
			cntZeros += 1
		elif num == 1:
			cntOnes += 1
		if isBalanced == False:
			break
	if isBalanced and cntZeros <= k // 2 and (cntOnes <= k // 2):
		print('YES')
	else:
		print('NO')


t = int(input())
for case in range(t):
	(n, k) = [int(s) for s in input().split(' ')]
	s = [char for char in input()]
	answer = 'YES'
	for i in range(k):
		x = set((s[j] for j in range(i, n, k)))
		if '1' in x and '0' in x:
			answer = 'NO'
			break
		elif '1' in x:
			for j in range(i, n, k):
				s[j] = '1'
		elif '0' in x:
			for j in range(i, n, k):
				s[j] = '0'
	for i in range(1):
		if s[i:i + k].count('1') > k / 2 or s[i:i + k].count('0') > k / 2:
			answer = 'NO'
			break
	print(answer)


for mm in range(int(input())):
	(n, k) = map(int, input().split(' '))
	a = str(input())
	x = y = 0
	for i in range(k):
		(A, B) = (0, 0)
		for j in range(i, n, k):
			if a[j] == '1':
				A += 1
			if a[j] == '0':
				B += 1
		if min(A, B) > 0:
			print('NO')
			break
		if A:
			x += 1
		elif B:
			x -= 1
		else:
			y += 1
	else:
		if abs(x) <= y:
			print('YES')
		else:
			print('NO')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(s, side, yes, counts) = (input().strip(), [set() for _ in range(k)], True, [0, 0])
	for i in range(n):
		if s[i] != '?':
			side[i % k].add(s[i])
	for i in range(k):
		if len(side[i]) > 1:
			yes = False
		elif side[i]:
			counts[int(side[i].pop())] += 1
	print(f"{('YES' if yes and max(counts) <= k // 2 else 'NO')}")


import sys, math
input = sys.stdin.readline
t = int(input())
for r in range(t):
	(n, k) = map(int, input().split())
	s = input().strip('\n')
	d = {}
	for i in range(n):
		d[i % k] = {'0': 0, '1': 0, '?': 0}
	for i in range(n):
		d[i % k][s[i]] += 1
	flag = True
	for i in d:
		zero = d[i]['0']
		one = d[i]['1']
		question = d[i]['?']
		if zero > 0 and one > 0:
			flag = False
			break
	if flag == False:
		print('NO')
	else:
		newD = {}
		for i in d:
			zero = d[i]['0']
			one = d[i]['1']
			if zero > 0:
				newD[i] = '0'
			elif one > 0:
				newD[i] = '1'
			else:
				newD[i] = '?'
		l = [0] * n
		for i in range(n):
			l[i] = newD[i % k]
		zeroes = 0
		ones = 0
		for i in range(k):
			if l[i] == '0':
				zeroes += 1
			elif l[i] == '1':
				ones += 1
		if zeroes > k // 2 or ones > k // 2:
			flag = False
		if flag == False:
			print('NO')
		else:
			print('YES')


T = int(input().strip())
for t in range(T):
	(N, K) = list(map(int, input().strip().split()))
	content = [a for a in input().strip()]
	if K % 2 == 1:
		print('NO')
		continue
	left = 0
	right = K
	worked = True
	while right < N:
		left_val = content[left]
		right_val = content[right]
		if right_val == '?':
			content[right] = left_val
			right_val = left_val
		if left_val == '?' and right_val != '?':
			content[left] = right_val
			left_val = right_val
			tmp = left - K
			while tmp >= 0:
				content[tmp] = right_val
				tmp -= K
		if left_val != right_val:
			print('NO')
			worked = False
			break
		left += 1
		right += 1
	if worked:
		ones = 0
		zeros = 0
		haha = 0
		for i in range(K):
			if content[i] == '1':
				ones += 1
			elif content[i] == '0':
				zeros += 1
			else:
				haha += 1
		if haha == 0 and ones != zeros:
			print('NO')
			continue
		if ones + haha < zeros or zeros + haha < ones:
			print('NO')
			continue
		print('YES')


def f(l):
	for i in range(k):
		m = []
		for it in range(i, n, k):
			m.append(l[it])
		if m.count('0') > 0 and m.count('1') > 0:
			return 0
		elif m.count('1') > 0:
			l[i] = '1'
		elif m.count('0') > 0:
			l[i] = '0'
	return l
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	l = []
	for i in s:
		l.append(i)
	x = f(l)
	if x == 0:
		print('NO')
		continue
	z = 0
	o = 0
	q = 0
	for i in range(k):
		if x[i] == '0':
			z += 1
		elif x[i] == '1':
			o += 1
		else:
			q += 1
	if z > k / 2 or o > k / 2:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 'YES'
	lst = ['3' for l in range(n)]
	for i in range(n):
		if s[i] == '?':
			continue
		elif s[i] != lst[i % k] and lst[i % k] != '3':
			ans = 'NO'
			break
		else:
			lst[i % k] = s[i]
	if ans == 'NO':
		print(ans)
	else:
		zero = lst.count('0')
		one = lst.count('1')
		if zero <= k // 2 and one <= k // 2:
			print('YES')
		else:
			print('NO')


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def out(ret):
	print(ret)

def outlt(ret):
	print(' '.join(ret))

class Solution:

	def main(self, n, k, s):
		for i in range(k):
			if s[i] == '?':
				for j in range(i + k, n, k):
					if s[j] != '?':
						s[i] = s[j]
						break
		for i in range(k):
			if s[i] != '?':
				for j in range(i + k, n, k):
					if s[j] == '?':
						s[j] = s[i]
					elif s[j] != s[i]:
						return 'NO'
					j += k
		substr = s[:k]
		if substr.count('0') <= k / 2 and substr.count('1') <= k / 2:
			return 'YES'
		else:
			return 'NO'
t = inp()
solution = Solution()
for _ in range(t):
	(n, k) = inlt()
	s = insr()
	out(solution.main(n, k, s))


def tc():
	(n, k) = map(int, input().split())
	s = input()
	mod_val = [None] * k
	for (i, ch) in enumerate(s):
		if ch == '?':
			continue
		m = i % k
		if mod_val[m] is None:
			mod_val[m] = ch
		elif mod_val[m] != ch:
			print('NO')
			return
	(ones, zeros) = (0, 0)
	for x in mod_val:
		if x == '1':
			ones += 1
		elif x == '0':
			zeros += 1
	if ones > k // 2 or zeros > k // 2:
		print('NO')
	else:
		print('YES')
T = int(input())
for _ in range(T):
	tc()


def function(n, k, array):
	for i in range(k):
		check = set()
		for j in range(i, n, k):
			check.add(array[j])
		if '0' in check and '1' in check:
			return 0
		elif '0' in check:
			for j in range(i, n, k):
				array[j] = '0'
		elif '1' in check:
			for j in range(i, n, k):
				array[j] = '1'
	ones = 0
	questions = 0
	for i in range(k):
		if array[i] == '1':
			ones += 1
		if array[i] == '?':
			questions += 1
	if ones > k // 2:
		return 0
	if ones + questions < k // 2:
		return 0
	return 1
for _ in range(int(input())):
	(n1, k1) = [int(x) for x in input().split()]
	array1 = list(input()) + ['?'] * n1
	if function(n1, k1, array1):
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = str(input())
	kek = [0] * k
	ok = 1
	for i in range(n):
		if kek[i % k] == 0:
			if s[i] == '0':
				kek[i % k] = 1
			elif s[i] == '1':
				kek[i % k] = 2
		elif kek[i % k] == 1:
			if s[i] == '1':
				ok = 0
		elif kek[i % k] == 2:
			if s[i] == '0':
				ok = 0
	if ok == 0:
		print('NO')
		continue
	(cnt, cnt1) = (0, 0)
	for i in range(n):
		if s[i] == '?':
			if kek[i % k] == 1:
				cnt += 1
			elif kek[i % k] == 2:
				cnt -= 1
			else:
				cnt1 += 1
		elif s[i] == '1':
			cnt -= 1
		else:
			cnt += 1
		if i >= k - 1:
			ok &= abs(cnt) <= cnt1 and (cnt1 - cnt) % 2 == 0
			if s[i - k + 1] == '?':
				if kek[(i - k + 1) % k] == 1:
					cnt -= 1
				elif kek[(i - k + 1) % k] == 2:
					cnt += 1
				else:
					cnt1 -= 1
			elif s[i - k + 1] == '1':
				cnt += 1
			else:
				cnt -= 1
	if ok == 0:
		print('NO')
	else:
		print('YES')


t = int(input())
for tt in range(t):
	(n, k) = map(int, input().split())
	s = input().strip()
	zero = 0
	one = 0
	flag = True
	for i in range(k):
		rem = -1
		for j in range(i, n, k):
			if s[j] != '?':
				if rem != -1 and int(s[j]) != rem:
					flag = False
					break
				rem = int(s[j])
		if not flag:
			break
		if rem != -1:
			if rem == 0:
				zero += 1
			else:
				one += 1
	if max(zero, one) > k / 2:
		flag = False
	if flag:
		print('YES')
	else:
		print('NO')


from collections import defaultdict

def getIntList():
	return list(map(int, input().split()))

def getTransIntList(n):
	first = getIntList()
	m = len(first)
	result = [[0] * n for _ in range(m)]
	for i in range(m):
		result[i][0] = first[i]
	for j in range(1, n):
		curr = getIntList()
		for i in range(m):
			result[i][j] = curr[i]
	return result
t = int(input())
results = []
for _ in range(t):
	(n, k) = getIntList()
	a = input()
	base_string = [a[i] for i in range(k)]
	for (i, l) in enumerate(a[k:]):
		if l == '?':
			continue
		base_latter = base_string[i % k]
		if base_latter == '1' and l == '0' or (base_latter == '0' and l == '1'):
			results.append('NO')
			break
		base_string[i % k] = l
	else:
		sign_map = defaultdict(int)
		for l in base_string:
			sign_map[l] += 1
		if sign_map['0'] > k / 2 or sign_map['1'] > k / 2:
			results.append('NO')
		else:
			results.append('YES')
for a in results:
	print(a)


l = []
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(input())
	q = set(a)
	if len(q) == 1 and '?' in q:
		l.append('YES')
		continue
	flag = 1
	for i in range(n - k):
		if a[i] == '?' and a[i + k] == '?':
			continue
		elif a[i] == '?':
			j = i
			while j >= 0:
				a[j] = a[i + k]
				j = j - k
		elif a[i + k] == '?':
			a[i + k] = a[i]
		elif a[i] == a[i + k]:
			continue
		else:
			flag = 0
			break
	if flag == 0:
		l.append('NO')
	else:
		c1 = a[:k].count('0')
		c2 = a[:k].count('1')
		for i in range(k, n):
			if a[i - k] == '0' and a[i] == '1':
				c1 = c1 - 1
				c2 = c2 + 1
			elif a[i - k] == '1' and a[i] == '0':
				c1 = c1 + 1
				c2 = c2 - 1
			elif a[i] == '1' and a[i - k] == '?':
				c2 = c2 + 1
			elif a[i] == '0' and a[i - k] == '?':
				c1 = c1 + 1
			if c1 > k // 2 or c2 > k // 2:
				l.append('NO')
				flag = 0
				break
		if flag == 1 and (c1 <= k // 2 and c2 <= k // 2):
			l.append('YES')
		elif flag == 1:
			l.append('NO')
for i in l:
	print(i)


def solve():
	(n, k) = (int(x) for x in input().split())
	s = list(input())
	ones = s[:k].count('1')
	zeros = s[:k].count('0')
	if k % 2 != 0 or ones > k // 2 or zeros > k // 2:
		print('NO')
		return
	for i in range(k):
		val = None
		for j in range(i, n, k):
			if s[j] == '?':
				continue
			if val is None:
				val = s[j]
				s[i] = s[j]
			if s[j] != val:
				print('NO')
				return
	ones = s[:k].count('1')
	zeros = s[:k].count('0')
	if k % 2 != 0 or ones > k // 2 or zeros > k // 2:
		print('NO')
		return
	for i in range(k):
		for j in range(i, n, k):
			if s[j] == '?':
				continue
			if s[j] != s[i]:
				print('NO')
				return
	print('YES')
for _ in range(int(input())):
	solve()


def solve(n, k, s):
	m = [-1] * k
	for i in range(n):
		if s[i] == '0':
			if m[i % k] == -1:
				m[i % k] = 0
			elif m[i % k] == 1:
				print('NO')
				return
		if s[i] == '1':
			if m[i % k] == -1:
				m[i % k] = 1
			elif m[i % k] == 0:
				print('NO')
				return
	half = k // 2
	ones = zeros = 0
	for x in m:
		if x == 0:
			zeros += 1
		elif x == 1:
			ones += 1
	if ones <= half and zeros <= half:
		print('YES')
	else:
		print('NO')
	return
for nt in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	solve(n, k, s)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	fl = True
	for i in range(k, n):
		if s[i] == '?':
			s[i] = s[i - k]
	for i in range(n - 1, k - 1, -1):
		if s[i - k] == '?':
			s[i - k] = s[i]
	a = 0
	b = 0
	c = 0
	for i in range(k):
		if s[i] == '?':
			a += 1
		elif s[i] == '1':
			b += 1
		elif s[i] == '0':
			c += 1
	if abs(b - c) > a:
		fl = False
	for i in range(k, n):
		if s[i] != s[i - k]:
			fl = False
			break
	if fl:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout, setrecursionlimit
import heapq
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
setrecursionlimit(100000)
mod = 1000000007
tc = ii1()
for _ in range(tc):
	(n, k) = iia()
	s = is1()
	arr = []
	for i in range(0, len(s), k):
		arr.append(s[i:i + k])
	ones = zero = ques = 0
	for i in range(k):
		cur = [0, 0, 0]
		for j in range(len(arr)):
			if i < len(arr[j]) and arr[j][i] == '1':
				cur[1] = 1
			if i < len(arr[j]) and arr[j][i] == '0':
				cur[0] = 1
			if i < len(arr[j]) and arr[j][i] == '?':
				cur[2] = 1
		if cur[0] == cur[1] == 1:
			print('NO')
			break
		elif cur[0] == 1:
			zero += 1
		elif cur[1] == 1:
			ones += 1
		else:
			ques += 1
	else:
		cur = ones + zero + ques
		if ones <= k // 2 and zero <= k // 2 and (cur == k):
			print('YES')
		else:
			print('NO')


import os
import heapq
import sys, threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10 ** 5)
from io import BytesIO, IOBase

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res

def inar():
	return [int(k) for k in input().split()]

def lcm(num1, num2):
	return num1 * num2 // gcd(num1, num2)

def main():
	t = int(input())
	for _ in range(t):
		(n, k) = inar()
		s = input()
		freq = [''] * k
		works = True
		for i in range(k):
			for j in range(i, n, k):
				if s[j] == '?':
					continue
				elif freq[i] == '':
					freq[i] = s[j]
				elif s[j] != freq[i]:
					works = False
				else:
					continue
		z = 0
		o = 0
		for i in range(k):
			if freq[i] == '1':
				o += 1
			if freq[i] == '0':
				z += 1
		if z > k // 2 or o > k // 2:
			print('NO')
		elif works:
			print('YES')
		else:
			print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys, math
try:
	(sys.stdin, sys.stdout) = (open('input.txt', 'r'), open('out.txt', 'w'))
except:
	pass
from sys import stdin, stdout
mod = int(1000000000.0 + 7)
from statistics import mode
from collections import *
from math import ceil, floor, inf, factorial, gcd, log2, sqrt, log
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()

def lcm(a, b):
	return a * b // gcd(a, b)

def setbits(n):
	return bin(n).count('1')

def resetbits(n):
	return bin(n).count('0')

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r):
	(num, den) = (1, 1)
	r = min(n, n - r)
	for i in range(r):
		num *= n - i
		den *= i + 1
	return num // den

def ncr_p(n, r, p):
	(num, den) = (1, 1)
	r = min(r, n - r)
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * modinv(den, p) % p

def isPrime(num):
	if num <= 1:
		return False
	if num == 2 or n == 3:
		return True
	if num % 2 == 0 or num % 3 == 0:
		return False
	m = int(num ** 0.5) + 1
	for i in range(5, m, 6):
		if num % i == 0 or num % (i + 2) == 0:
			return False
	return True

def bin_search(s, low, high, val):
	while low <= high:
		mid = low + (high - low) // 2
		if s[mid] == val:
			return mid
		elif s[mid] < val:
			low = mid + 1
		else:
			high = mid - 1
	return -1

def sumofdigit(num):
	count = 0
	while num:
		count += num % 10
		num //= 10
	return count

def inputmatrix():
	(r, c) = iia()
	mat = [0] * r
	for i in range(r):
		mat[i] = iia()
	return (r, c, mat)

def prefix_sum(n, s):
	for i in range(1, n):
		s[i] += s[i - 1]
	return s

def binomial(n, k):
	if 0 <= k <= n:
		ntok = 1
		ktok = 1
		for t in range(1, min(k, n - k) + 1):
			ntok *= n
			ktok *= t
			n -= 1
		return ntok // ktok
	else:
		return 0

def divisors(n):
	res = []
	for i in range(1, ceil(sqrt(n)) + 1):
		if n % i == 0:
			if i == n // i:
				res.append(i)
			else:
				res.append(i)
				res.append(n // i)
	return res
for _ in range(ii1()):
	(n, k) = iia()
	s = is1()
	flag = True
	ans = [None] * n
	for i in range(n):
		ans[i] = s[i]
	for i in range(k, n):
		if s[i] == '0':
			ans[i] = '0'
			if ans[i - k] == '1':
				flag = False
				break
			else:
				ans[i - k] = '0'
		elif s[i] == '1':
			ans[i] = '1'
			if ans[i - k] == '0':
				flag = False
				break
			else:
				ans[i - k] = '1'
		elif s[i] == '?':
			ans[i] = s[i]
			if ans[i - k] != '?':
				ans[i] = ans[i - k]
	if flag:
		(a, b, u) = (0, 0, 0)
		for i in range(k):
			if ans[i] == '0':
				a += 1
			elif ans[i] == '1':
				b += 1
			else:
				u += 1
		if abs(a - b) > u:
			flag = False
		for i in range(k, n):
			if ans[i] == '0':
				a += 1
			elif ans[i] == '1':
				b += 1
			else:
				u += 1
			if ans[i - k] == '0':
				a -= 1
			elif ans[i - k] == '1':
				b -= 1
			else:
				u -= 1
			if abs(a - b) > u:
				flag = False
				break
	if flag:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline

def print(val):
	sys.stdout.write(str(val) + '\n')

def prog():
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		s = input().strip()
		need = [0] * k
		fail = False
		for start in range(k):
			seen = [0] * 3
			for i in range(start, n, k):
				if s[i] == '?':
					seen[2] = 1
				elif s[i] == '0':
					seen[0] = 1
				elif s[i] == '1':
					seen[1] = 1
			if seen[0] == 1 and seen[1] == 1:
				fail = True
				break
			elif seen[1]:
				need[start] = 1
			elif seen[0] == 0:
				need[start] = 2
		if fail:
			print('NO')
		else:
			diff = 0
			free = 0
			for i in range(k):
				if need[i] == 0:
					diff -= 1
				elif need[i] == 1:
					diff += 1
				else:
					free += 1
			diff = max(diff, -diff)
			if diff > free:
				print('NO')
			else:
				print('YES')
prog()


for __ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	ar = list(input())
	kek = [-1] * k
	ans = 'YES'
	for j in range(0, n, k):
		for i in range(j, j + k):
			if i < n:
				if ar[i] == '0':
					if kek[i % k] == 1:
						ans = 'NO'
					kek[i % k] = 0
				elif ar[i] == '1':
					if kek[i % k] == 0:
						ans = 'NO'
					kek[i % k] = 1
	for i in range(n):
		if ar[i] == '?' and kek[i % k] != -1:
			ar[i] = str(kek[i % k])
	(a, b) = (0, 0)
	for i in range(k):
		if ar[i] == '0':
			a += 1
		elif ar[i] == '1':
			b += 1
	if a > k // 2 or b > k // 2:
		ans = 'NO'
	for i in range(k):
		if ar[i] == '?':
			if a < k // 2:
				a += 1
				ar[i] = '0'
				kek[i] = 0
			elif b < k // 2:
				b += 1
				ar[i] = '1'
				kek[i] = 1
			else:
				ans = 'NO'
	for i in range(n):
		if ar[i] == '?' and kek[i % k] != -1:
			ar[i] = str(kek[i % k])
	(a, b) = (0, 0)
	for i in range(k):
		if ar[i] == '0':
			a += 1
		elif ar[i] == '1':
			b += 1
	if a != b:
		ans = 'NO'
	for i in range(k, n):
		if ar[i] != ar[i - k]:
			ans = 'NO'
	print(ans)


def answer(n, k, A):
	for i in range(k):
		if A[i] == '?':
			j = i + k
			while j < n:
				if A[j] != '?':
					A[i] = A[j]
					break
				j += k
	for i in range(k):
		if A[i] != '?':
			j = i + k
			while j < n:
				if A[j] == '?':
					A[j] = A[i]
				elif A[i] != A[j]:
					return 'NO'
				j += k
	count0 = 0
	count1 = 0
	for i in range(n):
		if i < k:
			if A[i] == '0':
				count0 += 1
			if A[i] == '1':
				count1 += 1
		else:
			if max(count0, count1) > k // 2:
				return 'NO'
			if A[i - k] == '1':
				count1 -= 1
			if A[i - k] == '0':
				count0 -= 1
			if A[i] == '0':
				count0 += 1
			if A[i] == '1':
				count1 += 1
	if max(count0, count1) > k // 2:
		return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	print(answer(n, k, s))


def solve(s, n, k):
	b = ['?' for i in range(k)]
	for i in range(k):
		start = s[i]
		for j in range(i, n, k):
			if start == '?':
				start = s[j]
			elif s[j] != '?' and s[j] != start:
				return False
			if s[j] != '?':
				b[i] = s[j]
	if b.count('1') > k // 2 or b.count('0') > k // 2:
		return False
	return True
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split(' '))
	s = input()
	print('YES' if solve(s, n, k) else 'NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	A = input()
	v = ['?'] * k
	ok = True
	for i in range(k):
		for j in range(i, n, k):
			if A[j] == '?':
				continue
			if v[i] == '?' or v[i] == A[j]:
				v[i] = A[j]
			elif v[i] != A[j]:
				ok = False
	(c0, c1) = (0, 0)
	for i in range(k):
		if v[i] == '1':
			c1 += 1
		if v[i] == '0':
			c0 += 1
	if c0 > k // 2 or c1 > k // 2:
		ok = False
	if ok:
		print('YES')
	else:
		print('NO')


def solve(s):
	for i in range(k):
		u = [0, 0]
		for j in range(i, n, k):
			if s[j] == '?':
				continue
			u[ord(s[j]) - ord('0')] += 1
		if u[0] > 0 and u[1] > 0:
			return False
		if not u[0] and (not u[1]):
			continue
		x = chr(ord('0') + (u[0] == 0))
		if s[i] == '?':
			s[i] = x
	u = [0, 0]
	for i in range(k):
		if s[i] == '?':
			continue
		u[ord(s[i]) - ord('0')] += 1
	return u[0] <= k // 2 and u[1] <= k // 2
inp_arr = lambda : list(map(int, input().split()))
T = int(input())
for cas in range(1, T + 1):
	(n, k) = map(int, input().split())
	s = input()
	print('YES' if solve(list(s)) else 'NO')


def bitstring(k, n, s):
	arr = [-1 for i in range(k)]
	for i in range(n):
		if s[i] == '0':
			if arr[i % k] == 1:
				return 'NO'
			arr[i % k] = 0
		elif s[i] == '1':
			if arr[i % k] == 0:
				return 'NO'
			arr[i % k] = 1
	if arr.count(0) > k // 2 or arr.count(1) > k // 2:
		return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	print(bitstring(k, n, s))


import sys
from math import sqrt
from collections import defaultdict
inpt = sys.stdin.readline

def inp():
	return int(inpt())

def inlist():
	return list(map(int, inpt().split()))

def instr():
	s = inpt()
	return list(s[:len(s) - 1])

def inbitstr():
	s = inpt()
	return [int(i) for i in list(s[:len(s) - 1])]

def invr():
	return map(int, inpt().split())

class Graph:

	def __init__(self, n):
		self.n = n
		self.graph = defaultdict(list)
		self.marked = defaultdict(int)

	def addEdge(self, u, v):
		self.graph[u].append(v)
		self.graph[v].append(u)

	def dfs(self, u):
		self.marked[u] = 1
		for i in self.graph[u]:
			if self.marked[i] == 0:
				self.dfs(i)

def main():
	t = inp()
	for l in range(t):
		(n, k) = invr()
		s = instr()
		flag = True
		for i in range(k, n):
			(a, b) = (s[i], s[i - k])
			if a != '?' and b != '?':
				if a != b:
					flag = False
					break
			elif a == '?':
				s[i] = b
		(o, z, q) = (0, 0, 0)
		for i in range(0, n):
			if s[i] == '1':
				o += 1
			elif s[i] == '0':
				z += 1
			else:
				q += 1
			if i > k - 1:
				if s[i - k] == '1':
					o -= 1
				elif s[i - k] == '0':
					z -= 1
				else:
					q -= 1
			if i >= k - 1:
				if o > z + q or z > o + q:
					flag = False
					break
		if flag:
			print('YES')
		else:
			print('NO')
main()


t = int(input())
for tc in range(t):
	(n, k) = map(int, input().split())
	w = input()
	a = list(w)
	for r in range(k):
		o = z = q = 0
		for i in range(r, n, k):
			o += 1 if w[i] == '1' else 0
			z += 1 if w[i] == '0' else 0
			q += 1 if w[i] == '?' else 0
			if w[i] != '?':
				a[i % k] = w[i]
		if o > 0 and z > 0:
			print('NO')
			break
	else:
		o = 0
		q = 0
		for i in range(k):
			o += 1 if a[i] == '1' else 0
			q += 1 if a[i] == '?' else 0
		if o <= k // 2 and o + q >= k // 2:
			print('YES')
		else:
			print('NO')


import sys
input = sys.stdin.readline
from collections import defaultdict

def check(s, k, n):
	c = 0
	d = 0
	for i in range(k):
		if s[i] == '?':
			d += 1
		else:
			c += s[i]
	if d % 2 != abs(c) % 2 or d < abs(c):
		return False
	for i in range(k, n):
		if s[i] == '?':
			d += 1
		else:
			c += s[i]
		if s[i - k] == '?':
			d -= 1
		else:
			c -= s[i]
		if d % 2 != abs(c) % 2 or d < abs(c):
			return False
	return True
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input().strip())
	for i in range(n):
		if s[i] == '0':
			s[i] = -1
		if s[i] == '1':
			s[i] = 1
	if n % k != 0:
		s.extend([0] * (k - n % k))
		z = 1
	else:
		z = 0
	for i in range(k):
		x = 0
		f = 0
		for j in range(n // k + z):
			y = s[i + j * k]
			if y == '?' or y == 0:
				continue
			if x == 0:
				x = y
			elif y != x:
				f = 1
				break
		if x != 0 and x != '?':
			for j in range(n // k + z):
				s[i + j * k] = x
		if f == 1:
			break
	if f == 1:
		print('NO')
	elif check(s, k, n):
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n, k) = I()
	s = list(input().strip())
	pos = 0
	an = [-1] * k
	x = y = 0
	for i in range(n):
		c = i % k
		if s[i] != '?':
			if an[c] == -1:
				if s[i] == '0':
					an[c] = 0
					x += 1
				else:
					an[c] = 1
					y += 1
			elif an[c] != int(s[i]):
				pos = 1
				break
	if pos == 0 and (x > k // 2 or y > k // 2):
		pos = 1
	print('YNEOS'[pos::2])


t = int(input())
c = []
for i in range(t):
	ph = []
	blo = 'YES'
	a = input().split(' ')
	m = int(a[0])
	n = int(a[1])
	b = list(input())
	for j in range(n):
		sam = []
		for k in b[j::n]:
			sam.append(k)
		if '1' in sam and '0' in sam:
			blo = 'NO'
			break
		elif '1' in sam:
			ph.append('1')
		elif '0' in sam:
			ph.append('0')
		else:
			ph.append('?')
	x = ph.count('1')
	y = ph.count('0')
	z = ph.count('?')
	if x > y and z < x - y:
		blo = 'NO'
	elif x < y and z < y - x:
		blo = 'NO'
	c.append(blo)
for i in range(t):
	print(c[i])


import sys
input = sys.stdin.readline
for f in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = input()
	poss = True
	sol = [2] * n
	ones = 0
	zeros = 0
	for i in range(n):
		if i < k:
			if s[i] == '1':
				sol[i] = 1
				ones += 1
			if s[i] == '0':
				sol[i] = 0
				zeros += 1
		else:
			if s[i] == '1':
				sol[i] = 1
				if sol[i - k] == 0:
					poss = False
				if sol[i - k] == 2:
					ones += 1
			if s[i] == '0':
				sol[i] = 0
				if sol[i - k] == 1:
					poss = False
				if sol[i - k] == 2:
					zeros += 1
			if s[i] == '?':
				if sol[i - k] == 1:
					sol[i] = 1
				if sol[i - k] == 0:
					sol[i] = 0
	if not poss or ones > k // 2 or zeros > k // 2:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(str(input()))
	tt = 0
	c = 0
	a = 0
	aa = 0
	bb = 0
	for i in range(n):
		if i + k < n and s[i + k] == '?':
			if s[i] != '?':
				s[i + k] = s[i]
				if i - k >= 0 and s[i - k] == '?':
					s[i - k] = s[i]
		if s[i] == '?':
			if i + k < n and s[i + k] != '?':
				s[i] = s[i + k]
			elif i - k >= 0 and s[i - k] != '?':
				s[i] = s[i - k]
	for i in range(n):
		if s[i] == '0':
			c += 1
			if i + k < n and s[i + k] == '1':
				tt = 1
				break
			if c + a == k and c != a:
				tt = 1
				break
			if c + a == k and c == a:
				c = 0
				a = 0
		elif s[i] == '1':
			bb += 1
			a += 1
			if i + k < n and s[i + k] == '0':
				tt = 1
				break
			if c + a == k and c != a:
				tt = 1
				break
		if i >= k - 1:
			if c > k // 2 or a > k // 2:
				tt = 1
				break
			else:
				if s[aa] == '0':
					c -= 1
				elif s[aa] == '1':
					a -= 1
				aa += 1
	if tt == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()

	def solve(n, k, s):
		l = list(s)
		for i in range(k):
			t = s[i]
			for j in range(i, n, k):
				if s[j] != '?':
					if t != '?' and s[j] != t:
						return False
					t = s[j]
			for j in range(i, n, k):
				l[j] = t
		return max(l[:k].count('1'), l[:k].count('0')) <= k // 2
	if solve(n, k, s):
		print('YES')
	else:
		print('NO')


import math
t = int(input())
while t > 0:
	(n, k) = map(int, input().split())
	s = [char for char in input()]
	window = s[0:k]
	questions = window.count('?')
	ones = window.count('1')
	zeroes = window.count('0')
	poss = questions >= abs(ones - zeroes) and (questions - abs(ones - zeroes)) % 2 == 0
	rem_q = questions - abs(ones - zeroes)
	extra_ones = max(0, zeroes - ones)
	extra_zeroes = max(0, ones - zeroes)
	extra_ones += rem_q / 2
	extra_zeroes += rem_q / 2
	old = 0
	for new in range(k, len(s)):
		if s[new] == s[old]:
			pass
		elif s[new] == '?':
			s[new] = s[old]
		elif s[old] == '?':
			if s[new] == '1':
				extra_ones -= 1
			else:
				extra_zeroes -= 1
		else:
			poss = False
			break
		if any((val < 0 for val in [extra_zeroes, extra_ones])):
			poss = False
			break
		old += 1
	if poss:
		print('YES')
	else:
		print('NO')
	t -= 1


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]
for _ in range(II()):

	def solve():
		for si in range(k):
			cnt = [0] * 3
			for i in range(si, n, k):
				if s[i] == '0':
					cnt[0] += 1
				if s[i] == '1':
					cnt[1] += 1
				if s[i] == '?':
					cnt[2] += 1
			if cnt[0] and cnt[1]:
				return False
			if cnt[0]:
				s[si] = '0'
			if cnt[1]:
				s[si] = '1'
		return True
	(n, k) = MI()
	s = list(SI())
	if not solve():
		print('NO')
		continue
	cnt = [0] * 2
	for i in range(k):
		if s[i] == '0':
			cnt[0] += 1
		if s[i] == '1':
			cnt[1] += 1
	if cnt[0] > k // 2 or cnt[1] > k // 2:
		print('NO')
	else:
		print('YES')


import sys
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	cnt0 = 0
	cnt1 = 0
	f = 0
	for i in range(k):
		S = set()
		for j in range(i, n, k):
			if s[j] == '?':
				continue
			S.add(s[j])
		if len(S) > 1:
			f = 1
			print('NO')
			break
		if '1' in S:
			cnt1 += 1
		elif '0' in S:
			cnt0 += 1
	if not f:
		if cnt0 > k // 2:
			print('NO')
		elif cnt1 > k // 2:
			print('NO')
		else:
			print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	st = input()
	arr = [-1] * k
	flag = True
	for i in range(n):
		if st[i] == '1':
			if arr[i % k] == 0:
				flag = False
				break
			elif arr[i % k] == -1:
				arr[i % k] = 1
		elif st[i] == '0':
			if arr[i % k] == 1:
				flag = False
				break
			elif arr[i % k] == -1:
				arr[i % k] = 0
	if not flag:
		print('NO')
	else:
		x = arr.count(0)
		y = arr.count(1)
		z = arr.count(-1)
		if x > k / 2 or y > k / 2:
			print('NO')
		else:
			print('YES')


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
for ik in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(input())
	e = [0] * k
	f = 0
	for i in range(k):
		for j in range(i, n, k):
			if l[j] != '?':
				if e[i] != 0 and e[i] != l[j]:
					f = 1
					break
				e[i] = l[j]
	if f == 1:
		print('NO')
		continue
	f = 0
	for i in range(k, n):
		e.append(e[i - k])
	c = 0
	t = 0
	f = 0
	for i in range(k):
		if e[i] == '1':
			c += 1
			t += 1
		if e[i] == '0':
			c -= 1
			t += 1
	if k - t < abs(c):
		print('NO')
		continue
	for i in range(k, n):
		if e[i - k] != 0 and e[i - k] != e[i]:
			print('NO')
			f = 1
			break
		if e[i - k] != 0 and e[i - k] == e[i]:
			continue
		else:
			if e[i] == '1':
				c += 1
				t += 1
			if e[i] == '0':
				c -= 1
				t += 1
			if k - t < abs(c):
				print('NO', i)
				f = 1
				break
	if f == 0:
		print('YES')


from collections import Counter
t = int(input())
for test in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = list(input())
	result = True
	for i in range(k, n):
		if s[i] != '?':
			if s[i % k] == '?':
				s[i % k] = s[i]
			elif s[i % k] != s[i]:
				result = False
				break
	if result:
		count = Counter(s[:k])
		print('YES' if all((count[t] <= k // 2 for t in '01')) else 'NO')
	else:
		print('NO')


t = int(input())
while t > 0:
	t -= 1
	(n, k) = map(int, input().split())
	s = input()
	v = ['?' for i in range(k)]
	ans = True
	for i in range(k):
		for j in range(i, n, k):
			if s[j] == '?':
				continue
			elif v[i] == '?' or v[i] == s[j]:
				v[i] = s[j]
			elif v[i] != s[i] or s[i] != s[j]:
				ans = False
	(one, zero) = (0, 0)
	for i in range(k):
		if v[i] == '1':
			one += 1
		elif v[i] == '0':
			zero += 1
	if one > k // 2 or zero > k // 2:
		ans = False
	if ans:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout

def INPUT():
	return list((int(i) for i in stdin.readline().split()))

def inp():
	return stdin.readline()

def out(x):
	return stdout.write(x)
import math
import random
import sys
for _ in range(int(input())):
	bool = True
	(n, k) = INPUT()
	A = list(input())
	for i in range(n):
		if A[i] != '?' and A[i % k] != '?' and (A[i] != A[i % k]):
			bool = False
			break
		if A[i] != '?':
			A[i % k] = A[i]
	z = 0
	one = 0
	for i in range(k):
		one += A[i] == '1'
		z += A[i] == '0'
	if bool and z <= k // 2 and (one <= k // 2):
		print('YES')
	else:
		print('NO')


import sys
pprint = lambda s: print(' '.join(map(lambda x: str(x), s)))
input = lambda : sys.stdin.readline().strip()
ipnut = input
mod = 1000000007
for i in range(int(input())):
	(n, k) = map(int, input().split())
	slov = {'0': -1, '1': 1, '?': 0}
	s = list(map(lambda x: slov[x], input()))
	c = [0, 0, 0]
	for i in range(k):
		c[s[i] + 1] += 1
	f = False
	for i in range(k, n):
		if c[0] > k // 2 or c[2] > k // 2:
			f = True
			break
		c[s[i - k] + 1] -= 1
		c[s[i] + 1] += 1
	if c[0] > k // 2 or c[2] > k // 2:
		f = True
	if f:
		print('NO')
		continue
	kol = [[] for i in range(k)]
	for i in range(n):
		kol[i % k].append(s[i])
	s0 = 0
	s1 = 0
	for i in kol:
		f1 = -1 in i
		f2 = 1 in i
		if f1 and f2:
			f = True
			break
		elif f1:
			s0 += 1
		elif f2:
			s1 += 1
	if f or s0 > k // 2 or s1 > k // 2:
		print('NO')
	else:
		print('YES')


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]

def YES():
	print('YES')

def NO():
	print('NO')

def Yes():
	print('Yes')

def No():
	print('No')
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24

class MergeFind:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		self.num_sets -= 1
		self.parent[a] = b
		self.size[b] += self.size[a]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def lcm(a, b):
	return abs(a // gcd(a, b) * b)

def solve():
	(n, k) = sep()
	s = inp()
	a = 0
	b = 0
	q = 0
	for i in range(k):
		if s[i] == '1':
			a += 1
		if s[i] == '0':
			b += 1
		if s[i] == '?':
			q += 1
	if abs(a - b) > q:
		NO()
		return
	ar = ['?'] * n
	for i in range(k):
		ar[i] = s[i]
	for i in range(k, n):
		if s[i] == '?':
			ar[i] = ar[i - k]
			continue
		if s[i] == '1' and ar[i - k] == '0' or (s[i] == '0' and ar[i - k] == '1'):
			NO()
			return
		ar[i] = s[i]
	for i in range(k, n):
		if ar[i - k] == '1':
			a -= 1
		if ar[i - k] == '0':
			b -= 1
		if ar[i - k] == '?':
			q -= 1
		if ar[i] == '1':
			a += 1
		if ar[i] == '0':
			b += 1
		if ar[i] == '?':
			q += 1
		if abs(a - b) > q:
			NO()
			return
	YES()
testcase(int(inp()))


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		(n, k) = LI()
		s = S()
		ok = True
		t = [0, 0]
		for i in range(k):
			c = collections.Counter(s[i::k])
			if c['0'] > 0 and c['1'] > 0:
				ok = False
				break
			if c['0'] > 0:
				t[0] += 1
			elif c['1'] > 0:
				t[1] += 1
		rr.append(YES(ok and t[0] <= k // 2 and (t[1] <= k // 2)))
	return JA(rr, '\n')
print(main())


import math
import re
import itertools
import copy
import time
import sys

def input_linear(lamb=lambda x: int(x), seperator=' '):
	return map(lamb, input().split(seperator))

def input_array(lamb=lambda x: int(x), seperator=' '):
	return list(map(lamb, input().split(seperator)))

def input_matrix(rows, lamb=lambda x: int(x), seperator=' '):
	tmp_matrix = []
	for i in range(rows):
		tmp_matrix.append(input_array(lamb, seperator))
	return tmp_matrix

def input_graph_al_pair(vertices, lines, lamb=lambda x: int(x), seperator=' ', directional=False, remove_dup=False):
	temp_matrix = [x[:] for x in [[]] * vertices]
	if not directional:
		if not remove_dup:
			for i in range(lines):
				(ina, inb) = input_linear(lamb, seperator)
				temp_matrix[ina].append(inb)
				temp_matrix[inb].append(ina)
		else:
			for i in range(lines):
				(ina, inb) = input_linear(lamb, seperator)
				if inb not in temp_matrix[ina]:
					temp_matrix[ina].append(inb)
				if ina not in temp_matrix[inb]:
					temp_matrix[inb].append(ina)
		return temp_matrix
	else:
		if not remove_dup:
			for i in range(lines):
				(ina, inb) = input_linear(lamb, seperator)
				temp_matrix[ina].append(inb)
		else:
			for i in range(lines):
				(ina, inb) = input_linear(lamb, seperator)
				if inb not in temp_matrix[ina]:
					temp_matrix[ina].append(inb)
		return temp_matrix
for _ in range(int(input())):
	fl = True
	(n, k) = input_linear()
	a = input()
	karr = ['?'] * k
	for i in range(n):
		ck = i % k
		if karr[ck] == '?' and a[i] == '?':
			continue
		elif a[i] == '?':
			continue
		elif karr[ck] == '?' and (a[i] == '0' or a[i] == '1'):
			karr[ck] = a[i]
		elif karr[ck] != a[i]:
			fl = False
			break
	if not fl:
		print('NO')
		continue
	elif fl:
		nq = karr.count('?')
		nz = karr.count('0')
		no = karr.count('1')
		if abs(no - nz) <= nq:
			print('YES')
		else:
			print('NO')


import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	w = list(input())
	l = 0
	a = 0
	s = 0
	d = 0
	r = [[0, 0, 0]]
	for i in range(n):
		if i + k < n and w[i] == '1' and (w[i + k] == '?'):
			w[i + k] = '1'
		if i + k < n and w[i] == '0' and (w[i + k] == '?'):
			w[i + k] = '0'
		if i + k < n and w[i] == '?' and (w[i + k] == '1'):
			w[i] = '1'
		if i + k < n and w[i] == '?' and (w[i + k] == '0'):
			w[i] = '0'
		if i + k < n:
			if w[i] == '1' and w[i + k] == '0' or (w[i] == '0' and w[i + k] == '1'):
				l = 1
				break
		if w[i] == '0':
			a += 1
		elif w[i] == '1':
			s += 1
		else:
			d += 1
		r.append([a, s, d])
	if l == 0:
		for i in range(k, n + 1):
			a = r[i][0] - r[i - k][0]
			s = r[i][1] - r[i - k][1]
			d = r[i][2] - r[i - k][2]
			if max(a, s) - min(a, s) > d:
				l = 1
				break
	if l == 0:
		print('YES')
	else:
		print('NO')


from bisect import bisect_left, bisect_right
from sys import stdin, stdout
R = lambda : stdin.readline().strip()
RL = lambda f=None: list(map(f, R().split(' '))) if f else list(R().split(' '))
output = lambda x: stdout.write(str(x) + '\n')
output_list = lambda x: output(' '.join(map(str, x)))
for tc in range(int(R())):
	(n, k) = RL(int)
	s = list(R())
	flag = False
	for i in range(k, n):
		if s[i - k] == '?':
			s[i - k] = s[i]
		if s[i] == '?':
			s[i] = s[i - k]
		if s[i] != s[i - k]:
			output('NO')
			flag = True
			break
	if flag:
		continue
	d = {'0': 0, '1': 0, '?': 0}
	for i in s[:k]:
		d[i] += 1
	if max(d['0'], d['1']) > k // 2:
		output('NO')
		continue
	for i in range(k, n):
		d[s[i]] += 1
		d[s[i - k]] -= 1
		if max(d['0'], d['1']) > k // 2:
			output('NO')
			flag = True
			break
	if flag:
		continue
	output('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	one = [False] * k
	zero = [False] * k
	for (i, b) in enumerate(s):
		if b == '0':
			zero[i % k] = True
		elif b == '1':
			one[i % k] = True
	result = not max([A and B for (A, B) in zip(zero, one)]) and sum(one) <= k // 2 and (sum(zero) <= k // 2)
	print('YES' if result else 'NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	i = 0
	f = 0
	for g in range(k):
		i = g
		val = -1
		while i < len(s):
			if s[i] != '?':
				val = s[i]
				break
			i += k
		if val != -1:
			i = g
			while i < len(s):
				if s[i] == '?':
					s[i] = val
				if s[i] != val:
					f = 1
					break
				i += k
		if f == 1:
			break
	ck = s[:k]
	if ck.count('1') <= k // 2 and ck.count('0') <= k // 2:
		pass
	else:
		f = 1
	if f == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(length, k) = list(map(int, input().split()))
	string = list(input())
	f = 0
	for count in range(length):
		if string[count] == '?':
			pass
		else:
			string[count] = int(string[count])
	if k > length:
		print('No')
		f = 1
	else:
		block = [-1] * k
		l = [0, 0]
		for count in range(length):
			m = count % k
			val = string[count]
			if type(val) == type(1):
				if block[m] == 1 - val:
					print('No')
					f = 1
					break
				elif block[m] == val:
					pass
				else:
					block[m] = val
					l[val] += 1
					if max(l) > k // 2:
						print('No')
						f = 1
						break
	if not f:
		print('Yes')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for j in range(t):
	(n, k) = map(int, input().split())
	vals = [k for k in input()]
	broke = False
	for s in range(k):
		zer = 0
		one = 0
		que = 0
		for b in range(s, n, k):
			if vals[b] == '0':
				zer += 1
			elif vals[b] == '1':
				one += 1
			else:
				que += 1
		if zer > 0 and one > 0:
			broke = True
			break
		elif zer > 0:
			for b in range(s, n, k):
				vals[b] = '0'
		elif one > 0:
			for b in range(s, n, k):
				vals[b] = '1'
	dz = 0
	do = 0
	dq = 0
	for s in range(n + 1 - k):
		if s == 0:
			for i in range(s, s + k):
				if vals[i] == '0':
					dz += 1
				elif vals[i] == '1':
					do += 1
				else:
					dq += 1
		else:
			if vals[s + k - 1] == '0':
				dz += 1
			elif vals[s + k - 1] == '1':
				do += 1
			else:
				dq += 1
			if vals[s - 1] == '0':
				dz -= 1
			elif vals[s - 1] == '1':
				do -= 1
			else:
				dq -= 1
		if dq >= abs(dz - do) and (dq - abs(dz - do)) % 2 == 0:
			continue
		else:
			broke = True
			break
	if broke == False:
		print('YES')
	else:
		print('NO')


import sys

def check_it(s, n):
	ones = 0
	zeros = 0
	start = 0
	for h in range(n):
		if s[h] == '1':
			ones += 1
		elif s[h] == '0':
			zeros += 1
		if h > k - 1:
			if s[start] == '1':
				ones -= 1
			elif s[start] == '0':
				zeros -= 1
			start += 1
		if h >= k - 1:
			if ones > k // 2 or zeros > k // 2:
				return 'NO'
	return 'YES'

def slv(n, k, s):
	for g in range(n - k):
		if s[g] != s[g + k]:
			if s[g] == '?':
				s[g] = s[g + k]
			elif s[g + k] == '?':
				s[g + k] = s[g]
			else:
				return 'NO'
	return check_it(s, n)
t = int(sys.stdin.readline())
ans_arr = []
for i in range(t):
	[n, k] = [int(j) for j in sys.stdin.readline().split()]
	original = sys.stdin.readline().strip()
	s = []
	for r in range(n):
		s.append(original[r])
	ans_arr.append(slv(n, k, s))
print('\n'.join(ans_arr))


import sys
import math, bisect
sys.setrecursionlimit(10 ** 5)
from collections import defaultdict
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict

def I():
	return int(sys.stdin.readline())

def neo():
	return map(int, sys.stdin.readline().split())

def Neo():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
	(N, K) = map(int, input().split())
	S = input()
	X = [-1] * K
	for (i, s) in enumerate(S):
		ii = i % K
		if s == '?':
			continue
		if s == '0':
			if X[ii] == 1:
				print('NO')
				break
			X[ii] = 0
		elif s == '1':
			if X[ii] == 0:
				print('NO')
				break
			X[ii] = 1
	else:
		if max(X.count(1), X.count(0)) <= K // 2:
			print('YES')
		else:
			print('NO')


from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
	(n, k) = map(int, stdin.readline().split())
	s = list(stdin.readline()[:-1])
	z = [0] * k
	o = [0] * k
	q = [0] * k
	for i in range(n):
		if s[i] == '0':
			z[i % k] += 1
		elif s[i] == '1':
			o[i % k] += 1
		else:
			q[i % k] += 1
	ans = 'YES'
	zs = 0
	os = 0
	qs = 0
	for i in range(k):
		if z[i] * o[i] != 0:
			ans = 'NO'
			break
		if z[i] > 0:
			zs += 1
		elif o[i] > 0:
			os += 1
		else:
			qs += 1
	alls = zs + os + qs
	if zs * 2 > alls or os * 2 > alls:
		ans = 'NO'
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	for i in range(k):
		b1 = 0
		b2 = 0
		for j in range(i, n, k):
			if s[j] == '0':
				b1 = 1
			elif s[j] == '1':
				b2 = 1
		if b1 and b2:
			print('NO')
			break
		elif b1:
			for j in range(i, n, k):
				s[j] = '0'
		elif b2:
			for j in range(i, n, k):
				s[j] = '1'
	else:
		a = s[:k].count('0')
		b = s[:k].count('1')
		if a * 2 <= k and b * 2 <= k:
			print('YES')
		else:
			print('NO')


import os, sys
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def get_input(a=str):
	return a(input())

def get_int_input():
	return get_input(int)

def get_input_arr(a):
	return list(map(a, input().split()))

def get_int_input_arr():
	return get_input_arr(int)

def solve():
	t = get_int_input()
	for _ in range(t):
		(n, k) = get_int_input_arr()
		st = get_input()
		can_do = True
		zeroes = 0
		ones = 0
		un = 0
		for i in range(k):
			curr = i
			curr_val = st[i]
			while curr < len(st):
				if curr_val == '?' and st[curr] != '?':
					curr_val = st[curr]
				if curr_val != '?' and st[curr] != '?' and (curr_val != st[curr]):
					can_do = False
					break
				curr += k
			if not can_do:
				break
			if curr_val == '0':
				zeroes += 1
			if curr_val == '1':
				ones += 1
			if curr_val == '?':
				un += 1
		if not can_do:
			cout << 'NO' << endl
			continue
		if k // 2 >= zeroes and k // 2 >= ones and (k // 2 - zeroes + (k // 2 - ones) == un):
			cout << 'YES' << endl
		else:
			cout << 'NO' << endl

def main():
	solve()
main()


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()
t = II()
for q in range(t):
	(n, k) = MI()
	s = list(SI())
	boo = True
	su = 0
	for i in range(n):
		if i - k >= 0 and s[i] != s[i - k]:
			if s[i] == '?':
				s[i] = s[i - k]
			elif s[i - k] != '?':
				boo = False
				break
		if i + k < n and s[i] != s[i + k]:
			if s[i + k] == '?':
				s[i + k] = s[i]
			elif s[i] != '?':
				boo = False
				break
	o = s[:k].count('1')
	z = s[:k].count('0')
	if k // 2 - o < 0 or k // 2 - z < 0:
		boo = False
	for i in range(k, n):
		if boo == False:
			break
		if s[i - k] == '1':
			o -= 1
		elif s[i - k] == '0':
			z -= 1
		if s[i] == '1':
			o += 1
		elif s[i] == '0':
			z += 1
		if k // 2 - o < 0 or k // 2 - z < 0:
			boo = False
	print('YES' if boo else 'NO')


T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	s = list(input())
	targ = s[:k]
	ones = targ.count('1')
	hatenas = targ.count('?')
	zeros = k - ones - hatenas
	mode = 1
	if ones > k // 2 or zeros > k // 2:
		mode = 0
	for i in range(n - k):
		if s[i] == '0' and s[i + k] == '1':
			mode = 0
			break
		elif s[i] == '1' and s[i + k] == '0':
			mode = 0
			break
		elif s[i] == '?' and s[i + k] == '0':
			s[i] = '0'
			zeros += 1
			hatenas -= 1
		elif s[i] == '?' and s[i + k] == '1':
			s[i] = '1'
			ones += 1
			hatenas -= 1
		elif s[i] == '0' and s[i + k] == '?':
			s[i + k] = '0'
		elif s[i] == '1' and s[i + k] == '?':
			s[i + k] = '1'
		if ones > k // 2:
			mode = 0
			break
		elif zeros > k // 2:
			mode = 0
			break
	if mode:
		print('YES')
	else:
		print('NO')


def sol():
	(n, k) = map(int, input().split())
	s = list(input())
	gg = False
	one = 0
	zero = 0
	for i in range(k):
		ck = 0
		for y in range(i, n, k):
			if s[y] == '1':
				ck |= 2
			if s[y] == '0':
				ck |= 1
		if ck == 3:
			return 'NO'
		if ck == 2:
			one += 1
		elif ck == 1:
			zero += 1
	if max(zero, one) > k / 2:
		gg = True
	if gg:
		return 'NO'
	else:
		return 'YES'
t = int(input())
while t:
	t -= 1
	print(sol())


T = int(input())
for _ in range(0, T):
	(n, k) = map(int, input().split())
	s = input()
	ans = 'YES'
	pos = [0] * k
	for i in range(0, k):
		c0 = 0
		c1 = 0
		for j in range(i, len(s), k):
			if s[j] == '0':
				c0 += 1
			elif s[j] == '1':
				c1 += 1
		if c0 > 0 and c1 > 0:
			ans = 'NO'
			break
		elif c0 > 0:
			pos[i] = 0
		elif c1 > 0:
			pos[i] = 1
		else:
			pos[i] = -1
	t0 = 0
	t1 = 0
	tq = 0
	for i in range(k):
		if pos[i] == 0:
			t0 += 1
		elif pos[i] == 1:
			t1 += 1
		else:
			tq += 1
	if t0 > k // 2 or t1 > k // 2:
		ans = 'NO'
	print(ans)


from math import ceil
from collections import deque
for _ in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	s = [i for i in input()]
	if k % 2:
		print('NO')
		continue
	for i in range(k):
		c = '?'
		for j in range(i, n, k):
			if s[j] == '1':
				c = '1'
			elif s[j] == '0':
				c = '0'
		for j in range(i, n, k):
			if s[j] == '?':
				s[j] = c
	z = o = m = 0
	for i in s[:k]:
		if i == '0':
			z += 1
		elif i == '1':
			o += 1
		else:
			m += 1
	if m < abs(z - o):
		print('NO')
		continue
	f = 0
	for i in range(n - k):
		if s[i] == '0' and s[i + k] == '1' or (s[i] == '1' and s[i + k] == '0'):
			f = 1
	if f:
		print('NO')
	else:
		print('YES')


import collections
from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input().strip())
	flag = True
	for start in range(k):
		cur = set((s[i] for i in range(start, n, k)))
		if len(cur) == 1:
			continue
		if len(cur) == 2:
			if '?' not in cur:
				flag = False
				break
			if '1' in cur:
				for i in range(start, n, k):
					s[i] = '1'
			else:
				for i in range(start, n, k):
					s[i] = '0'
		if len(cur) == 3:
			flag = False
			break
	if not flag:
		print('NO')
		continue
	c = collections.Counter(s[:k])
	if abs(c['0'] - c['1']) <= c['?']:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	i = 0
	broke = False
	while i < k:
		p = i
		if broke:
			break
		if s[p] != '?':
			while p + k < n:
				p += k
				if s[p] == s[i] or s[p] == '?':
					pass
				else:
					broke = True
					break
		else:
			while p + k < n:
				p += k
				if s[p] != '?':
					j = p - k
					while j >= 0:
						s[j] = s[p]
						j -= k
					i -= 1
					break
		i += 1
	if broke:
		print('NO')
	else:
		(z, o) = (0, 0)
		for i in range(k):
			if s[i] == '1':
				o += 1
			elif s[i] == '0':
				z += 1
		if z <= k // 2 and o <= k // 2:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	z = 0
	o = 0
	q = 0
	for i in range(k):
		if s[i] == '0':
			z += 1
		elif s[i] == '1':
			o += 1
	q = k - z - o
	if abs(z - o) > q:
		print('NO')
		continue
	inf = [-1] * k
	possible = True
	for i in range(n):
		if s[i] != '?':
			if inf[i % k] == -1:
				inf[i % k] = s[i]
			elif s[i] != inf[i % k]:
				possible = False
				break
	if possible == False:
		print('NO')
		continue
	z = 0
	o = 0
	q = 0
	for i in range(k):
		if inf[i] == '0':
			z += 1
		elif inf[i] == '1':
			o += 1
	q = k - z - o
	if abs(z - o) > q:
		print('NO')
	else:
		print('YES')


from sys import stdin
from collections import Counter
input = stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input().rstrip())
	flag = True
	for i in range(k):
		for j in range(i, n, k):
			if s[i] != s[j] and s[i] != '?' and (s[j] != '?'):
				flag = False
			elif s[i] == '?' and s[j] != '?':
				s[i] = s[j]
	if not flag:
		print('NO')
		continue
	c = Counter(s[:k])
	if c['1'] > k // 2 or c['0'] > k // 2:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	f = 0
	for i in range(k):
		for j in range(i, n, k):
			if s[j] == '?':
				s[j] = s[i]
			elif s[j] != s[i]:
				if s[i] == '?':
					s[i] = s[j]
				else:
					f = 1
					break
	if f == 1:
		print('NO')
	else:
		t = s[:k]
		q = t.count('?')
		o = t.count('1')
		z = t.count('0')
		if o < z:
			if q >= z - o:
				print('YES')
			else:
				print('NO')
		elif o > z:
			if q >= o - z:
				print('YES')
			else:
				print('NO')
		else:
			print('YES')


import sys, io, os, time
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
IO = 0
if IO:
	input = sys.stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	if n == k:
		if s.count('1') > k // 2 or s.count('0') > k // 2:
			print('NO')
		else:
			print('YES')
		continue
	x = y = 0
	flag = 1
	for i in range(k):
		c = '2'
		for j in range(i, n, k):
			if s[j] != '?':
				if c != '2' and s[j] != c:
					flag = 0
				c = s[j]
		if c != '2':
			if c == '1':
				x += 1
			elif c == '0':
				y += 1
		if x > k // 2 or y > k // 2:
			flag = 0
			break
	if flag:
		print('YES')
	else:
		print('NO')


def main():
	t = int(input())
	for i in range(t):
		(n, k) = map(int, input().split())
		s = input()
		ar = [[0] * 3 for i in range(k)]
		k2 = k * 2
		for j in range(n):
			i = j % k
			if s[j] == '?':
				ar[i][2] += 1
			else:
				ar[i][int(s[j])] += 1
		if sum([ar[i][0] > 0 and ar[i][1] > 0 for i in range(k)]) > 0:
			print('NO')
			continue
		num1 = sum([ar[i][0] > 0 for i in range(k)])
		num2 = sum([ar[i][1] > 0 for i in range(k)])
		num3 = k - num1 - num2
		print(['NO', 'YES'][num3 >= abs(num1 - num2)])
main()


import sys
input = sys.stdin.buffer.readline
T = int(input())
for testcase in range(T):
	(n, k) = map(int, input().split())
	s = list(input())
	d = [63] * k
	flag = False
	for i in range(n):
		if s[i] == 63:
			continue
		if d[i % k] == 63:
			d[i % k] = s[i]
		elif d[i % k] != s[i]:
			flag = True
			break
	if flag:
		print('NO')
		continue
	hatena = 0
	zero = 0
	one = 0
	for i in range(k):
		if s[i] == 63:
			s[i] = d[i]
		if s[i] == 49:
			one += 1
		elif s[i] == 48:
			zero += 1
		else:
			hatena += 1
	if abs(one - zero) > hatena:
		flag = True
	if flag:
		print('NO')
	else:
		print('YES')


import itertools
import math
import sys
from collections import defaultdict

def stdinWrapper():
	data = '9\n6 4\n100110\n3 2\n1?1\n3 2\n1?0\n4 4\n????\n7 4\n1?0??1?\n10 10\n11??11??11\n4 2\n1??1\n4 4\n?0?0\n6 2\n????00\n\n'
	for line in data.split('\n'):
		yield line
if '--debug' not in sys.argv:

	def stdinWrapper():
		while True:
			yield input()
inputs = stdinWrapper()

def inputWrapper():
	return next(inputs)

def getType(_type):
	return _type(inputWrapper())

def getArray(_type):
	return [_type(x) for x in inputWrapper().split()]

def solve(s, k):
	s = list(s)
	i = len(s) - 1
	limit = max(0, i - k + 1)
	while i >= limit:
		cur = None
		pos = i
		while pos >= 0:
			char = s[pos]
			if cur is None:
				if char in '01':
					cur = char
			elif char == '?':
				s[pos] = cur
			elif s[pos] != cur:
				return 'NO'
			pos -= k
		i -= 1
	seq = s[0:k]
	ones = len([x for x in seq if x == '1'])
	zeros = len([x for x in seq if x == '0'])
	if ones <= len(seq) / 2 and zeros <= len(seq) / 2:
		return 'YES'
	return 'NO'
t = getType(int)
for _ in range(t):
	(n, k) = getArray(int)
	s = getType(str)
	print(solve(s, k))


def checker(a, n, k):
	(n0, n1, qm) = (0, 0, 0)
	for i in range(k):
		if a[i] == '0':
			n0 += 1
		elif a[i] == '1':
			n1 += 1
		else:
			qm += 1
	if n0 != n1 and qm < abs(n1 - n0):
		return 0
	for i in range(1, n - k + 1):
		if a[i - 1] == '1' and a[i + k - 1] == '0':
			return 0
		elif a[i - 1] == '0' and a[i + k - 1] == '1':
			return 0
		elif a[i - 1] == '0' and a[i + k - 1] == '?':
			a[i + k - 1] = '0'
		elif a[i - 1] == '1' and a[i + k - 1] == '?':
			a[i + k - 1] = '1'
	(n0, n1, qm) = (0, 0, 0)
	for i in range(n - k, n):
		if a[i] == '0':
			n0 += 1
		elif a[i] == '1':
			n1 += 1
		else:
			qm += 1
	if n0 != n1 and qm < abs(n1 - n0):
		return 0
	return 1

def main():
	t = int(input())
	res = []
	for i in range(t):
		(n, k) = list(map(int, input().split()))
		a = [i for i in input()]
		flag = checker(a, n, k)
		if flag == 1:
			print('YES')
		else:
			print('NO')
	for i in res:
		print(i)
main()


import sys

def input():
	return sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
	[n, k] = list(map(int, input().split()))
	str_val = list(input())
	total0inKwin = 0
	total1inKwin = 0
	for j in range(0, k):
		if str_val[j] == '0':
			total0inKwin += 1
		elif str_val[j] == '1':
			total1inKwin += 1
	if total1inKwin > k // 2 or total0inKwin > k // 2:
		print('NO')
		continue
	for i in range(0, n - k):
		if str_val[i] != '?' and str_val[i + k] == '?':
			str_val[i + k] = str_val[i]
			continue
		if str_val[i] == '0':
			total0inKwin -= 1
		elif str_val[i] == '1':
			total1inKwin -= 1
		if str_val[i + k] == '0':
			total0inKwin += 1
		elif str_val[i + k] == '1':
			total1inKwin += 1
		if total1inKwin > k // 2 or (total0inKwin > k // 2 or (str_val[i] != '?' and str_val[i + k] != '?' and (str_val[i] != str_val[i + k]))):
			print('NO')
			break
	else:
		print('YES')


t = int(input())
for i in range(t):
	check_0 = set()
	check_1 = set()
	(n, k) = map(int, input().split())
	s = list(input())
	for i in range(n):
		if s[i] == '0':
			check_0.add(i % k)
		if s[i] == '1':
			check_1.add(i % k)
	check = check_0.union(check_1)
	if not len(check) == len(check_0) + len(check_1):
		print('NO')
	elif len(check_0) <= k / 2 and len(check_1) <= k / 2:
		print('YES')
	else:
		print('NO')


from sys import stdin

def iinput():
	return int(stdin.readline())

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
t = iinput()
while t:
	t -= 1
	(n, k) = minput()
	s = list(input())
	ans = 'YES'
	zeros = ones = 0
	for i in range(k):
		temp = None
		for j in range(i, n, k):
			if s[j] != '?':
				if temp and s[j] != temp:
					ans = 'NO'
					break
				temp = s[j]
			elif temp:
				s[j] = temp
		if temp == '0':
			zeros += 1
		if temp == '1':
			ones += 1
	if zeros > k // 2 or ones > k // 2:
		ans = 'NO'
	print(ans)


import math
import sys
import io, os
input = sys.stdin.readline
t = int(input())
for ii in range(t):
	(n, k) = map(int, input().split())
	a = [i for i in input() if i != '\n']
	ok = True
	for i in range(n - k + 1):
		if i < n - k:
			if a[i] == '1' and a[i + k] == '0' or (a[i] == '0' and a[i + k] == '1'):
				ok = False
				break
			if a[i] == '?' and a[i + k] != '?':
				j = i
				while j > -1:
					if a[j] == '?':
						a[j] = a[i + k]
					else:
						break
					j -= k
			if a[i] != '?' and a[i + k] == '?':
				a[i + k] = a[i]
	slice_ = a[0:0 + k]
	one = slice_.count('1')
	zero = slice_.count('0')
	if one > k / 2 or zero > k / 2:
		ok = False
	for i in range(k, n):
		if a[i] == '1':
			one += 1
		if a[i] == '0':
			zero += 1
		if a[i - k] == '1':
			one -= 1
		if a[i - k] == '0':
			zero -= 1
		if one > k / 2 or zero > k / 2:
			ok = False
			break
	if ok:
		sys.stdout.write('YES' + '\n')
	else:
		sys.stdout.write('NO' + '\n')


import math, sys
from collections import defaultdict, Counter, deque
INF = float('inf')
MOD = int(1000000000.0) + 7

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def primeFactors(n):
	factors = defaultdict(int)
	i = 2
	while i ** 2 <= n:
		while n % i == 0:
			factors[i] += 1
			n //= i
		i += 1
	if n > 1:
		factors[n] += 1
	return factors

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

def main():
	(n, k) = vars()
	s = list(input())
	l = 0
	r = k
	res = 'YES'
	for i in range(k):
		val = None
		for j in range(i, n, k):
			if s[j] != '?' and (not val):
				val = s[j]
			elif s[j] != '?' and val and (val != s[j]):
				res = 'NO'
				break
		if res == 'NO':
			print(res)
			return
		if val:
			for j in range(i, n, k):
				s[j] = val
	(o, z, q) = (0, 0, 0)
	for i in range(k):
		o += int(s[i] == '1')
		z += int(s[i] == '0')
		q += int(s[i] == '?')
	if o + q < z or z + q < o:
		print('NO')
	else:
		print('YES')
t = 1
t = int(input())
for _ in range(t):
	main()


TC = int(input())
for tc in range(TC):
	(N, K) = map(int, input().split())
	S = input()
	ok = True
	cnt0 = 0
	cnt1 = 0
	cntQ = 0
	for i in range(K):
		is0 = False
		is1 = False
		isQ = False
		for j in range(i, N, K):
			is0 = is0 or S[j] == '0'
			is1 = is1 or S[j] == '1'
			isQ = isQ or S[j] == '?'
		if is0 and is1:
			ok = False
			break
		if is0:
			cnt0 += 1
		if is1:
			cnt1 += 1
		if isQ and (not is0) and (not is1):
			cntQ += 1
	if cnt1 > K // 2 or cnt0 > K // 2:
		ok = False
	if ok:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	l = [-1] * k
	f = 0
	for i in range(len(s)):
		if s[i] != '?':
			if l[i % k] == -1:
				l[i % k] = int(s[i])
			elif l[i % k] != int(s[i]):
				f = 1
		if f == 1:
			break
	if f == 1:
		print('NO')
	else:
		o = l.count(1)
		z = l.count(0)
		m = l.count(-1)
		if k // 2 - o < 0:
			print('NO')
			continue
		if k // 2 - z < 0:
			print('NO')
			continue
		do = k // 2 - o
		dz = k // 2 - z
		if do + dz != m:
			print('NO')
			continue
		print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	d = {}
	for i in range(k):
		d[i] = set()
	for i in range(n):
		d[i % k].add(s[i])
	cond = True
	for i in range(k):
		if '0' in d[i] and '1' in d[i]:
			cond = False
			break
	if cond == False:
		print('NO')
		continue
	p = ''
	for i in range(k):
		if '0' in d[i]:
			p += '0'
		elif '1' in d[i]:
			p += '1'
		else:
			p += '?'
	(zero, one) = (0, 0)
	for ele in p:
		if ele == '0':
			zero += 1
		elif ele == '1':
			one += 1
	if zero <= k // 2 and one <= k // 2:
		print('YES')
		continue
	print('NO')


from sys import stdin
N = int(stdin.readline())
for case in range(N):
	(length, k) = map(int, stdin.readline().split())
	string = str(stdin.readline())
	count = {'1': 0, '0': 0, '?': 0}
	flag = True
	for i in range(k):
		res = string[i]
		for j in range(i + k, length, k):
			if res != '?' and string[j] != res and (string[j] != '?'):
				flag = False
				break
			if res == '?' and string[j] != '?':
				res = string[j]
		if flag:
			count[res] += 1
	if count['1'] <= k // 2 and count['0'] <= k // 2 and flag:
		print('YES')
	else:
		print('NO')


def checker(A, k):
	tot = 0
	(o, z) = (0, 0)
	for i in range(k):
		if A[i] == 1:
			o += 1
		elif A[i] == -1:
			z += 1
	if z > k // 2 or o > k // 2:
		return False
	for j in range(k, len(A)):
		if A[j - k] == 1:
			o -= 1
		elif A[j - k] == -1:
			z -= 1
		if A[j] == 1:
			o += 1
		elif A[j] == -1:
			z += 1
		if z > k // 2 or o > k // 2:
			return False
	return True

def func(A, k):
	if k == len(A):
		(o, z) = (0, 0)
		for i in A:
			if i == '0':
				z += 1
			elif i == '1':
				o += 1
		if o > k // 2 or z > k // 2:
			return 'NO'
		return 'YES'
	res = [0] * len(A)
	(one, zero, ques) = ([], [], [])
	for i in range(k):
		new = set()
		ind = []
		for j in range(i, len(A), k):
			new.add(A[j])
			ind.append(j)
		if '0' in new and '1' in new:
			return 'NO'
		elif '0' in new:
			zero.extend(ind)
		elif '1' in new:
			one.extend(ind)
		else:
			ques.extend(ind)
	for i in one:
		res[i] = 1
	for i in zero:
		res[i] = -1
	for i in ques:
		res[i] = 0
	if checker(res, k):
		return 'YES'
	return 'NO'
t = int(input())
for i in range(t):
	(n, k) = list(map(int, input().split()))
	A = input()
	print(func(A, k))


def find_ans(n, k, s):
	for i in range(n - k):
		if s[i] != '?' and s[i + k] != '?':
			if s[i] != s[i + k]:
				return 'NO'
		elif s[i] != '?' and s[i + k] == '?':
			s[i + k] = s[i]
		elif s[i] == '?' and s[i + k] != '?':
			s[i] = s[i + k]
	num_zero = num_one = 0
	for i in range(n - k, n):
		if s[i] == '0':
			num_zero += 1
		elif s[i] == '1':
			num_one += 1
	if abs(num_one - num_zero) > k - (num_one + num_zero):
		return 'NO'
	return 'YES'
for i in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	print(find_ans(n, k, s))


t = int(input())
from collections import defaultdict
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	counts = defaultdict(int)
	for i in range(k):
		counts[s[i]] += 1
	for i in range(n - k):
		if abs(counts['0'] - counts['1']) > counts['?']:
			print('NO')
			break
		elif s[k + i] == '?':
			if s[i] == '?':
				continue
			else:
				s[k + i] = s[i]
		else:
			if s[i] == '?':
				s[i] = s[i + k]
				counts['?'] -= 1
				counts[s[i]] += 1
				continue
			if s[i] != s[i + k]:
				print('NO')
				break
	else:
		if abs(counts['0'] - counts['1']) > counts['?']:
			print('NO')
		else:
			print('YES')


def qchange(s, i, k, o, z):
	if o != z:
		if o == k / 2:
			for j in range(i, i + k):
				if s[j] == '?':
					s[j] = '0'
			return True
		elif z == k / 2:
			for j in range(i, i + k):
				if s[j] == '?':
					s[j] = '1'
			return True
	return False

def chk(o, z):
	if o > k / 2 or z > k / 2:
		return False
	return True
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = [i for i in input()]
	sind = 0
	suc = True
	while True:
		change = False
		fchange = False
		(o, z, q) = (0, 0, 0)
		for i in range(sind, sind + k):
			if s[i] == '1':
				o += 1
			elif s[i] == '0':
				z += 1
			elif s[i] == '?':
				q += 1
		if not chk(o, z):
			suc = False
			break
		if qchange(s, sind, k, o, z):
			(o, z, q) = (k // 2, k // 2, 0)
			change = True
			if not fchange:
				fchange = True
		for i in range(sind + 1, n - k + 1):
			old = s[i - 1]
			new = s[i + k - 1]
			if old == new:
				continue
			else:
				if old == '?' and new != '?':
					s[i - 1] = new
					change = True
					if not fchange:
						fchange = True
						sind = i
					q -= 1
					if new == '1':
						o += 1
					else:
						z += 1
				elif old != '?' and new == '?':
					s[i + k - 1] = old
					change = True
					if not fchange:
						fchange = True
						sind = i
				elif old != new:
					suc = False
					break
				if not chk(o, z):
					suc = False
					break
				if qchange(s, i, k, o, z):
					(o, z, q) = (k // 2, k // 2, 0)
					change = True
		if not suc:
			break
		if not change:
			break
	print('YES' if suc else 'NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = [*input()]
	ans = 'YES'
	for i in range(n):
		if i + k >= n:
			continue
		if s[i] == '?' and s[i + k] != '?':
			s[i] = s[i + k]
		if s[i + k] == '?' and s[i] != '?':
			s[i + k] = s[i]
		if s[i] != '?' and s[i + k] != '?' and (s[i + k] != s[i]):
			ans = 'NO'
	(o, z) = (s[:k].count('1'), s[:k].count('0'))
	(maxo, maxz) = (o, z)
	prev = 0
	for i in s[k:]:
		if s[prev] == '1':
			o -= 1
		if s[prev] == '0':
			z -= 1
		if i == '1':
			o += 1
		if i == '0':
			z += 1
		maxo = max(maxo, o)
		maxz = max(maxz, z)
		prev += 1
	maxoz = max(maxo, maxz)
	if maxoz > k // 2:
		ans = 'NO'
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for rrr in range(t):
	(n, k) = list(map(int, input().split()))
	s = input().rstrip()
	s = list(s)
	if n == k:
		if s.count('1') <= k // 2 and s.count('0') <= k // 2:
			print('YES')
		else:
			print('NO')
	else:
		ok = 1
		for i in range(k):
			tmp = set([])
			for j in range(i, n, k):
				tmp.add(s[j])
			for j in range(i, n, k):
				tmp.add(s[j])
				if '1' in tmp and s[j] == '?':
					s[j] = '1'
				if '0' in tmp and s[j] == '?':
					s[j] = '0'
				if '0' in tmp and '1' in tmp:
					ok = 0
		s = s[::-1]
		for i in range(k):
			tmp = set([])
			for j in range(i, n, k):
				tmp.add(s[j])
			for j in range(i, n, k):
				tmp.add(s[j])
				if '1' in tmp and s[j] == '?':
					s[j] = '1'
				if '0' in tmp and s[j] == '?':
					s[j] = '0'
				if '0' in tmp and '1' in tmp:
					ok = 0
		tmp = s[:k].count('1')
		if tmp > k // 2:
			ok = 0
		for i in range(k, n):
			if s[i] == '1':
				tmp += 1
			if s[i - k] == '1':
				tmp -= 1
			if tmp > k // 2:
				ok = 0
		tmp = s[:k].count('0')
		if tmp > k // 2:
			ok = 0
		for i in range(k, n):
			if s[i] == '0':
				tmp += 1
			if s[i - k] == '0':
				tmp -= 1
			if tmp > k // 2:
				ok = 0
		if ok == 1:
			print('YES')
		else:
			print('NO')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	f = True
	ans = 0
	temp = 0
	for i in range(k):
		j = i
		current = s[j]
		while j < n:
			if s[j] == '?':
				j += k
				continue
			if s[j] != current and current != '?':
				f = False
				break
			current = s[j]
			j += k
		if not f:
			break
		if current == '1':
			ans += 1
		if current == '0':
			ans -= 1
		if current == '?':
			temp += 1
	if abs(ans) > temp:
		f = False
	print(['NO', 'YES'][f])


def main():
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		s = list(input())
		ans = -1
		for i in range(k, n):
			if s[i] != '?':
				if s[i % k] == '?':
					s[i % k] = s[i]
				elif s[i] != s[i % k]:
					ans = 'NO'
					break
		if ans == -1:
			(o, z) = (0, 0)
			for i in range(k):
				if s[i] == '1':
					o += 1
				elif s[i] == '0':
					z += 1
			if o <= k // 2 and z <= k // 2:
				print('YES')
			else:
				print('NO')
		else:
			print('NO')
main()


t = int(input())
for _ in range(t):
	(n, k) = [int(p) for p in input().split()]
	s = list(input())
	sub = [''] * k
	done = False
	for i in range(k):
		for j in range(n):
			if j * k + i < n:
				if s[j * k + i] != '?':
					if sub[i] == '':
						sub[i] = s[j * k + i]
					elif sub[i] != s[j * k + i]:
						print('NO')
						done = True
						break
			else:
				break
			if done:
				break
	if done:
		continue
	g = max(sub.count('1'), sub.count('0'))
	if g > k // 2:
		print('NO')
	elif k - sub.count('1') - sub.count('0') == sub.count(''):
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	t -= 1
	(n, k) = input().split()
	(n, k) = (int(n), int(k))
	s = input()
	s = [c for c in s]
	ans = True
	for i in range(0, k):
		chars = set()
		for j in range(i, n, k):
			chars.add(s[j])
		if '1' in chars and '0' in chars:
			ans = False
			break
		if '1' in chars:
			s[i] = '1'
		if '0' in chars:
			s[i] = '0'
	if not ans:
		print('NO')
		continue
	(c0, c1, cq) = (0, 0, 0)
	for i in range(0, k):
		c0 += s[i] == '0'
		c1 += s[i] == '1'
		cq += s[i] == '?'
	(m, n) = ((cq + c1 - c0) / 2, (cq - c1 + c0) / 2)
	if m % 1 or n % 1:
		print('NO')
		continue
	if m < 0 or n < 0:
		print('NO')
		continue
	print('YES')


for i in range(int(input())):
	(n, k) = map(int, input().split())
	s = [i for i in input()]
	(zero, one, que) = (0, 0, 0)
	c = 1
	for i in range(n - k):
		if s[i] == '1':
			if s[i + k] == '?':
				s[i + k] = '1'
			elif s[i + k] == '0':
				c = 0
		elif s[i] == '0':
			if s[i + k] == '?':
				s[i + k] = '0'
			elif s[i + k] == '1':
				c = 0
		elif s[i + k] == '1':
			s[i] = '1'
		elif s[i + k] == '0':
			s[i] = '0'
	for i in range(k):
		if s[i] == '1':
			one += 1
		elif s[i] == '0':
			zero += 1
	if zero > k // 2 or one > k // 2:
		c = 0
	for i in range(k, n - k + 1):
		if s[i - k] == '1':
			one -= 1
		elif s[i - k] == '0':
			zero -= 1
		if s[i] == '1':
			one += 1
		elif s[i] == '0':
			zero += 1
		if zero > k // 2 or one > k // 2:
			c = 0
	if c:
		print('YES')
	else:
		print('NO')


def is_changed(st, st1, n):
	for i in range(0, n):
		if st[i] == st1[i]:
			pass
		elif st[i] == '?':
			pass
		else:
			return False
	return True

def process():
	(n, k) = list(map(int, input().split()))
	st = input()
	s = ['?' for i in range(k)]
	for i in range(n):
		if st[i] != '?':
			if s[i % k] == '?' or s[i % k] == st[i]:
				s[i % k] = st[i]
			else:
				return 'NO'
	co = 0
	cz = 0
	for i in s:
		if i == '1':
			co += 1
		if i == '0':
			cz += 1
	if co <= k // 2 and cz <= k // 2:
		return 'YES'
	return 'NO'
tests = int(input())
for i in range(tests):
	print(process())


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	flag = 0
	for i in range(k):
		j = i
		seti = set()
		while j < n:
			if s[j] != '?':
				seti.add(s[j])
			j += k
		if len(seti) > 1:
			flag = 1
			break
		j = i
		ba = [i for i in seti]
		if len(seti) == 1:
			while j < n:
				s[j] = ba[0]
				j += k
	if flag:
		print('NO')
	else:
		(z, o, q) = (0, 0, 0)
		for i in range(k):
			if s[i] == '1':
				o += 1
			elif s[i] == '0':
				z += 1
			else:
				q += 1
		if z == o and q % 2 == 0:
			print('YES')
		elif z > o and q != 0 and (q + o >= z) and ((q + o + z) % 2 == 0):
			print('YES')
		elif o > z and q != 0 and (q + z >= o) and ((q + o + z) % 2 == 0):
			print('YES')
		else:
			print('NO')


t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	s = input()
	l = list(s)
	for i in range(k):
		l.append(l[n + i - k])
	n = len(l)
	flag = 0
	for i in range(n - k):
		if l[i] == '?' and l[i + k] != '?' or (l[i] != '?' and l[i + k] == '?'):
			if l[i] == '?' and l[i + k] != '?':
				l[i] = l[i + k]
			else:
				l[i + k] = l[i]
		elif l[i] == '?' and l[i + k] == '?':
			continue
		elif l[i] != l[i + k]:
			flag = 1
			break
	count0 = l[:k].count('0')
	count1 = l[:k].count('1')
	countq = l[:k].count('?')
	i = k - 1
	if count1 > k // 2 or count0 > k // 2:
		flag = 1
	for i in range(k, n - k):
		if l[i - k] == '0':
			count0 -= 1
		elif l[i - k] == '1':
			count1 -= 1
		else:
			countq -= 1
		if l[i] == '0':
			count0 += 1
		elif l[i] == '1':
			count1 += 1
		else:
			countq += 1
		if count1 > k // 2 or count0 > k // 2:
			flag = 1
	if flag == 1:
		print('NO')
	else:
		print('YES')


def isBalanced(arr: list, k: int) -> bool:
	if '?' not in arr:
		for i in range(len(arr) - k + 1):
			if arr[i:i + k].count('0') != k / 2:
				return False
		return True
	pos = arr.index('?')
	temp = arr.copy()
	arr[pos] = '0'
	temp[pos] = '1'
	return isBalanced(arr, k) or isBalanced(temp, k)

def checkBalanced(arr, k):
	v = ['?' for _ in range(k)]
	for i in range(k):
		for j in range(i, len(arr), k):
			if arr[j] == '?':
				continue
			if v[i] == '?':
				v[i] = arr[j]
			elif arr[j] != v[i]:
				return False
	(one, zero) = (0, 0)
	for x in v:
		if x == '1':
			one += 1
		if x == '0':
			zero += 1
	if one > k / 2 or zero > k / 2:
		return False
	return True
t = int(input())
for _ in range(t):
	[n, k] = list(map(int, input().split()))
	arr = list(input())
	if checkBalanced(arr, k):
		print('YES')
	else:
		print('NO')


import sys
readline = sys.stdin.readline
T = int(readline())
Ans = ['NO'] * T
for qu in range(T):
	(N, K) = map(int, readline().split())
	S = [-1 if s == '?' else int(s) for s in readline().strip()]
	L = [-1] * K
	for i in range(N):
		j = i % K
		si = S[i]
		if L[j] == -1:
			L[j] = si
		elif S[i] == -1:
			continue
		elif L[j] != si:
			break
	else:
		one = 0
		zero = 0
		minus = 0
		for l in L:
			if l == 1:
				one += 1
			elif l == 0:
				zero += 1
			else:
				minus += 1
		if one < zero:
			(one, zero) = (zero, one)
		if zero + minus >= one:
			Ans[qu] = 'YES'
print('\n'.join(Ans))


def checker(a, b, c):
	req = k // 2
	if a > req:
		return False
	if b > req:
		return False
	an = req - a
	if c < an:
		return False
	c -= an
	bn = req - b
	if c < bn:
		return False
	c -= bn
	if c == 0:
		return True
	else:
		return False
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = input()
	arr = list(s)
	ans = True
	cnt = [0, 0, 0]
	f = True
	for i in range(k, n):
		if arr[i] != arr[i - k]:
			if arr[i] == '?':
				arr[i] = arr[i - k]
			elif arr[i - k] == '?':
				arr[i - k] = arr[i]
			else:
				f = False
	for i in range(k):
		if arr[i] == '1':
			cnt[1] += 1
		elif arr[i] == '0':
			cnt[0] += 1
		else:
			cnt[2] += 1
	if checker(cnt[1], cnt[0], cnt[2]) == False:
		f = False
	for i in range(k, n):
		if arr[i] == '1':
			cnt[1] += 1
		elif arr[i] == '0':
			cnt[0] += 1
		else:
			cnt[2] += 1
		if arr[i - k] == '1':
			cnt[1] -= 1
		elif arr[i - k] == '0':
			cnt[0] -= 1
		else:
			cnt[2] -= 1
		if checker(cnt[1], cnt[0], cnt[2]) == False:
			f = False
	if f == True:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	(n, k) = map(int, input().split())
	s = input()
	a = list(s)
	ans = 'YES'
	l1 = ['?' for i in range(0, k)]
	for i in range(0, len(a), k):
		count = 0
		for j in range(i, min(len(a), i + k)):
			if a[j] == '0':
				if l1[count] == '1':
					ans = 'NO'
					break
				else:
					l1[count] = '0'
			elif a[j] == '1':
				if l1[count] == '0':
					ans = 'NO'
					break
				else:
					l1[count] = '1'
			count = count + 1
		if ans == 'NO':
			break
	first = 0
	second = 0
	for i in l1:
		if i == '0':
			first = first + 1
		elif i == '1':
			second = second + 1
	if first > k // 2 or second > k // 2:
		ans = 'NO'
	print(ans)
	t = t - 1


t = int(input())
for you in range(t):
	l = input().split()
	n = int(l[0])
	k = int(l[1])
	s = input()
	s = [i for i in s]
	poss = 1
	for i in range(k):
		done = 0
		val = -1
		j = i
		while j < n:
			if s[j] != '?' and done == 0:
				done = 1
				val = s[j]
			elif s[j] != '?' and done == 1 and (s[j] != val):
				poss = 0
				break
			j += k
		if poss == 0:
			break
		if done == 1:
			for j in range(i, n, k):
				s[j] = val
	if poss == 0:
		print('NO')
		continue
	num0 = 0
	num1 = 0
	for i in range(k):
		if s[i] == '1':
			num1 += 1
		elif s[i] == '0':
			num0 += 1
	z = k // 2
	if num1 <= z and num0 <= z:
		print('YES')
	else:
		print('NO')


T = int(input())

def tf(n):
	c = 0
	while 2 * (n // 2) == n:
		n //= 2
		c += 1
	return c
for t in range(T):
	(n, k) = map(int, input().split())
	a = input()
	b = list(a)
	d = False
	for j in range(2):
		if d:
			break
		for i in range(k, n):
			if b[i] == '?' and b[i - k] != '?':
				b[i] = b[i - k]
			elif b[i] != '?' and b[i - k] == '?':
				b[i - k] = b[i]
			if (b[i] == '1' and b[i - k] == '0' or (b[i] == '0' and b[i - k] == '1')) and (not d):
				print('NO')
				d = True
				break
		if d:
			break
		c0 = b[:k].count('0')
		c1 = b[:k].count('1')
		cc = b[:k].count('?')
		qs = set()
		for i in range(k):
			if b[i] == '?':
				qs.add(i)
		if (c0 > k // 2 or c1 > k // 2) and (not d):
			print('NO')
			d = True
		for i in range(k, n):
			if b[i] == '1':
				c1 += 1
			elif b[i] == '0':
				c0 += 1
			elif b[i] == '?':
				cc += 1
				qs.add(i)
			if b[i - k] == '0':
				c0 -= 1
			elif b[i - k] == '1':
				c1 -= 1
			elif b[i - k] == '?':
				cc -= 1
				qs.remove(i - k)
			if c1 == k // 2:
				for i in qs:
					b[i] = '0'
				qs = set()
				c0 = k // 2
			elif c0 == k // 2:
				for i in qs:
					b[i] = '1'
				qs = set()
				c1 = k // 2
			if (c0 > k // 2 or c1 > k // 2) and (not d):
				print('NO')
				d = True
				break
	if not d and (c0 > k // 2 or c1 > k // 2):
		print('NO')
		d = True
	if not d:
		print('YES')


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	b = []
	for j in s:
		b.append(j)
	j = n - 1
	f = 0
	while j >= 0:
		if j - k >= 0:
			if b[j] == '?':
				if b[j - k] == '?':
					pass
				else:
					b[j] = b[j - k]
			elif b[j - k] == '?':
				b[j - k] = b[j]
			elif b[j] != b[j - k]:
				f = 1
				break
		j += -1
	if f == 1:
		print('NO')
	else:
		j = 0
		while j < n:
			if j + k < n:
				if b[j] == '?':
					if b[j + k] == '?':
						pass
					else:
						b[j] = b[j + k]
				elif b[j + k] == '?':
					b[j + k] = b[j]
				elif b[j] != b[j + k]:
					f = 1
					break
			j += 1
		if f == 1:
			print('NO')
		else:
			coz = 0
			coo = 0
			j = 0
			while j < k:
				if b[j] == '0':
					coz += 1
				elif b[j] == '1':
					coo += 1
				j += 1
			if coz > k // 2 or coo > k // 2:
				print('NO')
			else:
				print('YES')


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
mod = int(1000000000.0) + 7
INF = 100000
for t in range(int(data())):
	(n, k) = mdata()
	s = list(data())
	flag = True
	for i in range(k):
		s1 = set()
		ind = i
		while ind < n:
			if s[ind] != '?':
				s1.add(s[ind])
			ind += k
		if len(s1) > 1:
			flag = False
			break
		if s1:
			a = s1.pop()
			ind = i
			while ind < n:
				s[ind] = a
				ind += k
	(z1, o1) = (0, 0)
	for i in s[:k]:
		if i == '1':
			o1 += 1
		elif i == '0':
			z1 += 1
		if o1 > k // 2 or z1 > k // 2:
			flag = False
			break
	for i in range(k, n):
		if s[i] == '1':
			o1 += 1
		elif s[i] == '0':
			z1 += 1
		if s[i - k] == '1':
			o1 -= 1
		elif s[i - k] == '0':
			z1 -= 1
		if o1 > k // 2 or z1 > k // 2:
			flag = False
			break
	if flag:
		out('YES')
	else:
		out('NO')


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	s = list(s)
	pos = True
	for i in range(n):
		if s[i] != '?' and s[i % k] != '?' and (s[i] != s[i % k]):
			pos = False
			break
		elif s[i] == '?' and s[i % k] == '?':
			continue
		elif s[i] != '?' and s[i % k] == '?':
			s[i % k] = s[i]
		else:
			s[i] = s[i % k]
	if not pos:
		print('NO')
		continue
	c = Counter(s[:k])
	if c['1'] > k // 2 or c['0'] > k // 2:
		print('NO')
	else:
		print('YES')


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
t = int(input())

def check(arr, k):
	ones = arr[:k].count(1)
	zeros = arr[:k].count(0)
	if ones > k // 2 or zeros > k // 2:
		return 'NO'
	return 'YES'
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = [x for x in input()]
	ones = arr[:k].count('1')
	zeros = arr[:k].count('0')
	if ones > k // 2 or zeros > k // 2:
		print('NO')
		continue
	buckets = [[] for x in range(k)]
	buckInds = [[] for x in range(k)]
	for (ind, val) in enumerate(arr):
		buckets[ind % k].append(val)
		buckInds[ind % k].append(ind)
	ok = 'YES'
	finalArr = [-1 for x in range(n)]
	for (ind, bucket) in enumerate(buckets):
		one = bucket.count('1')
		zero = bucket.count('0')
		if one and zero:
			ok = 'NO'
			break
		if one:
			for x in buckInds[ind]:
				finalArr[x] = 1
		if zero:
			for x in buckInds[ind]:
				finalArr[x] = 0
	if ok == 'YES':
		ok = check(finalArr, k)
	print(ok)


def balanced_bitstring(bs, n, k):
	s = list(bs)
	for i in range(k):
		if s[i] == '?':
			for j in range(i + k, n, k):
				if s[j] != '?':
					s[i] = s[j]
					break
	for i in range(k):
		if s[i] != '?':
			for j in range(i + k, n, k):
				if s[j] == '?':
					s[j] = s[i]
				elif s[j] != s[i]:
					return 'NO'
	(ones, zeroes) = (s[0:k].count('1'), s[0:k].count('0'))
	if ones <= k / 2 and zeroes <= k / 2:
		return 'Yes'
	return 'NO'
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split(' '))
	s = str(input())
	print(balanced_bitstring(s, n, k))


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	zc = oc = qc = 0
	flag = True
	for i in range(k):
		hasZero = hasOne = hasQue = False
		for j in range(i, n, k):
			if s[j] == '0':
				hasZero = True
			elif s[j] == '1':
				hasOne = True
			else:
				hasQue = True
		if hasZero and hasOne:
			flag = False
			break
		if hasZero:
			zc += 1
		elif hasOne:
			oc += 1
		else:
			qc += 1
	if oc > k // 2 or zc > k // 2:
		flag = False
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	liczby = list(input())
	wzor = ['?'] * k
	odp = 'YES'
	for x in range(n):
		if liczby[x] != '?':
			if wzor[x % k] == '?':
				wzor[x % k] = liczby[x]
			elif wzor[x % k] != liczby[x]:
				odp = 'NO'
	else:
		(ile_1, ile_0) = (0, 0)
		for x in wzor:
			if x == '1':
				ile_1 += 1
			elif x == '0':
				ile_0 += 1
		if ile_0 > k // 2 or ile_1 > k // 2:
			odp = 'NO'
		print(odp)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	x = y = 0
	for i in range(k):
		a = b = 0
		for j in range(i, n, k):
			if s[j] == '1':
				a += 1
			elif s[j] == '0':
				b += 1
		if min(a, b) > 0:
			print('NO')
			break
		if a:
			x += 1
		elif b:
			x -= 1
		else:
			y += 1
	else:
		if abs(x) <= y:
			print('YES')
		else:
			print('NO')


from sys import stdin
import math
from collections import deque
from bisect import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(t,) = gil()
for _ in range(t):
	(n, k) = gil()
	a = gcl()
	flag = True
	for i in range(k, n):
		if a[i] != '?' and a[i - k] == '?':
			a[i - k] = a[i]
		elif a[i - k] != '?' and a[i] == '?':
			a[i] = a[i - k]
		elif a[i - k] != '?' and a[i] != '?' and (a[i] != a[i - k]):
			flag = False
	cnt = {'?': 0, '0': 0, '1': 0}
	for i in range(k):
		cnt[a[i]] += 1
	if max(cnt['0'], cnt['1']) > k // 2:
		flag = False
	if flag:
		for i in range(k, n):
			cnt[a[i]] += 1
			cnt[a[i - k]] -= 1
			if max(cnt['0'], cnt['1']) > k // 2:
				flag = False
				break
	print('YES' if flag else 'NO')


a = int(input())
for i in range(a):
	(n, k) = map(int, input().split())
	s = input()
	ans = []
	for i in range(len(s)):
		ans.append(s[i])
	c1 = 0
	c2 = 0
	c3 = 0
	for i in range(k):
		if s[i] == '?':
			c3 += 1
		elif s[i] == '1':
			c1 += 1
		else:
			c2 += 1
	if abs(c1 - c2) > c3 or min(c1, c2) > k // 2:
		print('NO')
		continue
	flag = 0
	for i in range(k, len(s)):
		if ans[i - k] == ans[i]:
			continue
		elif ans[i - k] != '?' and ans[i] != '?':
			flag = 1
			break
		elif ans[i] == '?':
			ans[i] = ans[i - k]
			continue
		elif ans[i] == '1':
			if c1 < k // 2:
				c1 += 1
				c3 -= 1
				continue
			else:
				flag = 1
				break
		elif c2 < k // 2:
			c2 += 1
			c3 -= 1
			continue
		else:
			flag = 1
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	t = [[] for i in range(k)]
	for i in range(n):
		t[i % k].append(s[i])
	(ans0, ans1) = (0, 0)
	ex = 0
	for i in range(k):
		if t[i].count('0') > 0 and t[i].count('1') > 0:
			print('NO')
			break
		elif t[i].count('0') > 0:
			ans0 += 1
		elif t[i].count('1') > 0:
			ans1 += 1
		else:
			ex += 1
	else:
		if ex == 0:
			if ans0 == ans1:
				print('YES')
			else:
				print('NO')
		elif ans0 + ex >= k // 2 and ans1 + ex >= k // 2:
			print('YES')
		else:
			print('NO')


def main():
	t = int(input())
	for _ in range(t):
		(n, k) = map(int, input().split())
		a = list(input())
		d = {}
		ans = True
		for i in range(n):
			if a[i] != '?':
				x = i % k
				if x in d:
					if d[x] != a[i]:
						ans = False
				else:
					d[x] = a[i]
			if not ans:
				break
		if not ans:
			print('NO')
			continue
		for i in range(n):
			x = i % k
			if a[i] == '?':
				if x in d:
					a[i] = d[x]
		(count0, count1, count2) = (0, 0, 0)
		for i in a[:k]:
			if i == '0':
				count0 += 1
			if i == '1':
				count1 += 1
			if i == '?':
				count2 += 1
		x = abs(count0 - count1)
		if x > count2:
			ans = False
		if ans:
			print('YES')
		else:
			print('NO')
main()


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))

def solve():
	(n, k) = get_tuple()
	st = input()
	if k % 2:
		return 'NO'
	(any, c_o, c_z) = (0, 0, 0)
	for i in range(k):
		(one, zero) = (False, False)
		for j in range(i, n, k):
			if st[j] == '1':
				one = True
			elif st[j] == '0':
				zero = True
		if one and zero:
			return 'NO'
		elif not one and (not zero):
			any += 1
		elif one:
			c_o += 1
		else:
			c_z += 1
	if abs(c_o - c_z) <= any:
		return 'YES'
	else:
		return 'NO'

def main():
	print(solve())
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


import os
from heapq import heapify, heappop, heappush
import sys
import math
import operator
import bisect
from collections import defaultdict
from io import BytesIO, IOBase
sys.setrecursionlimit(10 ** 5)

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		(n, k) = inpt()
		s = list(input())
		lt = ['*'] * k
		ok = True
		for i in range(n):
			if s[i] != '?':
				if lt[i % k] == s[i] or lt[i % k] == '*':
					lt[i % k] = s[i]
				else:
					ok = False
					print('NO')
					break
		if ok:
			(one, zr, bl) = (0, 0, 0)
			for i in lt:
				if i == '*':
					bl += 1
				elif i == '1':
					one += 1
				else:
					zr += 1
			if abs(one - zr) <= bl:
				print('YES')
			else:
				print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from collections import Counter
for _ in range(int(input())):
	(n, k) = [int(_) for _ in input().split()]
	A = input()
	S = []
	for _ in range(n):
		S.append(A[_])
	for _ in range(n - k):
		if S[_] != '?' and S[_ + k] == '?':
			S[_ + k] = S[_]
		elif S[_] == '?' and S[_ + k] != '?':
			S[_] = S[_ + k]
		elif S[_] != S[_ + k]:
			print('NO')
			break
	else:
		D = Counter(S[:k])
		if abs(D['0'] - D['1']) > D['?']:
			print('NO')
		else:
			for _ in range(n - k):
				D[S[_]] -= 1
				D[S[_ + k]] += 1
				if abs(D['0'] - D['1']) > D['?']:
					print('NO')
					break
			else:
				print('YES')


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split(' '))
	k = input()
	l = []
	for j in range(a):
		l.append(k[j])
	flag = True
	check = [[] for i in range(b)]
	for j in range(a):
		check[j % b].append(l[j])
	for j in range(b):
		if set(check[j]) == {'0', '?'}:
			for p in range(j, a, b):
				l[p] = '0'
		elif set(check[j]) == {'1', '?'}:
			for p in range(j, a, b):
				l[p] = '1'
		elif set(check[j]) == {'0', '1', '?'} or set(check[j]) == {'0', '1'}:
			flag = False
	if not flag:
		print('NO')
		continue
	cnt = [0, 0]
	for i in range(b):
		if l[i] == '0':
			cnt[0] += 1
		elif l[i] == '1':
			cnt[1] += 1
	if cnt[0] > b // 2 or cnt[1] > b // 2:
		flag = False
	if not flag:
		print('NO')
	else:
		print('YES')


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(100000000)
	threading.stack_size(10240000)
	thread = threading.Thread(target=main)
	thread.start()
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(n - r))

def ceil(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def main():
	for _ in range(ii()):
		(n, k) = mi()
		s = si()
		s = ' ' + s
		f = [0] * (n + 5)
		g = [0] * (n + 5)
		pf = [0] * (n + 5)
		pg = [0] * (n + 5)
		s = list(s)
		for i in range(1, n + 1):
			if s[i] == '1':
				f[i] = 1
			if s[i] == '0':
				g[i] = 1
			f[i] += f[i - 1]
			g[i] += g[i - 1]
		flag = 1
		for i in range(k, n + 1):
			one = f[i] - f[i - k]
			zero = g[i] - g[i - k]
			if one > k // 2 or zero > k // 2:
				flag = 0
				break
		for i in range(1, n + 1):
			if s[i] == '1':
				pf[i % k + 1] = 1
			if s[i] == '0':
				pg[i % k + 1] = 1
		for i in range(1, k + 1):
			if pf[i] == pg[i] and pf[i] == 1:
				flag = 0
				break
		for i in range(1, n + 1):
			if pf[i % k + 1] == 1:
				s[i] = '1'
			if pg[i % k + 1] == 1:
				s[i] = '0'
		for i in range(1, n + 1):
			if s[i] == '1':
				f[i] = 1
			else:
				f[i] = 0
			if s[i] == '0':
				g[i] = 1
			else:
				g[i] = 0
			f[i] += f[i - 1]
			g[i] += g[i - 1]
		for i in range(k, n + 1):
			one = f[i] - f[i - k]
			zero = g[i] - g[i - k]
			if one > k // 2 or zero > k // 2:
				flag = 0
				break
		if flag:
			print('YES')
		else:
			print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
import math
input = sys.stdin.readline

def inInt():
	return int(input())

def inStr():
	return input().strip('\n')

def inIList():
	return list(map(int, input().split()))

def inSList():
	return input().split()

def solve(s, n, k):
	count_1 = [0 for x in range(k)]
	count_0 = [0 for x in range(k)]
	count_q = [0 for x in range(k)]
	for p in range(n):
		if s[p] == '1':
			count_1[p % k] += 1
		if s[p] == '0':
			count_0[p % k] += 1
		if s[p] == '?':
			count_q[p % k] += 1
	total_0 = 0
	total_1 = 0
	total_q = 0
	for x in range(k):
		if count_1[x] == 0 and count_0[x] == 0:
			total_q = total_q + 1
		elif count_1[x] > 0 and count_0[x] > 0:
			return 'NO'
		else:
			if count_0[x] == 0:
				total_1 += 1
			if count_1[x] == 0:
				total_0 += 1
	if total_0 > k / 2:
		return 'NO'
	if total_1 > k / 2:
		return 'NO'
	return 'YES'
tasks = inInt()
for t in range(tasks):
	pk = inIList()
	n = pk[0]
	k = pk[1]
	s = inStr()
	print(solve(s, n, k))


for i in range(int(input())):
	(n, k) = map(int, input().split())
	s = [j for j in input()]
	flag = True
	for j in range(n):
		if s[j] == '1':
			if j + k < n:
				if s[j + k] == '0':
					flag = False
					break
				s[j + k] = '1'
		elif s[j] == '0':
			if j + k < n:
				if s[j + k] == '1':
					flag = False
					break
				s[j + k] = '0'
	for j in range(n - 1, -1, -1):
		if s[j] == '1':
			if j - k >= 0:
				if s[j - k] == '0':
					flag = False
					break
				s[j - k] = '1'
		elif s[j] == '0':
			if j - k >= 0:
				if s[j - k] == '1':
					flag = False
					break
				s[j - k] = '0'
	if not flag:
		print('NO')
		continue
	count = 0
	for j in range(k):
		if s[j] == '1':
			count += 1
		elif s[j] == '0':
			count -= 1
	for j in range(n):
		if s[j] == '?':
			if j - k >= 0:
				if s[j - k] == '1':
					s[j] = '1'
					count += 1
				elif s[j - k] == '0':
					s[j - k] = '0'
					count -= 1
				continue
			if count > 0:
				s[j] = '0'
				count -= 1
			else:
				s[j] = '1'
				count += 1
	count = 0
	for j in range(k):
		if s[j] == '1':
			count += 1
		else:
			count -= 1
	if count:
		print('No')
		continue
	for j in range(k, n):
		if s[j - k] == '1':
			count -= 1
		else:
			count += 1
		if s[j] == '1':
			count += 1
		else:
			count -= 1
		if count:
			flag = False
			break
	if not flag:
		print('No')
	else:
		print('Yes')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	st = list(input())
	ans = 'YES'
	(c0, c1, cq) = (0, 0, 0)
	for i in range(k, n):
		h = i % k
		if st[h] == '?' and st[i] != '?':
			st[h] = st[i]
		elif st[h] != '?':
			if st[h] != st[i] and st[i] != '?':
				ans = 'NO'
				break
	for j in range(k):
		if st[j] == '1':
			c1 += 1
		elif st[j] == '0':
			c1 -= 1
		else:
			cq += 1
	if cq < abs(c1):
		ans = 'NO'
	print(ans)


q = int(input())
for _ in range(q):
	(n, k) = map(int, input().split())
	s = [x for x in input()]
	flg = True
	for i in range(n - k):
		if s[i] == s[i + k]:
			continue
		elif s[i] == '?':
			s[i] = s[i + k]
		elif s[i + k] == '?':
			s[i + k] = s[i]
		else:
			flg = False
	for i in range(n - 1, k - 1, -1):
		if s[i] == s[i - k]:
			continue
		elif s[i] == '?':
			s[i] = s[i - k]
		elif s[i - k] == '?':
			s[i - k] = s[i]
		else:
			flg = False
	if flg:
		S = ''.join(s)
		S = S[:k]
		cnt0 = S.count('0')
		cnt1 = S.count('1')
		if cnt0 > k // 2 or cnt1 > k // 2:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def NLI(n):
	return [[int(i) for i in input().split()] for i in range(n)]

def NLI_(n):
	return [[int(i) - 1 for i in input().split()] for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def RLI(n=8, a=1, b=10):
	return [random.randint(a, b) for i in range(n)]

def RI(a=1, b=10):
	return random.randint(a, b)

def Rtest(T):
	(case, err) = (0, 0)
	for i in range(T):
		inp = INP()
		(a1, ls) = naive(*inp)
		a2 = solve(*inp)
		if a1 != a2:
			print((a1, a2), inp)
			err += 1
		case += 1
	print('Tested', case, 'case with', err, 'errors')

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()
show_flg = False
show_flg = True
ans = 0
for _ in range(I()):
	ans = 'Yes'
	(n, k) = LI()
	s = input()
	a = []
	d = [2] * k
	for i in range(k):
		x = set(s[i::k])
		if len(x) > 2 or x == set('01'):
			ans = 'No'
			break
		elif '1' in x:
			d[i] = 1
		elif '0' in x:
			d[i] = 0
		a += [s[i::k]]
	for i in a:
		if len(set(i)) > 2:
			ans = 'No'
	if d.count(0) > k // 2 or d.count(1) > k // 2:
		ans = 'No'
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	bad = False
	for i in range(n - 1, -1, -1):
		if i >= k:
			if s[i - k] == '?':
				s[i - k] = s[i]
			elif s[i] != '?':
				if s[i] != s[i - k]:
					bad = True
					break
	for i in range(n):
		if i + k < n:
			if s[i + k] == '?':
				s[i + k] = s[i]
			elif s[i] != '?':
				if s[i] != s[i + k]:
					bad = True
					break
	o = s[:k].count('1')
	q = s[:k].count('?')
	if o <= k // 2 <= o + q and (not bad):
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	g = []
	l = 0
	c = 0
	for j in range(k):
		one = 0
		zero = 0
		while j < n:
			if s[j] == '1':
				one = one + 1
			elif s[j] == '0':
				zero = zero + 1
			j = j + k
		if one > 0 and zero > 0:
			c = c + 1
		elif one > 0:
			g.append(1)
			l = l + 1
		elif zero > 0:
			g.append(0)
			l = l + 1
	one = 0
	zero = 0
	for j in range(l):
		if g[j] == 0:
			zero = zero + 1
		else:
			one = one + 1
	if zero > k // 2 or one > k // 2:
		c = c + 1
	if c > 0:
		print('NO')
	else:
		print('YES')


for _ in range(0, int(input())):
	(n, k) = map(int, input().split())
	s = input()
	f = 1
	store = [''] * k
	for i in range(0, n):
		if i < k:
			store[i] = s[i]
		else:
			cur = s[i]
			if store[i % k] != '?' and store[i % k] != cur and (cur != '?'):
				f = 0
				break
			elif cur != '?':
				store[i % k] = cur
	if f == 0:
		print('NO')
	else:
		(one, zero) = (0, 0)
		for i in store:
			if i == '0':
				zero += 1
			if i == '1':
				one += 1
		if zero <= k / 2 and one <= k / 2:
			print('yes')
		else:
			print('No')


import math
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = [i for i in input()]
	if k % 2 == 1:
		print('NO')
		continue
	a = 0
	b = 0
	y = 1
	t = math.ceil(n / k)
	m = l[:k]
	for i in l:
		j = m[a % k]
		if i == '1' and j == '0' or (i == '0' and j == '1'):
			b = 1
			break
		elif (i == '1' or i == '0') and i != j:
			m[a % k] = i
		a += 1
	if b:
		print('NO')
		continue
	(u, v, w) = (0, 0, 0)
	for i in m:
		if i == '1':
			u += 1
		elif i == '0':
			v += 1
		else:
			w += 1
	if u == v and w % 2 == 0:
		y = 1
	elif abs(u - v) == w:
		y = 1
	elif w == n:
		y = 1
	elif abs(u - v) < w and (w - abs(u - v)) % 2 == 0:
		y = 1
	else:
		y = 0
	if y:
		print('YES')
	else:
		print('NO')


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def sumDigits(no):
	return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
MOD = 1000000007
PMOD = 998244353
N = 10 ** 5
T = 1
T = int(stdin.readline())
for _ in range(T):
	(n, k) = list(map(int, stdin.readline().rstrip().split()))
	s = list(stdin.readline().strip('\n'))
	(i, j) = (0, k)
	isOK = True
	while j < n:
		if s[i] == '?' and s[j] == '?':
			i += 1
			j += 1
			continue
		elif s[i] == '?':
			s[i] = s[j]
		elif s[j] == '?':
			s[j] = s[i]
		if s[i] != s[j]:
			isOK = False
			break
		i += 1
		j += 1
	s.reverse()
	(i, j) = (0, k)
	while j < n:
		if s[i] == '?' and s[j] == '?':
			i += 1
			j += 1
			continue
		elif s[i] == '?':
			s[i] = s[j]
		elif s[j] == '?':
			s[j] = s[i]
		if s[i] != s[j]:
			isOK = False
			break
		i += 1
		j += 1
	(oc, zc, qc) = (0, 0, 0)
	for i in range(k):
		oc += s[i] == '1'
		zc += s[i] == '0'
		qc += s[i] == '?'
	if abs(oc - zc) > qc or abs(oc - zc) % 2 != qc % 2:
		isOK = False
	if isOK:
		print('YES')
	else:
		print('NO')


from math import *
from bisect import *
from collections import Counter, defaultdict, deque
I = lambda : int(input())
M = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
for _ in range(I()):
	(n, k) = M()
	s = input()
	s = list(s)
	flag = 1
	z = 0
	o = 0
	for i in range(0, k):
		if s[i] == '0':
			z += 1
		elif s[i] == '1':
			o += 1
	if z > k // 2 or o > k // 2:
		print('NO')
		continue
	else:
		z = k // 2 - z
		o = k // 2 - o
		j = 0
		for i in range(k, n):
			if s[j] == '?':
				if s[i] != '?':
					if s[i] == '1' and o != 0:
						j += 1
						o -= 1
						continue
					elif s[i] == '0' and z != 0:
						j += 1
						z -= 1
						continue
					else:
						flag = 0
						break
			elif s[i] == '?':
				s[i] = s[j]
			elif s[i] != s[j]:
				flag = 0
				break
			j += 1
		if flag:
			print('YES')
		else:
			print('NO')


def Test():
	t = int(input())
	while t > 0:
		t -= 1
		soln()

def soln():
	nk = list(map(int, input().strip().split(' ')))
	n = nk[0]
	k = nk[1]
	b = input()
	a = []
	for i in range(len(b)):
		a.append(b[i])
	if k % 2 != 0:
		print('NO')
		return
	c1 = 0
	c0 = 0
	cnt = 0
	for i in range(k):
		if a[i] == '0':
			c0 += 1
		elif a[i] == '1':
			c1 += 1
		else:
			cnt += 1
	hl = k // 2
	if c0 > hl or c1 > hl:
		print('NO')
		return
	i = 0
	while i + k < n:
		if a[i] != '?' and a[i + k] != '?' and (a[i] != a[i + k]):
			print('NO')
			return
		elif a[i] != '?' and a[i + k] == '?':
			a[i + k] = a[i]
		elif a[i] == '?' and a[i + k] != '?':
			a[i] = a[i + k]
			if a[i] == '0':
				c0 += 1
			else:
				c1 += 1
			if c0 > hl or c1 > hl:
				print('NO')
				return
		i += 1
	print('YES')
Test()


t = int(input())
while t:
	t -= 1
	(n, k) = tuple(map(int, input().split()))
	s = list(input())
	p = s[:k]
	(e, q) = (0, 0)
	for i in p:
		if i == '1':
			e += 1
		elif i == '?':
			q += 1
	if e > k // 2 or e + q < k // 2:
		print('NO')
		continue
	if e == k // 2:
		for i in range(k):
			if p[i] == '?':
				p[i] = '0'
	if e + q == k // 2:
		for i in range(k):
			if p[i] == '?':
				p[i] = '1'
	(i, b) = (1, False)
	while i < n // k + int(n % k) and (not b):
		for j in range(i * k, min(i * k + k, len(s))):
			if s[j] == p[j % k] or s[j] == '?':
				continue
			elif p[j % k] == '?':
				p[j % k] = s[j]
			else:
				b = True
				break
		i += 1
	(e, q) = (0, 0)
	for i in p:
		if i == '1':
			e += 1
		elif i == '?':
			q += 1
	if e > k // 2 or e + q < k // 2:
		b = True
	if not b:
		print('YES')
	else:
		print('NO')


def func_name(x, y, z):
	z = list(z)
	temp = 0
	temp1 = 0
	for i in range(y):
		if z[i] == '?':
			for j in range(i + y, x, y):
				if z[j] != '?':
					z[i] = z[j]
					break
	for i in range(y):
		if z[i] != '?':
			for j in range(i + y, x, y):
				if z[j] == '?':
					z[j] = z[i]
				elif z[j] != z[i]:
					return 'NO'
	for i in range(y):
		if z[i] == '1':
			temp += 1
		if z[i] == '0':
			temp1 += 1
	if temp <= y / 2 and temp1 <= y / 2:
		return 'YES'
	else:
		return 'NO'
t = int(input())
for i in range(t):
	temp = list(map(int, input().split()))
	x = temp[0]
	y = temp[1]
	z = input()
	answer = func_name(x, y, z)
	print(answer)


def z(m, k, l, p):
	for i in range(l, l + k):
		if m[i] == -1:
			m[i] = p
for t in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	m = [0] * n
	for i in range(n):
		if s[i] == '1':
			m[i] = 1
		if s[i] == '?':
			m[i] = -1
	(a, b, c, e) = (0, 0, 0, 1)
	for i in range(k):
		if m[i] == 0:
			a += 1
		elif m[i] == 1:
			b += 1
		else:
			c += 1
	e = 1
	while e > 0:
		(a, b, c, e) = (0, 0, 0, 0)
		for i in range(k):
			if m[i] == 0:
				a += 1
			elif m[i] == 1:
				b += 1
			else:
				c += 1
		for l in range(n - k):
			if abs(a - b) > c:
				e = -1
				print('NO')
				break
			elif c > 0 and abs(a - b) == c:
				e += c
				if a > b:
					z(m, k, l, 1)
					b = a
				else:
					z(m, k, l, 0)
					a = b
				c = 0
			if m[l] == -1:
				if m[l + k] == 0:
					m[l] = 0
					e += 1
					a += 1
					c -= 1
				if m[l + k] == 1:
					m[l] = 1
					e += 1
					b += 1
					c -= 1
			elif m[l + k] == -1:
				if m[l] == 0:
					m[l + k] = 0
					e += 1
				if m[l] == 1:
					m[l + k] = 1
					e += 1
			elif m[l] != m[l + k]:
				e = -1
				print('NO')
				break
		if n == k and abs(a - b) > c:
			e = -1
			print('NO')
		m = m[::-1]
	else:
		if e == 0:
			print('YES')


import sys
input = lambda : sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
	(N, K) = map(int, input().split())
	S = input()
	X = [-1] * K
	for (i, s) in enumerate(S):
		ii = i % K
		if s == '?':
			continue
		if s == '0':
			if X[ii] == 1:
				print('NO')
				break
			X[ii] = 0
		elif s == '1':
			if X[ii] == 0:
				print('NO')
				break
			X[ii] = 1
	else:
		if max(X.count(1), X.count(0)) <= K // 2:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	ch = 1
	for i in range(k, n):
		if s[i] == '?' and s[i - k] == '?':
			continue
		if s[i] == '?':
			s[i] = s[i - k]
		if s[i - k] == '?':
			s[i - k] = s[i]
		if s[i] != s[i - k]:
			ch = 0
			break
	for i in range(n - k - 1, -1, -1):
		if s[i] == '?' and s[i + k] == '?':
			continue
		if s[i] == '?':
			s[i] = s[i + k]
		if s[i + k] == '?':
			s[i + k] = s[i]
		if s[i] != s[i + k]:
			ch = 0
			break
	one = 0
	q = 0
	for i in range(k):
		if s[i] == '1':
			one += 1
		elif s[i] == '0':
			one -= 1
		else:
			q += 1
	one = abs(one)
	if one > q:
		ch = 0
	if ch == 1:
		print('YES')
	else:
		print('NO')


T = int(input())
while T > 0:
	(n, k) = map(int, input().split())
	arr = list(input())
	form = True
	for i in range(k):
		index = i
		common = ''
		not_assign = []
		while index < n:
			if arr[index] == '?':
				if common != '':
					arr[index] = common
				else:
					not_assign.append(index)
			elif common == '':
				common = arr[index]
			elif arr[index] != common:
				form = False
				break
			index += k
		if form is False:
			break
		if common != '':
			for x in not_assign:
				arr[x] = common
	if form is False:
		print('NO')
	else:
		ones = 0
		zeros = 0
		marks = 0
		for i in range(k):
			if arr[i] == '0':
				zeros += 1
			elif arr[i] == '1':
				ones += 1
			else:
				marks += 1
		diff = abs(zeros - ones)
		if diff > marks:
			print('NO')
		else:
			print('YES')
	T -= 1


import sys
read = lambda : sys.stdin.readline().strip()
readi = lambda : map(int, read().split())
for _ in range(int(read())):
	(n, k) = readi()
	nums = list(read())
	substr = ['?'] * k
	for i in range(k):
		for j in range(i, n, k):
			if nums[j] != '?':
				if substr[i] == '?':
					substr[i] = nums[j]
				elif substr[i] != nums[j]:
					break
		else:
			continue
		break
	else:
		ones = substr.count('1')
		zeros = substr.count('0')
		qs = k - zeros - ones
		if abs(ones - zeros) <= qs:
			print('YES')
			continue
	print('NO')


t = int(input())
for ii in range(t):
	(n, k) = [int(i) for i in input().split()]
	s = list(input())
	a = 0
	b = 0
	flag = True
	for i in range(n - k):
		if s[i] != '?' and s[i + k] == '?':
			s[i + k] = s[i]
	for i in range(k):
		if s[i] == '0':
			a += 1
		elif s[i] == '1':
			b += 1
	if a > k // 2 or b > k // 2:
		flag = False
	else:
		for i in range(k, n):
			if s[i - k] == '0':
				a -= 1
			if s[i - k] == '1':
				b -= 1
			if s[i] == '0':
				a += 1
			if s[i] == '1':
				b += 1
			if a > k // 2 or b > k // 2:
				flag = False
				break
			elif s[i] != '?' and s[i - k] != '?':
				if s[i] != s[i - k]:
					flag = False
					break
	if flag == False:
		print('NO')
	else:
		print('YES')


from collections import Counter
T = int(input())
for t in range(T):
	(n, k) = [int(x) for x in input().split()]
	s = input()
	decided = ['?'] * k
	breakouter = False
	for (i, c) in enumerate(s):
		windex = i % k
		if c != '?':
			if decided[windex] not in ['?', c]:
				print('NO')
				breakouter = True
				break
			else:
				decided[windex] = c
	if breakouter:
		continue
	freq = Counter(decided)
	for char in freq:
		if char == '?':
			continue
		if freq[char] > k // 2:
			print('NO')
			break
	else:
		print('YES')


t = int(input())
for o in range(t):
	(n, k) = map(int, input().strip().split())
	s = input()
	numOccurances = {'-1': 0, '1': 0, '0': 0}
	can = True
	for i in range(k):
		char = '-1'
		pos = i
		while pos < n:
			if char != '-1' and s[pos] != '?' and (s[pos] != char):
				can = False
			elif char == '-1' and s[pos] != '?':
				char = s[pos]
			pos += k
		if can == False:
			break
		numOccurances[char] += 1
	if numOccurances['1'] > k / 2 or numOccurances['0'] > k / 2:
		can = False
	if can:
		print('YES')
	else:
		print('NO')


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdarr = lambda : map(int, stdstr().split())
mod = 1000000007
for _ in range(stdint()):
	(n, k) = stdarr()
	s = input()
	s = list(s)
	pos = True
	if n == k:
		one = n // 2 - s.count('1')
		zer = n // 2 - s.count('0')
		if one >= 0 and zer >= 0 and (one + zer == s.count('?')):
			print('YES')
		else:
			print('NO')
	else:
		for i in range(n):
			if s[i] != '?' and s[i % k] != '?' and (s[i] != s[i % k]):
				pos = False
				break
			elif s[i] == '?' and s[i % k] == '?':
				continue
			elif s[i] != '?' and s[i % k] == '?':
				s[i % k] = s[i]
			else:
				s[i] = s[i % k]
		if not pos:
			print('NO')
			continue
		c = Counter(s[:k])
		if c['1'] > k // 2 or c['0'] > k // 2:
			print('NO')
		else:
			print('YES')


import sys
import bisect
from bisect import bisect_right
import math
from sys import stdin, stdout

def sieve(N):
	primeNumbers = [True] * (N + 1)
	primeNumbers[0] = False
	primeNumbers[1] = False
	i = 2
	while i * i <= N:
		j = i
		if primeNumbers[j]:
			while j * i <= N:
				primeNumbers[j * i] = False
				j += 1
		i += 1
	return primeNumbers

def getPrime(N):
	primes = sieve(N)
	result = []
	for i in range(len(primes)):
		if primes[i]:
			result.append(i)
	return result

def factor(N):
	factors = []
	i = 1
	while i * i <= N:
		if N % i == 0:
			factors.append(i)
			if i != N // i:
				factors.append(N // i)
		i += 1
	return sorted(factors)

def gcd(a, b):
	if a < b:
		return gcd(b, a)
	if b == 0:
		return a
	return gcd(b, a % b)

def extendedGcd(a, b):
	if a < b:
		return extendedGcd(b, a)
	if b == 0:
		return [a, 1, 0]
	res = extendedGcd(b, a % b)
	x = res[2]
	y = res[1] - math.floor(a / b) * res[2]
	res[1] = x
	res[2] = y
	return res

def iterativeModularFunc(a, b, c):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % c
		a = a * a % c
		b = b // 2
	return res

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_int():
	return int(sys.stdin.readline())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def isBalanced(i, j, k):
	return

def Solution(s, n, k):
	s = [c for c in s]
	flag = False
	for i in range(k):
		temp = s[i]
		j = i + k
		while j < n:
			if temp == '0' and s[j] == '1':
				flag = True
				break
			elif temp == '1' and s[j] == '0':
				flag = True
				break
			elif temp == '?' and s[j] != '?':
				temp = s[j]
			j += k
		if flag:
			break
		s[i] = temp
	if flag:
		print('NO')
		return
	else:
		one = 0
		zero = 0
		questionMark = 0
		for i in range(k):
			if s[i] == '0':
				zero += 1
			elif s[i] == '1':
				one += 1
			else:
				questionMark += 1
		if zero > k // 2 or one > k // 2:
			print('NO')
		else:
			print('YES')

def main():
	for _ in range(get_int()):
		(n, k) = get_ints_in_variables()
		s = get_string()
		Solution(s, n, k)
main()


def check(s, n, k):
	a = ['?'] * k
	for i in range(n):
		if s[i] != '?':
			if a[i % k] == '?':
				a[i % k] = s[i]
			elif a[i % k] != s[i]:
				return 'NO'
	c0 = a.count('0')
	c1 = a.count('1')
	if max(c1, c0) > k // 2:
		return 'NO'
	return 'YES'
for it in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	s = input()
	print(check(s, n, k))


t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	a = list(input())
	ans = 'YES'
	for i in range(0, k):
		o = 0
		z = 0
		x = 0
		for j in range(i, n, k):
			if a[j] == '0':
				z += 1
			elif a[j] == '1':
				o += 1
			else:
				x += 1
		if o != 0 and z != 0:
			ans = 'NO'
			break
		if z != 0:
			a[i] = '0'
		elif o != 0:
			a[i] = '1'
	o = 0
	z = 0
	x = 0
	for i in range(0, k):
		if a[i] == '0':
			z += 1
		elif a[i] == '1':
			o += 1
		else:
			x += 1
	if z > k // 2 or o > k // 2:
		ans = 'NO'
	print(ans)


def main():
	(n, k) = map(int, input().split())
	line = input()
	mass = ['?' for _ in range(k)]
	for i in range(0, n):
		if mass[i % k] == '?':
			mass[i % k] = line[i]
		elif line[i] != '?':
			if mass[i % k] != line[i]:
				print('NO')
				return
	one = 0
	zero = 0
	questionmark = 0
	for i in mass:
		if i == '0':
			zero += 1
		elif i == '1':
			one += 1
		else:
			questionmark += 1
	if zero <= one <= zero + questionmark or one <= zero <= one + questionmark:
		print('YES')
		return
	print('NO')
t = int(input())
for i in range(t):
	main()


t = int(input())
for _ in range(t):
	(n, k) = [int(i) for i in input().split()]
	s = input()
	check = False
	s = [i for i in s]
	for i in range(k):
		d = {'1': 0, '0': 0, '?': 0}
		position = i
		while position < n:
			d[s[position]] += 1
			position += k
		buf = 'sosi'
		if d['1'] != 0 and d['0'] != 0:
			print('NO')
			check = True
			break
		elif d['1'] != 0:
			buf = '1'
		elif d['0'] != 0:
			buf = '0'
		if buf != 'sosi' and d['?'] != 0:
			count = i
			while count < n:
				s[count] = buf
				count += k
	if not check:
		one = 0
		zero = 0
		for i in range(k):
			if s[i] == '0':
				zero += 1
			if s[i] == '1':
				one += 1
			if one > k / 2 or zero > k / 2:
				print('NO')
				break
		else:
			print('YES')


import sys
import math
from collections import *
from queue import *
from bisect import *
MOD = int(1000000000.0 + 7)
inf = float('inf')
ninf = float('-inf')
input = sys.stdin.readline

def ii():
	return list(map(int, input().strip().split()))
for _ in range(int(input())):
	(n, k) = ii()
	s = list(input().strip())
	(ar, done) = ([], 0)
	for j in range(k):
		sg = '?'
		for p in range(j, n, k):
			if s[p] != '?':
				if sg == '?':
					sg = s[p]
				elif sg != s[p]:
					done = 1
					print('NO')
					break
		ar.append(sg)
		if done:
			break
	if not done:
		c1 = ar.count('1')
		c0 = ar.count('0')
		cq = k - c1 - c0
		if c1 == c0:
			if cq % 2 == 0:
				print('YES')
			else:
				print('NO')
		else:
			pi = abs(c1 - c0)
			cq -= pi
			if cq >= 0 and cq % 2 == 0:
				print('YES')
			else:
				print('NO')


import math
for t in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = input()
	a = []
	a[:0] = s
	has = []
	ans = True
	count = 0
	total = 0
	for i in range(k):
		has.append('?')
		for j in range(i, n, k):
			if ans:
				if has[i] != '?' and a[j] != '?' and (has[i] != a[j]):
					ans = False
				if has[i] == '?':
					has[i] = a[j]
		if ans and has[i] != '?':
			a[i] = has[i]
	for i in range(k):
		if a[i] == '1':
			total += 1
		if a[i] == '0':
			total -= 1
		if a[i] == '?':
			count += 1
	if abs(total) > count:
		ans = False
	if ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	d = list(map(int, input().split()))
	n = d[0]
	k = d[1]
	s = input()
	mas = [s[i] for i in range(n)]
	flag = 0
	for i in range(k, n):
		if mas[i] == '?':
			mas[i] = mas[i - k]
	for i in range(n - k - 1, -1, -1):
		if mas[i] == '?':
			mas[i] = mas[i + k]
	for i in range(n - k):
		if mas[i] != mas[i + k]:
			flag = 1
			break
	sum = 0
	p = 0
	for i in range(k):
		if mas[i] == '1':
			sum += 1
		if mas[i] == '?':
			p += 1
	if sum > k // 2 or sum + p < k // 2:
		flag = 1
	if flag == 1:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.readline
import heapq
import copy
import math
import decimal

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def insr2():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
ans = 0

def pr_list(a):
	print(*a, sep=' ')

def swap(a, b):
	temp = a
	a = b
	b = temp
	return [a, b]

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	ans = 0
	stack = []
	hashm = {}
	arr = []
	heapq.heapify(arr)
	for test in range(tests):
		[n, k] = inlt()
		s = insr()
		hashm = {}
		hashm['1'] = 0
		hashm['0'] = 0
		hashm['?'] = 0
		present = []
		flag = 0
		h = k // 2
		for i in range(n - k):
			if s[i] != s[i + k]:
				if s[i] == '?':
					s[i] = s[i + k]
				elif s[i + k] == '?':
					s[i + k] = s[i]
				else:
					flag = 1
					break
		if flag == 1:
			print('NO')
			continue
		for i in range(k):
			hashm[s[i]] = hashm[s[i]] + 1
		if abs(hashm['1'] - hashm['0']) > hashm['?']:
			flag = 1
		for i in range(n - k):
			if abs(hashm['1'] - hashm['0']) > hashm['?']:
				flag = 1
			hashm[s[i]] = hashm[s[i]] - 1
			hashm[s[i + k]] = hashm[s[i + k]] + 1
		if flag == 1:
			print('NO')
			continue
		else:
			print('YES')
main()


import math
from decimal import *
import random
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	arr = ['?' for i in range(k)]
	(ones, zeroes, qs) = (0, 0, 0)
	ok = True
	for i in range(n):
		if s[i] != arr[i % k] and arr[i % k] == '?':
			arr[i % k] = str(s[i])
		elif s[i] != arr[i % k] and s[i] == '?':
			pass
		elif s[i] != arr[i % k] and arr[i % k] != '?':
			ok = False
	if ok:
		for i in range(k):
			if arr[i] == '1':
				ones += 1
			elif arr[i] == '0':
				zeroes += 1
			elif arr[i] == '?':
				qs += 1
		if ones < zeroes:
			mn = min(zeroes - ones, qs)
			ones += mn
			qs -= mn
		elif ones > zeroes:
			mn = min(ones - zeroes, qs)
			zeroes += mn
			qs -= mn
		if ones != zeroes or qs % 2 == 1:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


from collections import Counter
for i in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	s = list(s)
	flag = True
	a = Counter(s)
	for j in range(k):
		temp = ''
		o = True
		for p in range(j, n, k):
			if s[p] != '?' and o == True:
				o = False
				temp = s[p]
			if o == False:
				if s[p] != '?':
					if s[p] != temp:
						print('NO')
						flag = False
						break
				else:
					s[p] = temp
		if flag == False:
			break
		else:
			s[j] = temp
	count1 = 0
	count0 = 0
	if flag == True:
		for j in range(n):
			if j % k == 0:
				count1 = 0
				count0 = 0
			if s[j] == '1':
				count1 += 1
			elif s[j] == '0':
				count0 += 1
			if count0 > k // 2 or count1 > k // 2:
				print('NO')
				flag = False
				break
		if flag == True:
			print('YES')


import sys
r = sys.stdin.readline
for _ in range(int(r())):
	(n, k) = map(int, r().split())
	s = r()
	(x, y) = (0, 0)
	for i in range(k):
		(ones, zeros) = (0, 0)
		for j in range(i, n, k):
			ones += s[j] == '1'
			zeros += s[j] == '0'
		if min(ones, zeros) > 0:
			print('NO')
			break
		x += ones > 0
		x -= zeros > 0
		y += ones == zeros == 0
	else:
		print('YES' if abs(x) <= y else 'NO')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	s = list(input())
	temp = [-1 for i in range(m)]
	flag = 1
	for i in range(n):
		if temp[i % m] == -1:
			if s[i] == '1':
				temp[i % m] = 1
			elif s[i] == '0':
				temp[i % m] = 0
		elif s[i] == '?':
			s[i] = temp[i % m]
		elif int(s[i]) != temp[i % m]:
			flag = 0
			break
	balance = 0
	t = 0
	for i in range(m):
		if temp[i] == -1:
			t += 1
		elif temp[i] == 1:
			balance += 1
	if balance > m // 2 or balance + t < m // 2:
		flag = 0
	if flag == 0:
		print('NO')
	else:
		print('YES')


import sys, collections
tc = int(sys.stdin.readline())
for _ in range(tc):
	(n, k) = map(int, sys.stdin.readline().split())
	arr = list(sys.stdin.readline().rstrip())
	zero = 0
	one = 0
	can = True
	for i in range(k):
		j = i
		comp = ''
		while j < n:
			if arr[j] != '?':
				if comp != '' and arr[j] != comp:
					can = False
					break
				comp = arr[j]
			j += k
		if comp != '':
			if comp == '0':
				zero += 1
			else:
				one += 1
	if max(one, zero) > k // 2:
		can = False
	print('YES' if can else 'NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	(flag, zero, one) = (True, 0, 0)
	for i in range(k):
		letter = None
		for j in range(i, n, k):
			if s[j] != '?':
				if letter and s[j] != letter:
					flag = False
					break
				letter = s[j]
		if letter:
			zero += 1 if letter == '0' else 0
			one += 1 if letter == '1' else 0
	if max(zero, one) > k // 2:
		flag = False
	print('YES' if flag else 'NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
from collections import Counter
from collections import defaultdict as dc
for _ in range(N()):
	(n, k) = RL()
	s = input()
	dic = dc(set)
	for i in range(n):
		dic[i % k].add(s[i])
	flag = True
	(one, zero) = (0, 0)
	for i in range(k):
		if '1' in dic[i] and '0' in dic[i]:
			flag = False
			break
		elif '1' in dic[i]:
			dic[i] = '1'
			one += 1
		elif '0' in dic[i]:
			dic[i] = '0'
			zero += 1
	if not flag:
		print('NO')
	elif one > k >> 1 or zero > k >> 1:
		print('NO')
	else:
		print('YES')


for T in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = list(input())
	l = [[0, 0, 0] for _ in range(k)]
	for i in range(n):
		l[i % k][0] += s[i] == '0'
		l[i % k][1] += s[i] == '1'
		l[i % k][2] += s[i] == '?'
		if l[i % k][0] != 0 and l[i % k][1] != 0:
			print('NO')
			break
	else:
		(q, z, o) = (0, 0, 0)
		for i in range(k):
			z += 1 if l[i % k][0] else 0
			o += 1 if l[i % k][1] else 0
			q += 1 if l[i % k][2] and (not l[i % k][0]) and (not l[i % k][1]) else 0
		if abs(z - o) <= q:
			print('YES')
			continue
		for i in range(n):
			if s[i] == '?':
				if l[i % k][0]:
					s[i] = '0'
				else:
					s[i] = '1'
		(z, o) = (0, 0)
		for i in range(k - 1):
			z += s[i] == '0'
			o += s[i] == '1'
		for i in range(k - 1, n):
			z += s[i] == '0'
			o += s[i] == '1'
			if z != o:
				print('NO')
				break
			z -= s[i - k + 1] == '0'
			o -= s[i - k + 1] == '1'
		else:
			print('YES')


import sys
import string
import math
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache
from fractions import Fraction

def mi(s):
	return map(int, s.strip().split())

def lmi(s):
	return list(mi(s))

def tmi(s):
	return tuple(mi(s))

def mf(f, s):
	return map(f, s)

def lmf(f, s):
	return list(mf(f, s))

def js(lst):
	return ' '.join((str(d) for d in lst))

def jsns(lst):
	return ''.join((str(d) for d in lst))

def line():
	return sys.stdin.readline().strip()

def linesp():
	return line().split()

def iline():
	return int(line())

def mat(n):
	matr = []
	for _ in range(n):
		matr.append(linesp())
	return matr

def matns(n):
	mat = []
	for _ in range(n):
		mat.append([c for c in line()])
	return mat

def mati(n):
	mat = []
	for _ in range(n):
		mat.append(lmi(line()))
	return mat

def pmat(mat):
	for row in mat:
		print(js(row))

def solve():
	(_, k) = mi(line())
	S = [c for c in line()]
	for i in range(k):
		ss = None
		pos = True
		for j in range(i, len(S), k):
			if S[j] == '?':
				continue
			if not ss and S[j] != '?':
				ss = S[j]
				S[i] = S[j]
			elif ss and S[j] != ss:
				pos = False
				break
		if not pos:
			print('NO')
			return
	(zero, one, q) = (0, 0, 0)
	for i in range(k):
		if S[i] == '0':
			zero += 1
		if S[i] == '1':
			one += 1
		if S[i] == '?':
			q += 1
	(zero_req, one_req) = (k / 2 - zero, k / 2 - one)
	if zero_req < 0 or one_req < 0:
		print('NO')
		return
	print('YES')

def main():
	t = iline()
	for _ in range(t):
		solve()
main()


import sys
ii = lambda : sys.stdin.readline().strip()
idata = lambda : [int(x) for x in ii().split()]

def solve():
	(n, k) = idata()
	data = list(map(str, ii()))
	slov = {}
	for i in range(k):
		slov[i] = '?'
	for i in range(n):
		if data[i] != '?':
			if slov[i % k] != '?':
				if slov[i % k] != data[i]:
					print('NO')
					return
			else:
				slov[i % k] = data[i]
	for i in range(n):
		data[i] = slov[i % k]
	if data[:k].count('?') == 0:
		if data[:k].count('1') == data[:k].count('0'):
			print('YES')
			return
	elif data[:k].count('1') <= k // 2 and data[:k].count('0') <= k // 2:
		print('YES')
		return
	print('NO')
	return
for t in range(int(ii())):
	solve()


def solve(n, k, l):
	s = list(l)
	for i in range(k):
		if s[i] == '?':
			for j in range(i + k, n, k):
				if s[j] != '?':
					s[i] = s[j]
					break
	for i in range(k):
		if s[i] != '?':
			for j in range(i + k, n, k):
				if s[j] == '?':
					s[j] = s[i]
				elif s[i] != s[j]:
					return 'NO'
	z = 0
	o = 0
	for i in range(k):
		if s[i] == '0':
			z += 1
		elif s[i] == '1':
			o += 1
	if o <= k // 2 and z <= k // 2:
		return 'YES'
	else:
		return 'NO'
t = int(input())
while t:
	(n, k) = map(int, input().split(' '))
	in_ = input()
	print(solve(n, k, in_))
	t -= 1


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def solve():
	(a, b) = mints()
	s = minp()
	r = [2] * b
	ss = [0] * a
	for i in range(len(s)):
		c = s[i]
		if c == '?':
			ss[i] = 2
		elif c == '0':
			ss[i] = 0
			r[i % b] = 0
		else:
			ss[i] = 1
			r[i % b] = 1
	c = [0, 0, 0]
	for i in r:
		c[i] += 1
	for i in range(b):
		if r[i] == 2:
			if c[0] < c[1]:
				r[i] = 0
				c[0] += 1
			else:
				r[i] = 1
				c[1] += 1
	if c[0] != c[1]:
		return False
	for i in range(len(s)):
		if ss[i] != 2 and r[i % b] != ss[i]:
			return False
	return True
for i in range(mint()):
	print(['NO', 'YES'][solve()])


import sys
import math, bisect
sys.setrecursionlimit(10 ** 5)
from collections import defaultdict
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict

def I():
	return int(sys.stdin.readline())

def neo():
	return map(int, sys.stdin.readline().split())

def Neo():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
	(n, k) = neo()
	s = list(input())
	(i, o, z, t, r, q, f, g) = (0, 0, 0, 0, 0, 0, 0, 0)
	for i in range(k):
		if s[i] == '0':
			if i - k >= 0 and s[i - k] == '?':
				s[i - k] = '0'
			if i + k < n and s[i + k] == '?':
				s[i + k] = '0'
			z += 1
		if s[i] == '1':
			if i - k >= 0 and s[i - k] == '?':
				s[i - k] = '1'
			if i + k < n and s[i + k] == '?':
				s[i + k] = '1'
			o += 1
		if i + k < n and (s[i] == '0' and s[i + k] == '1' or (s[i] == '1' and s[i + k] == '0')):
			f = 1
	if z > k // 2 or o > k // 2:
		f = 1
	for i in range(k, n):
		if s[i] == '0':
			if i - k >= 0 and s[i - k] == '?':
				s[i - k] = '0'
			if i + k < n and s[i + k] == '?':
				s[i + k] = '0'
			z += 1
		if s[i] == '1':
			if i - k >= 0 and s[i - k] == '?':
				s[i - k] = '1'
			if i + k < n and s[i + k] == '?':
				s[i + k] = '1'
			o += 1
		if s[i - k] == '0':
			z -= 1
		if s[i - k] == '1':
			o -= 1
		if z > k / 2 or o > k / 2:
			f = 1
		if i + k < n and (s[i] == '0' and s[i + k] == '1' or (s[i] == '1' and s[i + k] == '0')):
			f = 1
	(z, o) = (0, 0)
	for i in range(k):
		if s[i] == '0':
			z += 1
		if s[i] == '1':
			o += 1
	if z > k / 2 or o > k / 2:
		f = 1
	for i in range(k, n):
		if s[i] == '0':
			z += 1
		if s[i] == '1':
			o += 1
		if s[i - k] == '0':
			z -= 1
		if s[i - k] == '1':
			o -= 1
		if z > k / 2 or o > k / 2:
			f = 1
	if f == 1:
		print('NO')
	else:
		print('YES')


def solve():
	(N, K) = [int(s) for s in input().split()]
	S = [s for s in input()]
	if S.count('?') >= N - 1:
		return 'YES'
	for k in range(K):
		used = set()
		qu = False
		for i in range(k, N, K):
			if S[i] in ('1', '0'):
				used.add(S[i])
				if len(used) > 1:
					return 'NO'
			else:
				qu = True
		if len(used) > 0 and qu:
			rep = next(iter(used))
			for i in range(k, N, K):
				if S[i] == '?':
					S[i] = rep
	Z = S[:K]
	zz = max(Z.count('0'), Z.count('1'))
	if zz > K // 2:
		return 'NO'
	return 'YES'
T = int(input())
for t in range(T):
	print(solve())


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * (2 * 10 ** 5 + 5)
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(2 * 10 ** 5 + 5):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return

def readTree(n, e):
	adj = [set() for i in range(n + 1)]
	for i in range(e):
		(u1, u2) = IP()
		adj[u1].add(u2)
	return adj
mod = 10 ** 9 + 7

def solve():
	(n, k) = IP()
	s = input()
	li = [-1 for i in range(k)]
	for i in range(0, n, k):
		for j in range(i, min(i + k, n)):
			if s[j] == '1' and li[j % k] == -1:
				li[j % k] = 1
			elif s[j] == '1' and li[j % k] == 0:
				P('NO')
				return
			if s[j] == '0' and li[j % k] == -1:
				li[j % k] = 0
			elif s[j] == '0' and li[j % k] == 1:
				P('NO')
				return
	(cnt_0, cnt_1) = (li.count(0), li.count(1))
	if cnt_0 > k / 2 or cnt_1 > k / 2:
		P('NO')
		return
	P('YES')
	return
t = II()
for i in range(t):
	solve()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input().rstrip())
	flag = True
	for i in range(n - k):
		if s[i] == '?' and s[i + k] == '?':
			continue
		elif s[i] == '?':
			s[i] = s[i + k]
		elif s[i + k] == '?':
			s[i + k] = s[i]
		elif s[i] == s[i + k]:
			continue
		else:
			flag = False
	for i in range(n - k - 1, -1, -1):
		if s[i] == '?' and s[i + k] == '?':
			continue
		elif s[i] == '?':
			s[i] = s[i + k]
		elif s[i + k] == '?':
			s[i + k] = s[i]
		elif s[i] == s[i + k]:
			continue
		else:
			flag = False
	if not flag:
		print('NO')
		continue
	one = zero = 0
	for i in range(k):
		if s[i] == '0':
			zero += 1
		elif s[i] == '1':
			one += 1
	if one * 2 > k or zero * 2 > k:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	fucc = False
	line = []
	for i in range(k):
		now = ''
		for e in range(i, n, k):
			if s[e] != '?':
				now = s[e]
				break
		if now:
			line.append(now)
			for e in range(i, n, k):
				if s[e] != '?' and s[e] != now:
					fucc = True
					break
		else:
			line.append('?')
		if fucc:
			break
	if fucc:
		print('NO')
	else:
		zero = 0
		one = 0
		for i in line:
			if i == '1':
				one += 1
			elif i == '0':
				zero += 1
		if one <= k // 2 and zero <= k // 2:
			print('YES')
		else:
			print('NO')


t = int(input())
while t > 0:
	(n, k) = list(map(int, input().split()))
	c = input()
	sum1 = 0
	ans = 0
	l = [-1] * n
	aone = [0] * n
	azero = [0] * n
	if c[0] == '0':
		azero[0] = 1
	elif c[0] == '1':
		aone[0] = 1
	for i in range(n):
		if c[i] == '1':
			l[i] = 1
		elif c[i] == '0':
			l[i] = 0
	for i in range(k, n):
		p = i - k
		if l[p] == 0:
			if l[i] == 1:
				ans = 1
				break
			elif l[i] == -1:
				l[i] = 0
		if l[p] == 1:
			if l[i] == 0:
				ans = 1
				break
			elif l[i] == -1:
				l[i] = 1
	for i in range(1, n):
		if l[i] == 0:
			azero[i] += azero[i - 1] + 1
			aone[i] += aone[i - 1]
		elif l[i] == 1:
			aone[i] += aone[i - 1] + 1
			azero[i] += azero[i - 1]
		else:
			aone[i] += aone[i - 1]
			azero[i] += azero[i - 1]
	if azero[k - 1] > k // 2 or aone[k - 1] > k // 2:
		ans = 1
	for i in range(k, n):
		p = i - k
		totzero = azero[i] - azero[p]
		totone = aone[i] - aone[p]
		if totzero > k // 2 or totone > k // 2:
			ans = 1
			break
	if ans == 0:
		print('YES')
	else:
		print('NO')
	t -= 1


import random

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = []
	t = input()
	for i in t:
		s.append(i)
	f = 0
	for i in range(k):
		z = 0
		o = 0
		ques = 0
		j = 0
		while i + j < n:
			if s[i + j] == '1':
				o += 1
			elif s[i + j] == '0':
				z += 1
			else:
				ques += 1
			j += k
		if z != 0 and o != 0:
			f = 1
		else:
			j = 0
			while i + j < n:
				if z:
					s[i + j] = '0'
				elif o:
					s[i + j] = '1'
				j += k
	z = 0
	o = 0
	ques = 0
	for i in range(n):
		if i == 0:
			for j in range(k):
				if s[j] == '0':
					z += 1
				elif s[j] == '1':
					o += 1
				else:
					ques += 1
		elif i + k - 1 < n:
			if s[i - 1] == '0':
				z -= 1
			elif s[i - 1] == '1':
				o -= 1
			else:
				ques -= 1
			if s[i + k - 1] == '0':
				z += 1
			elif s[i + k - 1] == '1':
				o += 1
			else:
				ques += 1
		else:
			break
		if o + ques < k // 2 or o > k // 2:
			f = 1
	if f == 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	f = True
	for i in range(n - k):
		if s[i] != '?' and s[i + k] == '?':
			s[i + k] = s[i]
		elif s[i] == '?' and s[i + k] != '?':
			s[i] = s[i + k]
		elif s[i] != '?' and s[i + k] != '?' and (s[i] != s[i + k]):
			f = False
			break
	o = 0
	z = 0
	for i in range(k):
		if s[i] == '1':
			o += 1
		if s[i] == '0':
			z += 1
	x = []
	y = []
	x.append(z)
	y.append(o)
	for i in range(k, n):
		if s[i - k] == '0':
			z -= 1
		elif s[i - k] == '1':
			o -= 1
		if s[i] == '0':
			z += 1
		elif s[i] == '1':
			o += 1
		x.append(z)
		y.append(o)
	for i in range(len(x)):
		if y[i] > k / 2 or x[i] > k / 2:
			f = False
			break
	if f:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, ws) = map(int, input().split())
	st = input()
	s = list(st)
	f = 0
	(start, end) = (0, ws)
	while end < n:
		if s[end] != '?' and s[start] != '?':
			if s[end] != s[start]:
				f = 1
				break
		elif s[end] == '?' and s[start] != '?':
			s[end] = s[start]
		elif s[end] != '?' and s[start] == '?':
			s[start] = s[end]
		end += 1
		start += 1
	if f == 1:
		print('NO')
		continue
	start = n - ws - 1
	end = n - 1
	while start >= 0:
		if s[start] == '?' and s[end] != '?':
			s[start] = s[end]
		elif s[start] != '?' and s[end] == '?':
			s[end] = s[start]
		start -= 1
		end -= 1
	f = 0
	(one, zero) = (0, 0)
	for i in range(ws):
		if s[i] == '1':
			one += 1
		elif s[i] == '0':
			zero += 1
	if one > ws // 2 or zero > ws // 2:
		print('NO')
	else:
		(start, end) = (0, ws)
		f = 0
		while end < n:
			if s[start] == '1':
				one -= 1
			elif s[start] == '0':
				zero -= 1
			if s[end] == '0':
				zero += 1
			elif s[end] == '1':
				one += 1
			if one > ws // 2 or zero > ws // 2:
				f = 1
				break
			start += 1
			end += 1
		if f == 0:
			print('YES')
		else:
			print('NO')


def solve(n, k, s):
	period = [None] * k
	for (i, char) in enumerate(s):
		if char != '?':
			if period[i % k] is None:
				period[i % k] = char
			elif period[i % k] != char:
				return 'NO'
	count1 = count0 = 0
	for char in period:
		count1 += char == '1'
		count0 += char == '0'
	if count1 <= k // 2 and count0 <= k // 2:
		return 'YES'
	return 'NO'
t = int(input())
for _ in range(t):
	(n, k) = [*map(int, input().split())][:2]
	s = input()
	print(solve(n, k, s))


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]

def dec(c):
	global z, o, q
	if c == '0':
		z -= 1
	if c == '1':
		o -= 1
	if c == '?':
		q -= 1

def inc(c):
	global z, o, q
	if c == '0':
		z += 1
	if c == '1':
		o += 1
	if c == '?':
		q += 1
for _ in range(int(data())):
	(n, k) = sp()
	s = list(data())
	half = k // 2
	window = deque()
	o = z = q = 0
	for i in range(k):
		window.append(s[i])
		inc(s[i])
	if z > half or o > half:
		out('NO')
		continue
	r = True
	for i in range(k, n):
		temp = window.popleft()
		dec(temp)
		window.append(s[i])
		inc(s[i])
		if z > half or o > half:
			r = False
			break
		if s[i] != '?' and temp != '?':
			if s[i] != temp:
				r = False
				break
			continue
		if temp == '?':
			continue
		if s[i] == '?':
			s[i] = temp
			window[-1] = temp
			dec('?')
			inc(temp)
			continue
	if not r:
		out('NO')
		continue
	out('YES')


def custsubs(list, n, k):
	l = []
	a = 0
	for a in range(n):
		l.append(list[a:a + k])
		if a + k >= n:
			return l

def count(list, ch):
	cnt = 0
	for i in list:
		if ch == i:
			cnt += 1
	return cnt
T = int(input())
for t in range(T):
	(n, k) = map(int, input().split())
	s = str(input())
	s = list(s)
	if k % 2 != 0:
		print('NO')
	else:
		ans = 0
		for a in range(k):
			for i in range(k + a, n, k):
				if s[a] != '?' and s[i] != '?' and (s[i] != s[a]):
					ans = -1
					print('NO')
					break
				if s[a] == '?' and s[i] != '?':
					s[a] = s[i]
		if ans == 0:
			if count(s[:k], '1') > int(k / 2) or count(s[:k], '0') > int(k / 2):
				print('NO')
			else:
				print('YES')


test_case = int(input())
for i in range(test_case):
	num = input().split(' ')
	LLLL = [0] * 2
	for j in range(2):
		LLLL[j] = int(num[j])
	(n, k) = (LLLL[0], LLLL[1])
	L = [None] * k
	s = input()
	never = False
	for j in range(n):
		if s[j] == '?':
			pass
		elif s[j] == '1':
			if L[j % k] == '1':
				pass
			elif L[j % k] == None:
				L[j % k] = '1'
			else:
				never = True
				break
		elif L[j % k] == '0':
			pass
		elif L[j % k] == None:
			L[j % k] = '0'
		else:
			never = True
			break
	counter_1 = 0
	counter_0 = 0
	for j in range(k):
		if L[j] == '1':
			counter_1 += 1
		if L[j] == '0':
			counter_0 += 1
	if counter_0 > k / 2:
		never = True
	if counter_1 > k / 2:
		never = True
	if never:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	j = 0
	flag = True
	while j < k:
		sett = set()
		for i in range(j, n, k):
			sett.add(s[i])
		if '0' in sett and '1' in sett:
			flag = False
			break
		elif '0' in sett:
			s[j] = '0'
		elif '1' in sett:
			s[j] = '1'
		j += 1
	if flag:
		one = zero = q = 0
		for x in range(k):
			if s[x] == '1':
				one += 1
			elif s[x] == '0':
				zero += 1
			else:
				q += 1
		if abs(one - zero) > q:
			flag = False
	if flag:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
from collections import deque as dq
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(input())
	for i in range(n - k + 1):
		if a[i] in ['0', '1'] and a[i + k] == '?':
			a[i + k] = a[i]
		elif a[i + k] in ['0', '1'] and a[i] == '?':
			a[i] = a[i + k]
	for i in range(n - 1, k - 1, -1):
		if a[i] in ['0', '1'] and a[i - k] == '?':
			a[i - k] = a[i]
		elif a[i - k] in ['0', '1'] and a[i] == '?':
			a[i] = a[i - k]
	(o, z, u) = (0, 0, 0)
	for i in range(k):
		if a[i] == '0':
			z += 1
		elif a[i] == '1':
			o += 1
		else:
			u += 1
	if o <= k // 2 and z <= k // 2:
		ans = True
	else:
		ans = False
	for i in range(k, n):
		if a[i - k] != a[i]:
			ans = ans and False
	if ans:
		print('YES')
	else:
		print('NO')


import sys, os
import math
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def MI():
	return map(int, input().split())

def out(var):
	sys.stdout.write(str(var) + '\n')
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	i = 0
	broke = False
	while i < k:
		p = i
		if broke:
			break
		if s[p] != '?':
			while p + k < n:
				p += k
				if s[p] == s[i] or s[p] == '?':
					pass
				else:
					broke = True
					break
		else:
			while p + k < n:
				p += k
				if s[p] != '?':
					j = p - k
					while j >= 0:
						s[j] = s[p]
						j -= k
					i -= 1
					break
		i += 1
	if broke:
		out('NO')
	else:
		(z, o) = (0, 0)
		for i in range(k):
			if s[i] == '1':
				o += 1
			elif s[i] == '0':
				z += 1
		if z <= k // 2 and o <= k // 2:
			out('YES')
		else:
			out('NO')


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	(n, k) = value()
	s = list(input())
	ok = 'YES'
	c1 = s[:k].count('1')
	c0 = s[:k].count('0')
	if c1 > k // 2 or c0 > k // 2:
		ok = 'NO'
	for i in range(k, n):
		if s[i] == '?' and s[i - k] != '?':
			s[i] = s[i - k]
		if s[i] != '?' and s[i - k] != '?' and (s[i] != s[i - k]):
			ok = 'NO'
		if s[i] == '1':
			c1 += 1
		elif s[i] == '0':
			c0 += 1
		if s[i - k] == '1':
			c1 -= 1
		elif s[i - k] == '0':
			c0 -= 1
		if c1 > k // 2 or c0 > k // 2:
			ok = 'NO'
	print(ok)


import math, sys
input = sys.stdin.readline
S = lambda : input().rstrip()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
mod1 = 1000000007
mod2 = 998244353
for _ in range(I()):
	(n, k) = M()
	s = S()
	f = o = z = 0
	for i in range(k):
		s1 = ''
		for j in range(i, n, k):
			s1 += s[j]
		if '1' in s1 and '0' in s1:
			f = 1
			break
		elif '1' in s1:
			o += 1
		elif '0' in s1:
			z += 1
		else:
			o += 1
			z += 1
	print('YES' if f == 0 and o >= k // 2 and (z >= k // 2) else 'NO')


from collections import Counter

def good(s, k):
	n = len(s)
	x = [None] * k
	for i in range(n):
		if s[i] != '?':
			if x[i % k] is None:
				x[i % k] = s[i]
			elif x[i % k] != s[i]:
				return False
	c = Counter(x)
	return abs(c['1'] - c['0']) <= c[None]
tc = int(input())
for _ in range(tc):
	(_, k) = map(int, input().split())
	s = input()
	print('YES' if good(s, k) else 'NO')


def modinv(n, p):
	return pow(n, p - 2, p)

def main():
	for case in range(int(input())):
		(n, k) = [int(x) for x in input().split()]
		s = input()
		if n % k != 0:
			s += '?' * (k - n % k)
		n = len(s)
		base = [x for x in s[:k]]
		not_possible = False
		for j in range(k, n, k):
			for t in range(j, j + k):
				if s[t] == '?':
					continue
				elif base[t - j] == '?':
					base[t - j] = s[t]
				elif base[t - j] != s[t]:
					not_possible = True
					break
				else:
					continue
		if not_possible:
			print('NO')
		elif base.count('0') <= k // 2 and base.count('1') <= k // 2:
			print('YES')
		else:
			print('NO')
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
from os import path
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
input = sys.stdin.readline
t = input()
t = int(t)

def solve(n, k, s):
	shat = [s[i] for i in range(k)]
	for i in range(k):
		for j in range(i, n, k):
			if shat[i] != '?' and s[j] != '?' and (s[j] != shat[i]):
				print('NO')
				return
			elif shat[i] == '?' and s[j] != shat[i]:
				shat[i] = s[j]
			else:
				pass
	n1 = 0
	n0 = 0
	for i in range(k):
		if shat[i] == '1':
			n1 += 1
		elif shat[i] == '0':
			n0 += 1
	if n1 > k // 2 or n0 > k // 2:
		print('NO')
		return
	print('YES')
	return
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	solve(n, k, s)


t = int(input())
while t != 0:
	(n, k) = map(int, input().split())
	s = list(input())
	d = {}
	for i in range(k):
		d[i] = []
	for i in range(k):
		j = i
		while j < n:
			d[i].append(s[j])
			j += k
	f = 0
	for i in d.keys():
		d[i] = list(set(d[i]))
		d[i].sort()
		if '1' in d[i]:
			if '0' in d[i]:
				print('NO')
				f = 1
				break
		if f == 1:
			break
	if f == 1:
		t -= 1
		continue
	(z, o, que) = (0, 0, 0)
	for i in d.keys():
		if d[i][0] == '1':
			o += 1
		if d[i][0] == '0':
			z += 1
		if d[i][0] == '?':
			que += 1
	if z == o:
		print('YES')
	elif que > 0 and que >= abs(z - o):
		print('YES')
	else:
		print('NO')
	t -= 1


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = [x for x in input()]
	ans = True
	for i in range(k):
		j = i + k
		while j < n:
			if s[i] == '?':
				s[i] = s[j]
			elif s[i] != s[j] and s[j] != '?':
				ans = False
				break
			j += k
	on = [0, 0, 0]
	for el in s[:k]:
		if el == '0':
			on[0] += 1
		elif el == '1':
			on[1] += 1
		else:
			on[2] += 1
	if abs(on[0] - on[1]) > on[2]:
		ans = False
	print('YES' if ans else 'NO')


from fractions import Fraction
import bisect
import os
import io
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
from queue import Queue

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def solve(s, n, k):
	s = list(s)
	for i in range(k, n):
		val1 = s[i]
		val2 = s[i - k]
		if val1 == '?' and val2 == '?':
			continue
		elif val1 == '?':
			s[i] = val2
		elif val2 == '?':
			s[i - k] = val1
		elif val1 != val2:
			return False
	count0 = 0
	count1 = 0
	for i in range(n):
		if i % k == 0:
			count0 = 0
			count1 = 0
		if s[i] == '1':
			count1 += 1
		elif s[i] == '0':
			count0 += 1
		if count1 > k / 2 or count0 > k / 2:
			return False
	return True
t = 1
t = iinput()
for i in range(t):
	(n, k) = rinput()
	s = input()
	print(['NO', 'YES'][solve(s, n, k)])


for i in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(input())
	count = 0
	Kcount = 0
	ind = 0
	T = True
	if n % k != 0:
		for i in range(n - n % k):
			a.append('?')
		n += n - n % k
	x = []
	for i in range(n // k):
		x.append(a[i * k:i * k + k])
	for i in range(k):
		o = 0
		z = 0
		for j in range(len(x)):
			if x[j][i] == '1':
				o += 1
			if x[j][i] == '0':
				z += 1
		if z > 0 and o > 0:
			T = False
			break
		elif a[i] == '?':
			if z > 0:
				a[i] = '0'
			if o > 0:
				a[i] = '1'
	x = a[0:k]
	zer = x.count('0')
	one = x.count('1')
	que = x.count('?')
	for i in range(k):
		if a[i] == '?':
			if zer >= one:
				one += 1
			elif zer < one:
				zer += 1
	if one != zer:
		T = False
	if T:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	A = input()
	B = [[] for j in range(k)]
	for j in range(n):
		if A[j] != '?':
			B[j % k].append(A[j])
	ng = 0
	zo = [0, 0]
	for j in range(k):
		if len(set(B[j])) > 1:
			ng = 1
			break
		elif len(B[j]) > 0:
			zo[int(B[j][0])] += 1
	if zo[0] > k // 2 or zo[1] > k // 2:
		ng = 1
	print('NO' if ng == 1 else 'YES')


from sys import stdin
input = stdin.readline

def e667():
	t = int(input())
	for _ in range(t):
		(n, k) = map(int, input().split())
		x = sorted(list(map(int, input().split())))
		y = list(map(int, input().split()))
		mostSaved = [0] * (n + 1)
		savedFromStart = [0] * n
		(i, j) = (0, 0)
		prevX = -1
		while i < n:
			if x[i] == prevX:
				savedFromStart[i] = savedFromStart[i - 1]
			else:
				prevX = x[i]
				while j < n and x[j] <= x[i] + k:
					j += 1
				savedFromStart[i] = j - i
			i += 1
		for i in range(n - 1, -1, -1):
			mostSaved[i] = max(mostSaved[i + 1], savedFromStart[i])
		out = [savedFromStart[i] + mostSaved[min(n, i + savedFromStart[i])] for i in range(n)]
		print(max(out))

def c668():
	t = int(input())
	for _ in range(t):
		(n, k) = map(int, input().split())
		s = list(input().rstrip())
		b = ['?'] * k
		truth = True
		for i in range(n):
			c = s[i]
			if c != b[i % k]:
				if b[i % k] == '?':
					b[i % k] = c
				elif c != '?':
					truth = False
					break
		if not truth:
			print('NO')
		else:
			arr = [0, 0]
			for i in b:
				if i == '1':
					arr[1] += 1
				elif i == '0':
					arr[0] += 1
			if max(arr) <= k // 2:
				print('YES')
			else:
				print('NO')
c668()


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
file = 1

def solve():
	(n, k) = mi()
	s = list(si())
	for i in range(n):
		if s[i] != '?' and s[i % k] == '?':
			s[i % k] = s[i]
	for i in range(n):
		if s[i] != '?' and s[i % k] != '?' and (s[i] != s[i % k]):
			print('NO')
			return
	k1 = k // 2
	(cnt0, cnt1) = (0, 0)
	for i in range(k):
		if s[i] == '1':
			cnt1 += 1
		if s[i] == '0':
			cnt0 += 1
	if cnt1 > k1 or cnt0 > k1:
		print('NO')
		return
	print('YES')
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
for _ in range(ii()):
	solve()


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	gg = input()
	s = list(gg)
	flag = 1
	for i in range(k):
		x = 0
		y = 0
		for j in range(i, n, k):
			if s[j] == '1':
				x = 1
			if s[j] == '0':
				y = 1
		if x == 1 and y == 1:
			flag = 0
		elif x == 1:
			s[i] = '1'
		elif y == 1:
			s[i] = '0'
	if flag == 0:
		print('NO')
	else:
		x = 0
		y = 0
		tt = 0
		for i in range(k):
			if s[i] == '0':
				x += 1
			elif s[i] == '1':
				y += 1
			else:
				tt += 1
		if x == y or abs(x - y) <= tt:
			print('YES')
		else:
			print('NO')


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	(n, r) = R()
	a = list(S())
	v = ['?'] * r
	flg = True
	for i in range(n):
		if v[i % r] == '?':
			v[i % r] = a[i]
		elif v[i % r] != a[i] != '?':
			flg = False
			break
	if not flg:
		print('NO')
		continue
	d = {'1': 0, '0': 0, '?': 0}
	for i in v:
		d[i] += 1
	if abs(d['1'] - d['0']) % 2 != d['?'] % 2 or abs(d['1'] - d['0']) > d['?']:
		print('NO')
	else:
		print('YES')


from sys import stdin

class Problem:

	def __init__(self, n, k, s):
		self.n = n
		self.k = k
		self.a = list(s)

	def solve(self):
		le = 0
		ri = 0
		count = {'0': 0, '1': 0, '?': 0}
		while ri < k:
			count[self.a[ri]] += 1
			ri += 1
		while le <= n - k:
			if count['0'] * 2 > k:
				return 'NO'
			if count['1'] * 2 > k:
				return 'NO'
			if ri < n and self.a[le] != self.a[ri]:
				count[self.a[le]] -= 1
				if self.a[ri] == '?':
					self.a[ri] = self.a[le]
				elif self.a[le] != '?':
					return 'NO'
				count[self.a[ri]] += 1
			ri += 1
			le += 1
		return 'YES'
cases = int(stdin.readline())
for test in range(cases):
	(n, k) = list(map(int, stdin.readline().split()))
	a = stdin.readline().strip()
	ans = Problem(n, k, a).solve()
	print(ans)


def ii():
	return int(input())

def li():
	return [int(i) for i in input().split()]
for t in range(ii()):
	(n, k) = li()
	S = input()
	s = []
	for i in S:
		s.append(i)
	q = 0
	one = 0
	zer = 0
	flag = 1
	for i in range(k):
		if s[i] == '1':
			one += 1
		elif s[i] == '0':
			zer += 1
	if one > k // 2 or zer > k // 2:
		print('NO')
	else:
		for i in range(k):
			if s[i] == '?':
				lol = -1
				for j in range(i + k, n, k):
					if lol == -1:
						if s[j] != '?':
							lol = s[j]
					elif s[j] != lol and s[j] != '?':
						flag = 0
						break
				if flag == 1:
					if lol == '1':
						one += 1
					elif lol == '0':
						zer += 1
				else:
					break
			else:
				for j in range(i, n, k):
					if s[i] != s[j] and s[j] != '?':
						flag = 0
						break
				if flag == 0:
					break
		if flag == 0 or one > k // 2 or zer > k // 2:
			print('NO')
		else:
			print('YES')


def solve(n, k, a):
	f = True
	for pos in range(k):
		for i in range(pos, n, k):
			if a[i] != '?':
				for j in range(pos, n, k):
					if a[j] == '?':
						a[j] = a[i]
					elif a[j] != a[i]:
						f = False
						break
				break
	sm = 0
	for i in range(k):
		if a[i] != '?':
			sm += int(a[i])
	for i in range(k):
		if sm * 2 < k:
			if a[i] == '?':
				a[i] = '1'
				sm += 1
		elif a[i] == '?':
			a[i] = '0'
	if sm * 2 != k:
		f = False
	test = a[:k] * (n // k) * 2
	for i in range(n // k * 2):
		if a[i] != test[i] and a[i] != '?':
			f = False
			break
	if f == False:
		return 'NO'
	return 'YES'

def main():
	t = int(input())
	for i in range(t):
		(n, k) = [int(i) for i in input().split()]
		s = [i for i in input()]
		print(solve(n, k, s))
main()


import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

def solve(srr, k):
	console('----- solving ------')
	console(srr, k)
	ones = 0
	zeroes = 0
	for i in range(k):
		sett = set(srr[i::k])
		if '1' in sett and '0' in sett:
			return 'NO'
		if '1' in sett:
			ones += 1
		if '0' in sett:
			zeroes += 1
	if ones > k // 2 or zeroes > k // 2:
		return 'NO'
	return 'YES'

def console(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
	return
for case_num in range(int(input())):
	(_, k) = list(map(int, input().split()))
	srr = input()
	res = solve(srr, k)
	print(res)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	ar = list(input())
	flag = True
	for i in range(k):
		val = '?'
		for j in range(i, n, k):
			if val == '?':
				if ar[j] != '?':
					val = ar[j]
			elif ar[j] != '?' and val != ar[j]:
				flag = False
				break
		if val != '?':
			for j in range(i, n, k):
				ar[j] = val
	if flag:
		val = 0
		what = 0
		for i in range(k):
			if ar[i] == '1':
				val += 1
			elif ar[i] == '0':
				val -= 1
			else:
				what += 1
		xx = what - abs(val)
		if xx >= 0 and xx % 2 == 0:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	s = input()
	l = [''] * k
	works = True
	for i in range(n):
		c = s[i]
		if c != '?':
			if l[i % k] == c or l[i % k] == '':
				l[i % k] = c
			else:
				works = False
				break
	if works:
		smol = 0
		big = k
		for c in l:
			if c == '0':
				big -= 1
			elif c == '1':
				smol += 1
		goal = k // 2
		if smol <= goal <= big:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


import math
from collections import defaultdict
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = str(input())
	s = list(s)
	an = True
	c1 = 0
	c0 = 0
	cq = 0
	for i in range(k):
		x1 = 0
		x0 = 0
		xq = 0
		for j in range(i, n, k):
			if s[j] == '1':
				x1 += 1
			elif s[j] == '0':
				x0 += 1
			else:
				xq += 1
		if x1 > 0 and x0 > 0:
			an = False
			break
		elif x1 > 0:
			c1 += 1
		elif x0 > 0:
			c0 += 1
		else:
			cq += 1
	if c1 > k // 2 or c0 > k // 2:
		an = False
	if an == True:
		print('Yes')
	else:
		print('No')


from collections import defaultdict as dd
import sys
input = sys.stdin.readline
t = int(input())
while t:
	(n, k) = map(int, input().split())
	l = list(input().strip())
	i = 0
	j = k
	lol = 0
	while j < n:
		if l[i] == l[j]:
			i += 1
			j += 1
		elif l[i] == '?':
			l[i] = l[j]
		elif l[j] == '?':
			l[j] = l[i]
		else:
			lol = 1
			break
	cou1 = 0
	cou0 = 0
	for i in range(k):
		if l[i] == '1':
			cou1 += 1
		elif l[i] == '0':
			cou0 += 1
	i = 0
	j = k - 1
	while j < n:
		if cou1 > k // 2 or cou0 > k // 2:
			lol = 1
			break
		if l[i] == '0':
			cou0 -= 1
		if l[i] == '1':
			cou1 -= 1
		i += 1
		j += 1
		if j < n:
			if l[j] == '1':
				cou1 += 1
			if l[j] == '0':
				cou0 += 1
	if lol:
		print('NO')
	else:
		print('YES')
	t -= 1


import sys
from collections import defaultdict as dd
from collections import deque
from functools import *
from fractions import Fraction as f
from copy import *
from bisect import *
from heapq import *
from itertools import permutations, product

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def inc(d, c, x=1):
	d[c] = d[c] + x if c in d else x

def bo(i):
	return ord(i) - ord('A')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j):
	return 0 <= i < n and 0 <= j < n

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()
while t > 0:
	t -= 1
	(n, k) = mi()
	s = si()
	a = []
	flag = 0
	d = []
	c = 0
	for i in range(k):
		z = o = 0
		for j in range(i, n, k):
			if s[j] == '0':
				z += 1
			elif s[j] == '1':
				o += 1
		if z > 0 and o > 0:
			flag = 1
			break
		if z + o == 0:
			continue
		d.append(-1 if z > 0 else 1)
	p = d.count(1)
	q = d.count(-1)
	if max(p, q) > (k + 1) // 2:
		flag = 1
	print('YES' if flag == 0 else 'NO')


def kBalanced(s):
	freq = [0] * 3
	for c in s:
		if c == '0':
			freq[0] += 1
		elif c == '1':
			freq[1] += 1
		else:
			freq[2] += 1
	if freq[0] == freq[1]:
		if freq[2] % 2 == 0:
			return True
		else:
			return False
	elif freq[2] < abs(freq[0] - freq[1]):
		return False
	elif (abs(freq[0] - freq[1]) - freq[2]) % 2 == 0:
		return True
	else:
		return False

def solve(n, k, s):
	k_bal = True
	startIndex = 0
	endIndex = k
	while endIndex < n:
		if s[startIndex] == s[endIndex]:
			pass
		elif s[startIndex] == '?' and s[endIndex] != '?':
			p = startIndex
			while p >= 0:
				s[p] = s[endIndex]
				p -= k
		elif s[startIndex] != '?' and s[endIndex] == '?':
			s[endIndex] = s[startIndex]
		elif s[startIndex] == '?' and s[endIndex] == '?':
			pass
		else:
			k_bal = False
		startIndex += 1
		endIndex += 1
	return kBalanced(s[0:k]) and k_bal

def main():
	t = int(input())
	for _ in range(t):
		(n, k) = [int(x) for x in input().split()]
		s = [c for c in input()]
		assert len(s) == n
		answer = 'YES' if solve(n, k, s) else 'NO'
		print(answer)
main()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	tmp = ['@'] * k
	ans = 1
	has = [0] * 2
	for i in range(0, n + k + 1, k):
		j = 0
		while j < k and i + j < n:
			if s[i + j] != '?' and tmp[j] != '@' and (tmp[j] != s[j + i]):
				ans = 0
				break
			elif s[i + j] != '?' and tmp[j] == '@':
				tmp[j] = s[i + j]
				has[int(s[i + j])] += 1
			j += 1
	if ans:
		if abs(has[0] - has[1]) <= k - has[0] - has[1]:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = mpos(sum_so_far, current_sum)
	return sum_so_far

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum

def graph(n, m):
	adj = dict()
	for i in range(1, n + 1):
		adj.setdefault(i, 0)
	for i in range(m):
		(a, b) = map(int, input().split())
		adj[a] += 1
		adj[b] += 1
	return adj
import sys
import threading
from math import *
sys.setrecursionlimit(300000)
threading.stack_size(10 ** 8)
po = int(log(10 ** 18)) + 2
li = []
for i in range(po):
	li.append(3 ** i)
li = li[::-1]
s = sum(li)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(input())
	f = 1
	for i in range(k, n):
		if a[i] == '?' and a[i - k] == '?':
			continue
		elif a[i] == '?':
			a[i] = a[i - k]
		elif a[i - k] == '?':
			a[i - k] = a[i]
		elif a[i] != a[i - k]:
			f = 0
			break
	for i in range(n - k - 1, -1, -1):
		if a[i] == '?' and a[i + k] == '?':
			continue
		elif a[i] == '?':
			a[i] = a[i + k]
		elif a[i + k] == '?':
			a[i + k] = a[i]
		elif a[i] != a[i + k]:
			f = 0
			break
	(o, q) = (0, 0)
	for i in range(k):
		if a[i] == '1':
			o += 1
		elif a[i] == '0':
			o -= 1
		else:
			q += 1
	o = abs(o)
	if o > q:
		f = 0
	if f:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(str(input()))
	z = 0
	o = 0
	q = 0
	for i in range(k):
		if s[i] == '0':
			o += 1
		elif s[i] == '1':
			z += 1
		else:
			q += 1
	flag = True
	if o <= k // 2 and z <= k // 2:
		for i in range(k):
			p = s[i]
			if p == '0' or p == '1':
				for j in range(1, n // k + 1):
					if i + j * k < n:
						if s[i + j * k] == p or s[i + j * k] == '?':
							pass
						else:
							flag = False
			else:
				for j in range(1, n // k + 1):
					if i + j * k < n:
						if s[i + j * k] == '0' or s[i + j * k] == '1':
							if p == '?':
								p = s[i + j * k]
								s[i] = p
							elif not p == s[i + j * k]:
								flag = False
						else:
							pass
		if flag:
			z = 0
			o = 0
			q = 0
			for i in range(k):
				if s[i] == '0':
					o += 1
				elif s[i] == '1':
					z += 1
				else:
					q += 1
			if o <= k // 2 and z <= k // 2:
				print('YES')
			else:
				print('NO')
		else:
			print('NO')
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	sa = input()
	s = list(sa)
	i = 0
	am = True
	while i < k:
		j = i
		l = []
		while j < n:
			if s[j] not in l:
				l.append(s[j])
			j += k
		if len(l) == 3 or (len(l) == 2 and '?' not in l):
			am = False
			break
		if len(l) == 1:
			i += 1
			continue
		c = '0'
		for I in l:
			if I != '?':
				c = I
				break
		j = i
		while j < n:
			s[j] = c
			j += k
		i += 1
	if not am:
		print('NO')
	elif s[:k].count('0') <= k // 2 and s[:k].count('1') <= k // 2:
		print('YES')
	else:
		print('NO')


import sys
dict_s = {'0': 0, '1': 1, '?': 2}
T = int(sys.stdin.readline())
for _ in range(T):
	(n, k) = map(int, sys.stdin.readline().split())
	list_s = list(sys.stdin.readline().strip())
	res = 'YES'
	for i in range(k):
		list_q = []
		if list_s[i] == '?':
			list_q.append(i)
		key = list_s[i]
		i += k
		while i < n:
			if list_s[i] == '?':
				list_q.append(i)
			elif key == '?':
				key = list_s[i]
			elif key != list_s[i]:
				res = 'NO'
				key = '?'
				break
			i += k
		if key != '?':
			while list_q:
				list_s[list_q.pop()] = key
	if res == 'NO':
		print(res)
		continue
	(l, r) = (0, 0)
	count = [0, 0, 0]
	list_q = []
	for r in range(k):
		count[dict_s[list_s[r]]] += 1
		if list_s[r] == '?':
			list_q.append(r)
	while True:
		if min(count[0], count[1]) + count[2] < max(count[0], count[1]):
			res = 'NO'
			break
		count[dict_s[list_s[l]]] -= 1
		l += 1
		r += 1
		if r >= n:
			break
		count[dict_s[list_s[r]]] += 1
	print(res)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	yep = False
	ans = [''] * k
	for i in range(n):
		if s[i] != '?':
			if ans[i % k] == s[i] or ans[i % k] == '':
				ans[i % k] = s[i]
			else:
				yep = True
				break
	if not yep:
		c0 = ans.count('0')
		c1 = ans.count('1')
		if max(c0, c1) > k // 2:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = [i for i in input()]
	ok = True
	(p1, p0) = ([0 for i in range(n)], [0 for i in range(n)])
	for i in range(k, n):
		if s[i] == s[i - k]:
			continue
		if s[i - k] == '?':
			s[i - k] = s[i]
		elif s[i] == '?':
			s[i] = s[i - k]
		else:
			ok = False
	for i in range(n - k - 1, -1):
		if s[i] == s[i + k]:
			continue
		if s[i + k] == '?':
			s[i + k] = s[i]
		elif s[i] == '?':
			s[i] = s[i + k]
		else:
			ok = False
	for i in range(n):
		if s[i] == '1':
			p1[i] = 1
		elif s[i] == '0':
			p0[i] = 1
		if i:
			p0[i] += p0[i - 1]
			p1[i] += p1[i - 1]
		if i < k - 1:
			continue
		a1 = p1[i]
		a0 = p0[i]
		if i != k - 1:
			a1 -= p1[i - k]
			a0 -= p0[i - k]
		q = k - (a1 + a0)
		dif = abs(a0 - a1)
		if dif > q:
			ok = False
		if q % 2 != dif % 2:
			ok = False
	print('YES' if ok else 'NO')


from collections import Counter

def main():
	(n, k) = map(int, input().split())
	s = list(input())
	for start in range(k):
		c = None
		for i in range(start, len(s), k):
			if s[i] == '?':
				continue
			if c is None:
				c = s[i]
				continue
			if c != s[i]:
				print('NO')
				return
		if c is None:
			continue
		for i in range(start, len(s), k):
			s[i] = c
	cnt = Counter(s[:k])
	if cnt['0'] == cnt['1']:
		print('YES')
		return
	if max(cnt['0'], cnt['1']) - min(cnt['0'], cnt['1']) > cnt['?']:
		print('NO')
		return
	print('YES')
t = int(input())
for _ in range(t):
	main()


from sys import stdin
for _ in range(int(input())):
	(n, k) = map(int, stdin.readline().rstrip().split(' '))
	l = list(stdin.readline().rstrip())
	ze = [0] * n
	on = [0] * n
	a = ['?'] * k
	l1 = l2 = 0
	r = 'YES'
	for i in range(n):
		if a[i % k] == '?':
			a[i % k] = l[i]
		elif l[i] != '?':
			if a[i % k] != l[i]:
				r = 'NO'
				break
	c_1 = c_0 = 0
	for i in range(k):
		if a[i] == '1':
			c_1 += 1
		elif a[i] == '0':
			c_0 += 1
	if c_1 > k // 2 or c_0 > k // 2:
		r = 'NO'
	print(r)


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	arr = stdin.readline()
	brr = []
	arr = arr[:n]
	for i in arr:
		if i == '0':
			brr.append(0)
		elif i == '1':
			brr.append(1)
		else:
			brr.append(-1)
	vis = [0] * n
	counter = 0
	for i in range(n):
		if vis[i] == 0 and brr[i] != -1:
			index = i + k
			vis[i] = 1
			counter = 0
			while index < n and (brr[index] == brr[i] or brr[index] == -1):
				if brr[index] == -1:
					brr[index] = brr[i]
				vis[index] = 1
				index += k
			if index < n:
				counter = 1
				break
	for i in range(n):
		if brr[i] == -1 and i + k < n:
			brr[i] = brr[i + k]
	if counter == 1:
		print('NO')
	else:
		count0 = 0
		count1 = 0
		count3 = 0
		for i in range(0, k):
			if brr[i] == 0:
				count0 += 1
			elif brr[i] == 1:
				count1 += 1
			else:
				count3 += 1
		if max(count0, count1) > k // 2:
			counter = 1
		else:
			counter = 0
		if counter == 1:
			print('NO')
		else:
			count0 = 0
			count1 = 0
			count3 = 0
			p = n - k
			for i in range(p, n):
				if brr[i] == 0:
					count0 += 1
				elif brr[i] == 1:
					count1 += 1
				else:
					count3 += 1
			if max(count0, count1) > k // 2:
				print('NO')
			else:
				print('YES')


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s1 = input()
	s = []
	for j in range(n):
		s.append(s1[j])
	if k % 2 == 1:
		print('NO')
	else:
		l = [0, 0, 0]
		for j in range(k):
			if s[j] == '0':
				l[0] += 1
			elif s[j] == '1':
				l[1] += 1
			else:
				l[2] += 1
		if abs(l[0] - l[1]) > l[2]:
			print('NO')
		else:
			b = 0
			for j in range(k, n):
				if s[j] == s[j - k]:
					continue
				elif s[j] == '?':
					s[j] = s[j - k]
				elif s[j - k] == '?':
					s[j - k] = s[j]
				else:
					print('NO')
					b = 1
					break
			if b == 0:
				for j in range(n - 1, k - 1, -1):
					s[j - k] = s[j]
				l = [0, 0, 0]
				for j in range(k):
					if s[j] == '0':
						l[0] += 1
					elif s[j] == '1':
						l[1] += 1
					else:
						l[2] += 1
				if abs(l[0] - l[1]) > l[2]:
					print('NO')
				else:
					print('YES')


from sys import stdin, stdout
input = stdin.readline
print = lambda x: stdout.write(str(x) + '\n')
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input().strip())
	pos = ['?'] * k
	for i in range(k):
		for j in range(i, n, k):
			if pos[i] == '?' and s[j] != '?':
				pos[i] = s[j]
			elif s[j] != '?' and pos[i] != s[j]:
				print('NO')
				break
		else:
			continue
		break
	else:
		ones = pos.count('1')
		zeros = pos.count('0')
		que = pos.count('?')
		t = abs(ones - zeros)
		if que < t:
			print('NO')
		elif (que - t) % 2:
			print('NO')
		else:
			print('YES')


import sys
sys.setrecursionlimit(10000)
t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	s = input()
	s = list(s)
	possible = True
	init = s[:k]
	for i in range(k, n):
		if s[i] == '?' and s[i - k] != '?':
			s[i] = s[i - k]
		elif s[i] != '?' and s[i - k] == '?':
			s[i - k] = s[i]
		elif s[i] != s[i - k]:
			possible = False
			break
	if possible is False:
		print('NO')
	else:
		init = s[:k]
		count_0 = init.count('0')
		count_1 = init.count('1')
		count_q = init.count('?')
		if abs(count_1 - count_0) > count_q:
			possible = False
		last = s[n - k:]
		count_0 = last.count('0')
		count_1 = last.count('1')
		count_q = last.count('?')
		if abs(count_1 - count_0) > count_q:
			possible = False
		if possible is True:
			print('YES')
		else:
			print('NO')


testcases = int(input())
for testcase in range(testcases):
	temparr = input()
	temparr = temparr.split()
	n = int(temparr[0])
	k = int(temparr[1])
	temparr = input()
	arr = []
	for i in temparr:
		arr.append(i)
	flag = 0
	freq = ['?' for _ in range(k)]
	for (j, i) in enumerate(arr):
		index = j % k
		if freq[index] == '?' and i != '?':
			freq[index] = i
			continue
		if freq[index] == '0' and i == '1':
			flag = 1
			break
		if freq[index] == '1' and i == '0':
			flag = 1
			break
	if flag == 1:
		print('NO')
	else:
		half = k // 2
		total = 0
		qmark = 0
		for i in freq:
			if i == '?':
				qmark += 1
				continue
			total += int(i)
		if total == half:
			print('YES')
			continue
		if total > half:
			print('NO')
			continue
		if total + qmark >= half:
			print('YES')
			continue
		else:
			print('NO')


for nt in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	(o, z) = (0, 0)
	for i in range(k):
		if s[i] == '1':
			o += 1
		elif s[i] == '0':
			z += 1
	if o > k // 2 or z > k // 2:
		print('NO')
		continue
	ans = 'YES'
	for i in range(k, n):
		if s[i] == '?' and s[i - k] != '?':
			s[i] = s[i - k]
		if s[i] != '?' and s[i - k] != '?' and (s[i] != s[i - k]):
			ans = 'NO'
			break
		if s[i] == '1':
			o += 1
		elif s[i] == '0':
			z += 1
		if s[i - k] == '1':
			o -= 1
		elif s[i - k] == '0':
			z -= 1
		if o > k // 2 or z > k // 2:
			ans = 'NO'
			break
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	s = input()
	res = True
	seq = [-1 for i in range(k)]
	for i in range(n):
		j = i % k
		if s[i] == '0':
			if seq[j] == 1:
				res = False
			else:
				seq[j] = 0
		if s[i] == '1':
			if seq[j] == 0:
				res = False
			else:
				seq[j] = 1
	num_0 = 0
	num_1 = 0
	for i in seq:
		if i == 0:
			num_0 += 1
		if i == 1:
			num_1 += 1
	bound = k // 2
	if res and num_0 <= bound and (num_1 <= bound):
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	congruences = [None] * k
	for (i, char) in enumerate(s):
		if char == '?':
			continue
		r = i % k
		if congruences[r] is None:
			congruences[r] = char
		elif char != congruences[r]:
			print('NO')
			break
	else:
		num_0 = congruences.count('0')
		num_1 = congruences.count('1')
		if num_0 > k // 2 or num_1 > k // 2:
			print('NO')
		else:
			print('YES')


import sys
LI = lambda : list(map(int, sys.stdin.readline().split()))
MI = lambda : map(int, sys.stdin.readline().split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline())
for _ in range(II()):
	(n, k) = MI()
	s = list(SI())
	ok = 1
	for i in range(k):
		t = set()
		for j in range(i, n, k):
			t.add(s[j])
		if len({'1', '0'}.intersection(t)) == 2:
			ok = 0
		if s[i] == '?':
			v = t.difference({'?'})
			c = v.pop() if v else '?'
			s[i] = c
	(a, b) = (s[:k].count('1'), s[:k].count('?'))
	v = k // 2
	if a > v or a + b < v:
		ok = 0
	print(['NO', 'YES'][ok])


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	a = list(s)
	ver = True
	for i in range(k):
		curr = a[i]
		if curr == '?':
			for j in range(i + k, n, k):
				if a[j] != '?':
					curr = a[j]
					break
		for j in range(i, n, k):
			if a[j] == '?':
				a[j] = curr
	ver = True
	zero = 0
	one = 0
	for i in range(k):
		if a[i] == '1':
			one += 1
		elif a[i] == '0':
			zero += 1
	if zero > k // 2 or one > k // 2:
		print('NO')
		continue
	for i in range(k):
		if a[i] == '?':
			if zero != k // 2:
				a[i] = '0'
				zero += 1
			else:
				a[i] = '1'
				one += 1
	for i in range(k):
		for j in range(i + k, n, k):
			if a[j] == '?':
				a[j] = a[i]
	zero = 0
	one = 0
	for i in range(k):
		curr = a[i]
		if a[i] == '1':
			one += 1
		else:
			zero += 1
		for j in range(i + k, n, k):
			if a[j] != '?':
				if curr != a[j]:
					ver = False
					break
	if ver and one == zero:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().strip().split())
	s = input().strip()
	ls = list(s)
	flg = True
	cntz = 0
	cnto = 0
	for i in range(k):
		temp = '?'
		for j in range(i, n, k):
			if ls[j] != '?':
				if temp != '?' and ls[j] != temp:
					flg = False
					break
				temp = ls[j]
		if not flg:
			break
		if temp != '?':
			cntz += temp == '0'
			cnto += temp == '1'
	if max(cntz, cnto) > k // 2:
		flg = False
	if flg:
		print('YES')
	else:
		print('NO')


import sys

def checker(x):
	ones = 0
	zeros = 0
	uk = 0
	for i in x:
		if i == '1':
			ones += 1
		elif i == '0':
			zeros += 1
		else:
			uk += 1
	if ones * zeros > 0:
		return 0
	elif ones + zeros == 0:
		return 1
	else:
		if ones > 0:
			for i in range(len(x)):
				x[i] = '1'
		else:
			for i in range(len(x)):
				x[i] = '0'
		return 1
t = int(input())
while t > 0:
	t -= 1
	(n, k) = map(int, sys.stdin.readline().split())
	ar = sys.stdin.readline()
	arr = []
	for x in ar:
		arr.append(x)
	arr.pop()
	cols = n // k + 1
	two_d = []
	for i in range(n % k):
		a = []
		for j in range(cols):
			a.append(arr[j * k + i])
		two_d.append(a)
	for i in range(n % k, k):
		a = []
		for j in range(cols - 1):
			a.append(arr[j * k + i])
		two_d.append(a)
	flag = 1
	for x in two_d:
		flag = checker(x)
		if flag == 0:
			break
	if flag == 0:
		print('NO')
	else:
		first_col = []
		for x in two_d:
			first_col.append(x[0])
		(ones, zeros, uk) = (0, 0, 0)
		for x in first_col:
			if x == '0':
				zeros += 1
			elif x == '1':
				ones += 1
			else:
				uk += 1
		if zeros > k // 2 or ones > k // 2:
			print('NO')
		else:
			print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(input())
	p = 0
	for i in range(0, n - k):
		if a[i] == a[i + k]:
			continue
		elif a[i + k] == '?':
			a[i + k] = a[i]
		elif a[i] == '?':
			a[i] = a[i + k]
		else:
			p = 1
			break
	if p == 1:
		print('NO')
	else:
		count0 = 0
		count1 = 0
		countr = 0
		for i in range(k):
			if a[i] == '0':
				count0 += 1
			elif a[i] == '1':
				count1 += 1
			else:
				countr += 1
		count10 = 0
		count11 = 0
		count1r = 0
		for i in range(n - k, n):
			if a[i] == '0':
				count10 += 1
			elif a[i] == '1':
				count11 += 1
			else:
				count1r += 1
		if abs(count0 - count1) > countr or abs(count10 - count11) > count1r:
			print('NO')
		else:
			print('YES')


def check(n, k, s):
	f = []
	for i in range(k):
		ok = True
		c = '?'
		kk = 0
		while i + kk < n:
			j = i + kk
			if s[j] != '?':
				if c == '?':
					c = s[j]
				elif c != s[j]:
					ok = False
					break
			kk += k
		if not ok:
			return 'NO'
		f.append(c)
	su = 0
	q = 0
	for i in range(k):
		if f[i] == '1':
			su += 1
		elif f[i] == '?':
			q += 1
	if k / 2 - su >= 0 and k / 2 - su <= q:
		return 'YES'
	else:
		return 'NO'
T = int(input())
for t in range(T):
	st = input().split()
	n = int(st[0])
	k = int(st[1])
	s = list(input())
	print(check(n, k, s))


import sys
from math import ceil, factorial, gcd
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
MOD = 10 ** 9 + 7
INF = float('inf')
rl = lambda : list(map(int, sys.stdin.readline().split()))
rs = lambda : sys.stdin.readline().strip()
for _ in range(int(input())):
	(n, k) = rl()
	s = rs()
	arr = []
	flag = True
	for i in range(k):
		si = set(s[i::k])
		si.discard('?')
		if len(si) == 2:
			print('NO')
			flag = False
			break
		elif len(si) == 1:
			arr.append(si.pop())
	if flag:
		(one, zero) = (arr.count('1'), arr.count('0'))
		if one <= k // 2 and zero <= k // 2:
			print('YES')
		else:
			print('NO')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	l = list(s)
	(o, z) = (0, 0)
	for i in range(k):
		if l[i] == '0':
			z += 1
		if l[i] == '1':
			o += 1
	ch = 1
	if o > k // 2 or z > k // 2:
		ch = 0
	for i in range(k, n):
		if l[i] == '?':
			if l[i - k] != '?':
				l[i] = l[i - k]
			if i + k < n:
				if l[i + k] != '?':
					if l[i] == '?':
						l[i] = l[i + k]
					elif l[i] != l[i + k]:
						ch = 0
		if l[i - k] == '0':
			z -= 1
		if l[i - k] == '1':
			o -= 1
		if l[i] == '0':
			z += 1
		if l[i] == '1':
			o += 1
		if z > k // 2 or o > k // 2:
			ch = 0
		if l[i] != '?':
			if l[i - k] == '?':
				l[i - k] = l[i]
			elif l[i] != l[i - k]:
				ch = 0
			if i + k < n:
				if l[i + k] == '?':
					l[i + k] = l[i]
				elif l[i] != l[i + k]:
					ch = 0
	if ch == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	s = list(s)
	if k % 2 == 1:
		print('NO')
		continue
	ans = 'YES'
	for i in range(k):
		se = set(s[i::k])
		se.discard('?')
		if len(se) == 2:
			ans = 'NO'
		if len(se) == 1:
			s[i] = se.pop()
	if ans == 'NO':
		print('NO')
		continue
	one = zero = 0
	for x in s[:k]:
		if x == '1':
			one += 1
		if x == '0':
			zero += 1
	k //= 2
	if one <= k and zero <= k:
		print('YES')
	else:
		print('NO')


import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	flag = 0
	one = 0
	ze = 0
	q = 0
	for j in range(n):
		if s[j] == '0':
			ze += 1
		elif s[j] == '1':
			one += 1
		else:
			q += 1
		if j < k - 1:
			continue
		diff = one - ze
		if j == k - 1:
			if abs(diff) > q:
				flag = 1
				break
			if abs(diff) == q:
				if diff > 0:
					term = '0'
				else:
					term = '1'
				for kk in range(j + 1):
					if s[j] == '?':
						s[j] = term
		elif s[j] == '1':
			if s[j - k] == '0':
				flag = 1
				break
			s[j - k] = '1'
		elif s[j] == '0':
			if s[j - k] == '1':
				flag = 1
				break
			s[j - k] = '0'
		elif s[j - k] == '0':
			s[j] = '0'
		elif s[j - k] == '1':
			s[j] = '1'
	if flag == 1:
		print('NO')
		continue
	ze = 0
	one = 0
	q = 0
	for j in range(n):
		if s[j] == '0':
			ze += 1
		elif s[j] == '1':
			one += 1
		else:
			q += 1
		if j < k - 1:
			continue
		if j == k - 1:
			pass
		elif s[j - k] == '0':
			ze -= 1
		elif s[j - k] == '1':
			one -= 1
		else:
			q -= 1
		diff = abs(one - ze)
		if diff > q:
			print('NO')
			break
	else:
		print('YES')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	z = input()
	s = []
	for i in z:
		s.append(i)
	lis = [-1 for i in range(k)]
	f1 = 0
	for i in range(n):
		if s[i] == '?':
			pass
		elif s[i] == '0':
			v1 = i % k
			if lis[v1] == -1 or lis[v1] == '0':
				lis[v1] = '0'
			else:
				f1 = 1
				break
		elif s[i] == '1':
			v1 = i % k
			if lis[v1] == -1 or lis[v1] == '1':
				lis[v1] = '1'
			else:
				f1 = 1
				break
	if f1 == 1:
		print('NO')
	else:
		for i in range(n):
			v1 = i % k
			if lis[v1] != -1:
				s[i] = lis[v1]
		v2 = k // 2
		(ze, on) = (0, 0)
		for i in range(k):
			if s[i] == '0':
				ze += 1
			elif s[i] == '1':
				on += 1
		for i in range(0, n - k, 1):
			if ze > v2 or on > v2:
				f1 = 1
				break
			else:
				if s[i] == '0' and ze > 0:
					ze -= 1
				elif s[i] == '1' and on > 0:
					on -= 1
				if s[i + k] == '0':
					ze += 1
				elif s[i + k] == '1':
					on += 1
		if f1 == 1 or on > v2 or ze > v2:
			print('NO')
		else:
			print('YES')


from sys import stdin, stdout

def input():
	a = stdin.readline()
	if a[-1] == '\n':
		a = a[:-1]
	return a

def print(*argv, end='\n', sep=' '):
	n = len(argv)
	for i in range(n):
		if i == n - 1:
			stdout.write(str(argv[i]))
		else:
			stdout.write(str(argv[i]) + sep)
	stdout.write(end)
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def comb(lst, x):
	return list(c(lst, x))

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	lst = []
	for i in range(n):
		temp = [default] * m
		lst.append(temp)
	return lst

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

def smaller(lst, x):
	return bisect_left(lst, x) - 1

def smallerEq(lst, x):
	return bisect_right(lst, x) - 1

def solve(n, k, lst):
	val = {}
	for i in range(n):
		if lst[i] != '?':
			if val.get(i % k) == None:
				val[i % k] = lst[i]
			elif lst[i] != val[i % k]:
				return 'NO'
	for i in range(n):
		if val.get(i):
			if lst[i] == '?':
				lst[i] = val[i % k]
			elif lst[i] != val[i % k]:
				return 'aNO'
	lstS = ''.join(lst[:k])
	q = lstS.count('?')
	o = lstS.count('1')
	z = lstS.count('0')
	if abs(o - z) > q:
		return 'NO'
	for i in range(1, n - k):
		if lst[i - 1] == '?':
			q -= 1
		elif lst[i - 1] == '1':
			o -= 1
		else:
			z -= 1
		if lst[i + k - 1] == '?':
			q += 1
		elif lst[i + k - 1] == '1':
			o += 1
		else:
			z += 1
		if abs(o - z) > q:
			return 'cNO'
	return 'YES'
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	lst = list(input())
	print(solve(n, k, lst))


for _ in range(int(input())):
	(n, ws) = map(int, input().split())
	st = input()
	s = list(st)
	res = ['?' for i in range(ws)]
	f = 0
	(zero, one) = (0, 0)
	for i in range(ws):
		cur = i
		while cur < n:
			if res[i] == '?':
				res[i] = s[cur]
				if s[cur] != '?':
					if s[cur] == '1':
						one += 1
					else:
						zero += 1
			elif res[i] != '?':
				if s[cur] != '?':
					if s[cur] != res[i]:
						f = 1
						break
			cur += ws
	if f == 1 or one > ws // 2 or zero > ws // 2:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	arr = list(input())
	one = [False] * k
	zero = [False] * k
	for i in range(n):
		if arr[i] == '1':
			one[i % k] = True
		elif arr[i] == '0':
			zero[i % k] = True
	oneAm = 0
	zeroAm = 0
	questAm = 0
	fl = True
	for i in range(k):
		if one[i] and zero[i]:
			fl = False
			break
		elif one[i]:
			oneAm += 1
		elif zero[i]:
			zeroAm += 1
		else:
			questAm += 1
	if not fl:
		print('No')
		continue
	dif = abs(oneAm - zeroAm)
	if questAm >= dif:
		print('YES')
	else:
		print('NO')


def main():
	(n, k) = map(int, input().split())
	s = input()
	lst = ['2' for _ in range(k)]
	for i in range(n):
		if s[i] == '?':
			continue
		elif s[i] != lst[i % k] and lst[i % k] != '2':
			print('NO')
			return
		else:
			lst[i % k] = s[i]
	cnt0 = lst.count('0')
	cnt1 = lst.count('1')
	if cnt0 > k // 2 or cnt1 > k // 2:
		print('NO')
	else:
		print('YES')
t = int(input())
for _ in range(t):
	main()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = str(input())
	l = list(s)
	(f, zero, one, que) = (0, 0, 0, 0)
	temp = -1
	for i in range(k):
		for j in range(i, n, k):
			if s[j] != '?':
				if temp != -1:
					if temp != int(s[j]):
						f = 1
				temp = int(s[j])
		if temp == 0:
			zero += 1
		elif temp == 1:
			one += 1
		temp = -1
	if max(zero, one) > k // 2:
		print('NO')
	elif f == 1:
		print('NO')
	else:
		print('YES')


from sys import stdin, stdout

class SOLVE:

	def CHECK(self, n, k, s, check, cmd):
		for j in range(k):
			for l in range(j, n, k):
				if s[l] != '?':
					if cmd == 'build':
						check[j] = s[l]
						break
					elif check[j] != s[l]:
						return 0
		if cmd == 'check':
			if check.count('0') > k // 2 or check.count('1') > k // 2:
				return 0
			else:
				return 1

	def solve(self):
		R = stdin.readline
		W = stdout.write
		ans = []
		for i in range(int(input())):
			(n, k) = [int(x) for x in R().split()]
			s = R()
			check = [-1 for j in range(k)]
			self.CHECK(n, k, s, check, 'build')
			ret = self.CHECK(n, k, s, check, 'check')
			ans.append(['NO', 'YES'][ret])
		W('\n'.join(ans))

def main():
	s = SOLVE()
	s.solve()
main()


for aww in range(int(input())):
	(a, b) = [*map(int, input().split())]
	c = [i for i in input()]
	d = 0
	e = 0
	f = 0
	g = b // 2
	gg = False
	for i in range(b, a):
		if c[i] == '?':
			c[i] = c[i - b]
	for i in range(a - 1, b - 1, -1):
		if c[i - b] == '?':
			c[i - b] = c[i]
	for i in range(b):
		if c[i] == '1':
			d += 1
		elif c[i] == '0':
			e += 1
		else:
			f += 1
	if abs(d - e) > f:
		gg = True
		print('No')
	if gg == False:
		for i in range(b, a):
			if c[i] != c[i - b]:
				gg = True
				print('No')
				break
	if gg == False:
		print('YES')


t = int(input())
while t:
	(n, k) = map(int, input().split())
	s = input()
	a = [None] * k
	f = 0
	for i in range(n):
		if s[i] == '?':
			continue
		elif s[i] == '1':
			if a[i % k] == None or a[i % k] == 1:
				a[i % k] = 1
			else:
				f = 1
				break
		elif a[i % k] == None or a[i % k] == 0:
			a[i % k] = 0
		else:
			f = 1
			break
	one = 0
	zer = 0
	non = 0
	for i in range(len(a)):
		if a[i] == 1:
			one += 1
		elif a[i] == 0:
			zer += 1
		else:
			non += 1
	a = max(one, zer, non)
	if a == one:
		if a > zer + non:
			f = 1
	elif a == zer:
		if a > one + non:
			f = 1
	if f == 1:
		print('NO')
	else:
		print('YES')
	t -= 1


import sys, os, io
input = sys.stdin.readline
T = int(input())
ans = ['YES'] * T
for t in range(T):
	(n, k) = map(int, input().split())
	S = input()
	lis = ['?'] * k
	for i in range(k):
		for j in range(i, n, k):
			if lis[i] == '?':
				lis[i] = S[j]
			elif lis[i] == '0':
				if S[j] == '1':
					ans[t] = 'NO'
			elif S[j] == '0':
				ans[t] = 'NO'
	p = 0
	A = 0
	for s in lis:
		if s == '1':
			p += 1
		elif s == '?':
			A += 1
	a = k // 2 - p
	if not 0 <= a <= A:
		ans[t] = 'NO'
print(*ans, sep='\n')


for i in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = list(input())
	b = True
	for i in range(n):
		if i + k < n:
			if s[i] != '?':
				if s[i + k] != s[i] and s[i + k] != '?':
					b = False
					print('NO')
					break
				else:
					s[i + k] = s[i]
		if i - k >= 0:
			if s[i] != '?':
				if s[i - k] != s[i] and s[i - k] != '?':
					b = False
					print('NO')
					break
				else:
					s[i - k] = s[i]
	if b:
		cnto = 0
		cntz = 0
		for j in range(k):
			if s[j] == '0':
				cntz += 1
			elif s[j] == '1':
				cnto += 1
		if cnto > k / 2 or cntz > k / 2:
			print('NO')
			continue
		for j in range(k, n):
			cntz -= 1 if s[j - k] == '0' else 0
			cnto -= 1 if s[j - k] == '1' else 0
			cnto += 1 if s[j] == '1' else 0
			cntz += 1 if s[j] == '0' else 0
			if cnto > k / 2 or cntz > k / 2:
				b = False
				print('NO')
				break
			if not b:
				break
		if b:
			print('YES')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	t = ''
	for i in range(k):
		(zero, one, ques) = (False, False, False)
		for j in range(i, n, k):
			if s[j] == '0':
				zero = True
			elif s[j] == '1':
				one = True
			else:
				ques = True
		if zero == True and one == True:
			break
		if zero == True:
			t += '0'
		elif one == True:
			t += '1'
		else:
			t += '?'
	if len(t) != k:
		print('NO')
	else:
		(zero, one, ques) = (0, 0, 0)
		for i in range(k):
			if t[i] == '0':
				zero += 1
			elif t[i] == '1':
				one += 1
			else:
				ques += 1
		if zero > one + ques or one > zero + ques:
			print('NO')
		else:
			print('YES')


t = int(input())
from collections import Counter
ans = []
for _ in range(t):
	(n, k) = map(int, input().split())
	b = list(input())
	do = False
	cnt0 = 0
	cnt1 = 0
	for i in range(k):
		bb = set((b[i] for i in range(i, n, k)))
		if '0' in bb and '1' in bb:
			ans.append('NO')
			do = True
			break
		if '0' in bb:
			cnt0 += 1
		if '1' in bb:
			cnt1 += 1
	if not do:
		if cnt0 <= k // 2 and cnt1 <= k // 2:
			ans.append('YES')
		else:
			ans.append('NO')
for ai in ans:
	print(ai)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	sl = list(s)
	hatena_p = []
	for i in range(n - k):
		poped = sl[i]
		new = sl[i + k]
		if poped == '?' and new == '?':
			hatena_p.append((i, i + k))
		elif poped == new:
			pass
		elif poped == '0' and new == '1':
			print('NO')
			break
		elif poped == '1' and new == '0':
			print('NO')
			break
		elif poped == '?':
			sl[i] = new
		elif new == '?':
			sl[i + k] = poped
	else:
		for (pf, pb) in hatena_p[::-1]:
			sl[pf] = sl[pb]
		cnt_zero = 0
		cnt_one = 0
		for i in range(k):
			if sl[i] == '0':
				cnt_zero += 1
			elif sl[i] == '1':
				cnt_one += 1
		if cnt_zero > k // 2 or cnt_one > k // 2:
			print('NO')
		else:
			print('YES')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = [i for i in input().strip()]
	ok = True
	current = s[:k]
	for i in range(k):
		seen = set()
		for j in range(i, n, k):
			if s[j] == '0':
				seen.add(0)
			if s[j] == '1':
				seen.add(1)
		if len(seen) == 1:
			current[i] = str(seen.pop())
		elif len(seen) == 2:
			ok = False
			break
	one_count = current.count('1')
	zero_count = current.count('0')
	question_count = current.count('?')
	if min(one_count, zero_count) + question_count < max(one_count, zero_count):
		ok = False
	print('YES' if ok else 'NO')


import sys
input = sys.stdin.readline
t = int(input())
for t1 in range(t):
	(n, k) = list(map(int, input().split(' ')))
	s = input()
	s = s.strip()
	z = 0
	o = 0
	l = s
	s = []
	for i in range(n):
		s.append(l[i])
	rz = k / 2 - z
	ro = k / 2 - o
	from collections import deque
	for i in range(k):
		if s[i] == '0':
			z += 1
		elif s[i] == '1':
			o += 1
	f = 0
	if z > k / 2 or o > k / 2:
		f = 1
	for i in range(k, n):
		if s[i - k] == s[i]:
			continue
		elif s[i - k] != '?' and s[i] != '?':
			f = 1
		elif s[i - k] == '1' and s[i] == '?':
			s[i] = '1'
		elif s[i - k] == '0' and s[i] == '?':
			s[i] = '0'
	z = 0
	o = 0
	for i in range(k):
		if s[i] == '0':
			z += 1
		elif s[i] == '1':
			o += 1
	if z > k / 2 or o > k / 2:
		f = 1
	for i in range(k, n):
		if s[i - k] == '0':
			z -= 1
		elif s[i - k] == '1':
			o -= 1
		if s[i] == '0':
			z += 1
		elif s[i] == '1':
			o += 1
		if z > k / 2 or o > k / 2:
			f = 1
	if f == 1:
		print('NO')
	else:
		print('YES')


t = int(input())
while t > 0:
	t = t - 1
	[n, k] = input().split()
	status = 0
	n = int(n)
	k = int(k)
	s = input()
	a = [-1] * k
	for i in range(0, n):
		q = i % k
		if s[i] == '?':
			pass
		elif a[q] == -1:
			a[q] = int(s[i])
		elif a[q] != int(s[i]):
			status = 1
			break
		else:
			pass
	if status == 1:
		print('NO')
	else:
		count1 = 0
		count2 = 0
		for i in a:
			if i == 0:
				count1 += 1
			if i == 1:
				count2 += 1
		if (count1 <= k / 2) & (count2 <= k / 2):
			print('YES')
		else:
			print('NO')


from math import ceil
from collections import Counter
import sys

def main():
	t = Iint()
	for _ in range(t):
		(n, k) = Imap()
		s = Istr()
		if k == n:
			sc = Counter(s)
			if sc['0'] < sc['1'] and sc['0'] + sc['?'] < sc['1']:
				print('NO')
				continue
			elif sc['0'] > sc['1'] and sc['1'] + sc['?'] < sc['0']:
				print('NO')
				continue
			else:
				print('YES')
				continue
		substring_count = ceil(n / k)
		box = [-1] * k
		for j in range(substring_count):
			start = k * j
			debug(start)
			length = min(k, n - start)
			for i in range(length):
				b1 = box[i]
				bw = s[start + i]
				if b1 == -1:
					box[i] = bw
				elif b1 == '?':
					box[i] = bw
				elif b1 == bw:
					continue
				elif b1 == '0' and bw == '1':
					break
				elif b1 == '1' and bw == '0':
					break
			else:
				continue
			break
		else:
			sc = Counter(box)
			if sc['0'] < sc['1'] and sc['0'] + sc['?'] < sc['1']:
				print('NO')
				continue
			elif sc['0'] > sc['1'] and sc['1'] + sc['?'] < sc['0']:
				print('NO')
				continue
			else:
				print('YES')
				continue
		print('NO')
en = enumerate

def debug(*args, c=6):
	pass
FIO = 10
BUFSIZE = 8 * 1024

def Istr():
	return input()

def Iint():
	return int(input())

def Ilist():
	return list(map(int, input().split()))

def Imap():
	return map(int, input().split())

def Plist(li, s=' '):
	print(s.join(map(str, li)))
if not FIO:
	if __name__ == '__main__':
		main()
		exit()
else:
	from io import BytesIO, IOBase
	import os

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = [x for x in input()]
	freq = {'0': 0, '1': 0, '?': 0}
	res = True
	for i in range(k):
		cur = s[i]
		for j in range(i + k, n, k):
			if cur != '?' and s[j] != '?' and (cur != s[j]):
				res = False
				break
			if cur == '?':
				cur = s[j]
		for j in range(i, n, k):
			s[j] = cur
		if not res:
			break
	if res:
		for i in range(k):
			freq[s[i]] += 1
			res = abs(freq['0'] - freq['1']) <= freq['?']
	print('YES' if res else 'NO')


def solution():
	(n, k) = map(int, input().split())
	s = list(input())
	a = ['?'] * k
	flag = 0
	for i in range(n):
		if s[i] == '1' and a[i % k] == '0':
			flag = 1
			break
		elif s[i] == '0' and a[i % k] == '1':
			flag = 1
			break
		elif s[i] != '?':
			a[i % k] = s[i]
	if flag:
		print('NO')
	elif a.count('1') > k // 2 or a.count('0') > k // 2:
		print('NO')
	else:
		print('YES')
t = int(input())
for _ in range(t):
	solution()


t = int(input())
for _ in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = list(input())
	result = True
	for i in range(k, n):
		if s[i] != '?':
			if s[i % k] == '?':
				s[i % k] = s[i]
			elif s[i] != s[i % k]:
				result = False
				break
	if not result:
		print('NO')
	else:
		count0 = 0
		count1 = 0
		for i in range(k):
			if s[i] == '0':
				count0 += 1
			elif s[i] == '1':
				count1 += 1
		if count0 <= k // 2 and count1 <= k // 2:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	(n, k) = [int(_) for _ in input().split()]
	A = input()
	S = []
	for _ in range(n):
		S.append(A[_])
	for _ in range(n - k):
		if S[_] != '?' and S[_ + k] == '?':
			S[_ + k] = S[_]
		elif S[_] == '?' and S[_ + k] != '?':
			S[_] = S[_ + k]
		elif S[_] != S[_ + k]:
			print('NO')
			break
	else:
		(zrs, ons, q) = (0, 0, 0)
		for _ in range(k):
			if S[_] == '1':
				ons += 1
			elif S[_] == '0':
				zrs += 1
			else:
				q += 1
		if abs(ons - zrs) > q:
			print('NO')
		else:
			for _ in range(n - k):
				if S[_] == '1':
					ons -= 1
				elif S[_] == '0':
					zrs -= 1
				else:
					q -= 1
				if S[_ + k] == '1':
					ons += 1
				elif S[_ + k] == '0':
					zrs += 1
				else:
					q += 1
				if abs(ons - zrs) > q:
					print('NO')
					break
			else:
				print('YES')


from sys import stdin
for _ in range(int(stdin.readline())):
	(n, k) = map(int, stdin.readline().split())
	p = stdin.readline().strip()
	s = []
	for ele in p:
		s.append(ele)
	pos = []
	for i in range(n):
		if s[i] != '?':
			pos.append(i)
	if len(pos) == 0:
		print('YES')
		continue
	d = {}
	for i in range(k):
		d[i] = set()
	for i in range(n):
		d[i % k].add(s[i])
	cond = True
	for i in range(k):
		if '0' in d[i] and '1' in d[i]:
			cond = False
			break
	if cond == False:
		print('NO')
	else:
		p = ''
		for i in range(k):
			if '0' in d[i]:
				p += '0'
			elif '1' in d[i]:
				p += '1'
			else:
				p += '?'
		(zero, one) = (0, 0)
		for ele in p:
			if ele == '0':
				zero += 1
			elif ele == '1':
				one += 1
		if zero <= k // 2 and one <= k // 2:
			print('YES')
		else:
			print('NO')


from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	x = y = 0
	for i in range(k):
		a = b = 0
		for j in range(i, n, k):
			if s[j] == '1':
				a += 1
			elif s[j] == '0':
				b += 1
		if min(a, b) > 0:
			print('NO')
			break
		if a:
			x += 1
		elif b:
			x -= 1
		else:
			y += 1
	else:
		if abs(x) <= y:
			print('YES')
		else:
			print('NO')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	tf = True
	z = 0
	o = 0
	qs = 0
	for i in range(k):
		c = set()
		for j in range(i, n, k):
			if s[j] != '?':
				c.add(s[j])
		if len(c) > 1:
			tf = False
			break
		elif len(c) == 1:
			x = c.pop()
			if x == '1':
				o += 1
			else:
				z += 1
		else:
			qs += 1
	if tf:
		x = abs(o - z)
		if qs >= x:
			qs -= x
			if qs % 2 == 0:
				print('YES')
			else:
				print('NO')
		else:
			print('NO')
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	if set(s) == {'?'}:
		print('YES')
		continue
	ans = 'YES'
	for i in range(n - k):
		if s[i] == '?' == s[i + k]:
			continue
		if s[i] == '?':
			s[i] = s[i + k]
		elif s[i + k] == '?':
			s[i + k] = s[i]
		elif s[i] != s[i + k]:
			ans = 'NO'
			break
	for i in range(n - k - 1, -1, -1):
		if s[i] == '?' == s[i + k]:
			continue
		if s[i] == '?':
			s[i] = s[i + k]
		elif s[i + k] == '?':
			s[i + k] = s[i]
		elif s[i] != s[i + k]:
			ans = 'NO'
			break
	cz = 0
	co = 0
	cj = 0
	for i in range(k):
		cz += s[i] == '0'
		co += s[i] == '1'
		cj += s[i] == '?'
	if abs(cz - co) > cj:
		ans = 'NO'
	for i in range(k, n):
		cz -= s[i - k] == '0'
		co -= s[i - k] == '1'
		cj -= s[i - k] == '?'
		cz += s[i] == '0'
		co += s[i] == '1'
		cj += s[i] == '?'
		if abs(cz - co) > cj:
			ans = 'NO'
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	flag = True
	for i in range(k):
		x = k
		while i + x < n:
			if s[i] == s[i + x] or s[i] == '?' or s[i + x] == '?':
				if s[i] == '?' and s[i + x] != '?':
					s[i] = s[i + x]
				elif s[i] != '?' and s[i + x] == '?':
					s[i + x] = s[i]
			else:
				flag = False
				break
			x += k
		if not flag:
			break
	(o, z, q) = (0, 0, 0)
	for i in range(k):
		if s[i] == '1':
			o += 1
		elif s[i] == '0':
			z += 1
		else:
			q += 1
	d = abs(z - o)
	if d > 0:
		if q < d:
			flag = False
		elif q > d:
			q -= d
			flag = flag and q % 2 == 0
	print('YES' if flag else 'NO')


t = 1
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input().strip('\n')
	(cn0, cn1, c) = (0, 0, 0)
	chk = True
	for i in range(n - k):
		if s[i] == '?' or s[i + k] == '?':
			continue
		elif s[i] != s[i + k]:
			chk = False
			break
		else:
			continue
	for i in range(k):
		if s[i] == '1':
			cn1 += 1
		elif s[i] == '0':
			cn0 += 1
		else:
			c += 1
	f0 = k // 2 - cn0
	f1 = k // 2 - cn1
	if f1 + f0 != c or f0 < 0 or f1 < 0:
		chk = False
	s = list(s)
	for i in range(k, n):
		if s[i - k] == '1' and s[i] == '?':
			s[i] = '1'
		elif s[i - k] == '1' and s[i] == '0':
			chk = False
			break
		if s[i - k] == '0' and s[i] == '?':
			s[i] = '0'
		elif s[i - k] == '0' and s[i] == '1':
			chk = False
			break
	for i in range(k, n):
		if s[i - k] == '1':
			cn1 -= 1
		if s[i - k] == '0':
			cn0 -= 1
		if s[i - k] == '?':
			c -= 1
		if s[i] == '1':
			cn1 += 1
		if s[i] == '0':
			cn0 += 1
		if s[i] == '?':
			c += 1
		f0 = k // 2 - cn0
		f1 = k // 2 - cn1
		if f1 + f0 != c or f0 < 0 or f1 < 0:
			chk = False
			break
	if chk:
		print('YES')
	else:
		print('NO')


gans = []
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	u = list(input())
	if u == ['?'] * n:
		gans.append('YES')
		continue
	cnt1 = cnt0 = 0
	for j in range(k):
		ok1 = False
		ok0 = False
		for i in range(j, n, k):
			if u[i] == '0':
				ok0 = True
			elif u[i] == '1':
				ok1 = True
		if ok1 and ok0:
			gans.append('NO')
			break
		if ok1:
			cnt1 += 1
		elif ok0:
			cnt0 += 1
	else:
		if abs(cnt1 - cnt0) <= k - cnt1 - cnt0:
			gans.append('YES')
		else:
			gans.append('NO')
print('\n'.join(gans))


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	(n, k) = list(map(int, stdin.readline().split()))
	s = input()
	flag = 1
	s1 = [' '] * k
	for i in range(n):
		if s[i] != '?':
			if s1[i % k] != ' ' and s1[i % k] != s[i]:
				flag = 0
				break
			s1[i % k] = s[i]
	ones = s1.count('1')
	zeros = s1.count('0')
	if ones > k // 2 or zeros > k // 2:
		flag = 0
	for i in range(n):
		if s[i] == '?':
			continue
		if s[i] != s1[i % k]:
			flag = 0
	print('YES' if flag else 'NO')


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, K, S):
	assert K % 2 == 0
	first = []
	for i in range(K):
		freq = Counter()
		for j in range(i, N, K):
			freq[S[j]] += 1
		if '0' in freq and '1' in freq:
			return 'NO'
		elif '0' in freq:
			first.append('0')
		elif '1' in freq:
			first.append('1')
		else:
			first.append('?')
	assert len(first) == K
	freq = Counter(first)
	if freq['0'] > K // 2 or freq['1'] > K // 2:
		return 'NO'
	return 'YES'
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N, K) = [int(x) for x in input().split()]
	S = input().decode().rstrip()
	ans = solve(N, K, S)
	print(ans)


t = int(input())
while t > 0:
	(n, k) = [int(x) for x in input().split()]
	s = input()
	s = list(s)
	flag = 1
	for i in range(k):
		p = i
		while p < n and s[p] == '?':
			p = p + k
		if p < n and s[p] != '?':
			save = s[p]
			p = i
			while p < n:
				if s[p] == '?' or s[p] == save:
					s[p] = save
					p += k
				else:
					flag = 0
					break
	if flag == 0:
		print('NO')
	else:
		d = {'?': 0, '1': 0, '0': 0}
		for i in range(k):
			d[s[i]] += 1
		if d['0'] > k // 2 or d['1'] > k // 2:
			print('NO')
		else:
			print('YES')
	t -= 1


from bisect import bisect_left
import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
from collections import deque
from functools import cmp_to_key
import math
import heapq

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	s = set()
	for p in range(2, n + 1):
		if prime[p]:
			s.add(p)
	return s

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	for _ in range(iin()):
		(n, k) = ain()
		s = sin()
		l = ['?'] * k
		d = deque(l)
		flag = 0
		for i in range(k):
			for j in range(i, n, k):
				if s[j] == '1':
					if d[j % k] == '?' or d[j % k] == '1':
						d[j % k] = '1'
					else:
						flag = 1
				elif s[j] == '0':
					if d[j % k] == '?' or d[j % k] == '0':
						d[j % k] = '0'
					else:
						flag = 1
			if flag:
				break
		if flag:
			print('NO')
		else:
			one = d.count('1')
			zero = d.count('0')
			if one > k // 2 or zero > k // 2:
				print('NO')
			else:
				print('YES')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if os.getcwd() == 'D:\\code':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input().strip()
	s = [i for i in s]
	(a, b, c) = (0, 0, 0)
	for i in range(k):
		if s[i] == '0':
			a += 1
		elif s[i] == '1':
			b += 1
		else:
			c += 1
	if (c + b - a) % 2:
		print('no')
		continue
	delta1 = (c + b - a) // 2
	delta2 = c - delta1
	if delta1 < 0 or delta2 < 0:
		print('no')
		continue
	ans = 'yes'
	start = -1
	for i in range(k, n):
		start += 1
		if s[i] == '0':
			if s[start] == '1':
				ans = 'no'
				break
			elif s[start] == '?':
				s[start] = '0'
				if delta1:
					delta1 -= 1
				else:
					ans = 'no'
					break
		elif s[i] == '1':
			if s[start] == '0':
				ans = 'no'
				break
			elif s[start] == '?':
				s[start] = '1'
				if delta2:
					delta2 -= 1
				else:
					ans = 'no'
					break
		elif s[start] == '0':
			s[i] = '0'
		elif s[start] == '1':
			s[i] = '1'
	print(ans)


for w in range(int(input())):
	(n, k) = tuple(map(int, input().split()))
	s = str(input())
	a = list(s)
	i = 0
	c = 0
	for i in range(k):
		j = i
		if c != 1:
			while j < n - k:
				if a[j] == a[j + k] and (a[j] == '0' or a[j] == '1'):
					j += k
				elif a[j + k] == '?':
					a[j + k] = a[j]
					j += k
				elif a[j] == '?':
					a[j] = a[j + k]
					t = 1
					while j - t * k >= 0 and a[j - t * k] == '?':
						a[j - t * k] = a[j]
					j += k
				else:
					c = 1
					break
	if c == 1:
		print('NO')
	else:
		x1 = a[:k].count('1')
		x2 = a[:k].count('0')
		x3 = a[:k].count('?')
		if x3 == 0:
			if x1 != x2:
				c = 1
		elif x3 < abs(x1 - x2):
			c = 1
		for i in range(1, n - k):
			if a[i - 1] == '1':
				x1 -= 1
			elif a[i - 1] == '0':
				x2 -= 1
			else:
				x3 -= 1
			if a[i + k - 1] == '1':
				x1 += 1
			elif a[i + k - 1] == '0':
				x2 += 1
			else:
				x3 += 1
			if x3 < abs(x1 - x2):
				c = 1
				break
		if c == 1:
			print('NO')
		else:
			print('YES')


t = int(input())
while t > 0:
	(n, k) = map(int, input().split())
	s = list(input())
	f = 0
	for i in range(k):
		m = 0
		p = s[i]
		while f == 0 and m * k + i < n:
			if p == '?':
				if s[m * k + i] != '?':
					p = s[m * k + i]
			elif s[m * k + i] == '?':
				pass
			elif p != s[k * m + i]:
				f = 1
				break
			m += 1
		if f == 1:
			break
		elif p != '?':
			m = 0
			while m * k + i < n:
				s[m * k + i] = p
				m += 1
	if f == 1:
		print('NO')
	else:
		noo = 0
		noz = 0
		for i in range(k):
			if s[i] == '0':
				noz += 1
			elif s[i] == '1':
				noo += 1
		if noo > k // 2 or noz > k // 2:
			print('NO')
		else:
			print('YES')
	t -= 1


import sys
inp = sys.stdin.buffer.readline
inar = lambda : list(map(int, inp().split()))
inin = lambda : int(inp())
inst = lambda : inp().decode().rstrip('\n\r')
_ans = []

def pr(*args):
	global _ans
	for i in args:
		_ans.append(i)
	_ans.append('\n')
_T_ = inin()
for _t_ in range(_T_):
	(n, k) = inar()
	s = inst()
	ones = [False] * k
	zeros = [False] * k
	(c1, c0) = (0, 0)
	for i in range(n):
		if s[i] == '1':
			ones[i % k] = True
		if s[i] == '0':
			zeros[i % k] = True
	ans = 'YES'
	for i in range(k):
		if ones[i] and zeros[i]:
			ans = 'NO'
		if ones[i]:
			c1 += 1
		if zeros[i]:
			c0 += 1
	if c0 > k / 2 or c1 > k / 2:
		ans = 'NO'
	pr(ans)
print(*_ans, sep='')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	one = 0
	zero = 0
	q = 0
	for i in range(k):
		if s[i] == '?' and i < n - k:
			s[i] = s[i + k]
		if s[i] == '0':
			zero += 1
		elif s[i] == '1':
			one += 1
		else:
			q += 1
	if abs(one - zero) > q:
		print('NO')
		continue
	check = 0
	for i in range(n - k):
		if s[i] != s[i + k]:
			if abs(ord(s[i]) - ord(s[i + k])) == 1:
				print('NO')
				check = 1
				break
			elif s[i] == '?':
				s[i] = s[i + k]
			else:
				s[i + k] = s[i]
	if check == 0:
		one = 0
		zero = 0
		q = 0
		for i in range(n - k, n):
			if s[i] == '0':
				zero += 1
			elif s[i] == '1':
				one += 1
			else:
				q += 1
		if abs(one - zero) > q:
			print('NO')
		else:
			print('yes')


from sys import stdin, stdout
q = int(input())
for i in range(q):
	(n, k) = list(map(int, stdin.readline().split()))
	s = list(map(str, input()))
	sum1 = 0
	sum0 = 0
	sum2 = 0
	for i in range(k):
		if s[i] == '1':
			sum1 += 1
		elif s[i] == '0':
			sum0 += 1
	flag = 0
	if sum1 > k // 2 or sum0 > k // 2:
		flag = 1
	for i in range(k):
		temp = '?'
		for j in range(i, n, k):
			if temp == '?':
				if s[j] != '?':
					temp = s[j]
			elif temp != '?':
				if s[j] == '?':
					s[j] = temp
				elif s[j] != '?' and s[j] != temp:
					flag = 1
					break
		if flag == 1:
			break
		if temp != '?':
			for l in range(i, n, k):
				if s[l] == '?':
					s[l] = temp
				elif s[l] != temp:
					flag = 1
					break
	sum1 = 0
	sum0 = 0
	sum2 = 0
	for i in s:
		if i == '?':
			sum2 += 1
		elif i == '1':
			sum1 += 1
		else:
			sum0 += 1
	z = 0
	o = 0
	for i in range(k):
		if s[i] == '0':
			z += 1
		elif s[i] == '1':
			o += 1
	if z > k / 2 or o > k / 2:
		flag = 1
	for i in range(k, n):
		if s[i - k] == '0':
			z -= 1
		elif s[i - k] == '1':
			o -= 1
		if s[i] == '0':
			z += 1
		elif s[i] == '1':
			o += 1
		if z > k / 2 or o > k / 2:
			flag = 1
	if flag == 1:
		print('NO')
	else:
		print('YES')


def solve(n, k, s):
	a = [None for i in range(k)]
	for i in range(len(s)):
		j = i % k
		if not a[j] and s[i] != '?':
			a[j] = s[i]
		elif a[j] != s[i] and s[i] != '?':
			return 'NO'
	(z, o) = (0, 0)
	for i in range(k):
		if a[i] == '0':
			z += 1
		if a[i] == '1':
			o += 1
	return 'YES' if z <= k // 2 and o <= k // 2 else 'NO'
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	print(solve(n, k, list(input())))


import sys
T = int(sys.stdin.readline().strip())
for _ in range(T):
	(n, k) = map(int, sys.stdin.readline().strip().split())
	s = list(sys.stdin.readline().strip())
	ones = 0
	zeros = 0
	for i in range(k):
		if s[i] == '1':
			ones += 1
		elif s[i] == '0':
			zeros += 1
	if ones > k // 2 or zeros > k // 2:
		print('NO')
		continue
	questions = {'0': k // 2 - zeros, '1': k // 2 - ones}
	ones = k // 2
	zeros = k // 2
	works = True
	for i in range(n - k):
		if s[i + k] == '1':
			ones += 1
			if s[i] == '1':
				ones -= 1
			elif s[i] == '0':
				zeros -= 1
			else:
				if questions['1'] == 0:
					works = False
					break
				questions['1'] -= 1
				ones -= 1
		elif s[i + k] == '0':
			zeros += 1
			if s[i] == '1':
				ones -= 1
			elif s[i] == '0':
				zeros -= 1
			else:
				if questions['0'] == 0:
					works = False
					break
				questions['0'] -= 1
				zeros -= 1
		elif s[i] == '1':
			s[i + k] = '1'
		elif s[i] == '0':
			s[i + k] = '0'
		if ones > k // 2 or zeros > k // 2:
			works = False
			break
	if works:
		print('YES')
	else:
		print('NO')


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		(n, k) = ria()
		a = rs()
		a = list(a)
		r = 0
		for i in range(k):
			for j in range(1, n):
				if i + j * k >= n:
					break
				if a[i] != a[i + j * k] and (a[i] != '?' and a[i + j * k] != '?'):
					print('NO')
					r = 1
					break
				if a[i] == '?' and a[i + j * k] != '?':
					a[i] = a[i + j * k]
				if a[i] != '?' and a[i + j * k] == '?':
					a[i + j * k] = a[i]
		if r == 0:
			q = deque(a[:k])
			n1 = 0
			nq = 0
			n0 = 0
			for i in q:
				if i == '?':
					nq += 1
				if i == '1':
					n1 += 1
				if i == '0':
					n0 += 1
			if max(n1, n0) > k // 2:
				print('NO')
			else:
				c = True
				for i in range(k, n):
					if q[0] == '?':
						nq -= 1
					if q[0] == '1':
						n1 -= 1
					if q[0] == '0':
						n0 -= 1
					q.popleft()
					q.append(a[i])
					if q[-1] == '?':
						nq += 1
					if q[-1] == '1':
						n1 += 1
					if q[-1] == '0':
						n0 += 1
					if max(n1, n0) > k // 2:
						c = False
						break
				if c:
					print('YES')
				else:
					print('NO')
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


from sys import stdin, stdout
import heapq as hq
from collections import defaultdict
t = int(input())
for tc in range(t):
	(n, k) = list(map(int, stdin.readline().split()))
	ar = list(stdin.readline().strip())
	d = ('0', '1')
	res = True
	for i in range(n - k):
		if ar[i] in d:
			if ar[i + k] == '?':
				ar[i + k] = ar[i]
			elif ar[i + k] != ar[i]:
				res = False
	ar = ar[::-1]
	for i in range(n - k):
		if ar[i] in d:
			if ar[i + k] == '?':
				ar[i + k] = ar[i]
			elif ar[i + k] != ar[i]:
				res = False
	ar = ar[::-1]
	if not res:
		stdout.write(str('NO') + '\n')
		continue
	c0 = 0
	c1 = 0
	c_ = 0
	for c in ar[:k]:
		if c == '0':
			c0 += 1
		elif c == '1':
			c1 += 1
		else:
			c_ += 1
	if c1 + c_ < c0 or c0 + c_ < c1:
		stdout.write(str('NO') + '\n')
		continue
	for i in range(k, n):
		if ar[i] != ar[i - k]:
			stdout.write(str('NO') + '\n')
			continue
	stdout.write(str('YES') + '\n')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	s1 = list(s)
	f = []
	ind = []
	ans = 1
	for i in range(n):
		if len(f) - 1 < i % k:
			f.append([s[i]])
			ind.append([i])
		else:
			f[i % k].append(s[i])
			ind[i % k].append(i)
	for i in range(len(f)):
		sub = f[i]
		a = sub.count('1')
		b = sub.count('0')
		if a > 0 and b > 0:
			ans = 0
		elif a > 0:
			for x in ind[i]:
				s1[x] = '1'
		elif b > 0:
			for x in ind[i]:
				s1[x] = '0'
	if ans:
		sf = s1[:k]
		a = sf.count('1')
		b = sf.count('0')
		c = sf.count('?')
		if c - abs(a - b) < 0:
			ans = 0
	if ans:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	(u, v, w) = (0, 0, 0)
	for i in range(k):
		(o, z, q) = (0, 0, 0)
		for j in range(i, n, k):
			if s[j] == '0':
				z += 1
			elif s[j] == '1':
				o += 1
			else:
				q += 1
		if o and z:
			print('NO')
			break
		elif o:
			u += 1
		elif z:
			v += 1
		else:
			w += 1
	else:
		print('YES' if u <= k // 2 and v <= k // 2 else 'NO')


UNDEF = '?'

def solve(s, k):
	period = ['?'] * k
	for i in range(len(s)):
		idx = i % k
		if period[idx] == UNDEF and s[i] != UNDEF:
			period[idx] = s[i]
		if period[idx] != UNDEF and s[i] != UNDEF and (period[idx] != s[i]):
			return False
	return 2 * period.count('1') <= k and 2 * period.count('0') <= k
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	print('YES' if solve(s, k) else 'NO')


def possible(s, n, k):
	for i in range(k):
		value = '?'
		for j in range(i, n, k):
			if value != '?':
				break
			if s[j] != '?':
				value = s[j]
		for j in range(i, n, k):
			if s[j] == '?' and value != '?':
				s[j] = value
			elif s[j] != '?':
				if value == '?':
					value = s[j]
				elif s[j] != value:
					return 'NO'
	ct0 = 0
	ct1 = 0
	ctB = 0
	for i in range(k):
		if s[i] == '0':
			ct0 += 1
		elif s[i] == '1':
			ct1 += 1
		else:
			ctB += 1
	if abs(ct1 - ct0) <= ctB:
		return 'YES'
	else:
		return 'NO'
t = int(input())
for _ in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = [x for x in input()]
	print(possible(s, n, k))


t = int(input())
for _ in range(t):
	(n, k) = (int(x) for x in input().split(' '))
	s = input()
	m = len(s)
	is_ok = True
	(c0, c1) = (0, 0)
	for i in range(k):
		bit = None
		for c in s[i::k]:
			if c == '?':
				continue
			if bit is None:
				bit = c
			elif bit != c:
				is_ok = False
				break
		if not is_ok:
			break
		if bit == '0':
			c0 += 1
		elif bit == '1':
			c1 += 1
	if is_ok and 2 * c0 <= k and (2 * c1 <= k):
		print('YES')
	else:
		print('NO')


import math
t = int(input())
ans = []

def solve(n, k):
	for i in range(k):
		thisis = None
		for j in range(i, n, k):
			if thisis is None:
				if arr[j] == '?':
					continue
				if arr[j] == '1':
					thisis = '1'
					break
				else:
					thisis = '0'
					break
		for j in range(i, n, k):
			if arr[j] == '?':
				arr[j] = thisis
			if arr[j] != thisis:
				return False
	ones = 0
	zeros = 0
	for l in range(k):
		if arr[l] == '1':
			ones += 1
		elif arr[l] == '0':
			zeros += 1
		if ones <= k // 2 and zeros <= k // 2:
			continue
		else:
			return False
	return True
	pass
for i in range(t):
	s = input().split()
	n = int(s[0])
	k = int(s[1])
	arr = list(input())
	ans.append(solve(n, k))
for test in ans:
	if test:
		print('YES')
	else:
		print('NO')


import sys
from _collections import deque
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = input()
	c0 = c1 = 0
	ans = 'YES'
	for i in range(k):
		j = i
		ck = a[i]
		while j < n:
			if ck != '?':
				if a[j] != '?' and a[j] != ck:
					ans = 'NO'
					break
			if ck == '?':
				if a[j] != '?':
					ck = a[j]
			j += k
		if ck == '1':
			c0 += 1
		elif ck == '0':
			c1 += 1
		if ans == 'NO':
			break
	if c0 > k // 2 or c1 > k // 2:
		ans = 'NO'
	print(ans)


tests = int(input())
for t in range(tests):
	(n, k) = list(map(int, input().split()))
	ls = list(input())
	start = ls[:k]
	check = True
	end_idx = k
	for end_idx in range(2 * k, n, k):
		if not check:
			break
		for pos in range(k):
			if ls[end_idx - k + pos] != '?':
				if start[pos] == '?':
					start[pos] = ls[end_idx - k + pos]
				elif start[pos] != ls[end_idx - k + pos]:
					check = False
					break
	if check:
		for idx in range(end_idx, n):
			if ls[idx] != '?':
				if start[idx - end_idx] == '?':
					start[idx - end_idx] = ls[idx]
				elif start[idx - end_idx] != ls[idx]:
					check = False
		if check:
			count = 0
			wild = 0
			for item in start:
				if item == '1':
					count += 1
				elif item == '0':
					count -= 1
				else:
					wild += 1
			if wild >= abs(count):
				print('YES')
			else:
				print('NO')
		else:
			print('NO')
	else:
		print('NO')


import os
from sys import stdin, stdout

def solve(tc):
	(n, k) = map(int, stdin.readline().split())
	s = list(stdin.readline().strip())
	for i in range(n - k):
		if s[i] == '0':
			if s[i + k] == '1':
				print('NO')
				return
			elif s[i + k] == '?':
				s[i + k] = '0'
		elif s[i] == '1':
			if s[i + k] == '0':
				print('NO')
				return
			elif s[i + k] == '?':
				s[i + k] = '1'
		elif s[i + k] == '0':
			s[i] = '0'
		elif s[i + k] == '1':
			s[i] = '1'
	(plus, minus) = (0, 0)
	for i in range(k):
		if s[i] == '1':
			plus += 1
		elif s[i] == '0':
			minus += 1
	if plus > k // 2 or minus > k // 2:
		print('NO')
		return
	for i in range(k, n):
		if s[i] == '1':
			plus += 1
		elif s[i] == '0':
			minus += 1
		if s[i - k] == '1':
			plus -= 1
		elif s[i - k] == '0':
			minus -= 1
		if plus > k // 2 or minus > k // 2:
			print('NO')
			return
	print('YES')
tcs = 1
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


import os
import sys
from io import BytesIO, IOBase
import threading
from collections import defaultdict

def main():
	(n, k) = map(int, input().split())
	s = input()
	l = [[] for i in range(k)]
	ok = True
	for i in range(n):
		ch = s[i]
		if ch == '1':
			if '0' in l[i % k]:
				ok = False
				break
			if len(l[i % k]) == 0:
				l[i % k].append('1')
		if ch == '0':
			if '1' in l[i % k]:
				ok = False
				break
			if len(l[i % k]) == 0:
				l[i % k].append('0')
	n0 = 0
	n1 = 0
	for i in range(k):
		if len(l[i]):
			if l[i][0] == '1':
				n1 += 1
			else:
				n0 += 1
	if 2 * n1 > k or 2 * n0 > k:
		ok = False
	if ok:
		print('YES')
	else:
		print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(int(input())):
	main()


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	li = input()
	temp_1 = [-1] * k
	temp_0 = [-1] * k
	for i in range(n):
		if li[i] == '0':
			temp_0[i % k] = 1
		elif li[i] == '1':
			temp_1[i % k] = 1
	c_0 = c_1 = 0
	val = True
	for i in range(k):
		if temp_0[i] == 1 and temp_1[i] == 1:
			val = False
			break
	for i in range(k):
		if temp_1[i] == 1:
			c_1 += 1
		if temp_0[i] == 1:
			c_0 += 1
	if c_0 > k / 2 or c_1 > k / 2:
		val = False
	if val:
		print('Yes')
	else:
		print('No')


import sys
import math
for _ in range(int(input())):
	(n, k) = map(int, sys.stdin.readline().split())
	arr = list(map(str, sys.stdin.readline().rstrip()))
	flag = True
	ans = []
	for i in range(k):
		my_set = set([])
		for j in range(math.ceil(n / k)):
			if j * k + i < n:
				my_set.add(arr[j * k + i])
		if len(my_set) == 3:
			flag = False
			break
		elif len(my_set) == 2 and (not '?' in my_set):
			flag = False
			break
		elif '1' in my_set:
			ans.append(int(1))
		elif '0' in my_set:
			ans.append(int(0))
	if k // 2 - (k - len(ans)) > sum(ans) or sum(ans) > k // 2:
		flag = False
	if flag == True:
		print('YES')
	else:
		print('NO')


def balanced(string, k):
	zer = 0
	one = 0
	check = True
	for i in range(k):
		temp = -1
		for j in range(i, len(string), k):
			if string[j] != '?':
				if temp != -1 and int(string[j]) != temp:
					check = False
					break
				temp = int(string[j])
		if temp != -1:
			if temp == 0:
				zer += 1
			else:
				one += 1
		if max(zer, one) > k / 2:
			check = False
	if check:
		return 'YES'
	return 'NO'
for _ in range(int(input())):
	(a, b) = map(int, input().strip().split())
	print(balanced(input(), b))


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	(n, k) = inputnums()
	s = inputstring()
	done = False
	for i in range(k):
		zero = False
		one = False
		for j in range(i, n, k):
			if s[j] == '0':
				zero = True
			if s[j] == '1':
				one = True
		if zero and one:
			done = True
			break
		if zero:
			for j in range(i, n, k):
				s[j] = '0'
		if one:
			for j in range(i, n, k):
				s[j] = '1'
	s1 = s[0:k]
	cnt0 = s1.count('0')
	cnt1 = s1.count('1')
	cnt2 = s1.count('?')
	if done or abs(cnt1 - cnt0) > cnt2:
		print('NO')
	else:
		print('YES')