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<iostream>
#include<vector>
#include<queue>
#include<cmath>
#include<algorithm>

#define MAX 12
#define NIL (-1)

using namespace std;

inline bool inRange(int t){
	return (1 <= t && t < MAX-1);
}

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

	CMap(){ Reset(); }
	void Reset()
	{
		for(int i = 0; i < MAX; ++i)
			for(int j = 0; j < MAX; ++j)
				m[i][j] = 0;
	}
	bool PassParticle(int i, int j)
	{
		int cnt0 = 0;
		int cnt1 = 0;
		const int di[] = {0,-1,0,1,0};
		const int dj[] = {0,0,1,0,-1};

		if( !(inRange(i) && inRange(j)) )
			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] ) && inRange( j+dj[k] ) )
				if( m[i+di[k]][j+dj[k]] == 1 )
					cnt1++;
				else
					cnt0++;

		if( cnt1 < cnt0 )
			return false;

		for(int k = 0; k < sizeof(di)/sizeof(*di); ++k)
			if( inRange( i+di[k]) && inRange( j+dj[k]) )
				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(unsigned int i = 1; i < MAX - 1; ++i){
			for(unsigned int j = 1; 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 EraseBlocks(CMap &M){
	//cout << "Entered EraseBlocks Function.\n";
	bool bCont;
	do{
		bCont = false;
		for(unsigned int i = 1; i < MAX-1; ++i){
			for(unsigned int j = 1; j < MAX-1; ++j){
				if( M.m[i][j] == 1 ){
					int ci = NIL, cj = NIL, cntCand = 0;
					const int di[] = {-1,-1,0,1,1, 1, 0,-1};
					const int dj[] = { 0, 1,1,1,0,-1,-1,-1};
					const int sz = sizeof(di)/sizeof(*di);

					for(int k = 0; k < sz; k+=2){
						if( M.m[i+di[k]][j+dj[k]] == 0 && inRange(i+di[k]) && inRange(j+dj[k]) &&
							M.m[i+di[(k+1)%sz]][j+dj[(k+1)%sz]] == 0 && inRange(i+di[(k+1)%sz]) && inRange(j+dj[(k+1)%sz]) && 
							M.m[i+di[(k+2)%sz]][j+dj[(k+2)%sz]] == 0 && inRange(i+di[(k+2)%sz]) && inRange(j+dj[(k+2)%sz]) &&
							M.m[i+di[(k+3)%sz]][j+dj[(k+3)%sz]] == 0 && inRange(i+di[(k+3)%sz]) && inRange(j+dj[(k+3)%sz]) &&
							M.m[i+di[(k+4)%sz]][j+dj[(k+4)%sz]] == 0 && inRange(i+di[(k+4)%sz]) && inRange(j+dj[(k+4)%sz]) ){
								CMap Tmp = M;
								if( M.PassParticle( i+di[(k+6)%sz], j+dj[(k+6)%sz] ) ){
									cntCand++;
									ci = i+di[(k+6)%sz];
									cj = j+dj[(k+6)%sz];
									M = Tmp;
								}
						}
					}
					if( cntCand == 1 ){
						if( M.PassParticle( ci, cj ) ){
							bCont = true;
						}
					}
				}
			}
		}
	}while( bCont );
}

void BFS(CMap &stm, CMap &ret){
	queue< CMap > q;

	q.push( stm );
	while( !q.empty() ){
		bool bCont = true;
		CMap Tmp, M = q.front();
		
		q.pop();

		EraseBlocks( M );
		/*cout << "Erase End.\n";
		cout << endl;
		M.Print();*/
		if( M.NoLight() ){
			ret = M;
			break;
		}
		Tmp = M;

		for(unsigned int i = 1; i < MAX-1 && bCont; ++i){
			for(unsigned int j = 1; j < MAX-1 && bCont; ++j){
				if( M.m[i][j] == 1 ){
					int max1 = 0;
					int ci = NIL, cj = NIL;
					const int di[] = {-1,0,1,0};
					const int dj[] = {0,1,0,-1};
					const int sz = sizeof(di)/sizeof(*di);

					for(int k = 0; k < sz; ++k){
						if( M.m[i+di[k]][j+dj[k]] == 0 && inRange(i+di[k]) && inRange(j+dj[k]) &&
							M.m[i+di[(k+1)%sz]][j+dj[(k+1)%sz]] == 0 && inRange(i+di[(k+1)%sz]) && inRange(j+dj[(k+1)%sz])){
								if( M.PassParticle(i+di[(k+2)%sz], j+dj[(k+2)%sz]) ){
									q.push( M );
									M = Tmp;
									bCont = false;
								}
								if( M.PassParticle(i+di[(k+3)%sz], j+dj[(k+3)%sz]) ){
									q.push( M );
									M = Tmp;
									bCont = false;
								}
						}
					}
				}
			}
		}
	}
}
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];

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

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int dx[5] = { 0, 1, 0, -1, 0 };
const int dy[5] = {-1, 0, 1,  0, 0 };

int in[10][10];
int cnt[10][10];
int ans[10][10];

int color(int y, int x )
{
	int tmp = in[y][x];
	rep (k, 5 ){
		int nx = x + dx[k];
		int ny = y + dy[k];
		if (nx < 0 || nx >= 10 || ny < 0 || ny >= 10 ) continue;
		tmp += cnt[ny][nx];
	} // end rep
	
	return tmp%2;
}

int solve()
{
	for (int i = 1; i < 10; i++ ) rep (j, 10 ) if (color(i-1, j ) ) cnt[i][j] = 1;
	rep (i, 10 ) if (color(9, i ) ) return -1;
	
	int ret = 0;
	rep (i, 10 ) rep (j, 10 ) ret += cnt[i][j];

	return ret;	
}

void dfs ()
{
	int res = -1;
	rep (i, 1<<10 ){
		memset (cnt, 0, sizeof (cnt ) );
		rep (j, 10 ) cnt[0][j] = (i>>j) & 1;
		int t = solve();
		if (t >= 0 && (res < 0 || res > t ) ){
			res = t;
			memcpy (ans, cnt, sizeof (cnt ) );
		} // end if
	} // end rep
}

int main()
{
	int T; cin >> T;
	while (T--){
		memset (in, 0, sizeof (in ) );
		memset (ans, 0, sizeof (ans ) );
		rep (i, 10 ) rep (j, 10 ) cin >> in[i][j];
		dfs ();
		rep (i, 10 ) rep (j, 10 ) cout << ans[i][j] << ( j < 9 ? ' ' : '\n' );
	} // end while
		
	return 0;
}