[Question]
  [
One of the interesting aspects of gravity is that, as far as I'm aware, you can't just have stuff floating in midair.
However, it seems not everyone at the Association of Random Castle Builders is aware of this fact, leading to castles such as this one:
```
                      #
                      #
    #  #      #  #   ###
    ####      ####   # #
    #### #  # ####   ###
    ##############   ###
    ######  ######   ###
    #####    #####   ###
                     ###
``````````````````````````````
```
and this one:
```
                                       # # #    # # #   
                                       ##############
                                       ###  ####  ###
    #  #      #  #      #  #      #  # ###  ####  ### #  #      #  #      #  #      #  #
    ####      ####      ####      #### ############## ####      ####      ####      ####
    #### #  # #### #  # #### #  # #### ## ######## ## #### #  # #### #  # #### #  # ####
    ####################################################################################
    ######  ########  ########  ########  ########  ########  ########  ########  ######
    ###################################    ######    ###################################
    ###################################    ######    ###################################
                                       ##
                                         ##
                                           ##
                                             ##
                                               ##
````````````````````````````````````````````````````````````````````````````````````````````
```
and even this one:
```
       ##########
   ####   #      ###
#######################
            #
              #
                #
                  #
                    #  # # #
                  #   #  ###
                   #   # ###
                # # #  #  ##
                # # ##   ###
                 #  #  #####
                   #   #####
                  # #  #####
                       #####
                       ## ##
                       #####
                       #####
                       ## ##
                       ## ##
````````````````````````````````````````````
```
# Challenge
For a valid castle, all blocks will be connected to the ground either directly or indirectly. Your [program or function](http://meta.codegolf.stackexchange.com/a/2422/13486 "Default for Code Golf: Program, Function or Snippet?") will be given a castle such as the ones above as input, and your program must return a [truthy or falsy value](http://meta.codegolf.stackexchange.com/a/2194/13486 "Interpretation of Truthy/Falsey") reflecting whether the castle is valid or not.
## Rules
* Input is given as a string.
* All valid castles rest on a surface, ``````````. (If the input string does *not* contain a surface, the castle is invalid.)
* You can assume all input will satisfy these criteria:
	+ The surface will always be flat.
	+ The surface will always be at least as wide as the castle, so there will be no blocks to the left or right of the ground.
	+ The input will never have `#` below the surface.
	+ Input will only contain characters given in this challenge. (`#`, ```, space or newline.)
	+ You may assume the input will always contain at least one character.
* Blocks are connected if they are either horizontally or vertically adjacent. Diagonal does not count!
	+ Connected: 
	```
	#	or	##  
	#
	```
	+ Not connected: 
	```
	#      or	# #	or	 #  
	                  		#  
	#
	```
* Castles must exist to be valid. (In other words, inputs without any `#` must give a falsy value.)
* Input will only contain characters given in this challenge. (`#`, ```, space or newline.)
* You may assume the input will always contain at least one character.
* Standard [I/O](http://meta.codegolf.stackexchange.com/q/2447/13486 "Default for Code Golf: Input/Output methods") and [loophole](http://meta.codegolf.stackexchange.com/q/1061/13486 "Loopholes that are forbidden by default") rules apply.
## Test cases
### Falsy
* All the examples given above.
* ```
#  #      #  #  
####      ####  
#### #  # ####  
##############  
######  ######  
#####    #####
```
 (No ground.)
* ```
#  
 ###      ####  
#### #  # ####  
##############  
######  ######  
#####    #####  
``````````````
```
 (Topmost block is not connected either horizontally or vertically.)
* ```
     
```
```
 (No castle.)
* ```
  
  
                                     # # #    # # #     
                                     ##############  
                                     ##### ## #####  
  #  #      #  #      #  #      #  # #### #  # #### #  #      #  #      #  #      #  #  
  ####      ####      ####      ####  ##  ####  ##  ####      ####      ####      ####  
  #### #  # #### #  # #### #  # #### #  # #### #  # #### #  # #### #  # #### #  # ####  
  ####################################################################################  
  ######  ########  ########  ########  ########  ########  ########  ########  ######  
  ###################################    ######    ###################################  
  ###################################    ######    ###################################  
````````````````````````````````````````````````````````````````````````````````````````
```
(Central tower isn't connected to the rest of the castle because there are no horizontally or vertically adjacent blocks connecting it.)
* ```
     
```
 (No castle.)
* ```
  
```
 (No castle, just a single newline character.)
* ```
# #  
#  
```````
```
 (Rightmost block is not connected either horizontally or vertically.)
* ```
     
```
```
 (No castle.)
### Truthy
* ```
#  
`
```
* ```
#  #      #  #  
####      ####  
#### #  # ####  
##############  
######  ######  
#####    #####  
``````````````
```
* ```
                      #  
                      #  
    #  #      #  #   ###  
    ####      ####   # #  
    #### #  # ####   ###  
    ##############   ###  
    ######  ######   ###  
    #####    #####   ###  
    #####    #####   ###  
``````````````````````````````
```
* ```
                                       # # #    # # #     
                                       ##############  
                                       ###  ####  ###  
    #  #      #  #      #  #      #  # ###  ####  ### #  #      #  #      #  #      #  #  
    ####      ####      ####      #### ############## ####      ####      ####      ####  
    #### #  # #### #  # #### #  # #### ## ######## ## #### #  # #### #  # #### #  # ####  
    ####################################################################################  
    ######  ########  ########  ########  ########  ########  ########  ########  ######  
    ###################################    ######    ###################################  
    ###################################    ######    ###################################  
````````````````````````````````````````````````````````````````````````````````````````````
```
* ```
                      ####  ###  
                      #  #### ###  
                      #         ###  
                      #           ##  
                      #  
                     ###  
                    #####  
                   #######  
                  #########  
                 ##### #####  
                #####   #####  
               ######   ######  
              #################  
              # ############# #  
                #############  
                #############  
                #############  
                ###### ######  
                ###### ######  
                #############  
                #############  
                #############  
                #############  
                ###### ######  
                ###### ######  
                #############  
                #############  
                #############  
                #############  
                ###### ######  
                ###### ######  
                #############  
                #############  
                #############  
                #############  
                #############  
                #####   #####  
                #####   #####   
                #####   #####  
            `````````````````````  
                                    
```
* ```
                                                  
                                          ####    
                                         #####    
                                        ######    
                                         ####     
                                         ####     
                                        #####     
                                       ########   
                                      ##########  
                                      ##########  
                                     ###########  
                                    ############  
                                  ##############  
   #####                       ################   
  ###########                #################    
  ###########################################     
   ########################################       
    #####################################         
     ##################################           
        ############################              
            ###################                   
``````````````````````````````````````````````````
```
Good luck!
  
[Answer]
## [Snails](https://github.com/feresum/PMA), ~~21~~ 18 bytes
-3 bytes due to additional input constraints edited into challenge.
```
!{t=\#!(\#o`+\`}\#
```
Unfortunately the time complexity is factorial, so most of the inputs aren't runnable.
Outputs 0 for falsy cases, and the number of `#` for truthy cases.
```
                 ,,
!{ t             ,, Assert that nowhere in the grid,
    =\#          ,, there is a '#'
    !(           ,, such that there does not exist
        (\# o)+  ,, an orthogonally connected path of '#'
        \`       ,, ending at a '`'
    )            ,,
}                ,,
\#               ,, Match '#' at starting position
```
[Answer]
# Octave, ~~53~~ 51 bytes
```
@(s)([~,n]=bwlabel(s>32,4))|n==1&&nnz(diff(+s)==61)
```
[Try It Online!](https://tio.run/#alxIo)
\*Since op dropped requirement to check for empty input answer reverted to my first edit.
Explanation:
```
nnz(s)                       check for empty input
([~,n]=bwlabel(s~=' ',4))    label nonempty regions and count number of labels
n==1                         check if number of labels is 1.
nnz(diff(+s)==61)            check if blocks connected to the surface
```
[Answer]
## [Grime](https://github.com/iatorm/grime), 29 bytes
```
C=\`|\#&<0C>oX
e`\#&C!v#!&\##
```
[Try it online!](https://tio.run/nexus/grime#@@9sG5NQE6OsZmPgbJcfwZWaAGQ7K5YpK6rFKCv//6@srMClrKAMxApcCQkJAA "Grime – TIO Nexus")
Most test cases time out on TIO.
Replace the `<0C>` with `<0CoF>` to make it a little faster.
## Explanation
I'm checking that from every `#` there exists a path to a ```, and that there exists at least one `#`.
I recently added rotation commands to Grime, which make this challenge much easier.
```
C=\`|\#&<0C>oX  First line:
C=               Define nonterminal C as
  \`             the literal `
    |            or
     \#          the literal #
       &<  >     which is contained in a larger rectangle
         0C      containing said literal adjacent to a match of C
            oX   rotated by any multiple of 90 degrees.
e`\#&C!v#!&\##  Second line:
e`               Match entire input against this pattern:
         !       does not
       v#        contain
  \#             the literal #
    &C!          which is not a match of C,
          &      and
             #   contains
           \#    the literal #.
```
[Answer]
## JavaScript (ES6), ~~197~~ 196 bytes
```
f=(s,l=Math.max(...s.split`\n`.map(t=>t.length)),t=s.replace(/^.*/g,t=>t+' '.repeat(l-t.length)),u=t.replace(eval('/(#|`)([^]{'+l+'})?(?!\\1)[#`]/g'),'`$2`'))=>t==u?/#/.test(s)>/#/.test(t):f(s,l,u)
```
Where `\n` represents the literal newline character. Tries to remove all `#`s one at a time by finding one adjacent to a ``` and changing it to a ```. Returns `true` if there was at least one `#` originally but there were all removed. Version that requires padded input for ~~118~~ 117 bytes:
```
f=(s,t=s,u=t.replace(eval('/(#|`)([^]{'+s.search`\n`+'})?(?!\\1)[#`]/'),'`$2`'))=>t==u?/#/.test(s)>/#/.test(t):f(s,u)
```
[Answer]
# [Perl 6](http://perl6.org/), 180 bytes
```
{?/\#/&&?all map ->\c{my \b=[map {[$^a.comb]},.lines];sub f(\y,\x){with b[y;x] ->$_ {b[y;x]=0;/\#/??f(y+(1|-1),x)|f(y,x+(1|-1))!!/\`/??1!!|()}}(|c)},map {|($++X ^$^a.comb)},.lines}
```
Checks if the input contains at least one `#`, and if every `#` can find a path to a ```.
Rather inefficient, because the pathfinding is brute-forced using a recursive function that always visits all other `#` of the same connected region (i.e. doesn't short-circuit).
Uses some unholy interaction between [Junction](https://docs.perl6.org/type/Junction) operators and [slipping](https://docs.perl6.org/type/Slip), to ensure that the path test is skipped for space characters without requiring a separate check for that outside of the pathfinding function.
[Answer]
# [Python 3](https://docs.python.org/3/), ~~214~~ 206 bytes
```
def f(s):
 C=s.split('\n');n=max(map(len,C));o=[''];C=[*''.join(t.ljust(n)for t in C+o)]
 while C>o:o=C;C=['`'if z>' 'and'`'in{C[i+y]for y in(1,-1,n,-n)}else z for i,z in enumerate(C)]
 return'#'in{*s}-{*C}
```
[Try it online!](https://tio.run/nexus/python3#7VnbbuIwEH33V4zwgxMakPpalL74MygSSA3aVMGpSNBuqfh2loRcfItjp6TblTBIDJw5dmKPz3jI@TXawtbL/CcENMzm2XsS5x55YcRfsHC3@ePtNu9eErGA@v4iDZeErBY0XE4Jmb@lMfPyefJ2yHKP@dt0DznEDOhD6q8Q/P4VJxHQ5/QpDWnBIWsSb@H4TIBs2GvxjX3SZfzwsSqoHxeq9xjMHgMWzJh/ipIsgiMUUBwci34jdthF@00eebTofx/lhz0juOhmmp1mn1N6OtNNlidRFi7RZDJ5QaBt2Pg7Lt@chXEFXFoFXC0MHFA61wDHaJsCtJ8iwBs1oF7sBVgbWzEFgWke1D7LF2dYE4XmwKrnsrl/efJVS2RZMPSLp1rSavUzdIuvtdq@a9vM0ATPbZom@G5j2V4xP74VYdyOrYLU1tPN19Xb3b9krEdsisCIs9uoZKNXNksh36R607pp0E8NrpQMaSHAnfJaqbsGrJQRa5ejBDtVu6aZxuwAsZEJ0AvCYObwbl3DjwsnScAl2ZXEUgomSZhETeDHQHC7bk0bA4m/oDFT8RCNExJTwbFLwkrm6kvCFikY@NwOdinYJgG7Wmi09DtS8h0pQ47S7feko@abICuXjYtsNunF61vkyPHE/j9WLlrgXrncK5d75fLDBNROgoSo15@5qyDs9uD@w@jzMdQ9hn9GnM6y3ZvfIAztVtPUAa3SqSgWUKXc6alTpc0OHYN3XvfXUfgCiv8Rer/mH4B2bkEBBQfyjQ4PTXMgXK8JOfm7EDAeMgKMScCOhEajkJu/9TFsUKHrQHA8Gaq6jdtTV4/3dZakU4ExLwC4FanVwjm4Vytt716HhrU7F0tW3nLw9fheCcPOQCv0vo9Z7i23HvXLp1C0fLZ1fb608s9/AQ "Python 3 – TIO Nexus")
The first line here is devoted to padding all the lines to the same length: we split the string (`s.split('\n')` is one char shorter than `s.splitlines()`), find the maximum length of a line, and assign to C a flattened list of all the characters after padding each line. (The newlines are gone.)
Then we make a list where each non-space character adjacent to at least one backtick is replaced by a backtick, and continue until no change happened (when the old list `o` is equal to `C`. We can compare with `C>o` instead of `C!=o` because replacing # (ASCII 35) with ` (ASCII 96) can only increase the list's lexicographical ordering.)
If no # remains, and at least one was present initially, the castle is valid.
* Saved eight bytes checking for # in the set difference, rather than `'#'in s and'#'not in C`
]