#include <iostream>
#include <vector>
#include <map>
#include <cassert>

using namespace std;

class BoolMatr {
  size_t start;
  std::vector<std::map<size_t, bool>> data;

  public:
    BoolMatr () : start(0) {}

    BoolMatr (size_t n, size_t m, size_t start) : start(start) {
        resize (n, m);
    }

   void resize (size_t n, size_t m) {
       data.resize (n);
       for (size_t i=0; i < n; ++i)
         for (size_t j=0; j < m; ++j)
           if (data[i].find (j) == data[i].end())
             data[i][j] = false;
   }
   
   size_t rows() const { return data.size(); }
   size_t cols() const { return data[0].size(); }
   size_t from() const { return start; }

   bool& operator()(size_t i, size_t j) {
      assert (i-start < data.size() && j-start < data[i-start].size());
      return data[i-start][j-start];
   }

   bool operator()(size_t i, size_t j) const {
   	  return const_cast<BoolMatr*>(this)->operator()(i,j);
   }
   	
   BoolMatr& operator+= (const BoolMatr& rhs) {
      assert (data.size() != 0 && data.size() == rhs.data.size() && data[0].size() == rhs.data[0].size());
      for (size_t i=0; i < data.size(); ++i)
        for (size_t j=0; j < data[i].size(); ++j)
          (*this)(i+from(), j+from()) ^= rhs(i+rhs.from(), j+rhs.from());
      return *this;
   }
};

BoolMatr operator+ (const BoolMatr& lhs, const BoolMatr& rhs) {
  return BoolMatr (lhs)+=rhs;
}

std::ostream& operator << (std::ostream& os, const BoolMatr& m) {
	for (size_t i=0; i < m.rows(); ++i) {
  	  for (size_t j=0; j < m.cols(); ++j)
  	    std::cout << m(i+m.from(),j+m.from()) << ' ';
	  std::cout << std::endl;
	}
  return os;
}

int main() {
	BoolMatr a(2,3,1), b(2,3,1);
	
	for (size_t i=a.from(); i < a.from()+a.rows(); ++i)
  	  for (size_t j=a.from(); j < a.from()+a.cols(); ++j)
	     a(i,j)= (i+j)%2 == 0;
	     
	for (size_t i=b.from(); i < b.from()+b.rows(); ++i)
  	  for (size_t j=b.from(); j < b.from()+b.cols(); ++j)
	     b(i,j)= (i+j)%4 == 0;
	
	BoolMatr c = a+b;
	
	std::cout << a << std::endl << b << std::endl << c << std::endl;
	
	return 0;
}