#include<bits/stdc++.h>
using namespace std;
int wayc[4] = {0, 1, 0, -1};
int wayd[4] = {1, 0, -1, 0};
bool a[105][105];
char t;
int n, m, ans = 0;
queue< pair<int, int> > q1;
int bfs(int c, int d){
a[c][d] = 0;
q1.push({c, d});
while(q1.size()){
for(int i = 0; i < 4; i++){
int nx = q1.front().first+wayc[i];
int ny = q1.front().second+wayd[i];
if(1 <= nx && nx <= n && 1 <= ny && ny <= m && a[nx][ny]){
a[nx][ny] = 0;
q1.push({nx, ny});
}
}
q1.pop();
}
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> t;
if(t >= '1' && t <= '9')
a[i][j] = 1;
}
}
for(int i = 1; i <= n; i++){