#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long long inf = numeric_limits<long long>::max() / 2;
const long double eps = 1e-9;
const long double pi = acos(-1);
template <typename T>
inline bool mineq(T& a, T b) {
  return (a > b) ? (a = b, 1) : 0;
}
template <typename T>
inline bool maxeq(T& a, T b) {
  return (a < b) ? (a = b, 1) : 0;
}
inline void solve(), read();
const string file = "";
signed main() {
  if (file != "") {
    freopen((file + ".in").c_str(), "r", stdin);
    freopen((file + ".out").c_str(), "w", stdout);
  }
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  read();
  solve();
  return 0;
}
struct dsu {
  long long n;
  vector<long long> val;
  vector<long long> t;
  dsu() {}
  dsu(long long n_) {
    n = n_;
    t.resize(2 * n);
    iota((t).begin(), (t).end(), 0);
    val.assign(2 * n, 0);
  }
  long long find(long long v) {
    if (v == t[v]) {
      return v;
    }
    return t[v] = find(t[v]);
  }
  long long join(long long v, long long u) {
    v = find(v), u = find(u);
    if (v == u) {
      return 0;
    }
    t[u] = v;
    return 1;
  }
  void shift() {
    for (long long i = 0; i < n; i++) {
      t[i] = t[i + n] - n;
      val[i] = val[i + n];
      t[i + n] = i + n;
      val[i + n] = 0;
    }
  }
};
const long long MAXM = 1ll << 14;
long long n, m;
dsu d;
bool checkbit(long long n, long long p) { return n & (1ll << p); }
long long get(char c) {
  if ('0' <= c && c <= '9') {
    return c - '0';
  }
  return 10 + c - 'A';
}
inline void solve() {
  long long cnt1 = 0;
  long long cntm = 0;
  d = dsu(m);
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m / 4; j++) {
      char c;
      cin >> c;
      long long v = get(c);
      for (long long k = 0; k < 4; k++) {
        d.val[m + j * 4 + k] = checkbit(v, 3 - k);
        cnt1 += checkbit(v, 3 - k);
      }
    }
    for (long long i = 0; i < m; i++) {
      if (d.val[i] && d.val[i + m]) {
        cntm += d.join(i + m, i);
      }
      if (i && d.val[i + m] && d.val[i - 1 + m]) {
        cntm += d.join(i - 1 + m, i + m);
      }
    }
    d.shift();
  }
  long long ans = cnt1 - cntm;
  cout << ans << "\n";
}
inline void read() { cin >> n >> m; }
