import sequtils,queues

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': break
    result = 10 * result + k.ord - '0'.ord

let h = scan()
let w = scan()

var isWater : array[3001,array[3001,bool]]
for y in 0..<h:
  for x in 0..<w:
    isWater[x][y] = getchar_unlocked() == '1'
    discard getchar_unlocked()
const dxdy4 = @[(0,1),(1,0),(0,-1),(-1,0)].mapIt((x:it[0].int32,y:it[1].int32))
proc check(sx,sy:int32)  =
  var X = initQueue[int32]()
  var Y = initQueue[int32]()
  X.enqueue(sx)
  Y.enqueue(sy)
  while X.len() > 0:
    let x = X.dequeue()
    let y = Y.dequeue()
    isWater[sx][sy] = false
    for i in 0..<4:
      let nx = x+dxdy4[i].x
      let ny = y+dxdy4[i].y
      if nx < 0 or nx >= w : continue
      if ny < 0 or ny >= h : continue
      if not isWater[nx][ny] : continue
      X.enqueue(nx)
      Y.enqueue(ny)
      isWater[nx][ny] = false
var ans = 0
for x in 0.int32..<w.int32:
  for y in 0.int32..<h.int32:
    if not isWater[x][y]: continue
    check(x,y)
    ans += 1
echo ans