Grid traversal for given nxm grid