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: def dfs(startx, starty):
    dxdy = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]
    stack = [(startx, starty)]
    reached = set()
    while stack:
        d = stack.pop()
        reached.add(d)
        for a in dxdy:
            x = d[0] + a[0]
            y = d[1] + a[1]
            if (x, y) not in reached and 0 <= x < W and 0 <= y < H and maze[y][x] != '0':
                stack.append((x, y))
    return reached


while True:
    W, H = map(int, input().split())
    if H == W == 0:
        break
    maze = [input().split()for _ in range(H)]
    ans = 0
    l = set()
    for i in range(H):
        for j in range(W):
            if (j, i) not in l and maze[i][j] == '1':
                l = l | dfs(j, i)
                ans += 1
    print(ans)

Python: import sys
sys.setrecursionlimit(10000000)


while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    G = [list(map(int, input().split())) for i in range(H)]
    dh = (0, -1, -1, -1, 0, 1, 1, 1)
    dw = (-1, -1, 0, 1, 1, 1, 0, -1)

    def dfs(h, w):
        for i in range(8):
            nh = h + dh[i]
            nw = w + dw[i]
            if nh < 0 or nh >= H or nw < 0 or nw >= W:
                continue
            if not G[nh][nw]:
                continue
            G[nh][nw] = 0
            dfs(nh, nw)
    
    cnt = 0
    for i in range(H):
        for j in range(W):
            if G[i][j]:
                dfs(i, j)
                cnt += 1
    print(cnt)

Python: import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)


def search(i, j):
    global ans
    if c[i][j] == 1:
        c[i][j] = -1
        search(i, j+1)
        search(i+1, j-1)
        search(i+1, j)
        search(i+1, j+1)
        search(i, j-1)
        search(i-1, j)
        search(i-1, j+1)
        search(i-1, j-1)


while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [[] for _ in range(h + 2)]
    c[0] = [0] * (w + 2)
    c[-1] = [0] * (w + 2)
    for i in range(h):
        c[i + 1] = [0] + list(map(int, input().split())) + [0]
    ans = 0
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if c[i][j] == 1:
                ans += 1
                search(i, j)
    print(ans)

Python: import sys
sys.setrecursionlimit(10000)
d = [-1,0,1]
def search(x,y,m):
    if not(0<=x<len(m[0]) and 0<=y<len(m)) or m[y][x]=='0': return
    m[y][x] = '0'
    for dx,dy in [[-1,-1],[0,-1],[1,-1],[-1,0],[1,0],[-1,1],[0,1],[1,1]]:
        search(x+dx,y+dy,m)
while True:
    w,h = map(int,input().split())
    if w==h==0: break
    m = [input().split() for _ in range(h)]
    c = 0
    for y in range(h):
        for x in range(w):
            if m[y][x]=='1':
                c += 1
                search(x,y,m)
    print(c)

Python: import sys
sys.setrecursionlimit(1000000)


def dfs(h, w, field, seen, dhw):
    seen[h][w] = True
    for dh, dw in dhw:
        nh = h + dh
        nw = w + dw

        if field[nh][nw] == 0:
            continue
        if seen[nh][nw]:
            continue

        dfs(nh, nw, field, seen, dhw)


def main():
    res = []
    while True:
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break

        field = []
        field.append([0] * (w + 2))
        for i in range(h):
            line = list(map(int, input().split()))
            field.append([0] + line + [0])
        field.append([0] * (w + 2))

        seen = [[False] * (w + 2) for x in range(h + 2)]
        dhw = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (-1, 1), (1, -1)]

        count = 0
        for ih in range(1, h+2):
            for iw in range(1, w+2):
                if seen[ih][iw]:
                    continue
                if field[ih][iw] == 1:
                    dfs(ih, iw, field, seen, dhw)
                    count += 1
        res.append(count)

    for i in res:
        print(i)


main()

Python: import sys
sys.setrecursionlimit(10000)
dx = [1, -1, 0, 0, 1, 1, -1, -1]
dy = [0, 0, 1, -1, 1, -1, 1, -1]


def dfs(y, x):
    if x < 0 or w <= x:
        return
    if y < 0 or h <= y:
        return
    if V[y][x]:
        return
    if C[y][x] == 0:
        return

    V[y][x] = True
    for i in range(8):
        dfs(y + dy[i], x + dx[i])


while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    C = [list(map(int, input().split())) for _ in range(h)]
    V = [[False]*w for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if C[y][x] == 0:
                continue
            if V[y][x]:
                continue
            dfs(y, x)
            ans += 1
    print(ans)

Python: import sys
sys.setrecursionlimit(10**9)
H = 0
W = 0
field = []
def dfs(x, y, px, py):
    for dx, dy in ((1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,1),(1,-1),(-1,-1)):
        dx += x
        dy += y
        if dx == px and dy == py: continue
        if not (0 <= dx < W and 0 <= dy < H): continue
        if field[dy][dx] == 1:
            field[dy][dx] = 2
            dfs(dx, dy, x, y)

while True:
    W, H = map(int, input().split())
    if H+W == 0: break
    ans = 0
    field = [list(map(int, input().split())) for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if field[h][w] == 1:
                ans += 1
                dfs(w,h,-1,-1)
    print(ans)

Python: import sys
sys.setrecursionlimit(10**7) 

dx = [1,0,-1,0,1,-1,1,-1]
dy = [0,1,0,-1,1,1,-1,-1]

def dfs(C,h,w):
    global seen
    seen[h][w] = True
    for dir in range(8):
        nh = h + dx[dir]
        nw = w + dy[dir]
        if nh < 0 or nh >= H or nw < 0 or nw >= W:
            continue
        if C[nh][nw] == 0:
            continue
        if seen[nh][nw]:
            continue

        dfs(C,nh,nw)
    
while True:
    W,H = map(int, input().split())
    if W==0 and H==0:
        break
    C = []
    for i in range(H):
        array = list(map(int, input().split()))
        C.append(array)
    
    count = 0
    seen = [[False]*W for _ in range(H)]
    for dh in range(H):
        for dw in range(W):
            if C[dh][dw] != 0 and seen[dh][dw] == False:
                dfs(C,dh,dw)
                count += 1
    
    print(count)