#include <bits/stdc++.h>
using namespace std;
int parent[(1 << 15)];
int Find(int u) {
  if (parent[u] == u)
    return u;
  else
    return parent[u] = Find(parent[u]);
}
void MakeSet(int u) { parent[u] = u; }
bool Union(int a, int b) {
  a = Find(a);
  b = Find(b);
  if (a == b) return false;
  parent[b] = a;
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i <= 2 * m; i++) {
    MakeSet(i);
  }
  string s1, s2, s;
  s2 = "";
  for (int i = 0; i < m; i++) s2 += '0';
  int p1 = 0;
  int p2 = 1;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    s1 = "";
    cin >> s;
    for (int j = 0; j < m / 4; j++) {
      int num = (isdigit(s[j]) ? s[j] - '0' : s[j] - 'A' + 10);
      for (int k = 3; k >= 0; k--) s1 += ((num >> k) & 1) + '0';
    }
    for (int k = 0; k < m; k++) {
      if (s1[k] == '1') {
        ans++;
        if (s2[k] == '1') {
          if (Union(2 * k + p1, 2 * k + p2)) {
            ans--;
          }
        }
        if (k && s1[k - 1] == '1') {
          if (Union(2 * k + p1, 2 * k + p1 - 2)) ans--;
        }
      }
    }
    for (int k = 0; k < m; k++) {
      MakeSet(2 * k + p2);
    }
    s2 = s1;
    swap(p1, p2);
  }
  cout << ans << endl;
  return 0;
}
