#include <bits/stdc++.h>
using namespace std;
vector<bool> v;
int A[33000], z, m;
void convert(int i, char c) {
  string s;
  if (c == '0')
    s = "0000";
  else if (c == '1')
    s = "0001";
  else if (c == '2')
    s = "0010";
  else if (c == '3')
    s = "0011";
  else if (c == '4')
    s = "0100";
  else if (c == '5')
    s = "0101";
  else if (c == '6')
    s = "0110";
  else if (c == '7')
    s = "0111";
  else if (c == '8')
    s = "1000";
  else if (c == '9')
    s = "1001";
  else if (c == 'A')
    s = "1010";
  else if (c == 'B')
    s = "1011";
  else if (c == 'C')
    s = "1100";
  else if (c == 'D')
    s = "1101";
  else if (c == 'E')
    s = "1110";
  else if (c == 'F')
    s = "1111";
  v[i] = s[0] - '0';
  v[i + 1] = s[1] - '0';
  v[i + 2] = s[2] - '0';
  v[i + 3] = s[3] - '0';
}
int Find(int x) {
  int root = x, tmp;
  while (A[root] >= 0) root = A[root];
  while (x != root) {
    tmp = x;
    x = A[x];
    A[tmp] = root;
  }
  return root;
}
void Union(int a, int b) {
  int x = Find(a), y = Find(b);
  if (x != y) {
    z++;
    if (x >= m && y >= m) {
      if (A[x] > A[y]) swap(x, y);
    } else if (x < m)
      swap(x, y);
    A[x] += A[y];
    A[y] = x;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, i, o = 0;
  string s;
  cin >> n >> m;
  for (i = 0; i < 2 * m; i++) {
    v.push_back(0);
    A[i] = -1;
  }
  while (n--) {
    cin >> s;
    for (i = 0; i < m / 4; i++) convert(m + 4 * i, s[i]);
    for (i = m; i < 2 * m; i++)
      if (v[i]) {
        o++;
        if (i > m && v[i - 1]) Union(i - 1, i);
        if (v[i - m]) Union(i - m, i);
      }
    for (i = 0; i < m; i++) {
      if (A[i + m] < 0)
        A[i] = -1;
      else
        A[i] = Find(i + m) - m;
      v[i] = v[i + m];
    }
    for (i = m; i < 2 * m; i++) A[i] = -1;
  }
  cout << o - z;
  return 0;
}
