Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)
var (
	table, ans = [12][12]int{}, [12][12]int{}
)

func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 1; j < 11; j++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			tmp := [10]int{}
			for k := range input {
				tmp[k], _ = strconv.Atoi(input[k])
			}
			for l := 1; l < 11; l++ {
				table[j][l] = tmp[l-1]
			}
		}
		check(table, 1, ans)
	}
}

func check(table [12][12]int, i int, ans [12][12]int) {
	if i == 11 {
		solve(table, 2, 1, ans)
		return
	}
	ans[1][i] = 0
	check(table, i+1, ans)
	ans[1][i] = 1
	attack(table, 1, i)
	check(table, i+1, ans)
	attack(table, 1, i)
}

func solve(table [12][12]int, i, j int, ans [12][12]int) {
	if i == 11 {
		flag := true
		for k := 1; k < 11; k++ {
			if table[10][k] == 1 {
				flag = false
				break
			}
		}
		if flag {
			printans(ans)
		}
		return
	}
	if table[i-1][j] == 1 {
		ans[i][j] = 1
		attack(table, i, j)
		if j == 10 {
			solve(table, i+1, 1, ans)
		} else {
			solve(table, i, j+1, ans)
		}
		attack(table, i, j)
		ans[i][j] = 0
	} else {
		ans[i][j] = 0
		if j == 10 {
			solve(table, i+1, 1, ans)
		} else {
			solve(table, i, j+1, ans)
		}
	}
}

func printans(ans [12][12]int) {
	for i := 1; i < 11; i++ {
		for j := 1; j < 11; j++ {
			fmt.Print(ans[i][j])
			if j < 10 {
				fmt.Print(" ")
			}
		}
		fmt.Println()
	}
}

func attack(table [12][12]int, i, j int) {
	table[i][j] = 1 - table[i][j]
	table[i-1][j] = 1 - table[i-1][j]
	table[i+1][j] = 1 - table[i+1][j]
	table[i][j-1] = 1 - table[i][j-1]
	table[i][j+1] = 1 - table[i][j+1]
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <complex>
using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define EPS 1e-8
#define DEB 1

const int dx[] = {0,1,0,-1};
const int dy[] = {-1,0,1,0};
int field[16][16],tmp[16][16];
vector<pair<int,int> > v;

void touch(int x, int y){
	tmp[y][x] ^= 1;
	rep(k,4){
		int tx = x + dx[k];
		int ty = y + dy[k];
		if( tx<0 || ty<0 || tx>=10 || ty>=10 )continue;
		tmp[ty][tx] ^= 1;
	}
}
bool ok(){
	rep(i,10)rep(j,10)if( tmp[i][j]==1 ) return false;
	return true;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		rep(i,10)rep(j,10)scanf("%d",&field[i][j]);

		rep(i,1<<10){
			v.clear();
			rep(j,10)rep(k,10)tmp[j][k]=field[j][k];
			
			rep(j,10)if( (1<<j)&i ){
				touch(j,0);
				v.push_back(mp(j,0));
			}
			REP(k,1,10){
				rep(j,10){
					if( tmp[k-1][j]==1 ){
						touch(j,k);
						v.push_back(mp(j,k));
					}
				}
			}
			if( ok() ){
				break;
			}
		}
		int ans[10][10];
		memset(ans,0,sizeof(ans));
		rep(i,v.size())ans[v[i].second][v[i].first] = 1;
		rep(i,10){
			rep(j,10){
				if( j>0 )putchar(' ');
				printf("%d",ans[i][j]);
			}
			puts("");
		}
		   
	}
	
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<bitset>
#include<queue>
#include<cmath>
#include<algorithm>

#define MAX 12
#define INV (-1)

using namespace std;

inline bool inRange(int t, int min, int max){
	return (min <= t && t < max);
}

class CMap{
public:
	int m[MAX][MAX];
	int from;
	vector< pair<int, int > > vHist;

	CMap()
	{
		from = 0;
		for(int i = 0; i < MAX; ++i)
			for(int j = 0; j < MAX; ++j)
				m[i][j] = 0;
	}

	void Reset(){
		from = 0;
		for(int i = 0; i < MAX; ++i)
			for(int j = 0; j < MAX; ++j)
				m[i][j] = 0;
	}

	bool PassParticle(int i, int j)
	{
		const int di[] = {0,-1,0,1,0};
		const int dj[] = {0,0,1,0,-1};

		if( !(inRange(i,1,MAX-1) && inRange(j,1,MAX-1)) )
			return false;
		/*if( find( vHist.begin(), vHist.end(), make_pair(i,j) ) != vHist.end() )
			return false;*/

		for(int k = 0; k < sizeof(di)/sizeof(*di); ++k)
			if( inRange( i+di[k],1,MAX-1 ) && inRange( j+dj[k],1,MAX-1 ) )
				m[i+di[k]][j+dj[k]] ^= 1;

		vHist.push_back( make_pair( i, j ) );
		return true;
	}
	bool NoLight(int range = MAX - 1) const
	{
		for(int i = 1; i < range; ++i)
			for(int j = 1; j < range; ++j)
				if( m[i][j] == 1 ) return false;
		return true;
	}

	void Print() const
	{
		for(int i = 1; i < MAX - 1; ++i){
			cout << m[i][1];
			for(int j = 2; j < MAX - 1; ++j)
				cout << ' ' << m[i][j];
			cout << '\n';
		}
	}

	void PrintPassHist(){
		Reset();

		for(unsigned int i = 0; i < vHist.size(); ++i){
			m[ vHist[i].first ][ vHist[i].second ] = 1;
		}
		for(int i = 1; i < MAX - 1; ++i){
			cout << m[i][1];
			for(int j = 2; j < MAX - 1; ++j)
				cout << ' ' << m[i][j];
			cout << '\n';
		}
	}
};

void BFS(CMap &stm, CMap &ret){
	int n = 0;
	bool bFailed = true;
	queue< CMap > q;

	q.push( stm );
	while( !q.empty() ){
		/*++n;
		if( n % 100 == 0 ){
			cout << "*";
		}*/
		CMap M = q.front();
		
		q.pop();

		if( M.NoLight() )
		{
			ret = M;
			bFailed = false;
			break;
		}
		//M.Print();cout <<endl;
		
		bool bCont = true;

		for(unsigned int i = 1; i < MAX-1 && bCont; ++i){
			for(unsigned int j = 1; j < MAX-1 && bCont; ++j){
				const int di[] = {0,-1,1,0,0};
				const int dj[] = {0,0,0,1,-1};

				for(int k = 0; k < sizeof(di)/sizeof(*di); ++k){
					int cnt0 = 0;
					int cnt1 = 0;
					for(int l = 0; l < sizeof(di)/sizeof(*di); ++l){
						int ti = i + di[k] + di[l];
						int tj = j + dj[k] + dj[l];
						if( inRange( ti,1,MAX-1 ) && inRange( tj,1,MAX-1 ) )
							if( M.m[ ti ][ tj ] == 1 )
								++cnt1;
							else
								++cnt0;
					}
					if( cnt1 > cnt0 ){
						CMap add = M;
						if( add.PassParticle( i+di[k], j+dj[k] ) ){
							q.push( add );
							bCont = false;
						}
					}
				}
			}
		}
	}
	//cout << "\nTOTAL: " << n << ( bFailed ? "\nFAILED.\n" : "\nCOMPLETED.\n" );
}
int main(void){
	int n;

	cin >> n;
	for( int D = 0; D < n; ++D )
	{
		CMap M;
		CMap Answer;

		for(int i = 1; i < MAX - 1; ++i)
			for(int j = 1; j < MAX - 1 ; ++j)
				cin >> M.m[i][j];

		Answer.Reset();
		BFS( M, Answer );
		Answer.PrintPassHist();
	}
	return 0;
}