fn main() {
    proconio::input! {
        h: usize,
        w: usize,
        _k: usize,
        ss: [proconio::marker::Chars; h],
    }
    enum State {
        NotFound {
            start: usize,
        },
        Found {
            start: usize,
            found: usize,
            num: usize,
        },
    }
    let mut mat = vec![vec![0usize; w]; h];
    let mut state = State::NotFound { start: 0 };
    let mut y_group = vec![];
    for y in 0..h {
        match state {
            State::Found { start, found, num } => {
                let count = ss[y].iter().filter(|x| **x == '#').count();
                if count != 0 {
                    y_group.push((start, y, found, num));
                    state = State::Found {
                        start: y,
                        found: y,
                        num: count,
                    };
                }
            }
            State::NotFound { start } => {
                let count = ss[y].iter().filter(|x| **x == '#').count();
                if count != 0 {
                    state = State::Found {
                        start,
                        found: y,
                        num: count,
                    };
                }
            }
        }
    }
    if let State::Found { start, found, num } = state {
        y_group.push((start, h, found, num));
    } else {
        unreachable!();
    }

    let mut cur = 1;
    state = State::NotFound { start: 0 };
    for (y_start, y_end, y_found, _num) in y_group {
        for x in 0..w {
            match state {
                State::Found {
                    start,
                    found: _,
                    num: _,
                } => {
                    if ss[y_found][x] == '#' {
                        for y in y_start..y_end {
                            for x in start..x {
                                mat[y][x] = cur;
                            }
                        }
                        cur += 1;
                        state = State::Found {
                            start: x,
                            found: x,
                            num: 1,
                        };
                    }
                }
                State::NotFound { start } => {
                    if ss[y_found][x] == '#' {
                        state = State::Found {
                            start,
                            found: x,
                            num: 1,
                        };
                    }
                }
            }
        }
        if let State::Found {
            start,
            found: _,
            num: _,
        } = state
        {
            for y in y_start..y_end {
                for x in start..w {
                    mat[y][x] = cur;
                }
            }
            cur += 1;
            state = State::NotFound { start: 0 };
        }
    }
    let ans = mat
        .iter()
        .map(|line| {
            line.iter()
                .map(|cell| cell.to_string())
                .collect::<Vec<_>>()
                .join(" ")
        })
        .collect::<Vec<_>>()
        .join("\n");
    println!("{}", ans);
}


Measured memory_bytes: 4148