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 <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; ++i)
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define P pair<int, int>
const int H = 10;
const int W = 10;
typedef vector< vector<int> > ivv;
typedef vector<int> iv;

int my[5] = {1, -1, 0, 0, 0};
int mx[5] = {0, 0, 1, -1, 0};

class C{
  public:
    ivv u;
    int t, y, x;
    C(){}
    C(ivv _u, int _t, int _y, int _x){ u = _u; t = _t; y = _y; x = _x; }
};

queue<C> open;

void rev(ivv &v, int y, int x){
  REP(i, 5){
    int ny = y + my[i], nx = x + mx[i];
    if(ny >= 0 && nx >= 0 && ny < H && nx < W) v[ny][nx] = (v[ny][nx] + 1) % 2;
  }
}

bool next(ivv v, ivv &u, int y, int x){
  REP(i, H) REP(j, W) if(u[i][j]) rev(v, i, j);
  int a = 0, b = 0;
  REP(i, 5){
    int ny = y + my[i], nx = x + mx[i];
    if(ny >= 0 && nx >= 0 && ny < H && nx < W){
      if(v[ny][nx]) ++a;
      else ++b;
    }
  }
  return a > b;
}


bool end(ivv v, ivv &u){
  REP(i, H) REP(j, W) if(u[i][j]) rev(v, i, j);
  REP(i, H) REP(j, W) if(v[i][j] == 1) return false;
  return true;
}

ivv bfs(ivv &v){
  open = queue<C>();
  ivv _u(H, iv(W, 0));
  open.push(C(_u, 0, 0, -1));
  while(!open.empty()){
    C tmp = open.front(); open.pop();
    ivv u = tmp.u; int t = tmp.t;
    if(end(v, u)) return u;
    FOR(y, tmp.y, H){
      REP(x, W){
        if(y == tmp.y && x <= tmp.x) continue;
        if(!u[y][x] && next(v, u, y, x)){
          rev(v, y, x);
          u[y][x] = true;
          open.push(C(u, t + 1, y, x));
          u[y][x] = false;
          rev(v, y, x);
        }
      }
    }
  }
  return ivv();
}

int main() {
  int T; cin >>T;
  REP(t, T){
    ivv v(H, iv(W));
    REP(i, H) REP(j, W) cin >>v[i][j];
    ivv ans = bfs(v);
    REP(i, H){
      REP(j, W) cout <<ans[i][j] <<(j + 1 == W ? "" : " ");
      cout <<endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

bool field[11][11], ans[11][11];

int main(){
  int n;
  cin >> n;
  while(n--){
    for(int i = 0 ; i < 10 ; i++){
      for(int j = 0 ; j < 10 ; j++){
	cin >> field[i][j];
      }
    }
    
    memset(ans, 0, sizeof(ans));
    
    for(int y = 0 ; y < 10 ; y++){
      for(int x = 0 ; x < 10 ; x++){
	if(y == 0 && x == 0){
	  if(field[0][0] && field[0][1] && field[1][0] && field[1][1]) ans[0][0] = 1;
	}
	else if(y == 0 && x == 9){
	  if(field[0][9] && field[0][8] && field[1][8] && field[1][9]) ans[0][9] = 1;
	}
	else if(y == 9 && x == 0){
	  if(field[9][0] && field[8][0] && field[8][1] && field[9][1]) ans[9][0] = 1;
	}
	else if(y == 9 && x == 9){
	  if(field[9][9] && field[9][8] && field[8][9] && field[8][8]) ans[9][9] = 1;
	}
	else if(y == 0){
	  if(field[0][x] && field[0][x+1] && field[1][x] && field[0][x-1]) ans[0][x] = 1;
	}
	else if(x == 0){
	  if(field[y][0] && field[y+1][0] && field[y][1] && field[y-1][0]) ans[y][0] = 1;
	}
	else if(y == 9){
	  if(field[9][x] && field[9][x+1] && field[8][x] && field[9][x-1]) ans[9][x] = 1;
	}
	else if(x == 9){
	  if(field[y][9] && field[y+1][9] && field[y][8] && field[y-1][9]) ans[y][0] = 1;
	}
	else if(field[y][x] && field[y-1][x] && field[y+1][x] && field[y][x-1] && field[y][x+1]){
	  ans[y][x] = 1;
	}
	else if(!field[y][x] && !field[y][x+1] && field[y-1][x] && field[y][x-1] &&
		field[y+1][x] && field[y+1][x+1] && field[y-1][x+1] &&
		field[y][x+2]){
	  ans[y][x] = 1, ans[y][x+1] = 1;
	}
	else if(!field[y][x] && !field[y+1][x] && field[y-1][x] && field[y][x-1] &&
		field[y][x+1] && field[y+1][x-1] && field[y+1][x+1] &&
		field[y+2][x]){
	  ans[y][x] = 1, ans[y+1][x] = 1;
	}
      }
    }
    
    for(int i = 0 ; i < 10 ; i++){
      for(int j = 0 ; j < 10 ; j++){
	cout << ans[i][j];
	if(j != 9) cout << ' ';
      }
      cout << endl;
    }
    cout << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstring>
using namespace std;

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

int ori[10][10];
int arr[10][10];
int arr2[10][10];
    
void flip(int i, int j) {
  arr[i][j] = 1 - arr[i][j];
  arr2[i][j] = 1 - arr2[i][j];
  for(int k=0; k<4; k++) {
    int nx = j+dx[k], ny = i+dy[k];
    if(0<=ny && ny<10 && 0<=nx && nx<10) {
      arr[ny][nx] = 1 - arr[ny][nx];
    }
  }
}

int main() {
  int Tc; cin >> Tc;
  
  while(Tc--) {
    
    for(int i=0; i<10; i++)
      for(int j=0; j<10; j++)
	cin >> ori[i][j];
    
    bool good = 0;
    for(int I=0; I<(1<<10); I++) {
      memset(arr2, 0, sizeof(arr2));
      memcpy(arr, ori, sizeof(ori));
      bool ok = 1;
      for(int i=0; i<10; i++) {
	for(int j=0; j<10; j++) {
	  
	  if(i == 0) {
	    if((I>>j) & 1) {
	      if(arr[0][j] == 0) flip(0, j);
	    }
            else {
	      if(arr[0][j] == 1) flip(0, j);
            }
          }
	  else {
	    if(arr[i-1][j] == 1) flip(i, j);
	  }
	  
	} 
      }
      
      for(int i=0; i<10; i++) {
	if(arr[9][i]) ok = 0;
      }
      if(ok) {
	good = 1;
	break;
      }
    }
    if(good) {
      for(int i=0; i<10; i++) {
	for(int j=0; j<10; j++) {
	  if(j) cout << " ";
	  cout << arr2[i][j];
	}
	cout << endl;
      }
    }
  }
  
  return 0;
}