#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
char arr[410][410];
int chk[410][410];
int pos[410][410][2];
int res[160010];
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
int n, m;
char c;
int p;
int f(int x, int y, int px, int py)
{
int r, nx, ny, npx, npy, i;
chk[x][y] = p;
pos[x][y][0] = px;
pos[x][y][1] = py;
r = 1;
for(i = 0; i<4; i++)
{
nx = x + dx[i];
ny = y + dy[i];
npx = px;
npy = py;
if(nx < 0)
{
npx--;
nx += n;
}
else if(nx >= n)
{
npx++;
nx -= n;
}
if(ny < 0)
{
npy--;
ny += m;
}
else if(ny >= m)
{
npy++;
ny -= m;
}
if(arr[nx][ny] != c)
continue;
if(!chk[nx][ny])
{
int t = f(nx, ny, npx, npy);
if(t == -1)
return -1;
r += t;
continue;
}
if(pos[nx][ny][0] == npx && pos[nx][ny][1] == npy)
continue;
return -1;
}
return r;
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int t, i, j;
scanf("%d%d", &n, &m);
for(i = 0; i<n; i++)
scanf("%s", arr[i]);
p = 1;
for(i = 0; i<n; i++)
{
for(j = 0; j<m; j++)
{
if(chk[i][j])
{
printf("%d ", res[chk[i][j]]);
continue;
}
c = arr[i][j];
t = f(i, j, 0, 0);
res[p++] = t;
printf("%d ", t);
}
printf("\n");
}
}
/*
5 4
0000
1101
0100
0111
0001
*/