← Home
package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"sort"
	"strconv"
	"time"
)

type Cand struct {
	h1 uint64
	h2 uint64
	c  int32
	r  int32
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 1024*1024)
	scanner.Buffer(buf, 10*1024*1024)
	scanner.Split(bufio.ScanWords)

	var R1, R2 [300005]uint64
	rng := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < 300005; i++ {
		R1[i] = rng.Uint64()
		R2[i] = rng.Uint64()
	}

	if !scanner.Scan() {
		return
	}
	t, _ := strconv.Atoi(scanner.Text())

	var cands []Cand
	var colH1, colH2 []uint64

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	for tc := 0; tc < t; tc++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())

		grid := make([]string, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			grid[i] = scanner.Text()
		}

		if cap(colH1) < m {
			colH1 = make([]uint64, m)
			colH2 = make([]uint64, m)
		}
		colH1 = colH1[:m]
		colH2 = colH2[:m]
		for j := 0; j < m; j++ {
			colH1[j] = 0
			colH2[j] = 0
		}

		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				if grid[i][j] == '1' {
					colH1[j] ^= R1[i]
					colH2[j] ^= R2[i]
				}
			}
		}

		if cap(cands) < n*m {
			cands = make([]Cand, n*m)
		}
		cands = cands[:n*m]

		idx := 0
		for j := 0; j < m; j++ {
			for i := 0; i < n; i++ {
				cands[idx] = Cand{
					h1: colH1[j] ^ R1[i],
					h2: colH2[j] ^ R2[i],
					c:  int32(j),
					r:  int32(i),
				}
				idx++
			}
		}

		sort.Slice(cands, func(i, j int) bool {
			if cands[i].h1 != cands[j].h1 {
				return cands[i].h1 < cands[j].h1
			}
			return cands[i].h2 < cands[j].h2
		})

		maxFreq := 0
		bestCand := cands[0]
		currentFreq := 1
		for i := 1; i < len(cands); i++ {
			if cands[i].h1 == cands[i-1].h1 && cands[i].h2 == cands[i-1].h2 {
				currentFreq++
			} else {
				if currentFreq > maxFreq {
					maxFreq = currentFreq
					bestCand = cands[i-1]
				}
				currentFreq = 1
			}
		}
		if currentFreq > maxFreq {
			maxFreq = currentFreq
			bestCand = cands[len(cands)-1]
		}

		ansX := make([]byte, n)
		cIdx := int(bestCand.c)
		rIdx := int(bestCand.r)
		for i := 0; i < n; i++ {
			ansX[i] = grid[i][cIdx]
		}
		if ansX[rIdx] == '0' {
			ansX[rIdx] = '1'
		} else {
			ansX[rIdx] = '0'
		}

		fmt.Fprintf(out, "%d\n%s\n", maxFreq, string(ansX))
	}
}