library(png)
img <- readPNG("Unfilledcirc.png")
M <- img[ , , 1]
M <- ifelse(M < 0.5, 0, 1)
image(M, col = c(1, 0))


floodfill <- function(row, col, tcol, rcol) {
  if (tcol == rcol) return()
  if (M[row, col]!= tcol) return()
  M[row, col] <<- rcol
  floodfill(row - 1, col    , tcol, rcol) 
  floodfill(row + 1, col    , tcol, rcol) 
  floodfill(row    , col - 1, tcol, rcol) 
  floodfill(row    , col + 1, tcol, rcol) 
  return("filling completed")
}

options(expressions = 10000)
startrow <- 100; startcol <- 100
floodfill(startrow, startcol, 0, 2)

image(M, col = c(1, 0, 2))