Counting Grid C++ Implementation