#include <bits/stdc++.h>
using namespace std;
long long delbit(long long a, int k) { return a & (~(1ll << k)); }
int delbit(int a, int k) { return a & (~(1 << k)); }
bool getbit(long long a, int k) { return 1 & (a >> k); }
bool getbit(int a, int k) { return 1 & (a >> k); }
bool getbit(unsigned long long a, int k) { return 1 & (a >> k); }
long long setbit(long long a, long long k) { return a |= (1 << k); }
int setbit(int a, int k) { return a |= (1 << k); }
inline long long mulmod(long long first, long long n, long long _mod) {
  long long res = 0;
  while (n) {
    if (n & 1) res = (res + first) % _mod;
    first = (first + first) % _mod;
    n >>= 1;
  }
  return res;
}
inline long long powmod(long long first, long long n, long long _mod) {
  long long res = 1;
  while (n) {
    if (n & 1) res = (res * first) % _mod;
    first = (first * first) % _mod;
    n >>= 1;
  }
  return res;
}
inline long long gcd(long long a, long long b) {
  long long t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
inline int gcd(int a, int b) {
  int t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long gcd(long long a, long long b, long long c) {
  return gcd(gcd(a, b), c);
}
inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }
template <typename A, typename B>
ostream &operator<<(ostream &stream, const pair<A, B> &p) {
  stream << "{" << p.first << "," << p.second << "}";
  return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const vector<A> &v) {
  stream << "[";
  for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " ";
  stream << "]";
  return stream;
}
template <typename A, typename B>
ostream &operator<<(ostream &stream, const map<A, B> &v) {
  stream << "[";
  for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " ";
  stream << "]";
  return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const set<A> &v) {
  stream << "[";
  for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " ";
  stream << "]";
  return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const priority_queue<A> &v) {
  priority_queue<A> q = v;
  stream << "[";
  while (!q.empty()) {
    stream << q.top() << " ";
    q.pop();
  }
  stream << "]";
  return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const queue<A> &v) {
  queue<A> q = v;
  stream << "[";
  while (!q.empty()) {
    stream << q.front() << " ";
    q.pop();
  }
  stream << "]";
  return stream;
}
template <typename T, typename T2>
void printarr(T a[], T2 sz, T2 beg = 0) {
  for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
  cout << endl;
}
bitset<((1 << 14) + 3)> a[(1 << 12) + 3];
string t[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
              "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int p[(1 << 15) + 3], n, m;
int find(int first) {
  if (p[first] == first) return first;
  return p[first] = find(p[first]);
}
bool merge(int a, int b) {
  int ind_a = find(a);
  int ind_b = find(b);
  if (ind_a == ind_b) return 0;
  p[ind_a] = ind_b;
  return 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int(i) = (1); (i) < (n + 1); (i)++) {
    for (int(j) = (0); (j) < (m / 4); (j)++) {
      char c;
      cin >> c;
      if (isalpha(c)) {
        for (int(z) = (0); (z) < (4); (z)++) {
          a[i][1 + j * 4 + z] = t[c - 'A' + 10][z] - '0';
        }
      } else {
        for (int(z) = (0); (z) < (4); (z)++) {
          a[i][1 + j * 4 + z] = t[c - '0'][z] - '0';
        }
      }
    }
  }
  int cnt1 = 0, cntM = 0;
  for (int(i) = (1); (i) < (n + 1); (i)++) {
    for (int(j) = (1); (j) < (m + 1); (j)++) {
      p[j] = p[j + m] - m;
      p[j + m] = j + m;
    }
    for (int(j) = (1); (j) < (m + 1); (j)++) {
      if (a[i][j] && a[i][j - 1]) {
        cntM += merge(j + m, j + m - 1);
      }
    }
    for (int(j) = (1); (j) < (m + 1); (j)++) {
      if (a[i][j]) cnt1++;
      if (a[i][j] && a[i - 1][j]) {
        cntM += merge(j, j + m);
      }
    }
  }
  cout << cnt1 - cntM << endl;
  return 0;
}
