static void dilate(DenseGrid2i grid, DenseGrid2i tmp, bool corners = true)
        {
            if (tmp == null)
                tmp = new DenseGrid2i(grid.ni, grid.nj, 0);

            int ni = grid.ni, nj = grid.nj;

            for (int i = 1; i < ni - 1; i++) {
                for (int j = 1; j < nj - 1; j++) {
                    if ( grid[i,j] == 1 ) {
                        tmp[i, j] = 1;
                        tmp[i - 1, j] = 1;
                        tmp[i, j + 1] = 1;
                        tmp[i + 1, j] = 1;
                        tmp[i, j - 1] = 1;
                        if (corners) {
                            tmp[i - 1, j + 1] = 1;
                            tmp[i + 1, j + 1] = 1;
                            tmp[i + 1, j - 1] = 1;
                            tmp[i - 1, j - 1] = 1;
                        }
                    }
                }
            }
            grid.copy(tmp);
        }
        static void dilate_loners(DenseGrid2i grid, DenseGrid2i tmp, int mode)
        {
            if (tmp == null)
                tmp = new DenseGrid2i(grid.ni, grid.nj, 0);

            int ni = grid.ni, nj = grid.nj;

            for (int i = 1; i < ni - 1; i++) {
                for (int j = 1; j < nj - 1; j++) {
                    if (grid[i, j] == 1) {
                        tmp[i, j] = 1;

                        int nbrs =
                          grid[i - 1, j] + grid[i - 1, j + 1]
                        + grid[i, j + 1] + grid[i + 1, j + 1]
                        + grid[i + 1, j] + grid[i + 1, j - 1]
                        + grid[i, j - 1] + grid[i - 1, j - 1];

                        if (nbrs == 0) {
                            if (mode != 3) {
                                tmp[i - 1, j] = 1;
                                tmp[i + 1, j] = 1;
                                tmp[i, j + 1] = 1;
                                tmp[i, j - 1] = 1;
                            }
                            if (mode == 2 || mode == 3) {
                                tmp[i - 1, j + 1] = 1;
                                tmp[i + 1, j + 1] = 1;
                                tmp[i + 1, j - 1] = 1;
                                tmp[i - 1, j - 1] = 1;
                            }
                        }
                    }
                }
            }
            grid.copy(tmp);
        }