You are given a matrix of size *n* × *m*. Each element of the matrix is either 1 or 0. You have to determine the number of connected components consisting of 1's. Two cells belong to the same component if they have a common border, and both elements in these cells are 1's.

Note that the memory limit is unusual!
The first line contains two numbers *n* and *m* (1 ≤ *n* ≤ 2<sup class="upper-index">12</sup>, 4 ≤ *m* ≤ 2<sup class="upper-index">14</sup>) — the number of rows and columns, respectively. It is guaranteed that *m* is divisible by 4.

Then the representation of matrix follows. Each of *n* next lines contains <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ba8e33536d5a77032dd11e833c49b75c5e65a20b.png" style="max-width: 100.0%;max-height: 100.0%;"/> one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from 0 to 9 or as uppercase Latin letters from *A* to *F*). Binary representation of each of these numbers denotes next 4 elements of the matrix in the corresponding row. For example, if the number *B* is given, then the corresponding elements are 1011, and if the number is 5, then the corresponding elements are 0101.

Elements are not separated by whitespaces.
In the first example the matrix is: 

It is clear that it has three components.

The second example: 

It is clear that the number of components is 2.

There are no 1's in the third example, so the answer is 0.
3 4
1
A
8
3
2 8
5F
E3
2
1 4
0
0
1 4
D
2
10 120
4100B3BC23752433106B89343D9BA9
D0412141283A93738E2805121044D9
B111606365A975606A60A641110C15
1134D161132633643B33139A153114
BA3F155302119840B0C06860401767
3545720632A7CCB123127825403069
B6404119C41BA1386A826135516134
501CE41C0920635BA221421DAA0825
2C4632C5217332A43372B0009530BA
B0108731422A737A5D856202C5D328
161
# -*- coding: utf-8 -*-

import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys

"""
created by shhuan at 2017/11/8 09:50

"""

M, N = map(int, input().split())

A = []
for i in range(M):
    A.append(list(input()))


def tob(valChar):
    v = int(valChar, 16)
    v = bin(v)
    v = list(v[2:])
    v = ['0'] * (4-len(v)) + v
    return v


def one_index(a, r):
    ones = [0] * N
    for c in range(N // 4):
        v = tob(a[r][c])
        for i in range(4):
            if v[i] == '1':
                ones[c*4+i] = 1

    groups = set()
    if ones[0] != 0:
        groups.add(1)
    for i in range(1, N):
        if ones[i] == 1:
            if ones[i-1] != 0:
                ones[i] = ones[i-1]
            else:
                ones[i] = i + 1
                groups.add(i + 1)

    return ones, groups

def one_index2(a, r):
    ones = []
    for c in range(N // 4):
        v = tob(a[r][c])
        t = []
        for i in range(4):
            if v[i] == '1':
                j = c * 4 + i
                if t and t[-1][1] == j - 1:
                    u = t.pop()
                    t.append((u[0], j))
                else:
                    t.append((j, j))
        if t:
            if ones and ones[-1][1] == t[0][0] - 1:
                u = ones.pop()
                ones.append((u[0], t[0][1]))
                for u in t[1:]:
                    ones.append(u)
            else:
                for u in t:
                    ones.append(u)
    return ones

ans = 0


last_ones, last_groups = one_index(A, 0)
for r in range(1, M):
    ones, groups = one_index(A, r)
    rep = {}

    for i in range(N):
        if last_ones[i] != 0 and ones[i] != 0:
            v = last_ones[i]
            if v in rep:
                groups -= {ones[i]}
                ones[i] = rep[v]
                j = i-1
                while j >= 0 and ones[j] != rep[v] and ones[j] != 0:
                    ones[j] = rep[v]
                    j -= 1
                j = i + 1
                while j < N and ones[j] != rep[v] and ones[j] != 0:
                    ones[j] = rep[v]
                    j += 1

            else:
                rep[v] = ones[i]
                last_groups -= {last_ones[i]}
    ans += len(last_groups)
    last_ones, last_groups = ones, {v for v in ones if v != 0}


print(ans + len(last_groups))



