Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

Python: import sys
sys.setrecursionlimit(10000)

def place(i, j, w):
    return i*w + j

def get_path(w, h, c):
    path = list()
    for i in range(h):
        for j in range(w):
            # place = i*w + j
            can_go = list()
            if c[i][j] != 0:
                if i != 0 and c[i-1][j] != 0:
                    can_go.append( place(i-1, j, w) )
                
                if i != h-1 and c[i+1][j] != 0:
                    can_go.append( place(i+1, j, w) )

                if j != 0 and c[i][j-1] != 0:
                    can_go.append( place(i, j-1, w) )
                
                if j != w-1 and c[i][j+1] != 0:
                    can_go.append( place(i, j+1, w) )
                
                if i != 0 and j!= 0 and c[i-1][j-1] != 0:
                    can_go.append( place(i-1, j-1, w) )
                
                if i != 0 and j != w-1 and c[i-1][j+1] != 0:
                    can_go.append( place(i-1, j+1, w) )
                
                if i!= h-1 and j != 0 and c[i+1][j-1] != 0:
                    can_go.append( place(i+1, j-1, w) )
                
                if i!= h-1 and j != w-1 and c[i+1][j+1] != 0:
                    can_go.append( place(i+1, j+1, w) )
                
            path.append(can_go)
    
    return path

def dfs(p, path, visit, w, h, c):
    visit[p] = 1 # 来訪時刻の記録

    for nxt in path[p]: #繋がってる点の内
        if visit[nxt] == 0: # 未探索の場合には
            dfs(nxt, path, visit, w, h, c) # 先に進む
    
    return

# def print_path(i, j, path, w, h):
#     arr = path[i*w + j]
#     print("print path start")
#     for x in arr:
#         print(x//w, x%w)
#     print("print path fin")

def solve(w, h, c):
    path = get_path(w, h, c)
    visit = [0]*(w*h)

    # print_path(3, 18, path, w, h)
    ans = 0
    for place in range(w*h):
        i = place // w
        j = place % w
        if c[i][j] != 0 and visit[place] == 0:
            dfs(place, path, visit, w, h, c)
            # print(i, j)
            ans += 1
    
    print(ans)
    return 

w, h = map(int, input().split())
while w != 0 and h != 0:
    c = [list(map(int, input().split())) for _ in range(h)]
    solve(w, h, c)
    w, h = map(int, input().split())

Python: from collections import deque

while True:
    w,h=map(int,input().split())
    if (w,h)==(0,0):
        break
    p=[]
    p.append([False for j in range(w+2)])
    for i in range(1,h+1):
        p.append([False]+[cij==1 for cij in list(map(int,input().split()))]+[False])
    p.append([False for j in range(w+2)])

    r=[[False for j in range(w+2)] for i in range(h+2)]
    ans=0
    for i in range(1,h+1):
        for j in range(1,w+1):
            if p[i][j] and not r[i][j]:
                q=deque()
                q.append((i,j));r[i][j]=True
                while q:
                    u=q.popleft()
                    ui,uj=u
                    for v in [(vi,vj) for vi in [ui-1,ui,ui+1] for vj in [uj-1,uj,uj+1]]:
                        vi,vj=v
                        if p[vi][vj] and not r[vi][vj]:
                            q.append(v);r[vi][vj]=True
                ans+=1
    print(ans)

Python: import sys
def dfs(tiles, W, H, x, y):
    tiles[y][x] = '0'
    for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
        if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
            dfs(tiles, W, H, x+dx, y+dy)
    return

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [[c for c in input().split()] for h in range(H)]
        cnt = 0
        for h in range(H):
            for w in range(W):
                if tiles[h][w] == '1':
                    cnt += 1
                    dfs(tiles, W, H, w, h)
        print(cnt)

if __name__ == '__main__':
    sys.getrecursionlimit(10000)
    main()

Python: import sys
def f(n):
  print>>sys.stderr, n
  f(n+1)
f(1)

Python: import sys
def dfs(tiles, W, H, x, y):
    tiles[y][x] = '0'
    for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
        if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
            dfs(tiles, W, H, x+dx, y+dy)
    return

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [[c for c in input().split()] for h in range(H)]
        cnt = 0
        for h in range(H):
            for w in range(W):
                if tiles[h][w] == '1':
                    cnt += 1
                    dfs(tiles, W, H, w, h)
        print(cnt)

if __name__ == '__main__':
    sys.setrecursionlimit(100000)
    main()

Python: import sys
sys.setrecursionlimit(4100000)

def dfs(x, y):
  atlas[y][x] = 0
  for horizonal in range(-1, 2):
    for vertical in range(-1, 2):
      nx = x + horizonal
      ny = y + vertical
      if nx < 0 or w <= nx or ny < 0 or h <= ny:
        continue
      if atlas[ny][nx] == 0:
        continue
      dfs(nx, ny)

while True:
  count = 0
  w, h = map(int, input().split())
  atlas = []
  if w == 0 and h == 0:
    break
  for _ in range(h):
    atlas.append(list(map(int, input().split())))
  for x in range(w):
    for y in range(h):
      if atlas[y][x] == 0:
        continue
      dfs(x, y)
      count += 1
  print(count)

Python: def solve():
    w, h = [int(x) for x in input().split()]
    if w == h == 0:
        return False
    field = [[int(x) for x in input().split()] for _ in range(h)]

    ans = 0
    for x in range(w):
        for y in range(h):
            if field[y][x] > 0:
                mark(field, w, h, x, y)
                ans += 1
    print(ans)
    return True


def mark(field, w, h, x, y):
    if x < 0 or x >= w or y < 0 or y >= h:
        return

    if field[y][x] != 1:
        return

    field[y][x] = -1
    mark(field, w, h, x+1, y+1)
    mark(field, w, h, x+1, y)
    mark(field, w, h, x+1, y-1)
    mark(field, w, h, x, y+1)
    mark(field, w, h, x, y-1)
    mark(field, w, h, x-1, y+1)
    mark(field, w, h, x-1, y)
    mark(field, w, h, x-1, y-1)
    

while solve():
    pass

Python: def dfs(i,j,vis,e):
  q=[(i,j)]
  while q:
    x,y=q.pop()
    vis[x][y]=0
    for dx in [-1,0,1]:
      for dy in [-1,0,1]:
        nx=x+dx
        ny=y+dy
        if 0<=nx<h and 0<=ny<w and vis[nx][ny] and e[nx][ny]=='1':q+=[(nx,ny)]
  return 1
def main(w,h):
  e=[input().split() for _ in range(h)]
  vis=[[1]*w for _ in range(h)]
  cnt=0
  for i in range(h):
    for j in range(w):
      if vis[i][j] and e[i][j]=='1':cnt+=dfs(i,j,vis,e)
  print(cnt)
while True:
  w,h=map(int,input().split())
  if (w,h)==(0,0):break
  main(w,h)