import sys from collections import deque def main(): V, D = map(int, sys.stdin.readline().split()) E = [sys.stdin.readline().strip() for _ in range(V)] # Build adjacency list adj = [[] for _ in range(V)] for i in range(V): for j in range(V): if E[i][j] == '1': adj[i].append(j) # Check if the graph is connected visited = [False] * V queue = deque([0]) visited[0] = True while queue: u = queue.popleft() for v in adj[u]: if not visited[v]: visited[v] = True queue.append(v) if not all(visited): print("No") return # Check for self-loop has_self_loop = any(E[i][i] == '1' for i in range(V)) # Function to perform BFS and return farthest node and max distance def bfs(u): dist = [-1] * V dist[u] = 0 q = deque([u]) while q: current = q.popleft() for neighbor in adj[current]: if dist[neighbor] == -1: dist[neighbor] = dist[current] + 1 q.append(neighbor) max_dist = max(dist) far_node = dist.index(max_dist) return far_node, max_dist if has_self_loop: # Find diameter using two BFS passes u, _ = bfs(0) v, diameter = bfs(u) if D >= diameter: print("Yes") else: print("No") else: # Check if the graph is bipartite is_bipartite = True color = [-1] * V for start in range(V): if color[start] == -1: queue = deque([start]) color[start] = 0 while queue: u = queue.popleft() for v in adj[u]: if color[v] == -1: color[v] = color[u] ^ 1 queue.append(v) elif color[v] == color[u]: is_bipartite = False break if not is_bipartite: break if not is_bipartite: break if is_bipartite: print("No") return else: # Find diameter using two BFS passes u, _ = bfs(0) v, diameter = bfs(u) if D >= diameter: print("Yes") else: print("No") if __name__ == "__main__": main()