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 <cstring>
#include <cmath>
using namespace std;
 
int board[12][12];
int used[12];
int mask[12];
int all = (1 << 10) - 1;
 
int makeCurrMask(int curr, int m){
    for(int i=0;i<10;i++){
        if((m >> i) & 1){
            int t = (i == 0) ? (7 >> 1) : (7 << (i - 1));
            curr ^= t;
        }
    }
    return curr & all;
}

void solve(){
    memset(mask, 0, sizeof(mask));
    memset(used, 0, sizeof(used));
    for(int y=0;y<10;y++){
        int m = 0;
        for(int x=0;x<10;x++){
            m = ((m << 1) + board[y][x]);
        }
        mask[y] = m;
    }

    for(int line=0;line<1<<10;++line){
        int prev = makeCurrMask(mask[0], line);
        int curr = mask[1] ^ line;

        used[0] = line;

        for(int y=1;y<9;++y){
            int doit = prev;
            int nprev = makeCurrMask(curr, doit);
            int ncurr = mask[y+1] ^ doit;

            prev = nprev;
            curr = ncurr;

            used[y] = doit;
        }

        used[9] = prev;

        if(curr ^ prev){
            continue;
        } else {
            break;
        }
    }
}
 
void output(){
    for(int y=0;y<10;y++){
        int m = used[y];
        for(int x=9;x>=0;x--){
            if(x != 9)cout << " ";
            cout << (((m >> x) & 1) ? 1 : 0);
        }
        cout << endl;
    }
}

int main()
{
    int n;
    cin >> n;
 
    for(int t=0;t<n;t++){
        for(int y=0;y<10;y++){
            for(int x=0;x<10;x++)cin >> board[y][x];
        }
        solve();
        output();
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define REP(i,b,n ) for(int i=b;i<n;i++)
#define rep(i,n)    REP(i,0,n)

#define UNFILLED false
#define FILLED true

#define ORI true
#define DUP false;
char inp[20][20];
bool m[20][20];
int _r,_c;
typedef struct Info{
  int x,y,r,c,rot;
};

class Piece{
public:
  int r[4],c[4];
  bool m[4][20][20];
  vector<Info> Put;
  int black;
  bool dup[4];
  bool operator<(const Piece & a)const{
    return Put.size() > a.Put.size();
  }
};

void rotate(Piece & a,int k){
  static bool tmp[20][20];
  rep(i,a.r[k-1]){
    rep(j,a.c[k-1]){
      tmp[i][j]=a.m[k-1][i][j];
    }
  }
  
  rep(i,a.r[k-1]){
    rep(j,a.c[k-1]){
      a.m[k][j][i]=tmp[i][a.c[k-1]-j-1];
    }
  }
  a.r[k]=a.c[k-1];
  a.c[k]=a.r[k-1];
}

void duplicate_check(Piece & a){
  rep(i,4)a.dup[i]=ORI;
  REP(i,1,4){
    rotate(a,i);
  }
  return;
  rep(k,4){
    REP(l,k+1,4){
      bool flag = false;
      if ( a.r[k] !=a.r[l] ||a.c[k] != a.c[l])continue;
      rep(i,a.r[k] && !flag)
	rep(j,a.c[k] && !flag)
	if(a.m[k][i][j]!=a.m[l][i][j] )flag=true;
      if (flag == false)a.dup[l]=DUP;
    }
  }
}

bool can_put(int x,int y,Piece & a,int r,int c,int k){
  //if ( x + a.c[k]-1 >=c ||y+a.r[k]-1>= r)return false;
  rep(i,a.r[k]){
    rep(j,a.c[k]){
      if ( a.m[k][i][j]==UNFILLED ){
      }else if (a.m[k][i][j]==FILLED ){
	//if ( x+j >=c || y+i >=r)return false;
	if ( m[y+i][x+j] == FILLED)return false;
      }
    }
  }
  return true;
}

void construct(int n,Piece * in,int r,int c){
  Info ins;
  rep(l,n){
    rep(i,r){
      rep(j,c){
	rep(k,4){
	  if (in[l].dup[k] == ORI && can_put(j,i,in[l],r,c,k) == true){
	    ins.x=j,ins.y=i,ins.r=in[l].r[k],ins.c=in[l].c[k];
	    ins.rot=k;
	    in[l].Put.push_back(ins);
	  }
	}
      }
    }
  }
}


bool Operation(Piece &in,int me,int flag){
  //flag=0 check,flag=1 put ,flag=2 remove
  int x=in.Put[me].x,y=in.Put[me].y,r=in.Put[me].r,c=in.Put[me].c,k=in.Put[me].rot;
  rep(i,r){
    rep(j,c){
      if ( x+j>=_c || y+i>=_r)continue;
      if( flag == 0){
	if ( m[y+i][x+j]==FILLED){
	  if (in.m[k][i][j]==FILLED)return false;
	}
      }else if ( flag == 1){
	if( in.m[k][i][j]==FILLED)m[y+i][x+j]=FILLED;
      }else if ( flag == 2){
	if (in.m[k][i][j]==FILLED)m[y+i][x+j]=UNFILLED;
      }
    }
  }

  return true;
}



bool solve(int n,int now,Piece *in){
  if ( n == now){
    return true;
  }

  rep(i,in[now].Put.size()){
    if ( Operation(in[now],i,0) ){
      Operation(in[now],i,1);
      if (solve(n,now+1,in)){
	Operation(in[now],i,2);

	return true;
      }
      Operation(in[now],i,2);
    }
  }


  return false;
}


main(){
  int r,c;
  char tmp[20];


  while(cin>>r >>c && r){
    _r=r;_c=c;
    Piece in[10];
    Piece sol[10];
    rep(i,10)in[i].Put.clear(),sol[i].Put.clear();
    int white = 0;
    rep(i,r){
      cin>>inp[i];
      rep(j,c){
	if ( inp[i][j]=='#')m[i][j]=FILLED;
	else m[i][j]=UNFILLED,white++;
      }
    }
    int n;
    cin>>n;
    rep(k,n){
      cin>>in[k].r[0]>>in[k].c[0];
      in[k].black=0;
      rep(i,in[k].r[0]){
	cin>>tmp;
	rep(j,in[k].c[0]){
	  if ( tmp[j] == '#')in[k].m[0][i][j]=FILLED,in[k].black++;
	  else in[k].m[0][i][j]=UNFILLED;
	}
      }
      duplicate_check(in[k]);
    }
    


    construct(n,in,r,c);

    int q;
    int test[10];
    cin>>q;
    rep(i,q){
      int m;
      cin>>m;
      int total=0,ind;
      rep(j,m){
	cin>>ind;
	test[j]=ind;
	total+=in[ind-1].black;
	sol[j]=in[ind-1];
      }      

      rep(j,m){
	if(  test[j] >n)total=-1;
	REP(k,j+1,m)if (test[j] == test[k])total=-1;
      }
      if ( white !=total){puts("NO");continue;}
      
      //sort(sol,sol+m);
      
      if ( solve(m,0,sol) )puts("YES");
      else puts("NO");
      
    }


  }
  return false;
}